Class: Flydata::Parser::Mysql::MysqlDumpParser
- Inherits:
-
Object
- Object
- Flydata::Parser::Mysql::MysqlDumpParser
- Defined in:
- lib/flydata/parser/mysql/dump_parser.rb
Defined Under Namespace
Modules: State Classes: InsertParser
Instance Attribute Summary collapse
-
#binlog_pos ⇒ Object
Returns the value of attribute binlog_pos.
Instance Method Summary collapse
-
#initialize(option = {}) ⇒ MysqlDumpParser
constructor
A new instance of MysqlDumpParser.
- #parse(dump_io, create_table_block, insert_record_block, check_point_block) ⇒ Object
Constructor Details
#initialize(option = {}) ⇒ MysqlDumpParser
Returns a new instance of MysqlDumpParser.
252 253 254 255 |
# File 'lib/flydata/parser/mysql/dump_parser.rb', line 252 def initialize(option = {}) @binlog_pos = option[:binlog_pos] @option = option end |
Instance Attribute Details
#binlog_pos ⇒ Object
Returns the value of attribute binlog_pos.
250 251 252 |
# File 'lib/flydata/parser/mysql/dump_parser.rb', line 250 def binlog_pos @binlog_pos end |
Instance Method Details
#parse(dump_io, create_table_block, insert_record_block, check_point_block) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/flydata/parser/mysql/dump_parser.rb', line 257 def parse(dump_io, create_table_block, insert_record_block, check_point_block) unless dump_io.kind_of?(IO) raise ArgumentError.new("Invalid argument. The first parameter must be io.") end invalid_file = false current_state = State::START substate = nil buffered_line = nil bytesize = 0 readline_proc = Proc.new do line = nil if buffered_line line = buffered_line buffered_line = nil else rawline = dump_io.readline bytesize += rawline.bytesize line = rawline.strip end line end state_start = Proc.new do line = readline_proc.call # -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=120; m = /^\-\- CHANGE MASTER TO MASTER_LOG_FILE='(?<binfile>[^']+)', MASTER_LOG_POS=(?<pos>\d+)/.match(line) if m @binlog_pos = {binfile: m[:binfile], pos: m[:pos].to_i} current_state = State::CREATE_TABLE check_point_block.call(nil, dump_io.pos, bytesize, @binlog_pos, current_state) end end current_table = nil state_create_table = Proc.new do line = readline_proc.call # CREATE TABLE `active_admin_comments` ( m = /^CREATE TABLE `(?<table_name>[^`]+)`/.match(line) if m current_table = MysqlTable.new(m[:table_name]) current_state = State::CREATE_TABLE_COLUMNS end end state_create_table_constraints = Proc.new do line = readline_proc.call # PRIMARY KEY (`id`), if line.start_with?(')') create_table_block.call(current_table) current_state = State::INSERT_RECORD check_point_block.call(current_table, dump_io.pos, bytesize, @binlog_pos, current_state) elsif m = /^PRIMARY KEY \((?<primary_keys>[^\)]+)\)/.match(line) current_table.primary_keys = m[:primary_keys].split(',').collect do |pk_str| pk_str[1..-2] end end end state_create_table_columns = Proc.new do start_pos = dump_io.pos line = readline_proc.call # `author_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, if line.start_with?("\`") column = {} # parse column line line = line[0..-2] if line.end_with?(',') items = line.split column[:column_name] = items.shift[1..-2] column[:format_type_str] = format_type_str = items.shift pos = format_type_str.index('(') if pos ft = column[:format_type] = format_type_str[0..pos-1] if ft == 'decimal' precision, scale = format_type_str[pos+1..-2].split(',').collect{|v| v.to_i} column[:decimal_precision] = precision column[:decimal_scale] = scale else column[:format_size] = format_type_str[pos+1..-2].to_i end else column[:format_type] = format_type_str end while (item = items.shift) do case item when 'DEFAULT' value = items.shift value = value.start_with?('\'') ? value[1..-2] : value value = nil if value == 'NULL' column[:default] = value when 'NOT' if items[1] == 'NULL' items.shift column[:not_null] = true end when 'unsigned' column[:unsigned] = true else #ignore other options end end current_table.add_column(column) else current_state = State::CREATE_TABLE_CONSTRAINTS buffered_line = line state_create_table_constraints.call end end state_insert_record = Proc.new do line = readline_proc.call if line.start_with?('INSERT') buffered_line = line current_state = State::PARSING_INSERT_RECORD elsif line.start_with?('UNLOCK') current_state = State::CREATE_TABLE check_point_block.call(current_table, dump_io.pos, bytesize, @binlog_pos, current_state) end end state_parsing_insert_record = Proc.new do line = readline_proc.call values_set = InsertParser.new.parse(line) current_state = State::INSERT_RECORD if insert_record_block.call(current_table, values_set) check_point_block.call(current_table, dump_io.pos, bytesize, @binlog_pos, current_state) end end # Start reading file from top begin # resume(only when using dump file) if @option[:last_pos] && (@option[:last_pos].to_i != -1) dump_io.pos = @option[:last_pos].to_i current_state = @option[:state] substate = @option[:substate] current_table = @option[:mysql_table] bytesize = dump_io.pos end until dump_io.eof? do case current_state when State::START state_start.call when State::CREATE_TABLE state_create_table.call when State::CREATE_TABLE_COLUMNS state_create_table_columns.call when State::CREATE_TABLE_CONSTRAINTS state_create_table_constraints.call when State::INSERT_RECORD state_insert_record.call when State::PARSING_INSERT_RECORD state_parsing_insert_record.call end end end @binlog_pos end |