├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── README_ZH.md ├── example ├── Cargo.toml └── src │ ├── .env │ └── main.rs ├── src ├── binlog_client.rs ├── binlog_error.rs ├── binlog_parser.rs ├── binlog_stream.rs ├── column │ ├── column_type.rs │ ├── column_value.rs │ ├── json │ │ ├── json_binary.rs │ │ ├── json_formatter.rs │ │ ├── json_string_formatter.rs │ │ ├── mod.rs │ │ └── value_type.rs │ └── mod.rs ├── command │ ├── auth_native_password_command.rs │ ├── auth_plugin.rs │ ├── auth_sha2_password_command.rs │ ├── auth_sha2_rsa_password_command.rs │ ├── authenticator.rs │ ├── command_type.rs │ ├── command_util.rs │ ├── dump_binlog_command.rs │ ├── dump_binlog_gtid_command.rs │ ├── gtid_set.rs │ ├── mod.rs │ └── query_command.rs ├── constants.rs ├── event │ ├── checksum_type.rs │ ├── delete_rows_event.rs │ ├── event_data.rs │ ├── event_header.rs │ ├── event_type.rs │ ├── format_description_event.rs │ ├── gtid_event.rs │ ├── mod.rs │ ├── previous_gtids_event.rs │ ├── query_event.rs │ ├── rotate_event.rs │ ├── row_event.rs │ ├── rows_query_event.rs │ ├── table_map_event.rs │ ├── transaction_payload_event.rs │ ├── update_rows_event.rs │ ├── write_rows_event.rs │ ├── xa_prepare_event.rs │ └── xid_event.rs ├── ext │ ├── buf_ext.rs │ ├── cursor_ext.rs │ └── mod.rs ├── lib.rs └── network │ ├── auth_plugin_switch_packet.rs │ ├── error_packet.rs │ ├── greeting_packet.rs │ ├── mod.rs │ ├── packet_channel.rs │ └── result_set_row_packet.rs └── tests ├── .env ├── data_type_tests ├── binary_tests.rs ├── bit_tests.rs ├── blob_tests.rs ├── bytes_test_util.rs ├── char_tests.rs ├── charset_tests.rs ├── date_tests.rs ├── date_time_tests.rs ├── decimal_tests.rs ├── enum_tests.rs ├── json_tests.rs ├── mod.rs ├── numeric_tests.rs ├── set_tests.rs ├── text_tests.rs ├── time_tests.rs ├── timestamp_tests.rs └── year_tests.rs ├── ddl_tests ├── ddl_tests.rs └── mod.rs ├── dml_tests ├── delete_tests.rs ├── insert_tests.rs ├── mod.rs └── update_tests.rs ├── integration_test.rs ├── parse_file_tests ├── mod.rs ├── mysql-bin.000057 ├── mysql-bin.000080 ├── non-binlog └── parse_file_tests.rs └── runner ├── assert.rs ├── env.rs ├── mock.rs ├── mod.rs └── test_runner.rs /.gitignore: -------------------------------------------------------------------------------- 1 | ## File system 2 | .DS_Store 3 | 4 | ## Editor 5 | *.swp 6 | *.swo 7 | Session.vim 8 | .cproject 9 | .idea 10 | *.iml 11 | .vscode 12 | .project 13 | .favorites.json 14 | .settings/ 15 | 16 | ## Configuration 17 | /Cargo.lock 18 | 19 | ## Build 20 | /target 21 | 22 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | ".", 4 | "./example", 5 | ] 6 | 7 | [package] 8 | edition = "2021" 9 | name = "mysql-binlog-connector-rust" 10 | version = "0.3.2" 11 | authors = ["Shicai Xu "] 12 | categories = ["database"] 13 | description = "mysql binlog connector" 14 | documentation = "https://docs.rs/mysql-binlog-connector-rust" 15 | keywords = ["mysql", "binlog", "connector"] 16 | license = "MIT OR Apache-2.0" 17 | repository = "https://github.com/apecloud/mysql-binlog-connector-rust" 18 | homepage = "https://github.com/apecloud/mysql-binlog-connector-rust" 19 | readme = "README.md" 20 | 21 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 22 | 23 | [dependencies] 24 | byteorder = "1.4.3" 25 | num_enum = "0.7.3" 26 | serde = {version = "1", features = ["derive"]} 27 | serde_json = "1.0.87" 28 | serial_test = "3.1.1" 29 | sha1 = "0.10.0" 30 | sha2 = "0.10.6" 31 | zstd = "0.13.2" 32 | async-std = "1.12.0" 33 | dotenv = "0.15.0" 34 | url = "2.3.1" 35 | percent-encoding = "2.3.1" 36 | async-recursion = "1.0.2" 37 | lazy_static = "1.4.0" 38 | thiserror = "1.0.44" 39 | mysql_common = "0.32.4" 40 | base64 = "0.22.1" 41 | log = "0.4.26" 42 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 LaunchBadge, LLC 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 LaunchBadge, LLC 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mysql-binlog-connector-rust-example" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | dotenv = "0.15.0" 8 | futures = "0.3" 9 | mysql-binlog-connector-rust = { path = "../" } 10 | -------------------------------------------------------------------------------- /example/src/.env: -------------------------------------------------------------------------------- 1 | db_url=mysql://root:123456@127.0.0.1:3309 2 | server_id=200 3 | binlog_filename=mysql-bin.000033 4 | binlog_position=4 5 | gtid_enabled=true 6 | gtid_set=6d3960f6-4b36-11ef-8614-0242ac110002:1-10,787d08c4-4b36-11ef-8614-0242ac110006:1-5 -------------------------------------------------------------------------------- /example/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::{collections::HashMap, env, fs::File}; 2 | 3 | use futures::executor::block_on; 4 | use mysql_binlog_connector_rust::{ 5 | binlog_client::BinlogClient, 6 | binlog_parser::BinlogParser, 7 | column::{column_value::ColumnValue, json::json_binary::JsonBinary}, 8 | event::{event_data::EventData, row_event::RowEvent}, 9 | }; 10 | 11 | fn main() { 12 | // example 1: dump and parse binlogs from mysql 13 | block_on(dump_and_parse()) 14 | 15 | // example 2: parse mysql binlog file 16 | // block_on(parse_file()) 17 | } 18 | 19 | async fn dump_and_parse() { 20 | let env_path = env::current_dir().unwrap().join("example/src/.env"); 21 | dotenv::from_path(env_path).unwrap(); 22 | let url = env::var("db_url").unwrap(); 23 | let server_id: u64 = env::var("server_id").unwrap().parse().unwrap(); 24 | let binlog_filename = env::var("binlog_filename").unwrap(); 25 | let binlog_position: u32 = env::var("binlog_position").unwrap().parse().unwrap(); 26 | let gtid_enabled: bool = env::var("gtid_enabled").unwrap().parse().unwrap(); 27 | let gtid_set = env::var("gtid_set").unwrap(); 28 | 29 | let mut client = BinlogClient { 30 | url, 31 | binlog_filename, 32 | binlog_position, 33 | server_id, 34 | gtid_enabled, 35 | gtid_set, 36 | heartbeat_interval_secs: 5, 37 | timeout_secs: 60, 38 | }; 39 | 40 | let mut stream = client.connect().await.unwrap(); 41 | 42 | loop { 43 | let (header, data) = stream.read().await.unwrap(); 44 | println!("header: {:?}", header); 45 | println!("data: {:?}", data); 46 | println!(); 47 | } 48 | } 49 | 50 | #[allow(dead_code)] 51 | async fn parse_file() { 52 | let file_path = "path-to-binlog-file"; 53 | let mut file = File::open(file_path).unwrap(); 54 | 55 | let mut parser = BinlogParser { 56 | checksum_length: 4, 57 | table_map_event_by_table_id: HashMap::new(), 58 | }; 59 | 60 | assert!(parser.check_magic(&mut file).is_ok()); 61 | while let Ok((header, data)) = parser.next(&mut file) { 62 | println!("header: {:?}", header); 63 | println!("data: {:?}", data); 64 | println!(); 65 | } 66 | } 67 | 68 | #[allow(dead_code)] 69 | fn parse_json_columns(data: EventData) { 70 | let parse_row = |row: RowEvent| { 71 | for column_value in row.column_values { 72 | if let ColumnValue::Json(bytes) = column_value { 73 | println!( 74 | "json column: {}", 75 | JsonBinary::parse_as_string(&bytes).unwrap() 76 | ) 77 | } 78 | } 79 | }; 80 | 81 | match data { 82 | EventData::WriteRows(event) => { 83 | for row in event.rows { 84 | parse_row(row) 85 | } 86 | } 87 | EventData::DeleteRows(event) => { 88 | for row in event.rows { 89 | parse_row(row) 90 | } 91 | } 92 | EventData::UpdateRows(event) => { 93 | for (before, after) in event.rows { 94 | parse_row(before); 95 | parse_row(after); 96 | } 97 | } 98 | _ => {} 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/binlog_client.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | 3 | use crate::{ 4 | binlog_error::BinlogError, 5 | binlog_parser::BinlogParser, 6 | binlog_stream::BinlogStream, 7 | command::{authenticator::Authenticator, command_util::CommandUtil}, 8 | }; 9 | 10 | #[derive(Default)] 11 | pub struct BinlogClient { 12 | /// MySQL server connection URL in format "mysql://user:password@host:port" 13 | pub url: String, 14 | /// Name of the binlog file to start replication from, e.g. "mysql-bin.000001" 15 | /// Only used when gtid_enabled is false 16 | pub binlog_filename: String, 17 | /// Position in the binlog file to start replication from 18 | pub binlog_position: u32, 19 | /// Unique identifier for this replication client 20 | /// Must be different from other clients connected to the same MySQL server 21 | pub server_id: u64, 22 | /// Whether to enable GTID mode for replication 23 | pub gtid_enabled: bool, 24 | /// GTID set in format "uuid:1-100,uuid2:1-200" 25 | /// Only used when gtid_enabled is true 26 | pub gtid_set: String, 27 | /// Heartbeat interval in seconds 28 | /// Server will send a heartbeat event if no binlog events are received within this interval 29 | /// If heartbeat_interval_secs=0, server won't send heartbeat events 30 | pub heartbeat_interval_secs: u64, 31 | /// Network operation timeout in seconds 32 | /// Maximum wait time for operations like connection establishment and data reading 33 | /// If timeout_secs=0, the default value(60) will be used 34 | pub timeout_secs: u64, 35 | } 36 | 37 | const MIN_BINLOG_POSITION: u32 = 4; 38 | 39 | impl BinlogClient { 40 | pub async fn connect(&mut self) -> Result { 41 | // init connect 42 | let timeout_secs = if self.timeout_secs > 0 { 43 | self.timeout_secs 44 | } else { 45 | 60 46 | }; 47 | let mut authenticator = Authenticator::new(&self.url, timeout_secs)?; 48 | let mut channel = authenticator.connect().await?; 49 | 50 | if self.gtid_enabled { 51 | if self.gtid_set.is_empty() { 52 | let (_, _, gtid_set) = CommandUtil::fetch_binlog_info(&mut channel).await?; 53 | self.gtid_set = gtid_set; 54 | } 55 | } else { 56 | // fetch binlog info 57 | if self.binlog_filename.is_empty() { 58 | let (binlog_filename, binlog_position, _) = 59 | CommandUtil::fetch_binlog_info(&mut channel).await?; 60 | self.binlog_filename = binlog_filename; 61 | self.binlog_position = binlog_position; 62 | } 63 | 64 | if self.binlog_position < MIN_BINLOG_POSITION { 65 | self.binlog_position = MIN_BINLOG_POSITION; 66 | } 67 | } 68 | 69 | // fetch binlog checksum 70 | let binlog_checksum = CommandUtil::fetch_binlog_checksum(&mut channel).await?; 71 | 72 | // setup connection 73 | CommandUtil::setup_binlog_connection(&mut channel).await?; 74 | 75 | if self.heartbeat_interval_secs > 0 { 76 | CommandUtil::enable_heartbeat(&mut channel, self.heartbeat_interval_secs).await?; 77 | } 78 | 79 | // dump binlog 80 | CommandUtil::dump_binlog(&mut channel, self).await?; 81 | 82 | // list for binlog 83 | let parser = BinlogParser { 84 | checksum_length: binlog_checksum.get_length(), 85 | table_map_event_by_table_id: HashMap::new(), 86 | }; 87 | 88 | Ok(BinlogStream { channel, parser }) 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/binlog_error.rs: -------------------------------------------------------------------------------- 1 | use thiserror::Error; 2 | 3 | #[derive(Error, Debug)] 4 | pub enum BinlogError { 5 | #[error("unsupported column type: {0}")] 6 | UnsupportedColumnType(String), 7 | 8 | #[error("unexpected binlog data: {0}")] 9 | UnexpectedData(String), 10 | 11 | #[error("connect error: {0}")] 12 | ConnectError(String), 13 | 14 | #[error("fmt error: {0}")] 15 | FmtError(#[from] std::fmt::Error), 16 | 17 | #[error("parse int error: {0}")] 18 | ParseIntError(#[from] std::num::ParseIntError), 19 | 20 | #[error("io error: {0}")] 21 | IoError(#[from] std::io::Error), 22 | 23 | #[error("parse utf8 error: {0}")] 24 | FromUtf8Error(#[from] std::string::FromUtf8Error), 25 | 26 | #[error("parse url error: {0}")] 27 | ParseUrlError(#[from] url::ParseError), 28 | 29 | #[error("parse json error: {0}")] 30 | ParseJsonError(String), 31 | 32 | #[error("invalid gtid: {0}")] 33 | InvalidGtid(String), 34 | } 35 | -------------------------------------------------------------------------------- /src/binlog_parser.rs: -------------------------------------------------------------------------------- 1 | use crate::{ 2 | binlog_error::BinlogError, 3 | constants, 4 | event::{ 5 | delete_rows_event::DeleteRowsEvent, event_data::*, event_header::EventHeader, 6 | gtid_event::GtidEvent, previous_gtids_event::PreviousGtidsEvent, query_event::QueryEvent, 7 | rotate_event::RotateEvent, rows_query_event::RowsQueryEvent, 8 | table_map_event::TableMapEvent, transaction_payload_event::TransactionPayloadEvent, 9 | update_rows_event::UpdateRowsEvent, write_rows_event::WriteRowsEvent, 10 | xa_prepare_event::XaPrepareEvent, xid_event::XidEvent, 11 | }, 12 | event::{event_type::EventType, format_description_event::FormatDescriptionEvent}, 13 | }; 14 | 15 | use std::{ 16 | collections::HashMap, 17 | io::{Cursor, Read, Seek, SeekFrom}, 18 | }; 19 | 20 | pub struct BinlogParser { 21 | pub checksum_length: u8, 22 | pub table_map_event_by_table_id: HashMap, 23 | } 24 | 25 | const MAGIC_VALUE: [u8; 4] = [0xfeu8, 0x62, 0x69, 0x6e]; 26 | 27 | impl BinlogParser { 28 | pub fn check_magic(&mut self, stream: &mut S) -> Result<(), BinlogError> { 29 | let mut magic = [0u8; 4]; 30 | stream.read_exact(&mut magic)?; 31 | match magic { 32 | MAGIC_VALUE => Ok(()), 33 | _ => Err(BinlogError::UnexpectedData("bad magic".into())), 34 | } 35 | } 36 | 37 | pub fn next( 38 | &mut self, 39 | stream: &mut S, 40 | ) -> Result<(EventHeader, EventData), BinlogError> { 41 | let header = EventHeader::parse(stream)?; 42 | let data_length = header.event_length as usize 43 | - constants::EVENT_HEADER_LENGTH 44 | - self.checksum_length as usize; 45 | 46 | let buf = self.read_event_data(stream, data_length)?; 47 | let mut cursor = Cursor::new(&buf); 48 | 49 | let event_type = EventType::from_code(header.event_type); 50 | match event_type { 51 | EventType::FormatDescription => { 52 | let event_data = FormatDescriptionEvent::parse(&mut cursor, data_length)?; 53 | self.checksum_length = event_data.checksum_type.get_length(); 54 | Ok((header, EventData::FormatDescription(event_data))) 55 | } 56 | 57 | EventType::PreviousGtids => Ok(( 58 | header, 59 | EventData::PreviousGtids(PreviousGtidsEvent::parse(&mut cursor)?), 60 | )), 61 | 62 | EventType::Gtid => Ok((header, EventData::Gtid(GtidEvent::parse(&mut cursor)?))), 63 | 64 | EventType::Query => Ok((header, EventData::Query(QueryEvent::parse(&mut cursor)?))), 65 | 66 | EventType::TableMap => { 67 | let event_data = TableMapEvent::parse(&mut cursor)?; 68 | self.table_map_event_by_table_id 69 | .insert(event_data.table_id, event_data.clone()); 70 | Ok((header, EventData::TableMap(event_data))) 71 | } 72 | 73 | EventType::WriteRows | EventType::ExtWriteRows => { 74 | let row_event_version = Self::get_row_event_version(&event_type); 75 | let event_data = WriteRowsEvent::parse( 76 | &mut cursor, 77 | &mut self.table_map_event_by_table_id, 78 | row_event_version, 79 | )?; 80 | Ok((header, EventData::WriteRows(event_data))) 81 | } 82 | 83 | EventType::UpdateRows | EventType::ExtUpdateRows => { 84 | let row_event_version = Self::get_row_event_version(&event_type); 85 | let event_data = UpdateRowsEvent::parse( 86 | &mut cursor, 87 | &mut self.table_map_event_by_table_id, 88 | row_event_version, 89 | )?; 90 | Ok((header, EventData::UpdateRows(event_data))) 91 | } 92 | 93 | EventType::DeleteRows | EventType::ExtDeleteRows => { 94 | let row_event_version = Self::get_row_event_version(&event_type); 95 | let event_data = DeleteRowsEvent::parse( 96 | &mut cursor, 97 | &mut self.table_map_event_by_table_id, 98 | row_event_version, 99 | )?; 100 | Ok((header, EventData::DeleteRows(event_data))) 101 | } 102 | 103 | EventType::Xid => Ok((header, EventData::Xid(XidEvent::parse(&mut cursor)?))), 104 | 105 | EventType::XaPrepare => Ok(( 106 | header, 107 | EventData::XaPrepare(XaPrepareEvent::parse(&mut cursor)?), 108 | )), 109 | 110 | EventType::TransactionPayload => Ok(( 111 | header, 112 | EventData::TransactionPayload(TransactionPayloadEvent::parse(&mut cursor)?), 113 | )), 114 | 115 | EventType::RowsQuery => Ok(( 116 | header, 117 | EventData::RowsQuery(RowsQueryEvent::parse(&mut cursor)?), 118 | )), 119 | 120 | EventType::Rotate => Ok((header, EventData::Rotate(RotateEvent::parse(&mut cursor)?))), 121 | 122 | EventType::HeartBeat => Ok((header, EventData::HeartBeat)), 123 | 124 | _ => Ok((header, EventData::NotSupported)), 125 | } 126 | } 127 | 128 | fn read_event_data( 129 | &mut self, 130 | stream: &mut S, 131 | data_length: usize, 132 | ) -> Result, BinlogError> { 133 | // read data for current event 134 | let mut buf = vec![0u8; data_length]; 135 | stream.read_exact(&mut buf)?; 136 | // skip checksum 137 | stream.seek(SeekFrom::Current(self.checksum_length as i64))?; 138 | Ok(buf) 139 | } 140 | 141 | fn get_row_event_version(event_type: &EventType) -> u8 { 142 | match event_type { 143 | EventType::ExtWriteRows | EventType::ExtUpdateRows | EventType::ExtDeleteRows => 2, 144 | _ => 1, 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/binlog_stream.rs: -------------------------------------------------------------------------------- 1 | use std::io::Cursor; 2 | 3 | use byteorder::ReadBytesExt; 4 | 5 | use crate::{ 6 | binlog_error::BinlogError, 7 | binlog_parser::BinlogParser, 8 | command::command_util::CommandUtil, 9 | constants::MysqlRespCode, 10 | event::{event_data::EventData, event_header::EventHeader}, 11 | network::packet_channel::PacketChannel, 12 | }; 13 | 14 | pub struct BinlogStream { 15 | pub channel: PacketChannel, 16 | pub parser: BinlogParser, 17 | } 18 | 19 | impl BinlogStream { 20 | pub async fn read(&mut self) -> Result<(EventHeader, EventData), BinlogError> { 21 | let buf = self.channel.read().await?; 22 | let mut cursor = Cursor::new(&buf); 23 | 24 | if cursor.read_u8()? == MysqlRespCode::ERROR { 25 | CommandUtil::parse_result(&buf)?; 26 | } 27 | 28 | // parse events, execute the callback 29 | self.parser.next(&mut cursor) 30 | } 31 | 32 | pub async fn close(&mut self) -> Result<(), BinlogError> { 33 | self.channel.close().await?; 34 | Ok(()) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/column/column_type.rs: -------------------------------------------------------------------------------- 1 | use num_enum::{IntoPrimitive, TryFromPrimitive}; 2 | 3 | use serde::{Deserialize, Serialize}; 4 | 5 | use crate::binlog_error::BinlogError; 6 | 7 | /// Refer to: https://dev.mysql.com/doc/dev/mysql-server/8.0.33/classbinary__log_1_1Table__map__event.html 8 | /// Refer to: https://github.com/mysql/mysql-server/blob/trunk/include/mysql.h.pp 9 | #[derive(Debug, Deserialize, Serialize, Clone, IntoPrimitive, TryFromPrimitive)] 10 | #[repr(i16)] 11 | pub enum ColumnType { 12 | #[num_enum(default)] 13 | Unknown = -1, 14 | Decimal = 0, 15 | Tiny = 1, 16 | Short = 2, 17 | Long = 3, 18 | Float = 4, 19 | Double = 5, 20 | Null = 6, 21 | TimeStamp = 7, 22 | LongLong = 8, 23 | Int24 = 9, 24 | Date = 10, 25 | Time = 11, 26 | DateTime = 12, 27 | Year = 13, 28 | // This enumeration value is only used internally and cannot exist in a binlog. 29 | NewDate = 14, 30 | VarChar = 15, 31 | Bit = 16, 32 | TimeStamp2 = 17, 33 | DateTime2 = 18, 34 | Time2 = 19, 35 | Json = 245, 36 | NewDecimal = 246, 37 | // This enumeration value is only used internally and cannot exist in a binlog. 38 | Enum = 247, 39 | // This enumeration value is only used internally and cannot exist in a binlog. 40 | Set = 248, 41 | // This enumeration value is only used internally and cannot exist in a binlog. 42 | TinyBlob = 249, 43 | // This enumeration value is only used internally and cannot exist in a binlog. 44 | MediumBlob = 250, 45 | // This enumeration value is only used internally and cannot exist in a binlog. 46 | LongBlob = 251, 47 | Blob = 252, 48 | VarString = 253, 49 | String = 254, 50 | Geometry = 255, 51 | } 52 | 53 | impl ColumnType { 54 | pub fn from_code(code: u8) -> ColumnType { 55 | if let Ok(res) = ColumnType::try_from(code as i16) { 56 | return res; 57 | } 58 | ColumnType::Unknown 59 | } 60 | 61 | /// The column type of MYSQL_TYPE_STRING and MYSQL_TYPE_ENUM are String in binlog, we need to get 62 | /// the real column type for parsing column values. 63 | /// Refer to: https://github.com/mysql/mysql-server/blob/5.7/sql/log_event.cc#L2047 64 | pub fn parse_string_column_meta( 65 | column_meta: u16, 66 | column_type: u8, 67 | ) -> Result<(u8, u16), BinlogError> { 68 | let mut real_column_type = column_type; 69 | let mut column_length = column_meta; 70 | 71 | if column_type == ColumnType::String as u8 && column_meta >= 256 { 72 | let byte0 = column_meta >> 8; 73 | let byte1 = column_meta & 0xFF; 74 | if (byte0 & 0x30) != 0x30 { 75 | real_column_type = (byte0 | 0x30) as u8; 76 | column_length = byte1 | (((byte0 & 0x30) ^ 0x30) << 4); 77 | } else { 78 | if byte0 == ColumnType::Enum as u16 || byte0 == ColumnType::Set as u16 { 79 | real_column_type = byte0 as u8; 80 | } 81 | column_length = byte1; 82 | } 83 | } 84 | 85 | Ok((real_column_type, column_length)) 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/column/json/json_formatter.rs: -------------------------------------------------------------------------------- 1 | use crate::column::column_type::ColumnType; 2 | 3 | // refer: https://github.com/osheroff/mysql-binlog-connector-java/blob/master/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/json/JsonFormatter.java 4 | pub trait JsonFormatter { 5 | fn begin_object(&mut self, num_elements: u32); 6 | 7 | fn begin_array(&mut self, num_elements: u32); 8 | 9 | fn end_object(&mut self); 10 | 11 | fn end_array(&mut self); 12 | 13 | fn name(&mut self, name: &str); 14 | 15 | fn value_string(&mut self, value: &str); 16 | 17 | fn value_int(&mut self, value: i32); 18 | 19 | fn value_long(&mut self, value: i64); 20 | 21 | fn value_double(&mut self, value: f64); 22 | 23 | fn value_big_int(&mut self, value: i128); 24 | 25 | fn value_decimal(&mut self, value: &str); 26 | 27 | fn value_bool(&mut self, value: bool); 28 | 29 | fn value_null(&mut self); 30 | 31 | fn value_year(&mut self, year: i32); 32 | 33 | fn value_date(&mut self, year: i32, month: i32, day: i32); 34 | 35 | #[allow(clippy::too_many_arguments)] 36 | fn value_datetime( 37 | &mut self, 38 | year: i32, 39 | month: i32, 40 | day: i32, 41 | hour: i32, 42 | min: i32, 43 | sec: i32, 44 | micro_seconds: i32, 45 | ); 46 | 47 | fn value_time(&mut self, hour: i32, min: i32, sec: i32, micro_seconds: i32); 48 | 49 | fn value_timestamp(&mut self, seconds_past_epoch: i64, micro_seconds: i32); 50 | 51 | fn value_opaque(&mut self, column_type: &ColumnType, value: &[u8]); 52 | 53 | fn next_entry(&mut self); 54 | } 55 | -------------------------------------------------------------------------------- /src/column/json/json_string_formatter.rs: -------------------------------------------------------------------------------- 1 | use super::json_formatter::JsonFormatter; 2 | use crate::column::column_type::ColumnType; 3 | use lazy_static::lazy_static; 4 | use base64::{engine::general_purpose::STANDARD, Engine}; 5 | 6 | 7 | // refer: https://github.com/osheroff/mysql-binlog-connector-java/blob/master/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/json/JsonStringFormatter.java 8 | #[derive(Default)] 9 | pub struct JsonStringFormatter { 10 | sb: String, 11 | } 12 | 13 | const ESCAPE_GENERIC: i32 = -1; 14 | 15 | lazy_static! { 16 | static ref ESCAPES: [i32; 128] = { 17 | let mut escape = [0; 128]; 18 | #[allow(clippy::needless_range_loop)] 19 | for i in 0..32 { 20 | escape[i] = ESCAPE_GENERIC; 21 | } 22 | escape[b'"' as usize] = b'"' as i32; 23 | escape[b'\\' as usize] = b'\\' as i32; 24 | escape[0x08] = b'b' as i32; 25 | escape[0x09] = b't' as i32; 26 | escape[0x0C] = b'f' as i32; 27 | escape[0x0A] = b'n' as i32; 28 | escape[0x0D] = b'r' as i32; 29 | escape 30 | }; 31 | } 32 | 33 | const HEX_CODES: [char; 16] = [ 34 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 35 | ]; 36 | 37 | impl JsonStringFormatter { 38 | pub fn get_string(&self) -> String { 39 | self.sb.clone() 40 | } 41 | 42 | fn append_string(&mut self, original: &str) { 43 | for c in original.chars() { 44 | let ch = c as usize; 45 | if ch >= ESCAPES.len() || ESCAPES[ch] == 0 { 46 | self.sb.push(c); 47 | continue; 48 | } 49 | 50 | let escape = ESCAPES[ch]; 51 | if escape > 0 { 52 | // 2-char escape, fine 53 | self.sb.push('\\'); 54 | self.sb.push(escape as u8 as char); 55 | } else { 56 | self.unicode_escape(ch as i32); 57 | } 58 | } 59 | } 60 | 61 | fn unicode_escape(&mut self, char_to_escape: i32) { 62 | let mut char_to_escape = char_to_escape; 63 | self.sb.push('\\'); 64 | self.sb.push('u'); 65 | if char_to_escape > 0xFF { 66 | let hi = (char_to_escape >> 8) & 0xFF; 67 | self.sb.push(HEX_CODES[(hi >> 4) as usize]); 68 | self.sb.push(HEX_CODES[(hi & 0xF) as usize]); 69 | char_to_escape &= 0xFF; 70 | } else { 71 | self.sb.push('0'); 72 | self.sb.push('0'); 73 | } 74 | // We know it's a control char, so only the last 2 chars are non-0 75 | self.sb.push(HEX_CODES[(char_to_escape >> 4) as usize]); 76 | self.sb.push(HEX_CODES[(char_to_escape & 0xF) as usize]); 77 | } 78 | 79 | fn append_two_digit_unsigned_int(&mut self, value: i32) { 80 | if value < 10 { 81 | self.sb.push('0'); 82 | } 83 | self.sb.push_str(&value.to_string()); 84 | } 85 | 86 | fn append_four_digit_unsigned_int(&mut self, value: i32) { 87 | if value < 10 { 88 | self.sb.push_str("000"); 89 | } else if value < 100 { 90 | self.sb.push_str("00"); 91 | } else if value < 1000 { 92 | self.sb.push('0'); 93 | } 94 | self.sb.push_str(&value.to_string()); 95 | } 96 | 97 | pub fn append_six_digit_unsigned_int(&mut self, mut value: i32, trim_trailing_zeros: bool) { 98 | if value < 10 { 99 | self.sb.push_str("00000"); 100 | } else if value < 100 { 101 | self.sb.push_str("0000"); 102 | } else if value < 1000 { 103 | self.sb.push_str("000"); 104 | } else if value < 10000 { 105 | self.sb.push_str("00"); 106 | } else if value < 100000 { 107 | self.sb.push('0'); 108 | }; 109 | 110 | if trim_trailing_zeros { 111 | // Remove any trailing 0's ... 112 | for _ in 0..6 { 113 | if value % 10 == 0 { 114 | value /= 10; 115 | } 116 | } 117 | self.sb.push_str(&value.to_string()); 118 | } 119 | } 120 | 121 | fn append_date(&mut self, year: i32, month: i32, day: i32) { 122 | let mut year = year; 123 | if year < 0 { 124 | self.sb.push('-'); 125 | year = year.abs(); 126 | } 127 | self.append_four_digit_unsigned_int(year); 128 | self.sb.push('-'); 129 | self.append_two_digit_unsigned_int(month); 130 | self.sb.push('-'); 131 | self.append_two_digit_unsigned_int(day); 132 | } 133 | 134 | fn append_time(&mut self, hour: i32, min: i32, sec: i32, micro_seconds: i32) { 135 | self.append_two_digit_unsigned_int(hour); 136 | self.sb.push(':'); 137 | self.append_two_digit_unsigned_int(min); 138 | self.sb.push(':'); 139 | self.append_two_digit_unsigned_int(sec); 140 | if micro_seconds != 0 { 141 | self.sb.push('.'); 142 | self.append_six_digit_unsigned_int(micro_seconds, true); 143 | } 144 | } 145 | } 146 | 147 | impl JsonFormatter for JsonStringFormatter { 148 | fn begin_object(&mut self, _num_elements: u32) { 149 | self.sb.push('{'); 150 | } 151 | 152 | fn end_object(&mut self) { 153 | self.sb.push('}'); 154 | } 155 | 156 | fn begin_array(&mut self, _num_elements: u32) { 157 | self.sb.push('['); 158 | } 159 | 160 | fn end_array(&mut self) { 161 | self.sb.push(']'); 162 | } 163 | 164 | fn name(&mut self, name: &str) { 165 | self.sb.push('"'); 166 | self.append_string(name); 167 | self.sb.push_str(r#"":"#); 168 | } 169 | 170 | fn value_string(&mut self, value: &str) { 171 | self.sb.push('"'); 172 | self.append_string(value); 173 | self.sb.push('"'); 174 | } 175 | 176 | fn value_int(&mut self, value: i32) { 177 | self.sb.push_str(&value.to_string()); 178 | } 179 | 180 | fn value_long(&mut self, value: i64) { 181 | self.sb.push_str(&value.to_string()); 182 | } 183 | 184 | fn value_double(&mut self, value: f64) { 185 | self.sb.push_str(&value.to_string()); 186 | } 187 | 188 | fn value_big_int(&mut self, value: i128) { 189 | self.sb.push_str(&value.to_string()); 190 | } 191 | 192 | fn value_decimal(&mut self, value: &str) { 193 | self.sb.push_str(value); 194 | } 195 | 196 | fn value_bool(&mut self, value: bool) { 197 | self.sb.push_str(&value.to_string()); 198 | } 199 | 200 | fn value_null(&mut self) { 201 | self.sb.push_str("null"); 202 | } 203 | 204 | fn value_year(&mut self, year: i32) { 205 | self.sb.push_str(&year.to_string()) 206 | } 207 | 208 | fn value_date(&mut self, year: i32, month: i32, day: i32) { 209 | self.sb.push('"'); 210 | self.append_date(year, month, day); 211 | self.sb.push('"'); 212 | } 213 | 214 | fn value_datetime( 215 | &mut self, 216 | year: i32, 217 | month: i32, 218 | day: i32, 219 | hour: i32, 220 | min: i32, 221 | sec: i32, 222 | micro_seconds: i32, 223 | ) { 224 | self.sb.push('"'); 225 | self.append_date(year, month, day); 226 | self.sb.push(' '); 227 | self.append_time(hour, min, sec, micro_seconds); 228 | self.sb.push('"'); 229 | } 230 | 231 | fn value_time(&mut self, hour: i32, min: i32, sec: i32, micro_seconds: i32) { 232 | self.sb.push('"'); 233 | 234 | let mut hour = hour; 235 | if hour < 0 { 236 | self.sb.push('-'); 237 | hour = hour.abs(); 238 | } 239 | 240 | self.append_time(hour, min, sec, micro_seconds); 241 | self.sb.push('"'); 242 | } 243 | 244 | fn value_timestamp(&mut self, seconds_past_epoch: i64, micro_seconds: i32) { 245 | self.sb.push_str(&seconds_past_epoch.to_string()); 246 | self.append_six_digit_unsigned_int(micro_seconds, false); 247 | } 248 | 249 | fn value_opaque(&mut self, _column_type: &ColumnType, value: &[u8]) { 250 | self.sb.push('"'); 251 | self.sb.push_str(&STANDARD.encode(value)); 252 | self.sb.push('"'); 253 | } 254 | 255 | fn next_entry(&mut self) { 256 | self.sb.push(','); 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /src/column/json/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod json_binary; 2 | pub mod json_formatter; 3 | pub mod json_string_formatter; 4 | pub mod value_type; 5 | -------------------------------------------------------------------------------- /src/column/json/value_type.rs: -------------------------------------------------------------------------------- 1 | use serde::{Deserialize, Serialize}; 2 | 3 | // refer: https://github.com/osheroff/mysql-binlog-connector-java/blob/master/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/json/ValueType.java 4 | #[derive(Debug, Deserialize, Serialize, Clone)] 5 | pub(crate) enum ValueType { 6 | SmallDocument = 0x00, 7 | LargeDocument = 0x01, 8 | SmallArray = 0x02, 9 | LargeArray = 0x03, 10 | Literal = 0x04, 11 | Int16 = 0x05, 12 | Uint16 = 0x06, 13 | Int32 = 0x07, 14 | Uint32 = 0x08, 15 | Int64 = 0x09, 16 | Uint64 = 0x0a, 17 | Double = 0x0b, 18 | String = 0x0c, 19 | Custom = 0x0f, 20 | } 21 | 22 | impl ValueType { 23 | pub fn by_code(code: u8) -> Option { 24 | match code { 25 | 0x00 => Some(ValueType::SmallDocument), 26 | 0x01 => Some(ValueType::LargeDocument), 27 | 0x02 => Some(ValueType::SmallArray), 28 | 0x03 => Some(ValueType::LargeArray), 29 | 0x04 => Some(ValueType::Literal), 30 | 0x05 => Some(ValueType::Int16), 31 | 0x06 => Some(ValueType::Uint16), 32 | 0x07 => Some(ValueType::Int32), 33 | 0x08 => Some(ValueType::Uint32), 34 | 0x09 => Some(ValueType::Int64), 35 | 0x0a => Some(ValueType::Uint64), 36 | 0x0b => Some(ValueType::Double), 37 | 0x0c => Some(ValueType::String), 38 | 0x0f => Some(ValueType::Custom), 39 | _ => None, 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/column/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod column_type; 2 | pub mod column_value; 3 | pub mod json; 4 | -------------------------------------------------------------------------------- /src/command/auth_native_password_command.rs: -------------------------------------------------------------------------------- 1 | use std::io::Write; 2 | 3 | use byteorder::{LittleEndian, WriteBytesExt}; 4 | use sha1::{Digest, Sha1}; 5 | 6 | use crate::{binlog_error::BinlogError, constants::ClientCapabilities, ext::buf_ext::BufExt}; 7 | 8 | use super::auth_plugin::AuthPlugin; 9 | 10 | pub struct AuthNativePasswordCommand { 11 | pub schema: String, 12 | pub username: String, 13 | pub password: String, 14 | pub scramble: String, 15 | pub collation: u8, 16 | } 17 | 18 | impl AuthNativePasswordCommand { 19 | pub fn encrypted_password(&mut self) -> Result, BinlogError> { 20 | let encrypted_password = Self::encrypt_password_sha1(&self.password, &self.scramble)?; 21 | Ok(encrypted_password) 22 | } 23 | 24 | pub fn to_bytes(&mut self) -> Result, BinlogError> { 25 | let mut buf = Vec::new(); 26 | 27 | let mut client_capabilities = ClientCapabilities::LONG_FLAG 28 | | ClientCapabilities::PROTOCOL_41 29 | | ClientCapabilities::SECURE_CONNECTION 30 | | ClientCapabilities::PLUGIN_AUTH; 31 | if !self.schema.is_empty() { 32 | client_capabilities |= ClientCapabilities::CONNECT_WITH_DB; 33 | } 34 | buf.write_u32::(client_capabilities)?; 35 | 36 | // maximum packet length 37 | buf.write_u32::(0)?; 38 | buf.write_u8(self.collation)?; 39 | 40 | // reserved bytes 41 | for _ in 0..23 { 42 | buf.write_u8(0)?; 43 | } 44 | 45 | buf.write_null_terminated_string(&self.username)?; 46 | 47 | // encrypted password 48 | let encrypted_password = Self::encrypt_password_sha1(&self.password, &self.scramble)?; 49 | buf.write_u8(encrypted_password.len() as u8)?; 50 | buf.write_all(&encrypted_password)?; 51 | 52 | if !self.schema.is_empty() { 53 | buf.write_null_terminated_string(&self.schema)?; 54 | } 55 | 56 | buf.write_null_terminated_string(AuthPlugin::MySqlNativePassword.to_str())?; 57 | Ok(buf) 58 | } 59 | 60 | fn encrypt_password_sha1(password: &str, scramble: &str) -> Result, BinlogError> { 61 | let mut hash1 = Self::hash_sha1(password.as_bytes()); 62 | let scramble_concat_hash1 = 63 | [scramble.as_bytes().to_vec(), Self::hash_sha1(&hash1)].concat(); 64 | let hash2 = Self::hash_sha1(&scramble_concat_hash1); 65 | Ok(hash1.xor(hash2)) 66 | } 67 | 68 | fn hash_sha1(value: &[u8]) -> Vec { 69 | let mut hasher = Sha1::new(); 70 | hasher.update(value); 71 | hasher.finalize().as_slice().to_vec() 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/command/auth_plugin.rs: -------------------------------------------------------------------------------- 1 | use core::str; 2 | 3 | #[derive(PartialEq, Clone)] 4 | pub enum AuthPlugin { 5 | Unsupported, 6 | MySqlNativePassword, 7 | CachingSha2Password, 8 | } 9 | 10 | impl AuthPlugin { 11 | pub fn to_str(&self) -> &str { 12 | match self { 13 | AuthPlugin::MySqlNativePassword => "mysql_native_password", 14 | AuthPlugin::CachingSha2Password => "caching_sha2_password", 15 | _ => "unsupported", 16 | } 17 | } 18 | 19 | pub fn from_name(name: &str) -> Self { 20 | match name.to_lowercase().as_str() { 21 | "mysql_native_password" => AuthPlugin::MySqlNativePassword, 22 | "caching_sha2_password" => AuthPlugin::CachingSha2Password, 23 | _ => AuthPlugin::Unsupported, 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/command/auth_sha2_password_command.rs: -------------------------------------------------------------------------------- 1 | use std::io::Write; 2 | 3 | use byteorder::{LittleEndian, WriteBytesExt}; 4 | use sha1::Digest; 5 | use sha2::Sha256; 6 | 7 | use crate::{binlog_error::BinlogError, constants::ClientCapabilities, ext::buf_ext::BufExt}; 8 | 9 | use super::auth_plugin::AuthPlugin; 10 | 11 | pub struct AuthSha2PasswordCommand { 12 | pub schema: String, 13 | pub username: String, 14 | pub password: String, 15 | pub scramble: String, 16 | pub collation: u8, 17 | } 18 | 19 | impl AuthSha2PasswordCommand { 20 | pub fn encrypted_password(&mut self) -> Result, BinlogError> { 21 | let encrypted_password = Self::encrypt_password_sha256(&self.password, &self.scramble)?; 22 | Ok(encrypted_password) 23 | } 24 | 25 | pub fn to_bytes(&mut self) -> Result, BinlogError> { 26 | let mut buf = Vec::new(); 27 | 28 | let mut client_capabilities = ClientCapabilities::LONG_FLAG 29 | | ClientCapabilities::PROTOCOL_41 30 | | ClientCapabilities::SECURE_CONNECTION 31 | | ClientCapabilities::PLUGIN_AUTH 32 | | ClientCapabilities::PLUGIN_AUTH_LENENC_CLIENT_DATA; 33 | if !self.schema.is_empty() { 34 | client_capabilities |= ClientCapabilities::CONNECT_WITH_DB; 35 | } 36 | buf.write_u32::(client_capabilities)?; 37 | 38 | // maximum packet length 39 | buf.write_u32::(0)?; 40 | buf.write_u8(self.collation)?; 41 | 42 | // reserved bytes 43 | for _ in 0..23 { 44 | buf.write_u8(0)?; 45 | } 46 | 47 | buf.write_null_terminated_string(&self.username)?; 48 | 49 | // encrypted password 50 | let encrypted_password = Self::encrypt_password_sha256(&self.password, &self.scramble)?; 51 | buf.write_u8(encrypted_password.len() as u8)?; 52 | buf.write_all(&encrypted_password)?; 53 | 54 | if !self.schema.is_empty() { 55 | buf.write_null_terminated_string(&self.schema)?; 56 | } 57 | 58 | buf.write_null_terminated_string(AuthPlugin::CachingSha2Password.to_str())?; 59 | Ok(buf) 60 | } 61 | 62 | pub fn encrypt_password_sha256(password: &str, scramble: &str) -> Result, BinlogError> { 63 | let mut hash1 = Self::hash_sha256(password.as_bytes()); 64 | let hash2 = Self::hash_sha256(&hash1[0..]); 65 | 66 | let mut hasher = Sha256::new(); 67 | hasher.update(hash2); 68 | hasher.update(scramble.as_bytes()); 69 | let hash3 = hasher.finalize().as_slice().to_vec(); 70 | 71 | Ok(hash1.xor(hash3)) 72 | } 73 | 74 | fn hash_sha256(value: &[u8]) -> Vec { 75 | let mut hasher = Sha256::new(); 76 | hasher.update(value); 77 | hasher.finalize().as_slice().to_vec() 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/command/auth_sha2_rsa_password_command.rs: -------------------------------------------------------------------------------- 1 | use crate::{binlog_error::BinlogError, constants, ext::buf_ext::BufExt}; 2 | 3 | pub struct AuthSha2RsaPasswordCommand { 4 | pub rsa_res: Vec, 5 | pub password: String, 6 | pub scramble: String, 7 | } 8 | 9 | impl AuthSha2RsaPasswordCommand { 10 | pub fn to_bytes(&mut self) -> Result, BinlogError> { 11 | let mut password_buf = self.password.as_bytes().to_vec(); 12 | password_buf.push(constants::NULL_TERMINATOR); 13 | let encrypted_password = password_buf.xor(self.scramble.as_bytes().to_vec()); 14 | 15 | Ok(mysql_common::crypto::encrypt(&encrypted_password, self.rsa_res.as_slice())) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/command/authenticator.rs: -------------------------------------------------------------------------------- 1 | use async_recursion::async_recursion; 2 | use percent_encoding::percent_decode_str; 3 | use url::Url; 4 | 5 | use crate::{ 6 | binlog_error::BinlogError, 7 | constants::MysqlRespCode, 8 | network::{ 9 | auth_plugin_switch_packet::AuthPluginSwitchPacket, greeting_packet::GreetingPacket, 10 | packet_channel::PacketChannel, 11 | }, 12 | }; 13 | 14 | use super::{ 15 | auth_native_password_command::AuthNativePasswordCommand, auth_plugin::AuthPlugin, 16 | auth_sha2_password_command::AuthSha2PasswordCommand, 17 | auth_sha2_rsa_password_command::AuthSha2RsaPasswordCommand, command_util::CommandUtil, 18 | }; 19 | 20 | pub struct Authenticator { 21 | host: String, 22 | port: String, 23 | username: String, 24 | password: String, 25 | schema: String, 26 | scramble: String, 27 | collation: u8, 28 | timeout_secs: u64, 29 | } 30 | 31 | impl Authenticator { 32 | pub fn new(url: &str, timeout_secs: u64) -> Result { 33 | // url example: mysql://root:123456@127.0.0.1:3307/test_db?ssl-mode=disabled 34 | let url_info = Url::parse(url)?; 35 | let host = url_info.host_str().unwrap_or(""); 36 | let port = format!("{}", url_info.port().unwrap_or(3306)); 37 | let username = url_info.username(); 38 | let password = url_info.password().unwrap_or(""); 39 | let mut schema = ""; 40 | let pathes = url_info.path_segments().map(|c| c.collect::>()); 41 | if let Some(vec) = pathes { 42 | if !vec.is_empty() { 43 | schema = vec[0]; 44 | } 45 | } 46 | 47 | Ok(Self { 48 | host: percent_decode_str(host).decode_utf8_lossy().to_string(), 49 | port, 50 | username: percent_decode_str(username).decode_utf8_lossy().to_string(), 51 | password: percent_decode_str(password).decode_utf8_lossy().to_string(), 52 | schema: percent_decode_str(schema).decode_utf8_lossy().to_string(), 53 | scramble: String::new(), 54 | collation: 0, 55 | timeout_secs, 56 | }) 57 | } 58 | 59 | pub async fn connect(&mut self) -> Result { 60 | // connect to hostname:port 61 | let mut channel = PacketChannel::new(&self.host, &self.port, self.timeout_secs).await?; 62 | 63 | // read and parse greeting packet 64 | let (greeting_buf, sequence) = channel.read_with_sequece().await?; 65 | let greeting_packet = GreetingPacket::new(greeting_buf)?; 66 | 67 | self.collation = greeting_packet.server_collation; 68 | self.scramble = greeting_packet.scramble; 69 | 70 | // authenticate 71 | self.authenticate( 72 | &mut channel, 73 | &greeting_packet.plugin_provided_data, 74 | sequence, 75 | ) 76 | .await?; 77 | 78 | Ok(channel) 79 | } 80 | 81 | async fn authenticate( 82 | &mut self, 83 | channel: &mut PacketChannel, 84 | auth_plugin_name: &str, 85 | sequence: u8, 86 | ) -> Result<(), BinlogError> { 87 | let command_buf = match AuthPlugin::from_name(auth_plugin_name) { 88 | AuthPlugin::MySqlNativePassword => AuthNativePasswordCommand { 89 | schema: self.schema.clone(), 90 | username: self.username.clone(), 91 | password: self.password.clone(), 92 | scramble: self.scramble.clone(), 93 | collation: self.collation, 94 | } 95 | .to_bytes()?, 96 | 97 | AuthPlugin::CachingSha2Password => AuthSha2PasswordCommand { 98 | schema: self.schema.clone(), 99 | username: self.username.clone(), 100 | password: self.password.clone(), 101 | scramble: self.scramble.clone(), 102 | collation: self.collation, 103 | } 104 | .to_bytes()?, 105 | 106 | AuthPlugin::Unsupported => { 107 | return Err(BinlogError::ConnectError("unsupported auth plugin".into())); 108 | } 109 | }; 110 | 111 | channel.write(&command_buf, sequence + 1).await?; 112 | let (auth_res, sequence) = channel.read_with_sequece().await?; 113 | self.handle_auth_result(channel, auth_plugin_name, sequence, &auth_res) 114 | .await 115 | } 116 | 117 | async fn handle_auth_result( 118 | &mut self, 119 | channel: &mut PacketChannel, 120 | auth_plugin_name: &str, 121 | sequence: u8, 122 | auth_res: &Vec, 123 | ) -> Result<(), BinlogError> { 124 | // parse result 125 | match auth_res[0] { 126 | MysqlRespCode::OK => return Ok(()), 127 | 128 | MysqlRespCode::ERROR => return CommandUtil::check_error_packet(auth_res), 129 | 130 | MysqlRespCode::AUTH_PLUGIN_SWITCH => { 131 | return self 132 | .handle_auth_plugin_switch(channel, sequence, auth_res) 133 | .await; 134 | } 135 | 136 | _ => match AuthPlugin::from_name(auth_plugin_name) { 137 | AuthPlugin::MySqlNativePassword => { 138 | return Err(BinlogError::ConnectError(format!( 139 | "unexpected auth result for mysql_native_password: {}", 140 | auth_res[0] 141 | ))); 142 | } 143 | 144 | AuthPlugin::CachingSha2Password => { 145 | return self 146 | .handle_sha2_auth_result(channel, sequence, auth_res) 147 | .await; 148 | } 149 | 150 | // won't happen 151 | _ => {} 152 | }, 153 | }; 154 | 155 | Ok(()) 156 | } 157 | 158 | #[async_recursion] 159 | async fn handle_auth_plugin_switch( 160 | &mut self, 161 | channel: &mut PacketChannel, 162 | sequence: u8, 163 | auth_res: &Vec, 164 | ) -> Result<(), BinlogError> { 165 | let switch_packet = AuthPluginSwitchPacket::new(auth_res)?; 166 | let auth_plugin_name = &switch_packet.auth_plugin_name; 167 | self.scramble = switch_packet.scramble; 168 | 169 | let encrypted_password = match AuthPlugin::from_name(auth_plugin_name) { 170 | AuthPlugin::CachingSha2Password => AuthSha2PasswordCommand { 171 | schema: self.schema.clone(), 172 | username: self.username.clone(), 173 | password: self.password.clone(), 174 | scramble: self.scramble.clone(), 175 | collation: self.collation, 176 | } 177 | .encrypted_password()?, 178 | 179 | AuthPlugin::MySqlNativePassword => AuthNativePasswordCommand { 180 | schema: self.schema.clone(), 181 | username: self.username.clone(), 182 | password: self.password.clone(), 183 | scramble: self.scramble.clone(), 184 | collation: self.collation, 185 | } 186 | .encrypted_password()?, 187 | 188 | _ => { 189 | return Err(BinlogError::ConnectError(format!( 190 | "unexpected auth plugin for auth plugin switch: {}", 191 | auth_plugin_name 192 | ))); 193 | } 194 | }; 195 | 196 | channel.write(&encrypted_password, sequence + 1).await?; 197 | let (encrypted_auth_res, sequence) = channel.read_with_sequece().await?; 198 | self.handle_auth_result(channel, auth_plugin_name, sequence, &encrypted_auth_res) 199 | .await 200 | } 201 | 202 | async fn handle_sha2_auth_result( 203 | &self, 204 | channel: &mut PacketChannel, 205 | sequence: u8, 206 | auth_res: &[u8], 207 | ) -> Result<(), BinlogError> { 208 | // buf[0] is the length of buf, always 1 209 | match auth_res[1] { 210 | 0x03 => Ok(()), 211 | 212 | 0x04 => self.sha2_rsa_authenticate(channel, sequence).await, 213 | 214 | _ => Err(BinlogError::ConnectError(format!( 215 | "unexpected auth result for caching_sha2_password: {}", 216 | auth_res[1] 217 | ))), 218 | } 219 | } 220 | 221 | async fn sha2_rsa_authenticate( 222 | &self, 223 | channel: &mut PacketChannel, 224 | sequence: u8, 225 | ) -> Result<(), BinlogError> { 226 | // refer: https://mariadb.com/kb/en/caching_sha2_password-authentication-plugin/ 227 | // try to get RSA key from server 228 | channel.write(&[0x02], sequence + 1).await?; 229 | let (rsa_res, sequence) = channel.read_with_sequece().await?; 230 | match rsa_res[0] { 231 | 0x01 => { 232 | // try sha2 authentication with rsa 233 | let mut command = AuthSha2RsaPasswordCommand { 234 | rsa_res: rsa_res[1..].to_vec(), 235 | password: self.password.clone(), 236 | scramble: self.scramble.clone(), 237 | }; 238 | channel.write(&command.to_bytes()?, sequence + 1).await?; 239 | 240 | let (auth_res, _) = channel.read_with_sequece().await?; 241 | CommandUtil::parse_result(&auth_res) 242 | } 243 | 244 | _ => Err(BinlogError::ConnectError(format!( 245 | "failed to get RSA key from server for caching_sha2_password: {}", 246 | rsa_res[0] 247 | ))), 248 | } 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /src/command/command_type.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | pub enum CommandType { 3 | // Internal server command 4 | Sleep = 0, 5 | // Used to inform the server that client wants to close the connection 6 | Quit = 1, 7 | // Used to change the default schema of the connection 8 | InitDb = 2, 9 | // Used to send the server a text-based query that is executed immediately 10 | Query = 3, 11 | // Used to get column definitions of the specific table 12 | FieldList = 4, 13 | // Used to create new schema 14 | CreateDb = 5, 15 | // Used to drop existing schema 16 | DropDb = 6, 17 | // A low-level version of several FLUSH and RESET commands 18 | Refresh = 7, 19 | // Used to shutdown the mysql-server 20 | Shutdown = 8, 21 | // Used to get a human readable string of internal statistics 22 | Statistics = 9, 23 | // Used to get a list of active threads 24 | ProcessInfo = 10, 25 | // nternal server command 26 | Connect = 11, 27 | // Used to ask the server to terminate the connection 28 | ProcessKill = 12, 29 | // Triggers a dump on internal debug info to stdout of the mysql-server 30 | Debug = 13, 31 | // Used to check if the server is alive 32 | Ping = 14, 33 | // Internal server command 34 | Time = 15, 35 | // Internal server command 36 | DelayedInsert = 16, 37 | // Used to change user of the current connection and reset the connection state 38 | ChangeUser = 17, 39 | // Requests a binary log network stream from the master starting a given position 40 | BinlogDump = 18, 41 | // Used to dump a specific table 42 | TableDump = 19, 43 | // Internal server command 44 | ConnectOut = 20, 45 | // Registers a slave at the master Should be sent before requesting a binary log events with {@link #BINLOG_DUMP} 46 | RegisterSlave = 21, 47 | // Creates a prepared statement from the passed query string 48 | StmtPrepare = 22, 49 | // Used to execute a prepared statement as identified by statement id 50 | StmtExecute = 23, 51 | // Used to send some data for a column 52 | StmtSendLongData = 24, 53 | // Deallocates a prepared statement 54 | StmtClose = 25, 55 | // Resets the data of a prepared statement which was accumulated with {@link #STMT_SEND_LONG_DATA} commands 56 | StmtRest = 26, 57 | // Allows to enable and disable {@link comgithubshyikomysqlbinlognetworkClientCapabilities#MULTI_STATEMENTS} 58 | // for the current connection 59 | SetOption = 27, 60 | // Fetch a row from a existing resultset after a {@link #STMT_EXECUTE} 61 | StmtFetch = 28, 62 | // Internal server command 63 | Daemon = 29, 64 | // Used to request the binary log network stream based on a GTID 65 | BinlogDumpGtid = 30, 66 | } 67 | -------------------------------------------------------------------------------- /src/command/command_util.rs: -------------------------------------------------------------------------------- 1 | use crate::{ 2 | binlog_client::BinlogClient, 3 | binlog_error::BinlogError, 4 | constants::MysqlRespCode, 5 | event::checksum_type::ChecksumType, 6 | network::{ 7 | error_packet::ErrorPacket, packet_channel::PacketChannel, 8 | result_set_row_packet::ResultSetRowPacket, 9 | }, 10 | }; 11 | 12 | use super::{ 13 | dump_binlog_command::DumpBinlogCommand, dump_binlog_gtid_command::DumpBinlogGtidCommand, 14 | gtid_set::GtidSet, query_command::QueryCommand, 15 | }; 16 | 17 | pub struct CommandUtil {} 18 | 19 | impl CommandUtil { 20 | pub async fn execute_query( 21 | channel: &mut PacketChannel, 22 | sql: &str, 23 | ) -> Result, BinlogError> { 24 | Self::execute_sql(channel, sql).await?; 25 | // read to EOF 26 | while channel.read().await?[0] != MysqlRespCode::EOF {} 27 | // get result sets 28 | let mut result_sets = Vec::new(); 29 | 30 | let mut buf = channel.read().await?; 31 | while buf[0] != MysqlRespCode::EOF { 32 | Self::check_error_packet(&buf)?; 33 | let result_set = ResultSetRowPacket::new(&buf)?; 34 | result_sets.push(result_set); 35 | buf = channel.read().await?; 36 | } 37 | 38 | Ok(result_sets) 39 | } 40 | 41 | pub async fn execute_sql(channel: &mut PacketChannel, sql: &str) -> Result<(), BinlogError> { 42 | let mut command = QueryCommand { 43 | sql: sql.to_string(), 44 | }; 45 | 46 | // send the query command, sequence for non-authenticate commands are always 0 47 | channel.write(&command.to_bytes()?, 0).await?; 48 | 49 | // read the response packet 50 | let buf = channel.read().await?; 51 | Self::check_error_packet(&buf) 52 | } 53 | 54 | pub async fn fetch_binlog_info( 55 | channel: &mut PacketChannel, 56 | ) -> Result<(String, u32, String), BinlogError> { 57 | let result_sets = Self::execute_query(channel, "show master status").await?; 58 | if result_sets.is_empty() { 59 | return Err(BinlogError::ConnectError( 60 | "failed to fetch binlog filename and position".into(), 61 | )); 62 | } 63 | let binlog_filename = result_sets[0].values[0].clone(); 64 | let binlog_position = result_sets[0].values[1].clone().parse::()?; 65 | let gtid_set = result_sets[0].values[4].clone(); 66 | Ok((binlog_filename, binlog_position, gtid_set)) 67 | } 68 | 69 | pub async fn fetch_binlog_checksum( 70 | channel: &mut PacketChannel, 71 | ) -> Result { 72 | let result_set_rows = 73 | Self::execute_query(channel, "select @@global.binlog_checksum").await?; 74 | let mut checksum_name = ""; 75 | if !result_set_rows.is_empty() { 76 | checksum_name = result_set_rows[0].values[0].as_str(); 77 | } 78 | Ok(ChecksumType::from_name(checksum_name)) 79 | } 80 | 81 | pub async fn setup_binlog_connection(channel: &mut PacketChannel) -> Result<(), BinlogError> { 82 | let mut command = QueryCommand { 83 | sql: "set @master_binlog_checksum= @@global.binlog_checksum".to_string(), 84 | }; 85 | channel.write(&command.to_bytes()?, 0).await?; 86 | let buf = channel.read().await?; 87 | Self::check_error_packet(&buf) 88 | } 89 | 90 | pub async fn enable_heartbeat( 91 | channel: &mut PacketChannel, 92 | heartbeat_interval_secs: u64, 93 | ) -> Result<(), BinlogError> { 94 | let mut command = QueryCommand { 95 | sql: format!( 96 | "set @master_heartbeat_period={}", 97 | heartbeat_interval_secs * 1000_000_000 98 | ), 99 | }; 100 | channel.write(&command.to_bytes()?, 0).await?; 101 | let buf = channel.read().await?; 102 | Self::check_error_packet(&buf) 103 | } 104 | 105 | pub async fn dump_binlog( 106 | channel: &mut PacketChannel, 107 | client: &BinlogClient, 108 | ) -> Result<(), BinlogError> { 109 | let buf = if client.gtid_enabled { 110 | let mut command = DumpBinlogGtidCommand { 111 | server_id: client.server_id, 112 | gtid_set: GtidSet::new(&client.gtid_set)?, 113 | }; 114 | command.to_bytes()? 115 | } else { 116 | let mut command = DumpBinlogCommand { 117 | binlog_filename: client.binlog_filename.clone(), 118 | binlog_position: client.binlog_position, 119 | server_id: client.server_id, 120 | }; 121 | command.to_bytes()? 122 | }; 123 | channel.write(&buf, 0).await 124 | } 125 | 126 | pub fn parse_result(buf: &Vec) -> Result<(), BinlogError> { 127 | match buf[0] { 128 | MysqlRespCode::OK => Ok(()), 129 | 130 | MysqlRespCode::ERROR => Self::check_error_packet(buf), 131 | 132 | _ => Err(BinlogError::ConnectError("connect mysql failed".into())), 133 | } 134 | } 135 | 136 | pub fn check_error_packet(buf: &Vec) -> Result<(), BinlogError> { 137 | if buf[0] == MysqlRespCode::ERROR { 138 | let error_packet = ErrorPacket::new(buf)?; 139 | return Err(BinlogError::ConnectError(format!( 140 | "connect mysql failed: {}", 141 | error_packet.error_message 142 | ))); 143 | } 144 | Ok(()) 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/command/dump_binlog_command.rs: -------------------------------------------------------------------------------- 1 | use std::io::Write; 2 | 3 | use byteorder::{LittleEndian, WriteBytesExt}; 4 | 5 | use crate::binlog_error::BinlogError; 6 | 7 | use super::command_type::CommandType; 8 | 9 | pub struct DumpBinlogCommand { 10 | pub server_id: u64, 11 | pub binlog_filename: String, 12 | pub binlog_position: u32, 13 | } 14 | 15 | impl DumpBinlogCommand { 16 | pub fn to_bytes(&mut self) -> Result, BinlogError> { 17 | let mut buf = Vec::new(); 18 | buf.write_u8(CommandType::BinlogDump as u8)?; 19 | buf.write_u32::(self.binlog_position)?; 20 | 21 | let binlog_flags = 0; 22 | buf.write_u16::(binlog_flags)?; 23 | 24 | buf.write_u32::(self.server_id as u32)?; 25 | buf.write_all(self.binlog_filename.as_bytes())?; 26 | 27 | Ok(buf) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/command/dump_binlog_gtid_command.rs: -------------------------------------------------------------------------------- 1 | use std::io::Write; 2 | 3 | use byteorder::{LittleEndian, WriteBytesExt}; 4 | 5 | use crate::binlog_error::BinlogError; 6 | 7 | use super::{command_type::CommandType, gtid_set::GtidSet}; 8 | 9 | pub struct DumpBinlogGtidCommand { 10 | pub server_id: u64, 11 | pub gtid_set: GtidSet, 12 | } 13 | 14 | impl DumpBinlogGtidCommand { 15 | pub fn to_bytes(&mut self) -> Result, BinlogError> { 16 | let mut buf = Vec::new(); 17 | buf.write_u8(CommandType::BinlogDumpGtid as u8)?; 18 | 19 | // BINLOG_DUMP_NEVER_STOP = 0x00 20 | // BINLOG_DUMP_NON_BLOCK = 0x01 21 | // BINLOG_SEND_ANNOTATE_ROWS_EVENT = 0x02 22 | // BINLOG_THROUGH_POSITION = 0x02 23 | // BINLOG_THROUGH_GTID = 0x04 24 | let binlog_flags = 4; 25 | buf.write_u16::(binlog_flags)?; 26 | 27 | buf.write_u32::(self.server_id as u32)?; 28 | // binlog-filename-len 29 | buf.write_u32::(0)?; 30 | // binlog-filename, none 31 | // binlog-pos 32 | buf.write_u64::(4)?; 33 | 34 | let mut data_size = 8; // number of uuid_sets 35 | for uuid_set in self.gtid_set.map.values() { 36 | data_size += 16; // uuid 37 | data_size += 8; // number of intervals 38 | data_size += uuid_set.intervals.len() * 16; // start to end 39 | } 40 | buf.write_u32::(data_size as u32)?; 41 | 42 | buf.write_u64::(self.gtid_set.map.len() as u64)?; 43 | for (uuid, uuid_set) in self.gtid_set.map.iter() { 44 | let uuid_bytes = Self::hex_to_bytes(&uuid.replace('-', ""))?; 45 | buf.write_all(&uuid_bytes)?; 46 | 47 | // intervals 48 | buf.write_u64::(uuid_set.intervals.len() as u64)?; 49 | for interval in &uuid_set.intervals { 50 | buf.write_u64::(interval.start)?; 51 | buf.write_u64::(interval.end + 1)?; // right-open 52 | } 53 | } 54 | 55 | Ok(buf) 56 | } 57 | 58 | fn hex_to_bytes(uuid: &str) -> Result, BinlogError> { 59 | let mut bytes = Vec::with_capacity(uuid.len() / 2); 60 | for i in (0..uuid.len()).step_by(2) { 61 | let hex_byte = &uuid[i..i + 2]; 62 | bytes.push(u8::from_str_radix(hex_byte, 16)?); 63 | } 64 | Ok(bytes) 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/command/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod auth_native_password_command; 2 | pub mod auth_plugin; 3 | pub mod auth_sha2_password_command; 4 | pub mod auth_sha2_rsa_password_command; 5 | pub mod authenticator; 6 | pub mod command_type; 7 | pub mod command_util; 8 | pub mod dump_binlog_command; 9 | pub mod dump_binlog_gtid_command; 10 | pub mod gtid_set; 11 | pub mod query_command; 12 | -------------------------------------------------------------------------------- /src/command/query_command.rs: -------------------------------------------------------------------------------- 1 | use std::io::Write; 2 | 3 | use byteorder::WriteBytesExt; 4 | 5 | use crate::binlog_error::BinlogError; 6 | 7 | use super::command_type::CommandType; 8 | 9 | pub struct QueryCommand { 10 | pub sql: String, 11 | } 12 | 13 | impl QueryCommand { 14 | pub fn to_bytes(&mut self) -> Result, BinlogError> { 15 | let mut buf = Vec::new(); 16 | buf.write_u8(CommandType::Query as u8)?; 17 | buf.write_all(self.sql.as_bytes())?; 18 | Ok(buf) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/constants.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | pub struct ClientCapabilities {} 3 | 4 | impl ClientCapabilities { 5 | pub const LONG_PASSWORD: u32 = 1; 6 | pub const FOUND_ROWS: u32 = 1 << 1; 7 | pub const LONG_FLAG: u32 = 1 << 2; 8 | pub const CONNECT_WITH_DB: u32 = 1 << 3; 9 | pub const NO_SCHEMA: u32 = 1 << 4; 10 | pub const COMPRESS: u32 = 1 << 5; 11 | pub const ODBC: u32 = 1 << 6; 12 | pub const LOCAL_FILES: u32 = 1 << 7; 13 | pub const IGNORE_SPACE: u32 = 1 << 8; 14 | pub const PROTOCOL_41: u32 = 1 << 9; 15 | pub const INTERACTIVE: u32 = 1 << 10; 16 | pub const SSL: u32 = 1 << 11; 17 | pub const IGNORE_SIGPIPE: u32 = 1 << 12; 18 | pub const TRANSACTIONS: u32 = 1 << 13; 19 | pub const RESERVED: u32 = 1 << 14; 20 | pub const SECURE_CONNECTION: u32 = 1 << 15; 21 | pub const MULTI_STATEMENTS: u32 = 1 << 16; 22 | pub const MULTI_RESULTS: u32 = 1 << 17; 23 | pub const PS_MULTI_RESULTS: u32 = 1 << 18; 24 | pub const PLUGIN_AUTH: u32 = 1 << 19; 25 | pub const PLUGIN_AUTH_LENENC_CLIENT_DATA: u32 = 1 << 21; 26 | pub const SSL_VERIFY_SERVER_CERT: u32 = 1 << 30; 27 | pub const REMEMBER_OPTIONS: u32 = 1 << 31; 28 | } 29 | 30 | pub struct MysqlRespCode {} 31 | 32 | impl MysqlRespCode { 33 | pub const OK: u8 = 0x00; 34 | pub const ERROR: u8 = 0xFF; 35 | pub const EOF: u8 = 0xFE; 36 | pub const AUTH_PLUGIN_SWITCH: u8 = 0xFE; 37 | } 38 | 39 | pub const EVENT_HEADER_LENGTH: usize = 19; 40 | pub const NULL_TERMINATOR: u8 = 0; 41 | -------------------------------------------------------------------------------- /src/event/checksum_type.rs: -------------------------------------------------------------------------------- 1 | use serde::{Deserialize, Serialize}; 2 | 3 | #[derive(Debug, Deserialize, Serialize, Clone)] 4 | pub enum ChecksumType { 5 | None, 6 | CRC32, 7 | } 8 | 9 | impl ChecksumType { 10 | pub fn from_code(code: u8) -> Self { 11 | match code { 12 | 0x01 => ChecksumType::CRC32, 13 | _ => ChecksumType::None, 14 | } 15 | } 16 | 17 | pub fn from_name(name: &str) -> Self { 18 | match name { 19 | "CRC32" => ChecksumType::CRC32, 20 | _ => ChecksumType::None, 21 | } 22 | } 23 | 24 | pub fn get_length(&self) -> u8 { 25 | match self { 26 | ChecksumType::CRC32 => 4, 27 | _ => 0, 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/event/delete_rows_event.rs: -------------------------------------------------------------------------------- 1 | use std::{collections::HashMap, io::Cursor}; 2 | 3 | use serde::{Deserialize, Serialize}; 4 | 5 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 6 | 7 | use super::{event_header::EventHeader, row_event::RowEvent, table_map_event::TableMapEvent}; 8 | 9 | #[derive(Debug, Deserialize, Serialize, Clone)] 10 | pub struct DeleteRowsEvent { 11 | pub table_id: u64, 12 | pub included_columns: Vec, 13 | pub rows: Vec, 14 | } 15 | 16 | impl DeleteRowsEvent { 17 | pub fn parse( 18 | cursor: &mut Cursor<&Vec>, 19 | table_map_event_by_table_id: &mut HashMap, 20 | row_event_version: u8, 21 | ) -> Result { 22 | let (table_id, _column_count, included_columns) = 23 | EventHeader::parse_rows_event_common_header(cursor, row_event_version)?; 24 | let table_map_event = table_map_event_by_table_id.get(&table_id).unwrap(); 25 | 26 | let mut rows: Vec = Vec::new(); 27 | while cursor.available() > 0 { 28 | let row = RowEvent::parse(cursor, table_map_event, &included_columns)?; 29 | rows.push(row); 30 | } 31 | 32 | Ok(Self { 33 | table_id, 34 | included_columns, 35 | rows, 36 | }) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/event/event_data.rs: -------------------------------------------------------------------------------- 1 | use serde::{Deserialize, Serialize}; 2 | 3 | use super::{ 4 | delete_rows_event::DeleteRowsEvent, format_description_event::FormatDescriptionEvent, 5 | gtid_event::GtidEvent, previous_gtids_event::PreviousGtidsEvent, query_event::QueryEvent, 6 | rotate_event::RotateEvent, rows_query_event::RowsQueryEvent, table_map_event::TableMapEvent, 7 | transaction_payload_event::TransactionPayloadEvent, update_rows_event::UpdateRowsEvent, 8 | write_rows_event::WriteRowsEvent, xa_prepare_event::XaPrepareEvent, xid_event::XidEvent, 9 | }; 10 | 11 | #[derive(Debug, Deserialize, Serialize, Clone)] 12 | pub enum EventData { 13 | NotSupported, 14 | FormatDescription(FormatDescriptionEvent), 15 | PreviousGtids(PreviousGtidsEvent), 16 | Gtid(GtidEvent), 17 | Query(QueryEvent), 18 | TableMap(TableMapEvent), 19 | WriteRows(WriteRowsEvent), 20 | UpdateRows(UpdateRowsEvent), 21 | DeleteRows(DeleteRowsEvent), 22 | Xid(XidEvent), 23 | XaPrepare(XaPrepareEvent), 24 | Rotate(RotateEvent), 25 | TransactionPayload(TransactionPayloadEvent), 26 | RowsQuery(RowsQueryEvent), 27 | HeartBeat, 28 | } 29 | -------------------------------------------------------------------------------- /src/event/event_header.rs: -------------------------------------------------------------------------------- 1 | use std::io::{Cursor, Read, Seek, SeekFrom}; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | 6 | use crate::{binlog_error::BinlogError, constants, ext::cursor_ext::CursorExt}; 7 | 8 | #[derive(Debug, Deserialize, Serialize, Clone)] 9 | pub struct EventHeader { 10 | pub timestamp: u32, 11 | pub event_type: u8, 12 | pub server_id: u32, 13 | pub event_length: u32, 14 | pub next_event_position: u32, 15 | pub event_flags: u16, 16 | } 17 | 18 | impl EventHeader { 19 | pub fn parse(stream: &mut S) -> Result { 20 | // refer: https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Log__event__header.html 21 | let mut buf = [0u8; constants::EVENT_HEADER_LENGTH]; 22 | stream.read_exact(&mut buf)?; 23 | 24 | let mut cursor = Cursor::new(&buf); 25 | Ok(Self { 26 | timestamp: cursor.read_u32::()?, 27 | event_type: cursor.read_u8()?, 28 | server_id: cursor.read_u32::()?, 29 | event_length: cursor.read_u32::()?, 30 | next_event_position: cursor.read_u32::()?, 31 | event_flags: cursor.read_u16::()?, 32 | }) 33 | } 34 | 35 | // Parse the common header for rows events: 36 | // WriteRows / UpdateRows / DeleteRows 37 | // ExtWriteRows / ExtUpdateRows / ExtDeleteRows 38 | pub fn parse_rows_event_common_header( 39 | cursor: &mut Cursor<&Vec>, 40 | row_event_version: u8, 41 | ) -> Result<(u64, usize, Vec), BinlogError> { 42 | let table_id = cursor.read_u48::()?; 43 | let _flags = cursor.read_u16::()?; 44 | 45 | // ExtWriteRows/ExtUpdateRows/ExtDeleteRows, version 2, MySQL only 46 | if row_event_version == 2 { 47 | let extra_data_length = cursor.read_u16::()? as i64; 48 | cursor.seek(SeekFrom::Current(extra_data_length - 2))?; 49 | } 50 | 51 | let column_count = cursor.read_packed_number()?; 52 | let included_columns = cursor.read_bits(column_count, false)?; 53 | 54 | Ok((table_id, column_count, included_columns)) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/event/event_type.rs: -------------------------------------------------------------------------------- 1 | use num_enum::{IntoPrimitive, TryFromPrimitive}; 2 | 3 | #[derive(IntoPrimitive, TryFromPrimitive, Debug)] 4 | #[repr(u8)] 5 | pub enum EventType { 6 | // refer: https://github.com/mysql/mysql-server/blob/trunk/libs/mysql/binlog/event/binlog_event.h 7 | #[num_enum(default)] 8 | Unknown = 0, 9 | StartV3 = 1, 10 | Query = 2, 11 | Stop = 3, 12 | Rotate = 4, 13 | Intvar = 5, 14 | Load = 6, 15 | Slave = 7, 16 | CreateFile = 8, 17 | AppendBlock = 9, 18 | ExecLoad = 10, 19 | DeleteFile = 11, 20 | NewLoad = 12, 21 | Rand = 13, 22 | UserVar = 14, 23 | FormatDescription = 15, 24 | Xid = 16, 25 | BeginLoadQuery = 17, 26 | ExecuteLoadQuery = 18, 27 | TableMap = 19, 28 | PreGaWriteRows = 20, 29 | PreGaUpdateRows = 21, 30 | PreGaDeleteRows = 22, 31 | WriteRows = 23, 32 | UpdateRows = 24, 33 | DeleteRows = 25, 34 | Incident = 26, 35 | HeartBeat = 27, 36 | Ignorable = 28, 37 | RowsQuery = 29, 38 | ExtWriteRows = 30, 39 | ExtUpdateRows = 31, 40 | ExtDeleteRows = 32, 41 | Gtid = 33, 42 | AnonymousGtid = 34, 43 | PreviousGtids = 35, 44 | TransactionContext = 36, 45 | ViewChage = 37, 46 | XaPrepare = 38, 47 | PartialUpdateRowsEvent = 39, 48 | TransactionPayload = 40, 49 | AnnotateRows = 160, 50 | BinlogCheckpoint = 161, 51 | MariadbGtid = 162, 52 | MariadbGtidList = 163, 53 | } 54 | 55 | impl EventType { 56 | pub fn from_code(code: u8) -> EventType { 57 | if let Ok(res) = EventType::try_from(code) { 58 | return res; 59 | } 60 | EventType::Unknown 61 | } 62 | 63 | pub fn to_code(event_type: EventType) -> u8 { 64 | event_type.into() 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/event/format_description_event.rs: -------------------------------------------------------------------------------- 1 | use crate::{binlog_error::BinlogError, event::event_type::EventType}; 2 | use byteorder::{LittleEndian, ReadBytesExt}; 3 | use serde::{Deserialize, Serialize}; 4 | use std::io::{Cursor, Read, Seek, SeekFrom}; 5 | 6 | use super::checksum_type::ChecksumType; 7 | 8 | #[derive(Debug, Deserialize, Serialize, Clone)] 9 | pub struct FormatDescriptionEvent { 10 | pub binlog_version: u16, 11 | pub server_version: String, 12 | pub create_timestamp: u32, 13 | pub header_length: u8, 14 | pub checksum_type: ChecksumType, 15 | } 16 | 17 | impl FormatDescriptionEvent { 18 | pub fn parse(cursor: &mut Cursor<&Vec>, data_length: usize) -> Result { 19 | // refer: https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Format__description__event.html 20 | // binlog_version: 2 bytes 21 | let binlog_version = cursor.read_u16::()?; 22 | 23 | // server_version: 50 bytes 24 | let mut server_version_buf = [0u8; 50]; 25 | cursor.read_exact(&mut server_version_buf)?; 26 | let server_version = std::str::from_utf8(&server_version_buf) 27 | .unwrap() 28 | .to_string(); 29 | 30 | // create_timestamp: 4 bytes 31 | let create_timestamp = cursor.read_u32::()?; 32 | 33 | // header_length: 1 byte 34 | // Length of the Binlog Event Header of next events. Should always be 19. 35 | let header_length = cursor.read_u8()?; 36 | 37 | // post-header (76 : n), it is an array of n bytes, 38 | // one byte per event type that the server knows about, n = count of all event types, 39 | // the 14th (EventType::FormatDescription - 1) byte contains the payload length of FormatDescription, 40 | cursor.seek(SeekFrom::Current(EventType::FormatDescription as i64 - 1))?; 41 | let payload_length = cursor.read_u8()? as usize; 42 | 43 | // after the header and payload, it is the checksum type, 1 byte 44 | let mut checksum_type = 0; 45 | let checksum_block_length = data_length - payload_length; 46 | if checksum_block_length > 0 { 47 | // seek to the end of payload 48 | let current_pos = 2 + 50 + 4 + 1 + EventType::FormatDescription as u8; 49 | cursor.seek(SeekFrom::Current( 50 | payload_length as i64 - current_pos as i64, 51 | ))?; 52 | // read checksum type, refer: https://mariadb.com/kb/en/format_description_event/ 53 | checksum_type = cursor.read_u8()?; 54 | } 55 | 56 | Ok(Self { 57 | binlog_version, 58 | server_version, 59 | create_timestamp, 60 | header_length, 61 | checksum_type: ChecksumType::from_code(checksum_type), 62 | }) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/event/gtid_event.rs: -------------------------------------------------------------------------------- 1 | use crate::binlog_error::BinlogError; 2 | use byteorder::{LittleEndian, ReadBytesExt}; 3 | use serde::{Deserialize, Serialize}; 4 | use std::fmt::Write; 5 | use std::io::Cursor; 6 | 7 | #[derive(Debug, Deserialize, Serialize, Clone)] 8 | pub struct GtidEvent { 9 | pub flags: u8, 10 | pub gtid: String, 11 | } 12 | 13 | impl GtidEvent { 14 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 15 | // refer: https://dev.mysql.com/doc/refman/8.0/en/replication-gtids-concepts.html 16 | // refer: https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Gtid__event.html 17 | let flags = cursor.read_u8()?; 18 | let sid = Self::read_uuid(cursor)?; 19 | let gno = cursor.read_u64::()?; 20 | 21 | Ok(GtidEvent { 22 | flags, 23 | gtid: format!("{}:{}", sid, gno), 24 | }) 25 | } 26 | 27 | pub fn read_uuid(cursor: &mut Cursor<&Vec>) -> Result { 28 | Ok(format!( 29 | "{}-{}-{}-{}-{}", 30 | Self::bytes_to_hex_string(cursor, 4)?, 31 | Self::bytes_to_hex_string(cursor, 2)?, 32 | Self::bytes_to_hex_string(cursor, 2)?, 33 | Self::bytes_to_hex_string(cursor, 2)?, 34 | Self::bytes_to_hex_string(cursor, 6)?, 35 | )) 36 | } 37 | 38 | fn bytes_to_hex_string( 39 | cursor: &mut Cursor<&Vec>, 40 | byte_count: u8, 41 | ) -> Result { 42 | let mut res = String::new(); 43 | for _ in 0..byte_count { 44 | write!(&mut res, "{:02x}", cursor.read_u8()?)?; 45 | } 46 | Ok(res) 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/event/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod checksum_type; 2 | pub mod delete_rows_event; 3 | pub mod event_data; 4 | pub mod event_header; 5 | pub mod event_type; 6 | pub mod format_description_event; 7 | pub mod gtid_event; 8 | pub mod previous_gtids_event; 9 | pub mod query_event; 10 | pub mod rotate_event; 11 | pub mod row_event; 12 | pub mod rows_query_event; 13 | pub mod table_map_event; 14 | pub mod transaction_payload_event; 15 | pub mod update_rows_event; 16 | pub mod write_rows_event; 17 | pub mod xa_prepare_event; 18 | pub mod xid_event; 19 | -------------------------------------------------------------------------------- /src/event/previous_gtids_event.rs: -------------------------------------------------------------------------------- 1 | use super::gtid_event::GtidEvent; 2 | use crate::binlog_error::BinlogError; 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | use std::io::Cursor; 6 | 7 | #[derive(Debug, Deserialize, Serialize, Clone)] 8 | pub struct PreviousGtidsEvent { 9 | pub gtid_set: String, 10 | } 11 | 12 | impl PreviousGtidsEvent { 13 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 14 | let uuid_count = cursor.read_u64::()?; 15 | let mut gtids: Vec = Vec::with_capacity(uuid_count as usize); 16 | 17 | for _ in 0..uuid_count { 18 | let uuid = GtidEvent::read_uuid(cursor)?; 19 | let intervals = Self::read_interval(cursor)?; 20 | gtids.push(format!("{}:{}", uuid, intervals)); 21 | } 22 | 23 | Ok(Self { 24 | gtid_set: gtids.join(","), 25 | }) 26 | } 27 | 28 | fn read_interval(cursor: &mut Cursor<&Vec>) -> Result { 29 | let interval_count = cursor.read_u64::()?; 30 | let mut intervals = Vec::with_capacity(interval_count as usize); 31 | 32 | for _ in 0..interval_count { 33 | let start = cursor.read_u64::()?; 34 | let end = cursor.read_u64::()?; 35 | // mysql "show binlog events in 'mysql-bin.000005'" returns: 36 | // "58cf6502-63db-11ed-8079-0242ac110002:1-8" while we get interval_start = 1, interval_end = 9 37 | intervals.push(format!("{}-{}", start, end - 1)); 38 | } 39 | 40 | Ok(intervals.join(":")) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/event/query_event.rs: -------------------------------------------------------------------------------- 1 | use std::io::{Cursor, Read, Seek, SeekFrom}; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | 6 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 7 | 8 | #[derive(Debug, Deserialize, Serialize, Clone)] 9 | pub struct QueryEvent { 10 | pub thread_id: u32, 11 | pub exec_time: u32, 12 | pub error_code: u16, 13 | pub schema: String, 14 | pub query: String, 15 | } 16 | 17 | impl QueryEvent { 18 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 19 | // refer: https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Query__event.html 20 | // Post-Header for Query_event 21 | let thread_id = cursor.read_u32::()?; 22 | let exec_time = cursor.read_u32::()?; 23 | let schema_length = cursor.read_u8()?; 24 | let error_code = cursor.read_u16::()?; 25 | let status_vars_length = cursor.read_u16::()? as i64; 26 | 27 | // skip, Body for Query_event 28 | cursor.seek(SeekFrom::Current(status_vars_length))?; 29 | 30 | // Format: schema_length + 1, The currently selected database, as a null-terminated string. 31 | let schema = cursor.read_string_without_terminator(schema_length as usize)?; 32 | 33 | let mut query = String::new(); 34 | cursor.read_to_string(&mut query)?; 35 | 36 | Ok(Self { 37 | thread_id, 38 | exec_time, 39 | error_code, 40 | schema, 41 | query, 42 | }) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/event/rotate_event.rs: -------------------------------------------------------------------------------- 1 | use std::io::Cursor; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | 6 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 7 | 8 | #[derive(Debug, Deserialize, Serialize, Clone)] 9 | pub struct RotateEvent { 10 | pub binlog_filename: String, 11 | pub binlog_position: u64, 12 | } 13 | 14 | impl RotateEvent { 15 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 16 | let binlog_position = cursor.read_u64::()?; 17 | let binlog_filename = cursor.read_string_without_terminator(cursor.get_ref().len() - 8)?; 18 | Ok(Self { 19 | binlog_filename, 20 | binlog_position, 21 | }) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/event/row_event.rs: -------------------------------------------------------------------------------- 1 | use std::io::Cursor; 2 | 3 | use serde::{Deserialize, Serialize}; 4 | 5 | use crate::{ 6 | binlog_error::BinlogError, 7 | column::{column_type::ColumnType, column_value::ColumnValue}, 8 | ext::cursor_ext::CursorExt, 9 | }; 10 | 11 | use super::table_map_event::TableMapEvent; 12 | 13 | #[derive(Debug, Deserialize, Serialize, Clone)] 14 | pub struct RowEvent { 15 | pub column_values: Vec, 16 | } 17 | 18 | impl RowEvent { 19 | #[allow(clippy::needless_range_loop)] 20 | pub fn parse( 21 | cursor: &mut Cursor<&Vec>, 22 | table_map_event: &TableMapEvent, 23 | included_columns: &[bool], 24 | ) -> Result { 25 | let null_columns = cursor.read_bits(included_columns.len(), false)?; 26 | let mut column_values = Vec::with_capacity(table_map_event.column_types.len()); 27 | let mut skipped_column_count = 0; 28 | for i in 0..table_map_event.column_types.len() { 29 | if !included_columns[i] { 30 | skipped_column_count += 1; 31 | column_values.push(ColumnValue::None); 32 | continue; 33 | } 34 | 35 | let index = i - skipped_column_count; 36 | if null_columns[index] { 37 | column_values.push(ColumnValue::None); 38 | continue; 39 | } 40 | 41 | let column_meta = table_map_event.column_metas[i]; 42 | let mut column_type = table_map_event.column_types[i]; 43 | let mut column_length = column_meta; 44 | 45 | if column_type == ColumnType::String as u8 && column_meta >= 256 { 46 | (column_type, column_length) = 47 | ColumnType::parse_string_column_meta(column_meta, column_type)?; 48 | } 49 | 50 | let col_value = ColumnValue::parse( 51 | cursor, 52 | ColumnType::from_code(column_type), 53 | column_meta, 54 | column_length, 55 | )?; 56 | column_values.push(col_value); 57 | } 58 | 59 | Ok(Self { column_values }) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/event/rows_query_event.rs: -------------------------------------------------------------------------------- 1 | use serde::{Deserialize, Serialize}; 2 | 3 | use std::io::{Cursor, Seek, SeekFrom}; 4 | 5 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 6 | 7 | #[derive(Debug, Deserialize, Serialize, Clone)] 8 | pub struct RowsQueryEvent { 9 | pub query: String, 10 | } 11 | 12 | impl RowsQueryEvent { 13 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 14 | // refer: https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Rows__query__event.html 15 | // query length is stored using one byte, but it is ignored and the left bytes contain the full query 16 | cursor.seek(SeekFrom::Current(1))?; 17 | 18 | let query = cursor.read_string(cursor.get_ref().len() - 1)?; 19 | Ok(Self { query }) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/event/table_map_event.rs: -------------------------------------------------------------------------------- 1 | use std::io::{Cursor, Read}; 2 | 3 | use byteorder::{BigEndian, LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | 6 | use crate::{ 7 | binlog_error::BinlogError, column::column_type::ColumnType, ext::cursor_ext::CursorExt, 8 | }; 9 | 10 | #[derive(Debug, Deserialize, Serialize, Clone)] 11 | pub struct TableMapEvent { 12 | pub table_id: u64, 13 | pub database_name: String, 14 | pub table_name: String, 15 | pub column_types: Vec, 16 | pub column_metas: Vec, 17 | pub null_bits: Vec, 18 | } 19 | 20 | impl TableMapEvent { 21 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 22 | // refer: https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Table__map__event.html 23 | // table_id 24 | let table_id = cursor.read_u48::()?; 25 | 26 | // flags, Reserved for future use; currently always 0. 27 | let _flags = cursor.read_u16::(); 28 | 29 | // database_name 30 | let database_name_length = cursor.read_u8()?; 31 | let database_name = cursor.read_string_without_terminator(database_name_length as usize)?; 32 | 33 | // table_name 34 | let table_name_length = cursor.read_u8()?; 35 | let table_name = cursor.read_string_without_terminator(table_name_length as usize)?; 36 | 37 | // column_count 38 | let column_count = cursor.read_packed_number()?; 39 | 40 | // column_types 41 | let mut column_types = vec![0u8; column_count]; 42 | cursor.read_exact(&mut column_types)?; 43 | 44 | // metadata_length, won't be used 45 | let _metadata_length = cursor.read_packed_number()?; 46 | 47 | // metadata 48 | let column_metas = Self::read_metadatas(cursor, &column_types)?; 49 | 50 | // nullable_bits 51 | let null_bits = cursor.read_bits(column_count, false)?; 52 | 53 | // TODO: table_metadata 54 | 55 | Ok(Self { 56 | table_id, 57 | database_name, 58 | table_name, 59 | column_types, 60 | column_metas, 61 | null_bits, 62 | }) 63 | } 64 | 65 | fn read_metadatas( 66 | cursor: &mut Cursor<&Vec>, 67 | column_types: &Vec, 68 | ) -> Result, BinlogError> { 69 | let mut column_metadatas = Vec::with_capacity(column_types.len()); 70 | for column_type in column_types { 71 | let column_metadata = match ColumnType::from_code(*column_type) { 72 | ColumnType::Float 73 | | ColumnType::Double 74 | | ColumnType::Blob 75 | | ColumnType::TinyBlob 76 | | ColumnType::MediumBlob 77 | | ColumnType::LongBlob 78 | | ColumnType::Json 79 | | ColumnType::Geometry 80 | | ColumnType::Time2 81 | | ColumnType::DateTime2 82 | | ColumnType::TimeStamp2 => cursor.read_u8()? as u16, 83 | 84 | ColumnType::Bit | ColumnType::VarChar | ColumnType::NewDecimal => { 85 | cursor.read_u16::()? 86 | } 87 | 88 | ColumnType::Set | ColumnType::Enum | ColumnType::String => { 89 | cursor.read_u16::()? 90 | } 91 | 92 | _ => 0, 93 | }; 94 | column_metadatas.push(column_metadata); 95 | } 96 | 97 | Ok(column_metadatas) 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/event/transaction_payload_event.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | collections::HashMap, 3 | io::{Cursor, Seek, SeekFrom}, 4 | }; 5 | 6 | use serde::{Deserialize, Serialize}; 7 | 8 | use crate::{binlog_error::BinlogError, binlog_parser::BinlogParser, ext::cursor_ext::CursorExt}; 9 | 10 | use super::{event_data::EventData, event_header::EventHeader}; 11 | 12 | #[derive(Debug, Deserialize, Serialize, Clone)] 13 | pub struct TransactionPayloadEvent { 14 | pub uncompressed_size: u32, 15 | pub uncompressed_events: Vec<(EventHeader, EventData)>, 16 | } 17 | 18 | impl TransactionPayloadEvent { 19 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 20 | // refer: https://dev.mysql.com/doc/refman/8.0/en/binary-log-transaction-compression.html 21 | let (_compress_type, uncompressed_size) = Self::parse_meta(cursor)?; 22 | 23 | // read the rest data as payload and decompress it, currently only support zstd 24 | let mut uncompressed_payload: Vec = Vec::new(); 25 | zstd::stream::copy_decode(cursor, &mut uncompressed_payload)?; 26 | 27 | // construct a new parser from the payload 28 | let mut payload_cursor = Cursor::new(uncompressed_payload); 29 | let mut parser = BinlogParser { 30 | checksum_length: 0, 31 | table_map_event_by_table_id: HashMap::new(), 32 | }; 33 | 34 | // parse events in payload 35 | let mut uncompressed_events: Vec<(EventHeader, EventData)> = Vec::new(); 36 | while let Ok(e) = parser.next(&mut payload_cursor) { 37 | uncompressed_events.push(e); 38 | } 39 | 40 | Ok(Self { 41 | uncompressed_size: uncompressed_size as u32, 42 | uncompressed_events, 43 | }) 44 | } 45 | 46 | fn parse_meta(cursor: &mut Cursor<&Vec>) -> Result<(usize, usize), BinlogError> { 47 | let mut payload_size = 0; 48 | let mut compress_type = 0; 49 | let mut uncompressed_size = 0; 50 | 51 | while cursor.available() > 0 { 52 | let field_type = if cursor.available() >= 1 { 53 | cursor.read_packed_number()? 54 | } else { 55 | 0 56 | }; 57 | 58 | // we have reached the end of the Event Data Header 59 | if field_type == 0 { 60 | break; 61 | } 62 | 63 | let field_length = if cursor.available() >= 1 { 64 | cursor.read_packed_number()? 65 | } else { 66 | 0 67 | }; 68 | 69 | match field_type { 70 | 1 => payload_size = cursor.read_packed_number()?, 71 | 72 | 2 => compress_type = cursor.read_packed_number()?, 73 | 74 | 3 => uncompressed_size = cursor.read_packed_number()?, 75 | 76 | _ => { 77 | cursor.seek(SeekFrom::Current(field_length as i64))?; 78 | } 79 | } 80 | } 81 | 82 | if uncompressed_size == 0 { 83 | uncompressed_size = payload_size; 84 | } 85 | 86 | Ok((compress_type, uncompressed_size)) 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/event/update_rows_event.rs: -------------------------------------------------------------------------------- 1 | use std::{collections::HashMap, io::Cursor}; 2 | 3 | use serde::{Deserialize, Serialize}; 4 | 5 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 6 | 7 | use super::{event_header::EventHeader, row_event::RowEvent, table_map_event::TableMapEvent}; 8 | 9 | #[derive(Debug, Deserialize, Serialize, Clone)] 10 | pub struct UpdateRowsEvent { 11 | pub table_id: u64, 12 | pub included_columns_before: Vec, 13 | pub included_columns_after: Vec, 14 | pub rows: Vec<(RowEvent, RowEvent)>, 15 | } 16 | 17 | impl UpdateRowsEvent { 18 | pub fn parse( 19 | cursor: &mut Cursor<&Vec>, 20 | table_map_event_by_table_id: &mut HashMap, 21 | row_event_version: u8, 22 | ) -> Result { 23 | let (table_id, column_count, included_columns_before) = 24 | EventHeader::parse_rows_event_common_header(cursor, row_event_version)?; 25 | let included_columns_after = cursor.read_bits(column_count, false)?; 26 | let table_map_event = table_map_event_by_table_id.get(&table_id).unwrap(); 27 | 28 | let mut rows: Vec<(RowEvent, RowEvent)> = Vec::new(); 29 | while cursor.available() > 0 { 30 | let before = RowEvent::parse(cursor, table_map_event, &included_columns_before)?; 31 | let after = RowEvent::parse(cursor, table_map_event, &included_columns_after)?; 32 | rows.push((before, after)); 33 | } 34 | 35 | Ok(Self { 36 | table_id, 37 | included_columns_before, 38 | included_columns_after, 39 | rows, 40 | }) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/event/write_rows_event.rs: -------------------------------------------------------------------------------- 1 | use std::{collections::HashMap, io::Cursor}; 2 | 3 | use serde::{Deserialize, Serialize}; 4 | 5 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 6 | 7 | use super::{event_header::EventHeader, row_event::RowEvent, table_map_event::TableMapEvent}; 8 | 9 | #[derive(Debug, Deserialize, Serialize, Clone)] 10 | pub struct WriteRowsEvent { 11 | pub table_id: u64, 12 | pub included_columns: Vec, 13 | pub rows: Vec, 14 | } 15 | 16 | impl WriteRowsEvent { 17 | pub fn parse( 18 | cursor: &mut Cursor<&Vec>, 19 | table_map_event_by_table_id: &mut HashMap, 20 | row_event_version: u8, 21 | ) -> Result { 22 | // refer: https://mariadb.com/kb/en/rows_event_v1v2-rows_compressed_event_v1/ 23 | let (table_id, _column_count, included_columns) = 24 | EventHeader::parse_rows_event_common_header(cursor, row_event_version)?; 25 | let table_map_event = table_map_event_by_table_id.get(&table_id).unwrap(); 26 | 27 | let mut rows: Vec = Vec::new(); 28 | while cursor.available() > 0 { 29 | let row = RowEvent::parse(cursor, table_map_event, &included_columns)?; 30 | rows.push(row); 31 | } 32 | 33 | Ok(Self { 34 | table_id, 35 | included_columns, 36 | rows, 37 | }) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/event/xa_prepare_event.rs: -------------------------------------------------------------------------------- 1 | use std::io::Cursor; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | 6 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 7 | 8 | #[derive(Debug, Deserialize, Serialize, Clone)] 9 | pub struct XaPrepareEvent { 10 | pub one_phase: bool, 11 | pub format_id: u32, 12 | pub gtrid: String, 13 | pub bqual: String, 14 | } 15 | 16 | impl XaPrepareEvent { 17 | // refer: https://github.com/mysql/mysql-server/blob/5.7/libbinlogevents/src/control_events.cpp#L590 18 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 19 | let one_phase = cursor.read_u8()? == 0; 20 | let format_id = cursor.read_u32::()?; 21 | let gtrid_length = cursor.read_u32::()?; 22 | let bqual_length = cursor.read_u32::()?; 23 | let gtrid = cursor.read_string(gtrid_length as usize)?; 24 | let bqual = cursor.read_string(bqual_length as usize)?; 25 | 26 | Ok(Self { 27 | one_phase, 28 | format_id, 29 | gtrid, 30 | bqual, 31 | }) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/event/xid_event.rs: -------------------------------------------------------------------------------- 1 | use std::io::Cursor; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | use serde::{Deserialize, Serialize}; 5 | 6 | use crate::binlog_error::BinlogError; 7 | 8 | #[derive(Debug, Deserialize, Serialize, Clone)] 9 | pub struct XidEvent { 10 | pub xid: u64, 11 | } 12 | 13 | impl XidEvent { 14 | pub fn parse(cursor: &mut Cursor<&Vec>) -> Result { 15 | Ok(XidEvent { 16 | xid: cursor.read_u64::()?, 17 | }) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/ext/buf_ext.rs: -------------------------------------------------------------------------------- 1 | use std::io::Write; 2 | 3 | use byteorder::WriteBytesExt; 4 | 5 | use crate::{binlog_error::BinlogError, constants}; 6 | 7 | pub trait BufExt { 8 | fn write_null_terminated_string(&mut self, to_write: &str) -> Result<(), BinlogError>; 9 | 10 | fn reverse(&mut self); 11 | 12 | fn xor(&mut self, buf2: Vec) -> Vec; 13 | 14 | fn to_utf8_string(self) -> String; 15 | } 16 | 17 | impl BufExt for Vec { 18 | /// Write a string to buf with 0x00 as end 19 | fn write_null_terminated_string(&mut self, to_write: &str) -> Result<(), BinlogError> { 20 | self.write_all(to_write.as_bytes())?; 21 | self.write_u8(constants::NULL_TERMINATOR)?; 22 | Ok(()) 23 | } 24 | 25 | /// Reverse the order of contents in the buf 26 | fn reverse(&mut self) { 27 | for i in 0..self.len() >> 1 { 28 | let j = self.len() - 1 - i; 29 | self.swap(i, j); 30 | } 31 | } 32 | 33 | fn xor(&mut self, buf2: Vec) -> Vec { 34 | let mut res = Vec::with_capacity(self.len()); 35 | for i in 0..self.len() { 36 | res.push(self[i] ^ buf2[i % buf2.len()]); 37 | } 38 | res 39 | } 40 | 41 | fn to_utf8_string(self) -> String { 42 | if let Ok(str) = String::from_utf8(self.clone()) { 43 | return str; 44 | } 45 | String::from_utf8_lossy(&self).to_string() 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/ext/cursor_ext.rs: -------------------------------------------------------------------------------- 1 | use std::io::{BufRead, Cursor, Read, Seek, SeekFrom}; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | 5 | use crate::{binlog_error::BinlogError, constants}; 6 | 7 | use super::buf_ext::BufExt; 8 | 9 | pub trait CursorExt { 10 | fn read_string(&mut self, size: usize) -> Result; 11 | 12 | fn read_string_without_terminator(&mut self, size: usize) -> Result; 13 | 14 | fn read_null_terminated_string(&mut self) -> Result; 15 | 16 | fn read_packed_number(&mut self) -> Result; 17 | 18 | fn read_bits(&mut self, size: usize, big_endian: bool) -> Result, BinlogError>; 19 | 20 | fn read_bits_as_bytes(&mut self, size: usize, big_endian: bool) 21 | -> Result, BinlogError>; 22 | 23 | fn available(&mut self) -> usize; 24 | 25 | fn read_bytes(&mut self, size: usize) -> Result, BinlogError>; 26 | } 27 | 28 | impl CursorExt for Cursor<&Vec> { 29 | /// Read bytes from cursor and parse into utf8 string 30 | fn read_string(&mut self, size: usize) -> Result { 31 | let mut buf = vec![0; size]; 32 | self.read_exact(&mut buf)?; 33 | Ok(buf.to_utf8_string()) 34 | } 35 | 36 | /// Read a utf8 string from cursor and skip the end signal 37 | fn read_string_without_terminator(&mut self, size: usize) -> Result { 38 | let res = self.read_string(size); 39 | self.seek(SeekFrom::Current(1))?; 40 | res 41 | } 42 | 43 | /// Read variable-length string, the end is 0x00 44 | fn read_null_terminated_string(&mut self) -> Result { 45 | let mut buf = Vec::new(); 46 | self.read_until(constants::NULL_TERMINATOR, &mut buf)?; 47 | buf.pop(); 48 | Ok(buf.to_utf8_string()) 49 | } 50 | 51 | /// Format (first-byte-based): 52 | /// 0-250 - The first byte is the number (in the range 0-250). No additional bytes are used.
53 | /// 251 - SQL NULL value
54 | /// 252 - Two more bytes are used. The number is in the range 251-0xffff.
55 | /// 253 - Three more bytes are used. The number is in the range 0xffff-0xffffff.
56 | /// 254 - Eight more bytes are used. The number is in the range 0xffffff-0xffffffffffffffff. 57 | fn read_packed_number(&mut self) -> Result { 58 | let first = self.read_u8()?; 59 | if first < 0xfb { 60 | Ok(first as usize) 61 | } else if first == 0xfc { 62 | Ok(self.read_u16::()? as usize) 63 | } else if first == 0xfd { 64 | Ok(self.read_u24::()? as usize) 65 | } else if first == 0xfe { 66 | Ok(self.read_u64::()? as usize) 67 | } else { 68 | Err(BinlogError::UnexpectedData( 69 | "read packed number failed".into(), 70 | )) 71 | } 72 | } 73 | 74 | /// Read n bits from cursor to Vec, if the origin data is encoded in BigEndian, reverse the order first 75 | fn read_bits(&mut self, size: usize, big_endian: bool) -> Result, BinlogError> { 76 | // the number of bytes needed is int((count + 7) / 8) 77 | let mut bytes = vec![0u8; (size + 7) >> 3]; 78 | self.read_exact(&mut bytes)?; 79 | 80 | if big_endian { 81 | bytes.reverse(); 82 | } 83 | 84 | let mut bits = vec![false; size]; 85 | for i in 0..size { 86 | let belong_to_byte = bytes[i >> 3]; 87 | let index_in_byte = 1 << (i % 8); 88 | bits[i] = belong_to_byte & index_in_byte != 0; 89 | } 90 | Ok(bits) 91 | } 92 | 93 | /// Read n bits from cursor, if the origin data is encoded in BigEndian, reverse the order first 94 | fn read_bits_as_bytes( 95 | &mut self, 96 | size: usize, 97 | big_endian: bool, 98 | ) -> Result, BinlogError> { 99 | // the number of bytes needed is int((count + 7) / 8) 100 | let mut bytes = vec![0u8; (size + 7) >> 3]; 101 | self.read_exact(&mut bytes)?; 102 | 103 | if big_endian { 104 | bytes.reverse(); 105 | } 106 | Ok(bytes) 107 | } 108 | 109 | /// Read n bytes from cursor and return the buf 110 | fn read_bytes(&mut self, size: usize) -> Result, BinlogError> { 111 | let mut buf = vec![0; size]; 112 | self.read_exact(&mut buf)?; 113 | Ok(buf) 114 | } 115 | 116 | /// Return the available bytes count in cursor 117 | fn available(&mut self) -> usize { 118 | self.get_ref().len() - self.position() as usize 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/ext/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod buf_ext; 2 | pub mod cursor_ext; 3 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod binlog_client; 2 | pub mod binlog_error; 3 | pub mod binlog_parser; 4 | pub mod binlog_stream; 5 | pub mod column; 6 | pub mod command; 7 | mod constants; 8 | pub mod event; 9 | mod ext; 10 | mod network; 11 | -------------------------------------------------------------------------------- /src/network/auth_plugin_switch_packet.rs: -------------------------------------------------------------------------------- 1 | use std::io::{Cursor, Seek, SeekFrom}; 2 | 3 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 4 | 5 | pub struct AuthPluginSwitchPacket { 6 | pub auth_plugin_name: String, 7 | pub scramble: String, 8 | } 9 | 10 | impl AuthPluginSwitchPacket { 11 | pub fn new(packet: &Vec) -> Result { 12 | // refer to: https://mariadb.com/kb/en/connection/#authentication-switch-request 13 | let mut cursor = Cursor::new(packet); 14 | cursor.seek(SeekFrom::Current(1))?; 15 | 16 | let auth_plugin_name = cursor.read_null_terminated_string()?; 17 | let scramble = cursor.read_null_terminated_string()?; 18 | Ok(Self { 19 | auth_plugin_name, 20 | scramble, 21 | }) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/network/error_packet.rs: -------------------------------------------------------------------------------- 1 | use std::io::{Cursor, Seek, SeekFrom}; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | 5 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 6 | 7 | #[derive(Debug)] 8 | pub struct ErrorPacket { 9 | pub error_code: u16, 10 | pub sql_state: String, 11 | pub error_message: String, 12 | } 13 | 14 | impl ErrorPacket { 15 | pub fn new(buf: &Vec) -> Result { 16 | let mut cursor = Cursor::new(buf); 17 | // the first byte is always 0xFF, which means it is an error packet 18 | cursor.seek(SeekFrom::Current(1))?; 19 | 20 | let error_code = cursor.read_u16::()?; 21 | let mut sql_state = "".to_string(); 22 | 23 | if cursor.get_ref()[cursor.position() as usize] == b'#' { 24 | cursor.seek(SeekFrom::Current(1))?; 25 | sql_state = cursor.read_string(5)?; 26 | } 27 | 28 | let length = cursor.get_ref().len() - cursor.position() as usize; 29 | let error_message = cursor.read_string(length)?; 30 | 31 | Ok(Self { 32 | error_code, 33 | sql_state, 34 | error_message, 35 | }) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/network/greeting_packet.rs: -------------------------------------------------------------------------------- 1 | use std::io::{Cursor, Seek, SeekFrom}; 2 | 3 | use byteorder::{LittleEndian, ReadBytesExt}; 4 | 5 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 6 | 7 | pub struct GreetingPacket { 8 | pub protocol_version: u8, 9 | pub server_version: String, 10 | pub thread_id: u32, 11 | pub server_capabilities: u16, 12 | pub server_collation: u8, 13 | pub server_status: u16, 14 | pub scramble: String, 15 | pub plugin_provided_data: String, 16 | } 17 | 18 | impl GreetingPacket { 19 | pub fn new(buf: Vec) -> Result { 20 | let mut cursor = Cursor::new(&buf); 21 | let protocol_version = cursor.read_u8()?; 22 | let server_version = cursor.read_null_terminated_string()?; 23 | let thread_id = cursor.read_u32::()?; 24 | let mut scramble = cursor.read_null_terminated_string()?; 25 | let server_capabilities = cursor.read_u16::()?; 26 | let server_collation = cursor.read_u8()?; 27 | let server_status = cursor.read_u16::()?; 28 | 29 | // reserved 30 | cursor.seek(SeekFrom::Current(13))?; 31 | scramble.push_str(cursor.read_null_terminated_string()?.as_str()); 32 | 33 | let mut plugin_provided_data = "".to_string(); 34 | if cursor.available() > 0 { 35 | plugin_provided_data = cursor.read_null_terminated_string()?; 36 | } 37 | 38 | Ok(Self { 39 | protocol_version, 40 | server_version, 41 | thread_id, 42 | scramble, 43 | server_capabilities, 44 | server_collation, 45 | server_status, 46 | plugin_provided_data, 47 | }) 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/network/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod auth_plugin_switch_packet; 2 | pub mod error_packet; 3 | pub mod greeting_packet; 4 | pub mod packet_channel; 5 | pub mod result_set_row_packet; 6 | -------------------------------------------------------------------------------- /src/network/packet_channel.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | io::{Cursor, Write}, 3 | time::Duration, 4 | }; 5 | 6 | use async_std::prelude::*; 7 | use async_std::{future::timeout, net::TcpStream}; 8 | 9 | use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; 10 | use log::{trace, warn}; 11 | 12 | use crate::binlog_error::BinlogError; 13 | 14 | const MAX_PACKET_LENGTH: usize = 16777215; 15 | 16 | pub struct PacketChannel { 17 | stream: TcpStream, 18 | timeout_secs: u64, 19 | } 20 | 21 | impl PacketChannel { 22 | pub async fn new(ip: &str, port: &str, timeout_secs: u64) -> Result { 23 | let addr = format!("{}:{}", ip, port); 24 | let stream = 25 | match timeout(Duration::from_secs(timeout_secs), TcpStream::connect(&addr)).await { 26 | Ok(Ok(stream)) => stream, 27 | Ok(Err(e)) => return Err(BinlogError::from(e)), 28 | Err(_) => { 29 | return Err(BinlogError::ConnectError(format!( 30 | "Connection timeout after {} seconds while connecting to {}", 31 | timeout_secs, addr 32 | ))) 33 | } 34 | }; 35 | Ok(Self { 36 | stream, 37 | timeout_secs, 38 | }) 39 | } 40 | 41 | pub async fn close(&self) -> Result<(), BinlogError> { 42 | self.stream.shutdown(std::net::Shutdown::Both)?; 43 | Ok(()) 44 | } 45 | 46 | pub async fn write(&mut self, buf: &[u8], sequence: u8) -> Result<(), BinlogError> { 47 | let mut wtr = Vec::new(); 48 | wtr.write_u24::(buf.len() as u32)?; 49 | wtr.write_u8(sequence)?; 50 | Write::write(&mut wtr, buf)?; 51 | self.stream.write_all(&wtr).await?; 52 | Ok(()) 53 | } 54 | 55 | async fn read_packet_info(&mut self) -> Result<(usize, u8), BinlogError> { 56 | let mut buf = vec![0u8; 4]; 57 | // do not call self.read_exact since blocked by receiving no data is expected if there are no new binlog events 58 | self.stream.read_exact(&mut buf).await?; 59 | let mut rdr = Cursor::new(buf); 60 | let length = rdr.read_u24::()? as usize; 61 | let sequence = rdr.read_u8()?; 62 | Ok((length, sequence)) 63 | } 64 | 65 | pub async fn read_with_sequece(&mut self) -> Result<(Vec, u8), BinlogError> { 66 | let (length, sequence) = self.read_packet_info().await?; 67 | let buf = if length == MAX_PACKET_LENGTH { 68 | let mut all_buf = self.read_exact(length).await?; 69 | loop { 70 | let (chunk_length, _) = self.read_packet_info().await?; 71 | let mut chunk_buf = self.read_exact(chunk_length).await?; 72 | all_buf.append(&mut chunk_buf); 73 | if chunk_length != MAX_PACKET_LENGTH { 74 | break; 75 | } 76 | } 77 | trace!("Received big binlog data, full length: {}", all_buf.len()); 78 | all_buf 79 | } else { 80 | self.read_exact(length).await? 81 | }; 82 | Ok((buf, sequence)) 83 | } 84 | 85 | pub async fn read(&mut self) -> Result, BinlogError> { 86 | let (buf, _sequence) = Self::read_with_sequece(self).await?; 87 | Ok(buf) 88 | } 89 | 90 | async fn read_exact(&mut self, length: usize) -> Result, BinlogError> { 91 | let mut buf = vec![0u8; length]; 92 | // keep reading data until the complete packet is received 93 | // MySQL protocol packets may require multiple reads for complete reception 94 | let wait_data_millis = 10; 95 | let max_zero_reads = self.timeout_secs * 1000 / wait_data_millis; 96 | let mut read_count = 0; 97 | let mut zero_reads = 0; 98 | 99 | while read_count < length { 100 | match timeout( 101 | Duration::from_secs(self.timeout_secs), 102 | self.stream.read(&mut buf[read_count..]), 103 | ) 104 | .await 105 | { 106 | Ok(Ok(n)) => { 107 | if n == 0 { 108 | zero_reads += 1; 109 | if zero_reads >= max_zero_reads { 110 | return Err(BinlogError::UnexpectedData(format!( 111 | "Too many zero-length reads. Expected data length: {}, read so far: {}", 112 | length, read_count 113 | ))); 114 | } 115 | warn!( 116 | "Stream reading binlog returns zero-length data, Expected data length: {}, read so far: {}", 117 | length, read_count 118 | ); 119 | async_std::task::sleep(Duration::from_millis(wait_data_millis)).await; 120 | continue; 121 | } 122 | zero_reads = 0; 123 | read_count += n; 124 | trace!( 125 | "Stream reading binlog data, Expected data length: {}, read so far: {}", 126 | length, 127 | read_count 128 | ); 129 | } 130 | Ok(Err(e)) => { 131 | return Err(BinlogError::from(e)); 132 | } 133 | Err(_) => { 134 | return Err(BinlogError::UnexpectedData(format!( 135 | "Read binlog timeout, expect data length: {}, read so far: {}", 136 | length, read_count 137 | ))); 138 | } 139 | } 140 | } 141 | Ok(buf) 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/network/result_set_row_packet.rs: -------------------------------------------------------------------------------- 1 | use std::io::Cursor; 2 | 3 | use crate::{binlog_error::BinlogError, ext::cursor_ext::CursorExt}; 4 | 5 | pub struct ResultSetRowPacket { 6 | pub values: Vec, 7 | } 8 | 9 | impl ResultSetRowPacket { 10 | pub fn new(buf: &Vec) -> Result { 11 | let mut cursor = Cursor::new(buf); 12 | let mut values = Vec::new(); 13 | 14 | while cursor.available() > 0 { 15 | let length = cursor.read_packed_number()?; 16 | let value = cursor.read_string(length)?; 17 | values.push(value); 18 | } 19 | 20 | Ok(Self { values }) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/.env: -------------------------------------------------------------------------------- 1 | db_url=mysql://root:123456@127.0.0.1:3307 2 | server_id=200 3 | default_db="db_test" 4 | default_tb="tb_test" 5 | binlog_parse_millis=500 -------------------------------------------------------------------------------- /tests/data_type_tests/binary_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_type::ColumnType; 5 | use serial_test::serial; 6 | 7 | use crate::data_type_tests::bytes_test_util::BytesTestUtil; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_binary_255() { 12 | let (values, check_values) = BytesTestUtil::generate_visible_char_values(); 13 | BytesTestUtil::run_and_check("BINARY(255)", ColumnType::String, &values, &check_values); 14 | 15 | let (values, check_values) = BytesTestUtil::generate_trailing_space_values(true); 16 | BytesTestUtil::run_and_check("BINARY(255)", ColumnType::String, &values, &check_values); 17 | 18 | let (values, check_values) = BytesTestUtil::generate_trailing_nul_values(false); 19 | BytesTestUtil::run_and_check("BINARY(255)", ColumnType::String, &values, &check_values); 20 | } 21 | 22 | #[test] 23 | #[serial] 24 | fn test_varbinary_255() { 25 | let (values, check_values) = BytesTestUtil::generate_visible_char_values(); 26 | BytesTestUtil::run_and_check( 27 | "VARBINARY(255)", 28 | ColumnType::VarChar, 29 | &values, 30 | &check_values, 31 | ); 32 | 33 | let (values, check_values) = BytesTestUtil::generate_trailing_space_values(true); 34 | BytesTestUtil::run_and_check( 35 | "VARBINARY(255)", 36 | ColumnType::VarChar, 37 | &values, 38 | &check_values, 39 | ); 40 | 41 | let (values, check_values) = BytesTestUtil::generate_trailing_nul_values(true); 42 | BytesTestUtil::run_and_check( 43 | "VARBINARY(255)", 44 | ColumnType::VarChar, 45 | &values, 46 | &check_values, 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/data_type_tests/bit_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 5 | use serial_test::serial; 6 | 7 | use crate::runner::test_runner::test::TestRunner; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_bit3() { 12 | let col_type = "BIT(3)"; 13 | let values = vec!["0", "1", "2", "3", "4", "5", "6", "7"]; 14 | run_and_check(col_type, &values); 15 | } 16 | 17 | #[test] 18 | #[serial] 19 | fn test_bit64() { 20 | let col_type = "BIT(64)"; 21 | let values = vec![ 22 | "1234567890123", 23 | "2345678901234", 24 | "3456789012345", 25 | "4567890123456", 26 | "5678901234567", 27 | "6789012345678", 28 | "7890123456789", 29 | ]; 30 | run_and_check(col_type, &values); 31 | } 32 | 33 | fn run_and_check(col_type: &str, values: &Vec<&str>) { 34 | let runner = TestRunner::run_one_col_test(col_type, values, &vec![]); 35 | for i in 0..values.len() { 36 | let value: u64 = values[i].parse::().unwrap(); 37 | assert_eq!( 38 | runner.insert_events[0].rows[i].column_values[0], 39 | ColumnValue::Bit(value) 40 | ); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/data_type_tests/blob_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_type::ColumnType; 5 | use serial_test::serial; 6 | 7 | use crate::data_type_tests::bytes_test_util::BytesTestUtil; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_tinyblob() { 12 | run_and_check("TINYBLOB"); 13 | } 14 | 15 | #[test] 16 | #[serial] 17 | fn test_blob() { 18 | run_and_check("BLOB"); 19 | } 20 | 21 | #[test] 22 | #[serial] 23 | fn test_mediumblob() { 24 | run_and_check("MEDIUMBLOB"); 25 | } 26 | 27 | #[test] 28 | #[serial] 29 | fn test_longblob() { 30 | run_and_check("LONGBLOB"); 31 | } 32 | 33 | fn run_and_check(mysql_column_type: &str) { 34 | let (values, check_values) = BytesTestUtil::generate_visible_char_values(); 35 | BytesTestUtil::run_and_check(mysql_column_type, ColumnType::Blob, &values, &check_values); 36 | 37 | let (values, check_values) = BytesTestUtil::generate_trailing_space_values(true); 38 | BytesTestUtil::run_and_check(mysql_column_type, ColumnType::Blob, &values, &check_values); 39 | 40 | let (values, check_values) = BytesTestUtil::generate_trailing_nul_values(true); 41 | BytesTestUtil::run_and_check(mysql_column_type, ColumnType::Blob, &values, &check_values); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/data_type_tests/bytes_test_util.rs: -------------------------------------------------------------------------------- 1 | use mysql_binlog_connector_rust::column::{column_type::ColumnType, column_value::ColumnValue}; 2 | 3 | use crate::runner::test_runner::test::TestRunner; 4 | 5 | /// A util to run tests for column types whose parsed binlog values are raw bytes, including: 6 | /// CHAR, VARCHAR, BINARY, VARBINARY, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB 7 | pub(crate) struct BytesTestUtil {} 8 | 9 | // The visible character number range is 32-126 10 | const MAX_TEST_STR_LENGTH: u8 = 95; 11 | 12 | impl BytesTestUtil { 13 | pub fn run_and_check( 14 | mysql_column_type: &str, 15 | binlog_column_type: ColumnType, 16 | values: &Vec, 17 | check_values: &Vec>, 18 | ) { 19 | let values: Vec<&str> = values.into_iter().map(|i| i.as_str()).collect(); 20 | let runner = TestRunner::run_one_col_test(mysql_column_type, &values, &vec![]); 21 | for i in 0..check_values.len() { 22 | let column_value = match binlog_column_type { 23 | ColumnType::String | ColumnType::VarChar => { 24 | ColumnValue::String(check_values[i].clone()) 25 | } 26 | ColumnType::Blob => ColumnValue::Blob(check_values[i].clone()), 27 | _ => ColumnValue::None, 28 | }; 29 | assert_eq!( 30 | runner.insert_events[0].rows[i].column_values[0], 31 | column_value 32 | ); 33 | } 34 | } 35 | 36 | pub fn generate_visible_char_values() -> (Vec, Vec>) { 37 | let mut values = Vec::new(); 38 | let mut check_values = Vec::new(); 39 | 40 | let non_blank_str = |n: u8| -> (String, Vec) { 41 | let mut str = String::new(); 42 | let mut bytes = Vec::new(); 43 | // The visible character number range is 32-126 44 | for i in 1..n { 45 | bytes.push(32 + i as u8); 46 | str.push(char::from_u32(32 + i as u32).unwrap()); 47 | } 48 | (str, bytes) 49 | }; 50 | 51 | // generate non-blank string by visible characters, 52 | // the first visible character is space, the corresponding ascii code is 32 53 | // if MAX_TEST_STR_LENGTH = 4, then below strings will be generated 54 | // ' !' 55 | // ' !"' 56 | // ' !"#' 57 | for i in 0..MAX_TEST_STR_LENGTH { 58 | let (mut str, bytes) = non_blank_str(i + 1); 59 | // character escapes 60 | str = str.replace("\\", "\\\\"); 61 | str = str.replace("'", "\\\'"); 62 | values.push(str); 63 | check_values.push(bytes); 64 | } 65 | 66 | (Self::get_test_values(&values), check_values) 67 | } 68 | 69 | pub fn generate_trailing_space_values( 70 | keep_trailing_space_in_binlog: bool, 71 | ) -> (Vec, Vec>) { 72 | let mut values = Vec::new(); 73 | let mut check_values = Vec::new(); 74 | 75 | // cases with spaces 76 | values.push("".to_string()); 77 | values.push(" ".to_string()); 78 | values.push("a ".to_string()); 79 | values.push(" a".to_string()); 80 | values.push(" a ".to_string()); 81 | values.push(" a b ".to_string()); 82 | 83 | for i in check_values.len()..values.len() { 84 | let mut bytes = Vec::new(); 85 | 86 | let str = if keep_trailing_space_in_binlog { 87 | &values[i] 88 | } else { 89 | values[i].trim_end() 90 | }; 91 | 92 | for i in 0..str.len() { 93 | bytes.push(str.chars().nth(i).unwrap() as u8); 94 | } 95 | check_values.push(bytes); 96 | } 97 | 98 | (Self::get_test_values(&values), check_values) 99 | } 100 | 101 | pub fn generate_trailing_nul_values( 102 | keep_trailing_nul_in_binlog: bool, 103 | ) -> (Vec, Vec>) { 104 | let mut values = Vec::new(); 105 | let mut check_values = Vec::new(); 106 | 107 | values.push("\0\0\0\0\0\0".to_string()); 108 | values.push("a\0\0\0\0\0".to_string()); 109 | values.push("\0\0\0\0\0a".to_string()); 110 | values.push("\0\0a\0\0\0".to_string()); 111 | values.push("\0a\0a\0\0".to_string()); 112 | 113 | if keep_trailing_nul_in_binlog { 114 | check_values.push(vec![0u8, 0, 0, 0, 0, 0]); 115 | check_values.push(vec![97, 0, 0, 0, 0, 0]); 116 | check_values.push(vec![0, 0, 0, 0, 0, 97]); 117 | check_values.push(vec![0, 0, 97, 0, 0, 0]); 118 | check_values.push(vec![0, 97, 0, 97, 0, 0]); 119 | } else { 120 | check_values.push(vec![]); 121 | check_values.push(vec![97]); 122 | check_values.push(vec![0, 0, 0, 0, 0, 97]); 123 | check_values.push(vec![0, 0, 97]); 124 | check_values.push(vec![0, 97, 0, 97]); 125 | } 126 | 127 | (Self::get_test_values(&values), check_values) 128 | } 129 | 130 | fn get_test_values(values: &Vec) -> Vec { 131 | // ["a", "ab"] -> ["('a')", "('ab')"] 132 | let mut test_values = Vec::new(); 133 | for s in values.clone() { 134 | test_values.push(format!("('{}')", s)); 135 | } 136 | test_values 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /tests/data_type_tests/char_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_type::ColumnType; 5 | use serial_test::serial; 6 | 7 | use crate::data_type_tests::bytes_test_util::BytesTestUtil; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_char_255() { 12 | let (values, check_values) = BytesTestUtil::generate_visible_char_values(); 13 | BytesTestUtil::run_and_check("CHAR(255)", ColumnType::String, &values, &check_values); 14 | 15 | let (values, check_values) = BytesTestUtil::generate_trailing_space_values(false); 16 | BytesTestUtil::run_and_check("CHAR(255)", ColumnType::String, &values, &check_values); 17 | } 18 | 19 | #[test] 20 | #[serial] 21 | fn test_varchar_255() { 22 | let (values, check_values) = BytesTestUtil::generate_visible_char_values(); 23 | BytesTestUtil::run_and_check("VARCHAR(255)", ColumnType::VarChar, &values, &check_values); 24 | 25 | let (values, check_values) = BytesTestUtil::generate_trailing_space_values(true); 26 | BytesTestUtil::run_and_check("VARCHAR(255)", ColumnType::VarChar, &values, &check_values); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/data_type_tests/charset_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use std::vec; 4 | 5 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 6 | use serial_test::serial; 7 | 8 | use crate::runner::test_runner::test::TestRunner; 9 | 10 | #[test] 11 | #[serial] 12 | fn test_utf8mb4() { 13 | let col_type = "VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"; 14 | let values = vec!["('123abc中文😀')"]; 15 | // "123": [49, 50, 51] 16 | // "abc": [97, 98, 99]) 17 | // "中文": [228, 184, 173, 230, 150, 135] 18 | // "😀": [240, 159, 152, 128] 19 | let check_values = vec![vec![ 20 | 49, 50, 51, 97, 98, 99, 228, 184, 173, 230, 150, 135, 240, 159, 152, 128, 21 | ]]; 22 | 23 | run_and_check(col_type, "SET names utf8mb4", &values, &check_values); 24 | } 25 | 26 | #[test] 27 | #[serial] 28 | fn test_utf8() { 29 | let col_type = "VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"; 30 | let values = vec!["('123abc中文')"]; 31 | // "123": [49, 50, 51] 32 | // "abc": [97, 98, 99]) 33 | // "中文": [228, 184, 173, 230, 150, 135] 34 | let check_values = vec![vec![49, 50, 51, 97, 98, 99, 228, 184, 173, 230, 150, 135]]; 35 | 36 | run_and_check(col_type, "SET names utf8", &values, &check_values); 37 | } 38 | 39 | #[test] 40 | #[serial] 41 | fn test_latin1() { 42 | let col_type = "VARCHAR(255) CHARACTER SET latin1"; 43 | let values = vec!["('123abc')"]; 44 | // "123": [49, 50, 51] 45 | // "abc": [97, 98, 99]) 46 | let check_values = vec![vec![49, 50, 51, 97, 98, 99]]; 47 | 48 | run_and_check(col_type, "SET names utf8", &values, &check_values); 49 | } 50 | 51 | #[test] 52 | #[serial] 53 | fn test_gbk() { 54 | let col_type = "VARCHAR(255) CHARACTER SET gbk"; 55 | let values = vec!["('123abc中文')"]; 56 | // "123": [49, 50, 51] 57 | // "abc": [97, 98, 99]) 58 | // "中文": [214, 208, 206, 196] 59 | let check_values = vec![vec![49, 50, 51, 97, 98, 99, 214, 208, 206, 196]]; 60 | 61 | run_and_check(col_type, "SET names utf8", &values, &check_values); 62 | } 63 | 64 | #[test] 65 | #[serial] 66 | fn test_gb18030() { 67 | let col_type = "VARCHAR(255) CHARACTER SET gb18030"; 68 | let values = vec!["('123abc中文😀')"]; 69 | // "123": [49, 50, 51] 70 | // "abc": [97, 98, 99]) 71 | // "中文": [214, 208, 206, 196] 72 | // "😀": [148, 57, 252, 54] 73 | let check_values = vec![vec![ 74 | 49, 50, 51, 97, 98, 99, 214, 208, 206, 196, 148, 57, 252, 54, 75 | ]]; 76 | 77 | run_and_check(col_type, "SET names utf8mb4", &values, &check_values); 78 | } 79 | 80 | fn run_and_check( 81 | col_type: &str, 82 | init_sql: &str, 83 | values: &Vec<&str>, 84 | check_values: &Vec>, 85 | ) { 86 | let runner = TestRunner::run_one_col_test(col_type, values, &vec![init_sql]); 87 | for i in 0..check_values.len() { 88 | assert_eq!( 89 | runner.insert_events[0].rows[i].column_values[0], 90 | ColumnValue::String(check_values[i].clone()) 91 | ); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /tests/data_type_tests/date_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 4 | use serial_test::serial; 5 | 6 | use crate::runner::test_runner::test::TestRunner; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_date() { 11 | let col_type = "DATE"; 12 | let values = vec!["'1000-01-01'", "'9999-12-31'"]; 13 | let check_values = ["1000-01-01", "9999-12-31"]; 14 | 15 | let runner = 16 | TestRunner::run_one_col_test(col_type, &values, &vec!["SET @@session.time_zone='UTC'"]); 17 | 18 | assert_eq!(runner.insert_events[0].rows.len(), check_values.len()); 19 | for i in 0..check_values.len() { 20 | assert_eq!( 21 | runner.insert_events[0].rows[i].column_values[0], 22 | ColumnValue::Date(check_values[i].to_string()) 23 | ); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/data_type_tests/date_time_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 4 | use serial_test::serial; 5 | 6 | use crate::runner::test_runner::test::TestRunner; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_datetime6() { 11 | // https://dev.mysql.com/doc/refman/8.0/en/datetime.html 12 | let col_type = "DATETIME(6)"; 13 | 14 | // With the fractional part included, the format for these values is 'YYYY-MM-DD hh:mm:ss[.fraction]', 15 | // the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999' 16 | let values = vec![ 17 | "'1000-01-01 00:00:00.000000'", 18 | "'9999-12-31 23:59:59.999999'", 19 | "'2022-01-02 03:04:05.0'", 20 | "'2022-01-02 03:04:05.1'", 21 | "'2022-01-02 03:04:05.12'", 22 | "'2022-01-02 03:04:05.123'", 23 | "'2022-01-02 03:04:05.1234'", 24 | "'2022-01-02 03:04:05.12345'", 25 | "'2022-01-02 03:04:05.123456'", 26 | "'2022-01-02 03:04:05.000001'", 27 | "'2022-01-02 03:04:05.000012'", 28 | "'2022-01-02 03:04:05.000123'", 29 | "'2022-01-02 03:04:05.001234'", 30 | "'2022-01-02 03:04:05.012345'", 31 | ]; 32 | 33 | let check_values = [ 34 | "1000-01-01 00:00:00.000000", 35 | "9999-12-31 23:59:59.999999", 36 | "2022-01-02 03:04:05.000000", 37 | "2022-01-02 03:04:05.100000", 38 | "2022-01-02 03:04:05.120000", 39 | "2022-01-02 03:04:05.123000", 40 | "2022-01-02 03:04:05.123400", 41 | "2022-01-02 03:04:05.123450", 42 | "2022-01-02 03:04:05.123456", 43 | "2022-01-02 03:04:05.000001", 44 | "2022-01-02 03:04:05.000012", 45 | "2022-01-02 03:04:05.000123", 46 | "2022-01-02 03:04:05.001234", 47 | "2022-01-02 03:04:05.012345", 48 | ]; 49 | 50 | run_and_check(col_type, &values, &check_values); 51 | } 52 | 53 | #[test] 54 | #[serial] 55 | fn test_datetime3() { 56 | let col_type = "DATETIME(3)"; 57 | 58 | let values = vec![ 59 | "'1000-01-01 00:00:00.000'", 60 | "'9999-12-31 23:59:59.999'", 61 | "'2022-01-02 03:04:05.0'", 62 | "'2022-01-02 03:04:05.1'", 63 | "'2022-01-02 03:04:05.12'", 64 | "'2022-01-02 03:04:05.123'", 65 | "'2022-01-02 03:04:05.001'", 66 | "'2022-01-02 03:04:05.012'", 67 | ]; 68 | 69 | let check_values = [ 70 | "1000-01-01 00:00:00.000000", 71 | "9999-12-31 23:59:59.999000", 72 | "2022-01-02 03:04:05.000000", 73 | "2022-01-02 03:04:05.100000", 74 | "2022-01-02 03:04:05.120000", 75 | "2022-01-02 03:04:05.123000", 76 | "2022-01-02 03:04:05.001000", 77 | "2022-01-02 03:04:05.012000", 78 | ]; 79 | 80 | run_and_check(col_type, &values, &check_values); 81 | } 82 | 83 | #[test] 84 | #[serial] 85 | fn test_datetime() { 86 | // https://dev.mysql.com/doc/refman/8.0/en/datetime.html 87 | let col_type = "DATETIME"; 88 | let values = vec!["'1000-01-01 00:00:00.000000'", "'9999-12-31 23:59:59'"]; 89 | let check_values = ["1000-01-01 00:00:00.000000", "9999-12-31 23:59:59.000000"]; 90 | run_and_check(col_type, &values, &check_values); 91 | } 92 | 93 | fn run_and_check(col_type: &str, values: &[&str], check_values: &[&str]) { 94 | let runner = 95 | TestRunner::run_one_col_test(col_type, values, &vec!["SET @@session.time_zone='UTC'"]); 96 | 97 | assert_eq!(runner.insert_events[0].rows.len(), check_values.len()); 98 | for i in 0..check_values.len() { 99 | assert_eq!( 100 | runner.insert_events[0].rows[i].column_values[0], 101 | ColumnValue::DateTime(check_values[i].to_string()), 102 | ); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /tests/data_type_tests/decimal_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 5 | use serial_test::serial; 6 | 7 | use crate::runner::{mock::test::Mock, test_runner::test::TestRunner}; 8 | 9 | // refer to: https://dev.mysql.com/doc/refman/8.0/en/data-types.html 10 | // refer to: https://dev.mysql.com/doc/refman/8.0/en/fixed-point-types.html 11 | 12 | #[test] 13 | #[serial] 14 | fn test_decimal_4_0() { 15 | // DECIMAL(4,0), binlog: [2 bytes] . [0 bytes] 16 | let mut runner = TestRunner::new(); 17 | run_decimal_tests(&mut runner, 4, 0); 18 | } 19 | 20 | #[test] 21 | #[serial] 22 | fn test_decimal_4_4() { 23 | // DECIMAL(4,4), binlog: [0 bytes] . [2 bytes] 24 | let mut runner = TestRunner::new(); 25 | run_decimal_tests(&mut runner, 4, 4); 26 | } 27 | 28 | #[test] 29 | #[serial] 30 | fn test_decimal_10_0() { 31 | // DECIMAL(10,0), binlog: [1 byte][4 bytes] . [0 bytes] 32 | let mut runner = TestRunner::new(); 33 | run_decimal_tests(&mut runner, 10, 0); 34 | } 35 | 36 | #[test] 37 | #[serial] 38 | fn test_decimal_10_10() { 39 | // DECIMAL(10,10), binlog: [0 bytes] . [4 bytes][1 byte] 40 | let mut runner = TestRunner::new(); 41 | run_decimal_tests(&mut runner, 10, 10); 42 | } 43 | 44 | #[test] 45 | #[serial] 46 | fn test_decimal_10_4() { 47 | // DECIMAL(10,4), binlog: [3 bytes] . [2 bytes] 48 | let mut runner = TestRunner::new(); 49 | run_decimal_tests(&mut runner, 10, 4); 50 | } 51 | 52 | #[test] 53 | #[serial] 54 | fn test_decimal_18_9() { 55 | // DECIMAL(18,9), binlog: [4 bytes] . [4 bytes] 56 | let mut runner = TestRunner::new(); 57 | runner.binlog_parse_millis = 500; 58 | run_decimal_tests(&mut runner, 18, 9); 59 | } 60 | 61 | #[test] 62 | #[serial] 63 | fn test_decimal_27_13() { 64 | // DECIMAL(27,13), binlog: [3 bytes][4 bytes] . [4 bytes][2 bytes] 65 | let mut runner = TestRunner::new(); 66 | runner.binlog_parse_millis = 500; 67 | run_decimal_tests(&mut runner, 27, 13); 68 | } 69 | 70 | #[test] 71 | #[serial] 72 | fn test_decimal_47_25() { 73 | // DECIMAL(47,25), binlog: [2 bytes][4 bytes][4 bytes] . [4 bytes][4 bytes][4 bytes] 74 | let mut runner = TestRunner::new(); 75 | runner.binlog_parse_millis = 1000; 76 | run_decimal_tests(&mut runner, 47, 25); 77 | } 78 | 79 | fn run_decimal_tests(runner: &mut TestRunner, precision: u8, scale: u8) { 80 | let col_type = format!("DECIMAL({},{})", precision, scale); 81 | let prepare_sqls = vec![Mock::one_col_create_sql(&col_type)]; 82 | 83 | let (values, check_values) = generate_decimal_values(precision, scale); 84 | let mut insert_sqls = vec![]; 85 | for v in values { 86 | insert_sqls.push(Mock::one_col_insert_sql(&vec![v.as_str()])); 87 | } 88 | 89 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &insert_sqls); 90 | for i in 0..check_values.len() { 91 | assert_eq!( 92 | runner.insert_events[i].rows[0].column_values[0], 93 | ColumnValue::Decimal(check_values[i].clone()) 94 | ); 95 | } 96 | } 97 | 98 | fn generate_decimal_values(precision: u8, scale: u8) -> (Vec, Vec) { 99 | // given precesion = 10, scale = 4, integral = 6 100 | let integral = precision - scale; 101 | let mut tmp_values = Vec::new(); 102 | 103 | let n_digit_str = |c: char, n: u8| -> String { 104 | let mut res = String::new(); 105 | for _ in 0..n { 106 | res.push(c); 107 | } 108 | res 109 | }; 110 | 111 | // 9, 99, ... 999999 112 | for i in 0..integral { 113 | let intg = n_digit_str('9', i + 1); 114 | tmp_values.push(intg.clone()); 115 | } 116 | 117 | // 0.9, 0.99, ... 0.9999 118 | for j in 0..scale { 119 | let frac = n_digit_str('9', j + 1); 120 | tmp_values.push("0.".to_string() + &frac); 121 | } 122 | 123 | // 9.9, 9.99, 99.9, 99.99 ... 999999.9999 124 | for i in 0..integral { 125 | let intg = n_digit_str('9', i + 1); 126 | for j in 0..scale { 127 | let frac = n_digit_str('9', j + 1); 128 | tmp_values.push(intg.clone() + "." + &frac); 129 | } 130 | } 131 | 132 | // 9.9, 90.09, ... 900000.0009 133 | for i in 0..integral { 134 | let intg = n_digit_str('0', i); 135 | for j in 0..scale { 136 | let frac = n_digit_str('0', j); 137 | tmp_values.push("9".to_string() + &intg + "." + &frac + "9"); 138 | } 139 | } 140 | 141 | // negative values 142 | let mut values = tmp_values.clone(); 143 | for i in 0..tmp_values.len() { 144 | values.push("-".to_string() + &tmp_values[i]); 145 | } 146 | 147 | // 0 148 | values.push("0".to_string()); 149 | 150 | // ["0", "1.1"] -> ["(0)", "(1.1)"] 151 | let mut test_values = Vec::new(); 152 | for s in values.clone() { 153 | test_values.push("(".to_string() + &s + ")"); 154 | } 155 | 156 | // if scale = 4 157 | // ["0", "1.1"] -> ["(0.0000)", "(1.1000)"] 158 | let mut check_values = Vec::new(); 159 | for s in values.clone() { 160 | if scale <= 0 { 161 | check_values.push(s.clone()); 162 | } else if !s.contains(".") { 163 | check_values.push(s + "." + &n_digit_str('0', scale)); 164 | } else { 165 | let point_index = s.find(".").unwrap() as u8; 166 | let append_zeros = n_digit_str('0', scale - (s.len() as u8 - point_index - 1)); 167 | check_values.push(s + &append_zeros); 168 | } 169 | } 170 | 171 | (test_values, check_values) 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /tests/data_type_tests/enum_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use std::vec; 4 | 5 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 6 | use serial_test::serial; 7 | 8 | use crate::runner::test_runner::test::TestRunner; 9 | 10 | #[test] 11 | #[serial] 12 | fn test_enum() { 13 | // refer: https://dev.mysql.com/doc/refman/8.0/en/enum.html 14 | // An ENUM column can have a maximum of 65,535 distinct elements. 15 | let col_type = "ENUM('x-small', 'small', 'medium', 'large', 'x-large')"; 16 | let values = vec!["'x-small'", "'small'", "'medium'", "'large'", "'x-large'"]; 17 | let check_values = vec![1, 2, 3, 4, 5]; 18 | run_and_check(col_type, &values, &check_values); 19 | } 20 | 21 | fn run_and_check(col_type: &str, values: &Vec<&str>, check_values: &Vec) { 22 | let runner = TestRunner::run_one_col_test(col_type, values, &vec![]); 23 | for i in 0..check_values.len() { 24 | assert_eq!( 25 | runner.insert_events[0].rows[i].column_values[0], 26 | ColumnValue::Enum(check_values[i]), 27 | ); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/data_type_tests/mod.rs: -------------------------------------------------------------------------------- 1 | mod binary_tests; 2 | mod bit_tests; 3 | mod blob_tests; 4 | mod bytes_test_util; 5 | mod char_tests; 6 | mod charset_tests; 7 | mod date_tests; 8 | mod date_time_tests; 9 | mod decimal_tests; 10 | mod enum_tests; 11 | mod json_tests; 12 | mod numeric_tests; 13 | mod set_tests; 14 | mod text_tests; 15 | mod time_tests; 16 | mod timestamp_tests; 17 | mod year_tests; 18 | -------------------------------------------------------------------------------- /tests/data_type_tests/numeric_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 5 | use serial_test::serial; 6 | 7 | use crate::runner::test_runner::test::TestRunner; 8 | 9 | // refer to: https://dev.mysql.com/doc/refman/8.0/en/data-types.html 10 | 11 | #[test] 12 | #[serial] 13 | fn test_tinyint() { 14 | let runner = run("TINYINT", &vec!["-128", "127"]); 15 | assert_eq!( 16 | runner.insert_events[0].rows[0].column_values[0], 17 | ColumnValue::Tiny(-128) 18 | ); 19 | assert_eq!( 20 | runner.insert_events[0].rows[1].column_values[0], 21 | ColumnValue::Tiny(127) 22 | ); 23 | } 24 | 25 | #[test] 26 | #[serial] 27 | fn test_tinyint_unsigned() { 28 | let runner = run("TINYINT UNSIGNED", &vec!["255", "127"]); 29 | assert_eq!( 30 | runner.insert_events[0].rows[0].column_values[0], 31 | ColumnValue::Tiny(-1) 32 | ); 33 | assert_eq!( 34 | runner.insert_events[0].rows[1].column_values[0], 35 | ColumnValue::Tiny(127) 36 | ); 37 | } 38 | 39 | #[test] 40 | #[serial] 41 | fn test_smallint() { 42 | let runner = run("SMALLINT", &vec!["-32768", "32767"]); 43 | assert_eq!( 44 | runner.insert_events[0].rows[0].column_values[0], 45 | ColumnValue::Short(-32768) 46 | ); 47 | assert_eq!( 48 | runner.insert_events[0].rows[1].column_values[0], 49 | ColumnValue::Short(32767) 50 | ); 51 | } 52 | 53 | #[test] 54 | #[serial] 55 | fn test_smallint_unsigned() { 56 | let runner = run("SMALLINT UNSIGNED", &vec!["65535", "32767"]); 57 | assert_eq!( 58 | runner.insert_events[0].rows[0].column_values[0], 59 | ColumnValue::Short(-1) 60 | ); 61 | assert_eq!( 62 | runner.insert_events[0].rows[1].column_values[0], 63 | ColumnValue::Short(32767) 64 | ); 65 | } 66 | 67 | #[test] 68 | #[serial] 69 | fn test_mediumint() { 70 | let runner = run("MEDIUMINT", &vec!["-8388608", "8388607"]); 71 | assert_eq!( 72 | runner.insert_events[0].rows[0].column_values[0], 73 | ColumnValue::Long(-8388608) 74 | ); 75 | assert_eq!( 76 | runner.insert_events[0].rows[1].column_values[0], 77 | ColumnValue::Long(8388607) 78 | ); 79 | } 80 | 81 | #[test] 82 | #[serial] 83 | fn test_mediumint_unsigned() { 84 | let runner = run("MEDIUMINT UNSIGNED", &vec!["16777215", "8388607"]); 85 | assert_eq!( 86 | runner.insert_events[0].rows[0].column_values[0], 87 | ColumnValue::Long(-1) 88 | ); 89 | assert_eq!( 90 | runner.insert_events[0].rows[1].column_values[0], 91 | ColumnValue::Long(8388607) 92 | ); 93 | } 94 | 95 | #[test] 96 | #[serial] 97 | fn test_int() { 98 | let runner = run("INT", &vec!["-2147483648", "2147483647"]); 99 | assert_eq!( 100 | runner.insert_events[0].rows[0].column_values[0], 101 | ColumnValue::Long(-2147483648) 102 | ); 103 | assert_eq!( 104 | runner.insert_events[0].rows[1].column_values[0], 105 | ColumnValue::Long(2147483647) 106 | ); 107 | } 108 | 109 | #[test] 110 | #[serial] 111 | fn test_int_unsigned() { 112 | let runner = run("INT UNSIGNED", &vec!["4294967295", "2147483647"]); 113 | assert_eq!( 114 | runner.insert_events[0].rows[0].column_values[0], 115 | ColumnValue::Long(-1) 116 | ); 117 | assert_eq!( 118 | runner.insert_events[0].rows[1].column_values[0], 119 | ColumnValue::Long(2147483647) 120 | ); 121 | } 122 | 123 | #[test] 124 | #[serial] 125 | fn test_bigint() { 126 | let runner = run( 127 | "BIGINT", 128 | &vec!["-9223372036854775808", "9223372036854775807"], 129 | ); 130 | assert_eq!( 131 | runner.insert_events[0].rows[0].column_values[0], 132 | ColumnValue::LongLong(-9223372036854775808) 133 | ); 134 | assert_eq!( 135 | runner.insert_events[0].rows[1].column_values[0], 136 | ColumnValue::LongLong(9223372036854775807) 137 | ); 138 | } 139 | 140 | #[test] 141 | #[serial] 142 | fn test_bigint_unsigned() { 143 | let runner = run( 144 | "BIGINT UNSIGNED", 145 | &vec!["18446744073709551615", "9223372036854775807"], 146 | ); 147 | assert_eq!( 148 | runner.insert_events[0].rows[0].column_values[0], 149 | ColumnValue::LongLong(-1) 150 | ); 151 | assert_eq!( 152 | runner.insert_events[0].rows[1].column_values[0], 153 | ColumnValue::LongLong(9223372036854775807) 154 | ); 155 | } 156 | 157 | #[test] 158 | #[serial] 159 | fn test_float() { 160 | let runner = run("FLOAT(10,5)", &vec!["1234.12345"]); 161 | assert_eq!( 162 | runner.insert_events[0].rows[0].column_values[0], 163 | ColumnValue::Float(1234.12345), 164 | ); 165 | } 166 | 167 | #[test] 168 | #[serial] 169 | fn test_double() { 170 | let runner = run("DOUBLE(20, 10)", &vec!["1234567890.0123456789"]); 171 | assert_eq!( 172 | runner.insert_events[0].rows[0].column_values[0], 173 | ColumnValue::Double(1234567890.0123456789) 174 | ); 175 | } 176 | 177 | fn run(col_type: &str, values: &[&str]) -> TestRunner { 178 | TestRunner::run_one_col_test(col_type, values, &vec![]) 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /tests/data_type_tests/set_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use std::vec; 4 | 5 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 6 | use serial_test::serial; 7 | 8 | use crate::runner::test_runner::test::TestRunner; 9 | 10 | #[test] 11 | #[serial] 12 | fn test_set_name() { 13 | // refer: https://dev.mysql.com/doc/refman/8.0/en/set.html 14 | // A SET column can have a maximum of 64 distinct members. 15 | let col_type = 16 | "SET('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18')"; 17 | let values = vec![ 18 | "'1'", "'2'", "'3'", "'4'", "'5'", "'6'", "'7'", "'8'", "'9'", "'10'", "'11'", "'12'", 19 | "'13'", "'14'", "'15'", "'16'", "'17'", "'18'", 20 | ]; 21 | let check_values = vec![ 22 | 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 23 | 131072, 24 | ]; 25 | run_and_check(col_type, &values, &check_values); 26 | } 27 | 28 | #[test] 29 | #[serial] 30 | fn test_set_ordinal() { 31 | let col_type = "SET('1', '2', '3', '4', '5')"; 32 | let values = vec!["1", "2", "3", "4", "5"]; 33 | let check_values = vec![1, 2, 3, 4, 5]; 34 | run_and_check(col_type, &values, &check_values); 35 | } 36 | 37 | fn run_and_check(col_type: &str, values: &Vec<&str>, check_values: &Vec) { 38 | let runner = TestRunner::run_one_col_test(col_type, values, &vec![]); 39 | for i in 0..check_values.len() { 40 | assert_eq!( 41 | runner.insert_events[0].rows[i].column_values[0], 42 | ColumnValue::Set(check_values[i]), 43 | ); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/data_type_tests/text_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_type::ColumnType; 5 | use serial_test::serial; 6 | 7 | use crate::data_type_tests::bytes_test_util::BytesTestUtil; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_tinytext() { 12 | run_and_check("TINYTEXT"); 13 | } 14 | 15 | #[test] 16 | #[serial] 17 | fn test_text() { 18 | run_and_check("TEXT"); 19 | } 20 | 21 | #[test] 22 | #[serial] 23 | fn test_mediumtext() { 24 | run_and_check("MEDIUMTEXT"); 25 | } 26 | 27 | #[test] 28 | #[serial] 29 | fn test_longtext() { 30 | run_and_check("LONGTEXT"); 31 | } 32 | 33 | fn run_and_check(mysql_column_type: &str) { 34 | let (values, check_values) = BytesTestUtil::generate_visible_char_values(); 35 | BytesTestUtil::run_and_check(mysql_column_type, ColumnType::Blob, &values, &check_values); 36 | 37 | let (values, check_values) = BytesTestUtil::generate_trailing_space_values(true); 38 | BytesTestUtil::run_and_check(mysql_column_type, ColumnType::Blob, &values, &check_values); 39 | 40 | let (values, check_values) = BytesTestUtil::generate_trailing_nul_values(true); 41 | BytesTestUtil::run_and_check(mysql_column_type, ColumnType::Blob, &values, &check_values); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/data_type_tests/time_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 4 | use serial_test::serial; 5 | 6 | use crate::runner::test_runner::test::TestRunner; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_time6() { 11 | let col_type = "TIME(6)"; 12 | 13 | let values = vec![ 14 | "'00:00:00.000000'", 15 | "'23:59:59.999999'", 16 | "'03:04:05.0'", 17 | "'03:04:05.1'", 18 | "'03:04:05.12'", 19 | "'03:04:05.123'", 20 | "'03:04:05.1234'", 21 | "'03:04:05.12345'", 22 | "'03:04:05.123456'", 23 | "'03:04:05.000001'", 24 | "'03:04:05.000012'", 25 | "'03:04:05.000123'", 26 | "'03:04:05.001234'", 27 | "'03:04:05.012345'", 28 | "'838:59:59'", 29 | "'838:59:59.000000'", 30 | "'-00:00:00.000000'", 31 | "'-23:59:59.999999'", 32 | "'-03:04:05.0'", 33 | "'-03:04:05.1'", 34 | "'-03:04:05.12'", 35 | "'-03:04:05.123'", 36 | "'-03:04:05.1234'", 37 | "'-03:04:05.12345'", 38 | "'-03:04:05.123456'", 39 | "'-03:04:05.000001'", 40 | "'-03:04:05.000012'", 41 | "'-03:04:05.000123'", 42 | "'-03:04:05.001234'", 43 | "'-03:04:05.012345'", 44 | "'-838:59:59'", 45 | "'-838:59:59.000000'", 46 | ]; 47 | 48 | let check_values = [ 49 | "00:00:00.000000", 50 | "23:59:59.999999", 51 | "03:04:05.000000", 52 | "03:04:05.100000", 53 | "03:04:05.120000", 54 | "03:04:05.123000", 55 | "03:04:05.123400", 56 | "03:04:05.123450", 57 | "03:04:05.123456", 58 | "03:04:05.000001", 59 | "03:04:05.000012", 60 | "03:04:05.000123", 61 | "03:04:05.001234", 62 | "03:04:05.012345", 63 | "838:59:59.000000", 64 | "838:59:59.000000", 65 | "00:00:00.000000", 66 | "-23:59:59.999999", 67 | "-03:04:05.000000", 68 | "-03:04:05.100000", 69 | "-03:04:05.120000", 70 | "-03:04:05.123000", 71 | "-03:04:05.123400", 72 | "-03:04:05.123450", 73 | "-03:04:05.123456", 74 | "-03:04:05.000001", 75 | "-03:04:05.000012", 76 | "-03:04:05.000123", 77 | "-03:04:05.001234", 78 | "-03:04:05.012345", 79 | "-838:59:59.000000", 80 | "-838:59:59.000000", 81 | ]; 82 | 83 | run_and_check(col_type, &values, &check_values); 84 | } 85 | 86 | #[test] 87 | #[serial] 88 | fn test_time3() { 89 | let col_type = "TIME(3)"; 90 | 91 | let values = vec![ 92 | "'00:00:00.000'", 93 | "'23:59:59.999'", 94 | "'03:04:05.0'", 95 | "'03:04:05.1'", 96 | "'03:04:05.12'", 97 | "'03:04:05.123'", 98 | "'03:04:05.001'", 99 | "'03:04:05.012'", 100 | "'838:59:59'", 101 | "'838:59:59.000'", 102 | "'-00:00:00.000'", 103 | "'-23:59:59.999'", 104 | "'-03:04:05.0'", 105 | "'-03:04:05.1'", 106 | "'-03:04:05.12'", 107 | "'-03:04:05.123'", 108 | "'-03:04:05.001'", 109 | "'-03:04:05.012'", 110 | "'-838:59:59'", 111 | "'-838:59:59.000'", 112 | ]; 113 | 114 | let check_values = [ 115 | "00:00:00.000000", 116 | "23:59:59.999000", 117 | "03:04:05.000000", 118 | "03:04:05.100000", 119 | "03:04:05.120000", 120 | "03:04:05.123000", 121 | "03:04:05.001000", 122 | "03:04:05.012000", 123 | "838:59:59.000000", 124 | "838:59:59.000000", 125 | "00:00:00.000000", 126 | "-23:59:59.999000", 127 | "-03:04:05.000000", 128 | "-03:04:05.100000", 129 | "-03:04:05.120000", 130 | "-03:04:05.123000", 131 | "-03:04:05.001000", 132 | "-03:04:05.012000", 133 | "-838:59:59.000000", 134 | "-838:59:59.000000", 135 | ]; 136 | 137 | run_and_check(col_type, &values, &check_values); 138 | } 139 | 140 | #[test] 141 | #[serial] 142 | fn test_time() { 143 | let col_type = "TIME"; 144 | // the db values are actual: ["00:00:00", "23:59:59"] 145 | // the parsed binlog values are ["00:00:00.000000", "23:59:59.000000"] 146 | // we keep the 6 pending zeros since we don't know the field precision when parsing binlog 147 | let values = vec![ 148 | "'00:00:00'", 149 | "'23:59:59'", 150 | "'838:59:59'", 151 | "'-00:00:00'", 152 | "'-23:59:59'", 153 | "'-838:59:59'", 154 | ]; 155 | let check_values = [ 156 | "00:00:00.000000", 157 | "23:59:59.000000", 158 | "838:59:59.000000", 159 | "00:00:00.000000", 160 | "-23:59:59.000000", 161 | "-838:59:59.000000", 162 | ]; 163 | run_and_check(col_type, &values, &check_values); 164 | } 165 | 166 | fn run_and_check(col_type: &str, values: &[&str], check_values: &[&str]) { 167 | let runner = 168 | TestRunner::run_one_col_test(col_type, values, &vec!["SET @@session.time_zone='UTC'"]); 169 | 170 | assert_eq!(runner.insert_events[0].rows.len(), check_values.len()); 171 | for i in 0..check_values.len() { 172 | assert_eq!( 173 | runner.insert_events[0].rows[i].column_values[0], 174 | ColumnValue::Time(check_values[i].to_string()), 175 | ); 176 | } 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /tests/data_type_tests/timestamp_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 4 | use serial_test::serial; 5 | 6 | use crate::runner::test_runner::test::TestRunner; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_timestamp6() { 11 | let col_type = "TIMESTAMP(6)"; 12 | 13 | // refer: https://dev.mysql.com/doc/refman/8.0/en/datetime.html 14 | // The range is '1970-01-01 00:00:01.000000' UTC to '2038-01-19 03:14:07.999999' UTC. 15 | // TIMESTAMP values are stored as the number of seconds since the epoch ('1970-01-01 00:00:00' UTC). 16 | let values = vec![ 17 | "'1970-01-01 00:00:01.000000'", 18 | "'2038-01-19 03:14:07.999999'", 19 | "'2022-01-02 03:04:05.0'", 20 | "'2022-01-02 03:04:05.1'", 21 | "'2022-01-02 03:04:05.12'", 22 | "'2022-01-02 03:04:05.123'", 23 | "'2022-01-02 03:04:05.1234'", 24 | "'2022-01-02 03:04:05.12345'", 25 | "'2022-01-02 03:04:05.123456'", 26 | "'2022-01-02 03:04:05.000001'", 27 | "'2022-01-02 03:04:05.000012'", 28 | "'2022-01-02 03:04:05.000123'", 29 | "'2022-01-02 03:04:05.001234'", 30 | "'2022-01-02 03:04:05.012345'", 31 | ]; 32 | 33 | // MySQL converts TIMESTAMP values from the current time zone to UTC for storage, 34 | // and back from UTC to the current time zone for retrieval. 35 | // (This does not occur for other types such as DATETIME.) 36 | let micros_per_second = 1000000i64; 37 | // 2147483647 is the timestamp (UTC) for 2022-01-02 03:04:05 (UTC) 38 | // 1641092645 is the timestamp (UTC) for 2038-01-19 03:14:07 (UTC) 39 | let test_utc_timestamp = 1641092645 * micros_per_second; 40 | let check_values = [ 41 | 1000000, 42 | 2147483647 * micros_per_second + 999999, 43 | test_utc_timestamp, 44 | test_utc_timestamp + 100000, 45 | test_utc_timestamp + 120000, 46 | test_utc_timestamp + 123000, 47 | test_utc_timestamp + 123400, 48 | test_utc_timestamp + 123450, 49 | test_utc_timestamp + 123456, 50 | test_utc_timestamp + 000001, 51 | test_utc_timestamp + 000012, 52 | test_utc_timestamp + 000123, 53 | test_utc_timestamp + 001234, 54 | test_utc_timestamp + 012345, 55 | ]; 56 | 57 | run_and_check(col_type, &values, &check_values); 58 | } 59 | 60 | #[test] 61 | #[serial] 62 | fn test_timestamp3() { 63 | let col_type = "TIMESTAMP(3)"; 64 | 65 | let values = vec![ 66 | "'1970-01-01 00:00:01.000'", 67 | "'2038-01-19 03:14:07.999'", 68 | "'2022-01-02 03:04:05.0'", 69 | "'2022-01-02 03:04:05.1'", 70 | "'2022-01-02 03:04:05.12'", 71 | "'2022-01-02 03:04:05.123'", 72 | "'2022-01-02 03:04:05.001'", 73 | "'2022-01-02 03:04:05.012'", 74 | ]; 75 | 76 | let micros_per_second = 1000000i64; 77 | let test_utc_timestamp = 1641092645 * micros_per_second; 78 | let check_values = [ 79 | 1000000, 80 | 2147483647 * micros_per_second + 999000, 81 | test_utc_timestamp, 82 | test_utc_timestamp + 100000, 83 | test_utc_timestamp + 120000, 84 | test_utc_timestamp + 123000, 85 | test_utc_timestamp + 1000, 86 | test_utc_timestamp + 12000, 87 | ]; 88 | 89 | run_and_check(col_type, &values, &check_values); 90 | } 91 | 92 | #[test] 93 | #[serial] 94 | fn test_timestamp() { 95 | let col_type = "TIMESTAMP"; 96 | 97 | // since the precesion for TIMESTAMP is 0, 98 | // '2038-01-19 03:14:07.123456' will be truncated to '2038-01-19 03:14:07' 99 | let values = vec![ 100 | "'1970-01-01 00:00:01.000'", 101 | "'2038-01-19 03:14:07.123456'", 102 | "'2022-01-02 03:04:05.0'", 103 | "'2022-01-02 03:04:05.1'", 104 | "'2022-01-02 03:04:05.12'", 105 | "'2022-01-02 03:04:05.123'", 106 | "'2022-01-02 03:04:05.001'", 107 | "'2022-01-02 03:04:05.012'", 108 | ]; 109 | 110 | let micros_per_second = 1000000i64; 111 | let test_utc_timestamp = 1641092645 * micros_per_second; 112 | let check_values = [ 113 | 1000000, 114 | 2147483647 * micros_per_second, 115 | test_utc_timestamp, 116 | test_utc_timestamp, 117 | test_utc_timestamp, 118 | test_utc_timestamp, 119 | test_utc_timestamp, 120 | test_utc_timestamp, 121 | ]; 122 | 123 | run_and_check(col_type, &values, &check_values); 124 | } 125 | 126 | fn run_and_check(col_type: &str, values: &[&str], check_values: &[i64]) { 127 | let runner = 128 | TestRunner::run_one_col_test(col_type, values, &vec!["SET @@session.time_zone='UTC'"]); 129 | 130 | assert_eq!(runner.insert_events[0].rows.len(), check_values.len()); 131 | for i in 0..check_values.len() { 132 | assert_eq!( 133 | runner.insert_events[0].rows[i].column_values[0], 134 | ColumnValue::Timestamp(check_values[i]), 135 | ); 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /tests/data_type_tests/year_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 4 | use serial_test::serial; 5 | 6 | use crate::runner::test_runner::test::TestRunner; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_year() { 11 | let runner = TestRunner::run_one_col_test("YEAR", &vec!["'1901'", "'2155'"], &vec![]); 12 | let check_values = [1901, 2155]; 13 | assert_eq!(runner.insert_events[0].rows.len(), check_values.len()); 14 | for i in 0..check_values.len() { 15 | assert_eq!( 16 | runner.insert_events[0].rows[i].column_values[0], 17 | ColumnValue::Year(check_values[i]), 18 | ); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/ddl_tests/ddl_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use serial_test::serial; 5 | 6 | use crate::runner::test_runner::test::TestRunner; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_ddl_basic() { 11 | let prepare_sqls = vec!["DROP DATABASE IF EXISTS db_test_ddl"]; 12 | 13 | let test_sqls = vec![ 14 | "CREATE DATABASE db_test_ddl", 15 | "CREATE TABLE db_test_ddl.tb_test_ddl(id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, name VARCHAR(255))", 16 | 17 | "ALTER TABLE db_test_ddl.tb_test_ddl ADD COLUMN age INT", 18 | "ALTER TABLE db_test_ddl.tb_test_ddl ADD INDEX idx_name(name)", 19 | "ALTER TABLE db_test_ddl.tb_test_ddl ADD CONSTRAINT UNIQUE KEY(name)", 20 | "ALTER TABLE db_test_ddl.tb_test_ddl DROP COLUMN age", 21 | "ALTER TABLE db_test_ddl.tb_test_ddl DROP INDEX idx_name", 22 | "ALTER TABLE db_test_ddl.tb_test_ddl DROP INDEX name", 23 | 24 | // The binlog for CREATE VIEW will be like: "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `db_test_ddl`.`tb_test_ddl_v1` AS SELECT * FROM db_test_ddl.tb_test_ddl" 25 | // "CREATE VIEW db_test_ddl.tb_test_ddl_v1 AS SELECT * FROM db_test_ddl.tb_test_ddl", 26 | // "DROP VIEW db_test_ddl.tb_test_ddl_v1", 27 | 28 | "RENAME TABLE db_test_ddl.tb_test_ddl TO db_test_ddl.tb_test_ddl_2", 29 | "TRUNCATE TABLE db_test_ddl.tb_test_ddl_2", 30 | "DROP TABLE db_test_ddl.tb_test_ddl_2", 31 | "DROP DATABASE db_test_ddl", 32 | ]; 33 | 34 | run_sql_and_check_binlog(&prepare_sqls, &test_sqls, false); 35 | } 36 | 37 | #[test] 38 | #[serial] 39 | fn test_ddl_with_escapes() { 40 | let prepare_sqls = vec!["DROP DATABASE IF EXISTS `db_test_ddl`"]; 41 | let test_sqls = vec![ 42 | "CREATE DATABASE `db_test_ddl`", 43 | "CREATE TABLE `db_test_ddl`.`tb_test_ddl`(`id` INT AUTO_INCREMENT PRIMARY KEY NOT NULL, `name` VARCHAR(255))", 44 | ]; 45 | run_sql_and_check_binlog(&prepare_sqls, &test_sqls, true); 46 | } 47 | 48 | #[test] 49 | #[serial] 50 | fn test_ddl_different_cases() { 51 | let prepare_sqls = vec!["DROP DATABASE IF EXISTS db_TEST_ddl"]; 52 | let test_sqls = vec![ 53 | "Create DATAbase db_TEST_ddl", 54 | "Create Table db_TEST_ddl.tb_TEST_ddl(Id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, Name VARCHAR(255))", 55 | ]; 56 | run_sql_and_check_binlog(&prepare_sqls, &test_sqls, false); 57 | } 58 | 59 | fn run_sql_and_check_binlog(prepare_sqls: &[&str], test_sqls: &[&str], with_quotes: bool) { 60 | let prepare_sqls: Vec = prepare_sqls.into_iter().map(|i| i.to_string()).collect(); 61 | let test_sqls: Vec = test_sqls.into_iter().map(|i| i.to_string()).collect(); 62 | 63 | let mut runner = TestRunner::new(); 64 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &test_sqls); 65 | for i in 0..test_sqls.len() { 66 | // since comments may be added to ddl sql in binlog, here we check contain instead of equal. 67 | // example: the binlog for "drop table db_test_ddl.tb_test_ddl_2" may be 68 | // "drop table db_test_ddl.tb_test_ddl_2 /* generated by server */" 69 | if with_quotes { 70 | assert!(runner.query_events[i] 71 | .query 72 | .to_lowercase() 73 | .contains(&test_sqls[i].to_lowercase())); 74 | } else { 75 | assert!(runner.query_events[i] 76 | .query 77 | .to_lowercase() 78 | .replace("`", "") 79 | .contains(&test_sqls[i].to_lowercase())); 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /tests/ddl_tests/mod.rs: -------------------------------------------------------------------------------- 1 | mod ddl_tests; 2 | -------------------------------------------------------------------------------- /tests/dml_tests/delete_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 4 | use serial_test::serial; 5 | 6 | use crate::runner::{assert::test::Assert, mock::test::Mock, test_runner::test::TestRunner}; 7 | 8 | #[test] 9 | #[serial] 10 | fn test_delete_multiple_rows() { 11 | let prepare_sqls = vec![ 12 | Mock::default_create_sql(), 13 | "SET @@session.time_zone='UTC'".to_string(), 14 | ]; 15 | 16 | // insert 17 | let values = Mock::default_insert_values(); 18 | let insert_sqls = vec![Mock::insert_sql(&values)]; 19 | 20 | let mut runner = TestRunner::new(); 21 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &insert_sqls); 22 | 23 | // delete 24 | let delete_sqls = vec![Mock::delete_sql("pk", &vec![])]; 25 | runner.execute_sqls_and_get_binlogs(&vec![], &delete_sqls); 26 | 27 | assert_eq!(runner.delete_events.len(), 1); 28 | assert_eq!(runner.delete_events[0].rows.len(), 5); 29 | 30 | // row 0 31 | Mock::default_check_values( 32 | &runner.delete_events[0].rows[0], 33 | 0, 34 | 1, 35 | 2, 36 | 3, 37 | 4, 38 | 5, 39 | "123456.1234".to_string(), 40 | 1234.12, 41 | 12345.123, 42 | 3, 43 | "2022-01-02 03:04:05.123456".to_string(), 44 | "03:04:05.123456".to_string(), 45 | "2022-01-02".to_string(), 46 | 2022, 47 | 1641092645123456, 48 | vec![97u8, 98], 49 | vec![99u8, 100], 50 | vec![101u8, 102], 51 | vec![103u8, 104], 52 | vec![105u8, 106], 53 | vec![107u8, 108], 54 | vec![109u8, 110], 55 | vec![111u8, 112], 56 | vec![113u8, 114], 57 | vec![115u8, 116], 58 | vec![117u8, 118], 59 | vec![119u8, 120], 60 | 1, 61 | 1, 62 | ); 63 | 64 | // row 1 65 | Mock::default_check_values( 66 | &runner.delete_events[0].rows[1], 67 | 1, 68 | 10, 69 | 20, 70 | 30, 71 | 40, 72 | 50, 73 | "654321.4321".to_string(), 74 | 4321.21, 75 | 54321.321, 76 | 4, 77 | "2021-02-01 04:05:06.654321".to_string(), 78 | "04:05:06.654321".to_string(), 79 | "2012-02-01".to_string(), 80 | 2021, 81 | 1612152306654321, 82 | vec![49u8], 83 | vec![50u8], 84 | vec![51u8], 85 | vec![52u8], 86 | vec![53u8], 87 | vec![54u8], 88 | vec![55u8], 89 | vec![56u8], 90 | vec![57u8], 91 | vec![49u8, 48], 92 | vec![49u8, 49], 93 | vec![49u8, 50], 94 | 2, 95 | 2, 96 | ); 97 | 98 | // row 2 99 | Mock::default_check_values( 100 | &runner.delete_events[0].rows[2], 101 | 2, 102 | 6, 103 | 7, 104 | 8, 105 | 9, 106 | 10, 107 | "234561.2341".to_string(), 108 | 2341.12, 109 | 23451.231, 110 | 5, 111 | "2020-03-04 05:06:07.234561".to_string(), 112 | "05:06:07.234561".to_string(), 113 | "2022-05-06".to_string(), 114 | 2020, 115 | 1583298367234561, 116 | vec![97u8], 117 | vec![98u8], 118 | vec![99u8], 119 | vec![100u8], 120 | vec![101u8], 121 | vec![102u8], 122 | vec![103u8], 123 | vec![104u8], 124 | vec![105u8], 125 | vec![106u8], 126 | vec![107u8], 127 | vec![108u8], 128 | 3, 129 | 4, 130 | ); 131 | 132 | // row 3 133 | Assert::assert_numeric_eq(&runner.delete_events[0].rows[3].column_values[0], 3); 134 | // NULL fields 135 | for i in 0..13 { 136 | assert_eq!( 137 | runner.delete_events[0].rows[3].column_values[2 * i + 2], 138 | ColumnValue::None 139 | ); 140 | } 141 | // non-Null fields 142 | let row_3 = &runner.delete_events[0].rows[3].column_values; 143 | Assert::assert_numeric_eq(&row_3[1], 11 as i128); 144 | Assert::assert_numeric_eq(&row_3[3], 3 as i128); 145 | Assert::assert_numeric_eq(&row_3[5], 5 as i128); 146 | Assert::assert_float_eq(&row_3[7], 1234.12); 147 | Assert::assert_numeric_eq(&row_3[9], 3 as i128); 148 | Assert::assert_string_eq(&row_3[11], "03:04:05.123456".to_string()); 149 | Assert::assert_numeric_eq(&row_3[13], 2022 as i128); 150 | Assert::assert_bytes_eq(&row_3[15], vec![97u8, 98]); 151 | Assert::assert_bytes_eq(&row_3[17], vec![101u8, 102]); 152 | Assert::assert_bytes_eq(&row_3[19], vec![105u8, 106]); 153 | Assert::assert_bytes_eq(&row_3[21], vec![109u8, 110]); 154 | Assert::assert_bytes_eq(&row_3[23], vec![113u8, 114]); 155 | Assert::assert_bytes_eq(&row_3[25], vec![117u8, 118]); 156 | Assert::assert_unsigned_numeric_eq(&row_3[27], 1 as u64); 157 | 158 | // row 4 159 | Assert::assert_numeric_eq(&runner.delete_events[0].rows[4].column_values[0], 4); 160 | for i in 1..27 { 161 | assert_eq!( 162 | runner.delete_events[0].rows[4].column_values[i], 163 | ColumnValue::None 164 | ); 165 | } 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /tests/dml_tests/insert_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 5 | use serial_test::serial; 6 | 7 | use crate::runner::{assert::test::Assert, mock::test::Mock, test_runner::test::TestRunner}; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_insert_multiple_rows() { 12 | let prepare_sqls = vec![ 13 | Mock::default_create_sql(), 14 | "SET @@session.time_zone='UTC'".to_string(), 15 | ]; 16 | 17 | let values = Mock::default_insert_values(); 18 | let test_sqls = vec![ 19 | Mock::insert_sql(&values[0..2]), 20 | Mock::insert_sql(&values[2..3]), 21 | ]; 22 | 23 | let mut runner = TestRunner::new(); 24 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &test_sqls); 25 | 26 | assert_eq!(runner.insert_events.len(), 2); 27 | assert_eq!(runner.insert_events[0].rows.len(), 2); 28 | assert_eq!(runner.insert_events[1].rows.len(), 1); 29 | 30 | Mock::default_check_values( 31 | &runner.insert_events[0].rows[0], 32 | 0, 33 | 1, 34 | 2, 35 | 3, 36 | 4, 37 | 5, 38 | "123456.1234".to_string(), 39 | 1234.12, 40 | 12345.123, 41 | 3, 42 | "2022-01-02 03:04:05.123456".to_string(), 43 | "03:04:05.123456".to_string(), 44 | "2022-01-02".to_string(), 45 | 2022, 46 | 1641092645123456, 47 | vec![97u8, 98], 48 | vec![99u8, 100], 49 | vec![101u8, 102], 50 | vec![103u8, 104], 51 | vec![105u8, 106], 52 | vec![107u8, 108], 53 | vec![109u8, 110], 54 | vec![111u8, 112], 55 | vec![113u8, 114], 56 | vec![115u8, 116], 57 | vec![117u8, 118], 58 | vec![119u8, 120], 59 | 1, 60 | 1, 61 | ); 62 | 63 | Mock::default_check_values( 64 | &runner.insert_events[0].rows[1], 65 | 1, 66 | 10, 67 | 20, 68 | 30, 69 | 40, 70 | 50, 71 | "654321.4321".to_string(), 72 | 4321.21, 73 | 54321.321, 74 | 4, 75 | "2021-02-01 04:05:06.654321".to_string(), 76 | "04:05:06.654321".to_string(), 77 | "2012-02-01".to_string(), 78 | 2021, 79 | 1612152306654321, 80 | vec![49u8], 81 | vec![50u8], 82 | vec![51u8], 83 | vec![52u8], 84 | vec![53u8], 85 | vec![54u8], 86 | vec![55u8], 87 | vec![56u8], 88 | vec![57u8], 89 | vec![49u8, 48], 90 | vec![49u8, 49], 91 | vec![49u8, 50], 92 | 2, 93 | 2, 94 | ); 95 | 96 | Mock::default_check_values( 97 | &runner.insert_events[1].rows[0], 98 | 2, 99 | 6, 100 | 7, 101 | 8, 102 | 9, 103 | 10, 104 | "234561.2341".to_string(), 105 | 2341.12, 106 | 23451.231, 107 | 5, 108 | "2020-03-04 05:06:07.234561".to_string(), 109 | "05:06:07.234561".to_string(), 110 | "2022-05-06".to_string(), 111 | 2020, 112 | 1583298367234561, 113 | vec![97u8], 114 | vec![98u8], 115 | vec![99u8], 116 | vec![100u8], 117 | vec![101u8], 118 | vec![102u8], 119 | vec![103u8], 120 | vec![104u8], 121 | vec![105u8], 122 | vec![106u8], 123 | vec![107u8], 124 | vec![108u8], 125 | 3, 126 | 4, 127 | ); 128 | } 129 | 130 | #[test] 131 | #[serial] 132 | fn test_insert_partial_null_fields() { 133 | let prepare_sqls = vec![ 134 | Mock::default_create_sql(), 135 | "SET @@session.time_zone='UTC'".to_string(), 136 | ]; 137 | 138 | let values = Mock::default_insert_values(); 139 | let insert_sqls = vec![Mock::insert_sql(&values[3..4])]; 140 | 141 | let mut runner = TestRunner::new(); 142 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &insert_sqls); 143 | 144 | let column_values = &runner.insert_events[0].rows[0].column_values; 145 | Assert::assert_numeric_eq(&column_values[0], 3); 146 | // NULL fields 147 | for i in 0..13 { 148 | assert_eq!(column_values[2 * i + 2], ColumnValue::None); 149 | } 150 | // non-Null fields 151 | 152 | Assert::assert_numeric_eq(&column_values[1], 11 as i128); 153 | Assert::assert_numeric_eq(&column_values[3], 3 as i128); 154 | Assert::assert_numeric_eq(&column_values[5], 5 as i128); 155 | Assert::assert_float_eq(&column_values[7], 1234.12); 156 | Assert::assert_numeric_eq(&column_values[9], 3 as i128); 157 | Assert::assert_string_eq(&column_values[11], "03:04:05.123456".to_string()); 158 | Assert::assert_numeric_eq(&column_values[13], 2022 as i128); 159 | Assert::assert_bytes_eq(&column_values[15], vec![97u8, 98]); 160 | Assert::assert_bytes_eq(&column_values[17], vec![101u8, 102]); 161 | Assert::assert_bytes_eq(&column_values[19], vec![105u8, 106]); 162 | Assert::assert_bytes_eq(&column_values[21], vec![109u8, 110]); 163 | Assert::assert_bytes_eq(&column_values[23], vec![113u8, 114]); 164 | Assert::assert_bytes_eq(&column_values[25], vec![117u8, 118]); 165 | Assert::assert_unsigned_numeric_eq(&column_values[27], 1 as u64); 166 | } 167 | 168 | #[test] 169 | #[serial] 170 | fn test_insert_all_null_fields() { 171 | let prepare_sqls = vec![ 172 | Mock::default_create_sql(), 173 | "SET @@session.time_zone='UTC'".to_string(), 174 | ]; 175 | 176 | let values = Mock::default_insert_values(); 177 | let insert_sqls = vec![Mock::insert_sql(&values[4..5])]; 178 | 179 | let mut runner = TestRunner::new(); 180 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &insert_sqls); 181 | 182 | let column_values = &runner.insert_events[0].rows[0].column_values; 183 | Assert::assert_numeric_eq(&column_values[0], 4); 184 | for i in 1..28 { 185 | assert_eq!(column_values[i], ColumnValue::None); 186 | } 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /tests/dml_tests/mod.rs: -------------------------------------------------------------------------------- 1 | mod delete_tests; 2 | mod insert_tests; 3 | mod update_tests; 4 | -------------------------------------------------------------------------------- /tests/dml_tests/update_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | 4 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 5 | use serial_test::serial; 6 | 7 | use crate::runner::{assert::test::Assert, mock::test::Mock, test_runner::test::TestRunner}; 8 | 9 | #[test] 10 | #[serial] 11 | fn test_update_multiple_rows() { 12 | let prepare_sqls = vec![ 13 | Mock::default_create_sql(), 14 | "SET @@session.time_zone='UTC'".to_string(), 15 | ]; 16 | 17 | // insert 18 | let col_names = Mock::default_col_names(); 19 | let values = Mock::default_insert_values(); 20 | let insert_sqls = vec![Mock::insert_sql(&values)]; 21 | 22 | let mut runner = TestRunner::new(); 23 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &insert_sqls); 24 | 25 | // update 26 | let update_sqls = vec![ 27 | Mock::update_sql("pk", values[0][0], &col_names[1..], &values[1][1..]), 28 | Mock::update_sql("pk", values[1][0], &col_names[1..], &values[2][1..]), 29 | Mock::update_sql("pk", values[2][0], &col_names[1..], &values[3][1..]), 30 | Mock::update_sql("pk", values[3][0], &col_names[1..], &values[4][1..]), 31 | ]; 32 | runner.execute_sqls_and_get_binlogs( 33 | &vec!["SET @@session.time_zone='UTC'".to_string()], 34 | &update_sqls, 35 | ); 36 | 37 | assert_eq!(runner.update_events.len(), 4); 38 | 39 | // row 0, before 40 | Mock::default_check_values( 41 | &runner.update_events[0].rows[0].0, 42 | 0, 43 | 1, 44 | 2, 45 | 3, 46 | 4, 47 | 5, 48 | "123456.1234".to_string(), 49 | 1234.12, 50 | 12345.123, 51 | 3, 52 | "2022-01-02 03:04:05.123456".to_string(), 53 | "03:04:05.123456".to_string(), 54 | "2022-01-02".to_string(), 55 | 2022, 56 | 1641092645123456, 57 | vec![97u8, 98], 58 | vec![99u8, 100], 59 | vec![101u8, 102], 60 | vec![103u8, 104], 61 | vec![105u8, 106], 62 | vec![107u8, 108], 63 | vec![109u8, 110], 64 | vec![111u8, 112], 65 | vec![113u8, 114], 66 | vec![115u8, 116], 67 | vec![117u8, 118], 68 | vec![119u8, 120], 69 | 1, 70 | 1, 71 | ); 72 | 73 | // row 0, after 74 | Mock::default_check_values( 75 | &runner.update_events[0].rows[0].1, 76 | 0, 77 | 10, 78 | 20, 79 | 30, 80 | 40, 81 | 50, 82 | "654321.4321".to_string(), 83 | 4321.21, 84 | 54321.321, 85 | 4, 86 | "2021-02-01 04:05:06.654321".to_string(), 87 | "04:05:06.654321".to_string(), 88 | "2012-02-01".to_string(), 89 | 2021, 90 | 1612152306654321, 91 | vec![49u8], 92 | vec![50u8], 93 | vec![51u8], 94 | vec![52u8], 95 | vec![53u8], 96 | vec![54u8], 97 | vec![55u8], 98 | vec![56u8], 99 | vec![57u8], 100 | vec![49u8, 48], 101 | vec![49u8, 49], 102 | vec![49u8, 50], 103 | 2, 104 | 2, 105 | ); 106 | 107 | // row 1, before 108 | Mock::default_check_values( 109 | &runner.update_events[1].rows[0].0, 110 | 1, 111 | 10, 112 | 20, 113 | 30, 114 | 40, 115 | 50, 116 | "654321.4321".to_string(), 117 | 4321.21, 118 | 54321.321, 119 | 4, 120 | "2021-02-01 04:05:06.654321".to_string(), 121 | "04:05:06.654321".to_string(), 122 | "2012-02-01".to_string(), 123 | 2021, 124 | 1612152306654321, 125 | vec![49u8], 126 | vec![50u8], 127 | vec![51u8], 128 | vec![52u8], 129 | vec![53u8], 130 | vec![54u8], 131 | vec![55u8], 132 | vec![56u8], 133 | vec![57u8], 134 | vec![49u8, 48], 135 | vec![49u8, 49], 136 | vec![49u8, 50], 137 | 2, 138 | 2, 139 | ); 140 | 141 | // row 1, after 142 | Mock::default_check_values( 143 | &runner.update_events[1].rows[0].1, 144 | 1, 145 | 6, 146 | 7, 147 | 8, 148 | 9, 149 | 10, 150 | "234561.2341".to_string(), 151 | 2341.12, 152 | 23451.231, 153 | 5, 154 | "2020-03-04 05:06:07.234561".to_string(), 155 | "05:06:07.234561".to_string(), 156 | "2022-05-06".to_string(), 157 | 2020, 158 | 1583298367234561, 159 | vec![97u8], 160 | vec![98u8], 161 | vec![99u8], 162 | vec![100u8], 163 | vec![101u8], 164 | vec![102u8], 165 | vec![103u8], 166 | vec![104u8], 167 | vec![105u8], 168 | vec![106u8], 169 | vec![107u8], 170 | vec![108u8], 171 | 3, 172 | 4, 173 | ); 174 | 175 | // row 2, before 176 | Mock::default_check_values( 177 | &runner.update_events[2].rows[0].0, 178 | 2, 179 | 6, 180 | 7, 181 | 8, 182 | 9, 183 | 10, 184 | "234561.2341".to_string(), 185 | 2341.12, 186 | 23451.231, 187 | 5, 188 | "2020-03-04 05:06:07.234561".to_string(), 189 | "05:06:07.234561".to_string(), 190 | "2022-05-06".to_string(), 191 | 2020, 192 | 1583298367234561, 193 | vec![97u8], 194 | vec![98u8], 195 | vec![99u8], 196 | vec![100u8], 197 | vec![101u8], 198 | vec![102u8], 199 | vec![103u8], 200 | vec![104u8], 201 | vec![105u8], 202 | vec![106u8], 203 | vec![107u8], 204 | vec![108u8], 205 | 3, 206 | 4, 207 | ); 208 | 209 | // row 2, after 210 | Assert::assert_numeric_eq(&runner.update_events[2].rows[0].1.column_values[0], 2); 211 | // NULL fields 212 | for i in 0..13 { 213 | assert_eq!( 214 | runner.update_events[2].rows[0].1.column_values[2 * i + 2], 215 | ColumnValue::None 216 | ); 217 | } 218 | // non-Null fields 219 | let row_2_after = &runner.update_events[2].rows[0].1.column_values; 220 | Assert::assert_numeric_eq(&row_2_after[1], 11 as i128); 221 | Assert::assert_numeric_eq(&row_2_after[3], 3 as i128); 222 | Assert::assert_numeric_eq(&row_2_after[5], 5 as i128); 223 | Assert::assert_float_eq(&row_2_after[7], 1234.12); 224 | Assert::assert_numeric_eq(&row_2_after[9], 3 as i128); 225 | Assert::assert_string_eq(&row_2_after[11], "03:04:05.123456".to_string()); 226 | Assert::assert_numeric_eq(&row_2_after[13], 2022 as i128); 227 | Assert::assert_bytes_eq(&row_2_after[15], vec![97u8, 98]); 228 | Assert::assert_bytes_eq(&row_2_after[17], vec![101u8, 102]); 229 | Assert::assert_bytes_eq(&row_2_after[19], vec![105u8, 106]); 230 | Assert::assert_bytes_eq(&row_2_after[21], vec![109u8, 110]); 231 | Assert::assert_bytes_eq(&row_2_after[23], vec![113u8, 114]); 232 | Assert::assert_bytes_eq(&row_2_after[25], vec![117u8, 118]); 233 | Assert::assert_unsigned_numeric_eq(&row_2_after[27], 1 as u64); 234 | 235 | // row 3, before 236 | Assert::assert_numeric_eq(&runner.update_events[3].rows[0].0.column_values[0], 3); 237 | // NULL fields 238 | for i in 0..13 { 239 | assert_eq!( 240 | runner.update_events[3].rows[0].0.column_values[2 * i + 2], 241 | ColumnValue::None 242 | ); 243 | } 244 | // non-Null fields 245 | let row_3_before = &runner.update_events[3].rows[0].0.column_values; 246 | Assert::assert_numeric_eq(&row_3_before[1], 11 as i128); 247 | Assert::assert_numeric_eq(&row_3_before[3], 3 as i128); 248 | Assert::assert_numeric_eq(&row_3_before[5], 5 as i128); 249 | Assert::assert_float_eq(&row_3_before[7], 1234.12); 250 | Assert::assert_numeric_eq(&row_3_before[9], 3 as i128); 251 | Assert::assert_string_eq(&row_3_before[11], "03:04:05.123456".to_string()); 252 | Assert::assert_numeric_eq(&row_3_before[13], 2022 as i128); 253 | Assert::assert_bytes_eq(&row_3_before[15], vec![97u8, 98]); 254 | Assert::assert_bytes_eq(&row_3_before[17], vec![101u8, 102]); 255 | Assert::assert_bytes_eq(&row_3_before[19], vec![105u8, 106]); 256 | Assert::assert_bytes_eq(&row_3_before[21], vec![109u8, 110]); 257 | Assert::assert_bytes_eq(&row_3_before[23], vec![113u8, 114]); 258 | Assert::assert_bytes_eq(&row_3_before[25], vec![117u8, 118]); 259 | Assert::assert_unsigned_numeric_eq(&row_3_before[27], 1 as u64); 260 | 261 | // row 3, after 262 | for i in 1..28 { 263 | assert_eq!( 264 | runner.update_events[3].rows[0].1.column_values[i], 265 | ColumnValue::None 266 | ); 267 | } 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /tests/integration_test.rs: -------------------------------------------------------------------------------- 1 | mod data_type_tests; 2 | mod ddl_tests; 3 | mod dml_tests; 4 | mod parse_file_tests; 5 | mod runner; 6 | -------------------------------------------------------------------------------- /tests/parse_file_tests/mod.rs: -------------------------------------------------------------------------------- 1 | mod parse_file_tests; 2 | -------------------------------------------------------------------------------- /tests/parse_file_tests/mysql-bin.000057: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apecloud/mysql-binlog-connector-rust/62c53624f434fe2cfc18814517900efb2523ccdd/tests/parse_file_tests/mysql-bin.000057 -------------------------------------------------------------------------------- /tests/parse_file_tests/mysql-bin.000080: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apecloud/mysql-binlog-connector-rust/62c53624f434fe2cfc18814517900efb2523ccdd/tests/parse_file_tests/mysql-bin.000080 -------------------------------------------------------------------------------- /tests/parse_file_tests/non-binlog: -------------------------------------------------------------------------------- 1 | -----BEGIN PUBLIC KEY----- 2 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmha65/cRRAfwKjDpWUZ1 3 | L/IYaWFPiIU2SuLqd8wZVeeapo7V43dMYGF/GmMbUd9M6g14zCUaUIOP4BKv5ER+ 4 | V7KcMyBvmQC0BCzZzwyjyUDWrjOi8nIcofZbJMheAGOVy7OLyUjsiOXlDV3hZbrd 5 | fDSolfaZzutlwnvzASV2TwM/SSfz013yMKc0tgQQSjSArlpAhjfRBDfAGn2kkCuV 6 | IC9fJDRvEdyHs+ExtZFgGmXNUhOrHFUnpi8cgQP7h+7WeoQbLWQ/S9y9smAiFPOE 7 | TcjudogStcnwH4qYQJZ/Ohzl1AWEpyn8+08ONDx4BEX0D9hQP/ZUEBPbs+nfNnrm 8 | AwIDAQAB 9 | -----END PUBLIC KEY----- 10 | -------------------------------------------------------------------------------- /tests/parse_file_tests/parse_file_tests.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod test { 3 | use std::{collections::HashMap, env, fs::File}; 4 | 5 | use mysql_binlog_connector_rust::{binlog_error::BinlogError, binlog_parser::BinlogParser}; 6 | 7 | #[test] 8 | fn test_bad_magic() { 9 | let mut file = open_file("/non-binlog"); 10 | let mut parser = BinlogParser { 11 | checksum_length: 0, 12 | table_map_event_by_table_id: HashMap::new(), 13 | }; 14 | 15 | let res = parser.check_magic(&mut file); 16 | assert!(res.is_err()); 17 | match res.err().unwrap() { 18 | BinlogError::UnexpectedData(error) => { 19 | assert_eq!(error, "bad magic") 20 | } 21 | 22 | _ => { 23 | assert!(false) 24 | } 25 | } 26 | } 27 | 28 | #[test] 29 | fn test_parse_57_binlog() { 30 | test_parse_binlog("/mysql-bin.000057", 8); 31 | } 32 | 33 | #[test] 34 | fn test_parse_80_binlog() { 35 | test_parse_binlog("/mysql-bin.000080", 37); 36 | } 37 | 38 | fn open_file(file_name: &str) -> File { 39 | let current_dir = env::current_dir(); 40 | let file_path = format!( 41 | "{}/{}/{}", 42 | current_dir.unwrap().display(), 43 | "tests/parse_file_tests", 44 | file_name 45 | ); 46 | File::open(file_path).unwrap() 47 | } 48 | 49 | fn test_parse_binlog(file_name: &str, expect_event_count: i32) { 50 | let mut file = open_file(file_name); 51 | let mut parser = BinlogParser { 52 | checksum_length: 4, 53 | table_map_event_by_table_id: HashMap::new(), 54 | }; 55 | 56 | assert!(parser.check_magic(&mut file).is_ok()); 57 | 58 | let mut count = 0; 59 | while let Ok(_) = parser.next(&mut file) { 60 | count += 1; 61 | } 62 | assert_eq!(count, expect_event_count); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/runner/assert.rs: -------------------------------------------------------------------------------- 1 | pub(crate) mod test { 2 | use mysql_binlog_connector_rust::column::column_value::ColumnValue; 3 | 4 | pub struct Assert {} 5 | 6 | #[allow(dead_code)] 7 | impl Assert { 8 | pub fn assert_numeric_eq(column_value: &ColumnValue, value: i128) { 9 | match column_value { 10 | ColumnValue::Tiny(v) => { 11 | assert_eq!(*v, value as i8); 12 | } 13 | ColumnValue::Short(v) => { 14 | assert_eq!(*v, value as i16); 15 | } 16 | ColumnValue::Long(v) => { 17 | assert_eq!(*v, value as i32); 18 | } 19 | ColumnValue::LongLong(v) => { 20 | assert_eq!(*v, value as i64); 21 | } 22 | ColumnValue::Year(v) => { 23 | assert_eq!(*v, value as u16); 24 | } 25 | ColumnValue::Enum(v) => { 26 | assert_eq!(*v, value as u32); 27 | } 28 | ColumnValue::Set(v) => { 29 | assert_eq!(*v, value as u64); 30 | } 31 | ColumnValue::Bit(v) => { 32 | assert_eq!(*v, value as u64); 33 | } 34 | _ => { 35 | assert!(false) 36 | } 37 | } 38 | } 39 | 40 | pub fn assert_unsigned_numeric_eq(column_value: &ColumnValue, value: u64) { 41 | match column_value { 42 | ColumnValue::Enum(v) => { 43 | assert_eq!(*v, value as u32); 44 | } 45 | ColumnValue::Set(v) => { 46 | assert_eq!(*v, value as u64); 47 | } 48 | _ => { 49 | assert!(false) 50 | } 51 | } 52 | } 53 | 54 | pub fn assert_float_eq(column_value: &ColumnValue, value: f32) { 55 | match column_value { 56 | ColumnValue::Float(v) => { 57 | assert_eq!(*v, value); 58 | } 59 | _ => { 60 | assert!(false) 61 | } 62 | } 63 | } 64 | 65 | pub fn assert_double_eq(column_value: &ColumnValue, value: f64) { 66 | match column_value { 67 | ColumnValue::Double(v) => { 68 | assert_eq!(*v, value); 69 | } 70 | _ => { 71 | assert!(false) 72 | } 73 | } 74 | } 75 | 76 | pub fn assert_bytes_eq(column_value: &ColumnValue, value: Vec) { 77 | match column_value { 78 | ColumnValue::String(v) | ColumnValue::Blob(v) => { 79 | assert_eq!(*v, value); 80 | } 81 | _ => { 82 | assert!(false) 83 | } 84 | } 85 | } 86 | 87 | pub fn assert_string_eq(column_value: &ColumnValue, value: String) { 88 | match column_value { 89 | ColumnValue::Time(v) 90 | | ColumnValue::Date(v) 91 | | ColumnValue::DateTime(v) 92 | | ColumnValue::Decimal(v) => { 93 | assert_eq!(*v, value); 94 | } 95 | _ => { 96 | assert!(false) 97 | } 98 | } 99 | } 100 | 101 | pub fn assert_timestamp_eq(column_value: &ColumnValue, value: i64) { 102 | match column_value { 103 | ColumnValue::Timestamp(v) => { 104 | assert_eq!(*v, value); 105 | } 106 | _ => { 107 | assert!(false) 108 | } 109 | } 110 | } 111 | 112 | pub fn assert_json_string_eq(json_1: &str, json_2: &str) { 113 | let json_1: serde_json::Value = serde_json::from_str(json_1).unwrap(); 114 | let json_2: serde_json::Value = serde_json::from_str(json_2).unwrap(); 115 | assert_eq!(json_1, json_2); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /tests/runner/env.rs: -------------------------------------------------------------------------------- 1 | pub(crate) mod test { 2 | use std::{collections::HashMap, env}; 3 | 4 | pub struct Env {} 5 | 6 | impl Env { 7 | const ENV_FILE: &'static str = "tests/.env"; 8 | 9 | pub const DB_URL: &'static str = "db_url"; 10 | pub const SERVER_ID: &'static str = "server_id"; 11 | pub const DEFAULT_DB: &'static str = "default_db"; 12 | pub const DEFAULT_TB: &'static str = "default_tb"; 13 | pub const BINLOG_PARSE_MILLIS: &'static str = "binlog_parse_millis"; 14 | 15 | pub fn load_vars() -> HashMap { 16 | let env_path = env::current_dir().unwrap().join(Self::ENV_FILE); 17 | dotenv::from_path(env_path).unwrap(); 18 | 19 | let mut vars = HashMap::new(); 20 | vars.insert(Self::DB_URL.into(), env::var(Self::DB_URL).unwrap()); 21 | vars.insert(Self::SERVER_ID.into(), env::var(Self::SERVER_ID).unwrap()); 22 | vars.insert(Self::DEFAULT_DB.into(), env::var(Self::DEFAULT_DB).unwrap()); 23 | vars.insert(Self::DEFAULT_TB.into(), env::var(Self::DEFAULT_TB).unwrap()); 24 | vars.insert( 25 | Self::BINLOG_PARSE_MILLIS.into(), 26 | env::var(Self::BINLOG_PARSE_MILLIS).unwrap(), 27 | ); 28 | vars 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/runner/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod assert; 2 | pub mod env; 3 | pub mod mock; 4 | pub mod test_runner; 5 | -------------------------------------------------------------------------------- /tests/runner/test_runner.rs: -------------------------------------------------------------------------------- 1 | pub(crate) mod test { 2 | use std::{ 3 | sync::{Arc, Mutex}, 4 | thread, 5 | time::Duration, 6 | }; 7 | 8 | use async_std::task::block_on; 9 | use mysql_binlog_connector_rust::{ 10 | binlog_client::BinlogClient, 11 | binlog_error::BinlogError, 12 | command::{authenticator::Authenticator, command_util::CommandUtil}, 13 | event::{ 14 | delete_rows_event::DeleteRowsEvent, event_data::EventData, query_event::QueryEvent, 15 | update_rows_event::UpdateRowsEvent, write_rows_event::WriteRowsEvent, 16 | }, 17 | }; 18 | 19 | use crate::runner::{env::test::Env, mock::test::Mock}; 20 | 21 | pub struct TestRunner { 22 | pub insert_events: Vec, 23 | pub update_events: Vec, 24 | pub delete_events: Vec, 25 | pub query_events: Vec, 26 | pub binlog_parse_millis: u64, 27 | pub db_url: String, 28 | pub server_id: u64, 29 | pub default_db: String, 30 | pub default_tb: String, 31 | } 32 | 33 | #[allow(dead_code)] 34 | impl TestRunner { 35 | pub fn new() -> TestRunner { 36 | // load environment variables 37 | let env = Env::load_vars(); 38 | let runner = TestRunner { 39 | insert_events: Vec::new(), 40 | update_events: Vec::new(), 41 | delete_events: Vec::new(), 42 | query_events: Vec::new(), 43 | db_url: env.get(Env::DB_URL).unwrap().to_string(), 44 | default_db: env.get(Env::DEFAULT_DB).unwrap().to_string(), 45 | default_tb: env.get(Env::DEFAULT_TB).unwrap().to_string(), 46 | server_id: env 47 | .get(Env::SERVER_ID) 48 | .unwrap() 49 | .to_string() 50 | .parse::() 51 | .unwrap(), 52 | binlog_parse_millis: env 53 | .get(Env::BINLOG_PARSE_MILLIS) 54 | .unwrap() 55 | .to_string() 56 | .parse::() 57 | .unwrap(), 58 | }; 59 | 60 | // run init sqls to prepare test dabase 61 | let prepare_sqls = vec![ 62 | "DROP DATABASE IF EXISTS ".to_string() + &runner.default_db, 63 | "CREATE DATABASE ".to_string() + &runner.default_db, 64 | ]; 65 | let test_sqls = vec![]; 66 | let _ = block_on(runner.execute_sqls(&prepare_sqls, &test_sqls)); 67 | 68 | runner 69 | } 70 | 71 | pub fn run_one_col_test( 72 | col_type: &str, 73 | values: &[&str], 74 | prepare_sqls: &[&str], 75 | ) -> TestRunner { 76 | let mut runner = TestRunner::new(); 77 | let create_sql = Mock::one_col_create_sql(col_type); 78 | let insert_sql = Mock::one_col_insert_sql(values); 79 | 80 | let mut prepare_sqls: Vec = 81 | prepare_sqls.into_iter().map(|i| i.to_string()).collect(); 82 | prepare_sqls.push(create_sql); 83 | 84 | runner.execute_sqls_and_get_binlogs(&prepare_sqls, &vec![insert_sql]); 85 | runner 86 | } 87 | 88 | pub fn execute_sqls_and_get_binlogs( 89 | &mut self, 90 | prepare_sqls: &Vec, 91 | test_sqls: &Vec, 92 | ) { 93 | block_on(self.execute_sqls_and_get_binlogs_internal(&prepare_sqls, &test_sqls)); 94 | } 95 | 96 | async fn execute_sqls_and_get_binlogs_internal( 97 | &mut self, 98 | prepare_sqls: &Vec, 99 | test_sqls: &Vec, 100 | ) { 101 | // execute sqls, binlog_position will start from the first test sql, prepare sqls will be ignored. 102 | let (binlog_filename, binlog_position) = 103 | self.execute_sqls(prepare_sqls, test_sqls).await.unwrap(); 104 | 105 | // parse binlogs 106 | let client = BinlogClient { 107 | url: self.db_url.clone(), 108 | binlog_filename, 109 | binlog_position, 110 | server_id: self.server_id, 111 | gtid_enabled: false, 112 | gtid_set: String::new(), 113 | heartbeat_interval_secs: 5, 114 | timeout_secs: 60, 115 | }; 116 | 117 | let all_events = Arc::new(Mutex::new(Vec::new())); 118 | let all_events_clone = all_events.clone(); 119 | let parse_binlogs = |mut client: BinlogClient, events: Arc>>| async move { 120 | let mut stream = client.connect().await.unwrap(); 121 | loop { 122 | let result = stream.read().await; 123 | if let Err(_error) = result { 124 | break; 125 | } else { 126 | events.lock().unwrap().push(result.unwrap().1); 127 | } 128 | } 129 | }; 130 | thread::spawn(move || block_on(parse_binlogs(client, all_events_clone))); 131 | 132 | // wait for binlog parsing 133 | async_std::task::sleep(Duration::from_millis(self.binlog_parse_millis)).await; 134 | 135 | for data in all_events.lock().unwrap().to_vec() { 136 | match data { 137 | EventData::WriteRows(event) => { 138 | self.insert_events.push(event); 139 | } 140 | // mysql8.0 with binlog transaction compression 141 | EventData::TransactionPayload(event) => { 142 | for (_header, data) in event.uncompressed_events { 143 | match data { 144 | EventData::WriteRows(event) => { 145 | self.insert_events.push(event); 146 | } 147 | EventData::UpdateRows(event) => { 148 | self.update_events.push(event); 149 | } 150 | EventData::DeleteRows(event) => { 151 | self.delete_events.push(event); 152 | } 153 | _ => {} 154 | } 155 | } 156 | } 157 | EventData::UpdateRows(event) => { 158 | self.update_events.push(event); 159 | } 160 | EventData::DeleteRows(event) => { 161 | self.delete_events.push(event); 162 | } 163 | EventData::Query(event) => { 164 | self.query_events.push(event); 165 | } 166 | _ => {} 167 | } 168 | } 169 | } 170 | 171 | async fn execute_sqls( 172 | &self, 173 | prepare_sqls: &Vec, 174 | test_sqls: &Vec, 175 | ) -> Result<(String, u32), BinlogError> { 176 | let mut authenticator = Authenticator::new(&self.db_url, 60)?; 177 | let mut channel = authenticator.connect().await?; 178 | 179 | for sql in prepare_sqls { 180 | println!("execute prepare sql: {}", sql); 181 | CommandUtil::execute_sql(&mut channel, &sql).await?; 182 | } 183 | 184 | // get current binlog info 185 | let (binlog_filename, binlog_position, _) = 186 | CommandUtil::fetch_binlog_info(&mut channel).await?; 187 | 188 | for sql in test_sqls { 189 | println!("execute test sql: {}", sql); 190 | CommandUtil::execute_sql(&mut channel, &sql).await?; 191 | } 192 | 193 | Ok((binlog_filename, binlog_position)) 194 | } 195 | } 196 | 197 | impl Default for TestRunner { 198 | fn default() -> Self { 199 | Self::new() 200 | } 201 | } 202 | } 203 | --------------------------------------------------------------------------------