Class: MysqlReplicator::Binlogs::TableMapEventParser
- Inherits:
-
Object
- Object
- MysqlReplicator::Binlogs::TableMapEventParser
- Defined in:
- lib/mysql_replicator/binlogs/table_map_event_parser.rb
Class Method Summary collapse
- .extract_enum_from_column_type(data_type, column_type) ⇒ Object
- .get_table_columns(connection, database, table) ⇒ Object
- .parse(payload, connection) ⇒ Object
- .to_little_endian(bytes) ⇒ Object
Class Method Details
.extract_enum_from_column_type(data_type, column_type) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mysql_replicator/binlogs/table_map_event_parser.rb', line 140 def self.extract_enum_from_column_type(data_type, column_type) return nil unless data_type.downcase == 'enum' # Extract values from ENUM('value1','value2','value3') if column_type =~ /enum\((.*)\)/i enum_string = ::Regexp.last_match(1) || '' # Extract value by arround single quote values = enum_string.scan(/'([^']*)'/).flatten return values end nil end |
.get_table_columns(connection, database, table) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mysql_replicator/binlogs/table_map_event_parser.rb', line 84 def self.get_table_columns(connection, database, table) # Create a separate connection to query table structure query_connection = connection.dup # Query table structure # IMPORTANT: Column data is stored in ascending order of ORDINAL_POSITION query = <<~SQL SELECT ORDINAL_POSITION, DATA_TYPE, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_SET_NAME, COLLATION_NAME, COLUMN_KEY FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '#{database}' AND TABLE_NAME = '#{table}' ORDER BY ORDINAL_POSITION SQL result = query_connection.query(query) # Close the separated connection query_connection.close if result.nil? || !result[:rows].is_a?(Array) return [] end result[:rows].map do |row| { ordinal_position: row[:ordinal_position].to_i, data_type: row[:data_type].to_s, column_name: row[:column_name].to_s, column_type: row[:column_type].to_s, enum_values: extract_enum_from_column_type(row[:data_type].to_s, row[:column_type].to_s), nullable: row[:is_nullable] == 'YES', column_default: row[:column_default], numeric_precision: row[:numeric_precision].to_i, numeric_scale: row[:numeric_scale].to_i, character_maximum_length: row[:character_maximum_length].to_i, character_set_name: row[:character_set_name].to_s, collation_name: row[:collation_name].to_s, primary_key: row[:column_key] == 'PRI' } end end |
.parse(payload, connection) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mysql_replicator/binlogs/table_map_event_parser.rb', line 36 def self.parse(payload, connection) offset = 0 # Table ID (6 bytes) table_id = to_little_endian(MysqlReplicator::StringUtil.read_array_from_int8(payload[0, 6])) offset += 6 # Flags (2 bytes) flags = MysqlReplicator::StringUtil.read_uint16(payload[offset, 2]) offset += 2 # Database name length (1 byte) + database name + null terminator db_name_len = MysqlReplicator::StringUtil.read_uint8(payload[offset, 1]) offset += 1 database_name = MysqlReplicator::StringUtil.read_str(payload[offset, db_name_len]) offset += db_name_len + 1 # +1 for null terminator # Table name length (1 byte) + table name + null terminator table_name_len = MysqlReplicator::StringUtil.read_uint8(payload[offset, 1]) offset += 1 table_name = MysqlReplicator::StringUtil.read_str(payload[offset, table_name_len]) # Get actual column names from table schema columns = get_table_columns(connection, database_name, table_name) { database: database_name, table: table_name, table_id: table_id, columns: columns, flags: flags } end |
.to_little_endian(bytes) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/mysql_replicator/binlogs/table_map_event_parser.rb', line 72 def self.to_little_endian(bytes) result = 0 bytes.each_with_index do |byte, i| result |= (byte << (i * 8)) end result end |