Class: Coopy::SqlCompare
- Inherits:
-
Object
- Object
- Coopy::SqlCompare
- Defined in:
- lib/lib/coopy/sql_compare.rb
Instance Attribute Summary collapse
-
#alt ⇒ Object
Returns the value of attribute alt.
-
#db ⇒ Object
Returns the value of attribute db.
-
#local ⇒ Object
Returns the value of attribute local.
-
#remote ⇒ Object
Returns the value of attribute remote.
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(db, local, remote, alt, align = nil) ⇒ SqlCompare
constructor
A new instance of SqlCompare.
- #scan_columns(all_cols1, all_cols2, key_cols, present1, present2, align) ⇒ Object
- #validate_schema ⇒ Object
Constructor Details
#initialize(db, local, remote, alt, align = nil) ⇒ SqlCompare
Returns a new instance of SqlCompare.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lib/coopy/sql_compare.rb', line 7 def initialize(db,local,remote,alt,align = nil) @db = db @local = local @remote = remote @alt = alt @align = align @peered = false @alt_peered = false if local != nil && remote != nil if @remote.get_database. != nil if @remote.get_database. != @local.get_database. local.get_database.get_helper.attach(db,"__peer__",@remote.get_database.) @peered = true end end end if @alt != nil && local != nil if @alt.get_database. != nil if @alt.get_database. != @local.get_database. local.get_database.get_helper.attach(db,"__alt__",@alt.get_database.) @alt_peered = true end end end end |
Instance Attribute Details
#alt ⇒ Object
Returns the value of attribute alt.
36 37 38 |
# File 'lib/lib/coopy/sql_compare.rb', line 36 def alt @alt end |
#db ⇒ Object
Returns the value of attribute db.
33 34 35 |
# File 'lib/lib/coopy/sql_compare.rb', line 33 def db @db end |
#local ⇒ Object
Returns the value of attribute local.
34 35 36 |
# File 'lib/lib/coopy/sql_compare.rb', line 34 def local @local end |
#remote ⇒ Object
Returns the value of attribute remote.
35 36 37 |
# File 'lib/lib/coopy/sql_compare.rb', line 35 def remote @remote end |
Instance Method Details
#apply ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/lib/coopy/sql_compare.rb', line 230 def apply return nil if @db == nil @align = ::Coopy::Alignment.new if @align == nil return nil if !self.validate_schema rowid_name = @db.rowid key_cols = [] data_cols = [] all_cols = [] all_cols1 = [] all_cols2 = [] all_cols3 = [] common = @local if @local != nil key_cols = @local.get_primary_key data_cols = @local.get_all_but_primary_key all_cols = @local.get_column_names all_cols1 = @local.get_column_names end if @remote != nil all_cols2 = @remote.get_column_names common = @remote if common == nil end if @alt != nil all_cols3 = @alt.get_column_names common = @alt if common == nil else all_cols3 = all_cols2 end all_common_cols = Array.new data_common_cols = Array.new present1 = {} present2 = {} present3 = {} present_primary = {} has_column_add = false begin _g1 = 0 _g = key_cols.length while(_g1 < _g) i = _g1 _g1+=1 present_primary[key_cols[i]] = i end end begin _g11 = 0 _g2 = all_cols1.length while(_g11 < _g2) i1 = _g11 _g11+=1 key = all_cols1[i1] present1[key] = i1 end end begin _g12 = 0 _g3 = all_cols2.length while(_g12 < _g3) i2 = _g12 _g12+=1 key1 = all_cols2[i2] has_column_add = true if !present1.include?(key1) present2[key1] = i2 end end begin _g13 = 0 _g4 = all_cols3.length while(_g13 < _g4) i3 = _g13 _g13+=1 key2 = all_cols3[i3] has_column_add = true if !present1.include?(key2) present3[key2] = i3 if present1.include?(key2) if present2.include?(key2) all_common_cols.push(key2) data_common_cols.push(key2) if !present_primary.include?(key2) end end end end @align. = ::Coopy::Alignment.new begin _g14 = 0 _g5 = all_cols1.length while(_g14 < _g5) i4 = _g14 _g14+=1 key3 = all_cols1[i4] if present2.include?(key3) @align..link(i4,present2[key3]) else @align..link(i4,-1) end end end begin _g15 = 0 _g6 = all_cols2.length while(_g15 < _g6) i5 = _g15 _g15+=1 key4 = all_cols2[i5] @align..link(-1,i5) if !present1.include?(key4) end end self.scan_columns(all_cols1,all_cols2,key_cols,present1,present2,@align) @align.tables(@local,@remote) if @alt != nil self.scan_columns(all_cols1,all_cols3,key_cols,present1,present3,@align.reference) @align.reference.tables(@local,@alt) end sql_table1 = "" sql_table2 = "" sql_table3 = "" sql_table1 = @local.get_quoted_table_name if @local != nil sql_table2 = @remote.get_quoted_table_name if @remote != nil sql_table3 = @alt.get_quoted_table_name if @alt != nil if @peered sql_table1 = "main." + _hx_str(sql_table1) sql_table2 = "__peer__." + _hx_str(sql_table2) end sql_table2 = "__alt__." + _hx_str(sql_table3) if @alt_peered sql_key_cols = "" begin _g16 = 0 _g7 = key_cols.length while(_g16 < _g7) i6 = _g16 _g16+=1 sql_key_cols += "," if i6 > 0 sql_key_cols += common.get_quoted_column_name(key_cols[i6]) end end sql_all_cols = "" begin _g17 = 0 _g8 = all_common_cols.length while(_g17 < _g8) i7 = _g17 _g17+=1 sql_all_cols += "," if i7 > 0 sql_all_cols += common.get_quoted_column_name(all_common_cols[i7]) end end sql_all_cols1 = "" begin _g18 = 0 _g9 = all_cols1.length while(_g18 < _g9) i8 = _g18 _g18+=1 sql_all_cols1 += "," if i8 > 0 sql_all_cols1 += @local.get_quoted_column_name(all_cols1[i8]) end end sql_all_cols2 = "" begin _g19 = 0 _g10 = all_cols2.length while(_g19 < _g10) i9 = _g19 _g19+=1 sql_all_cols2 += "," if i9 > 0 sql_all_cols2 += @remote.get_quoted_column_name(all_cols2[i9]) end end sql_all_cols3 = "" if @alt != nil _g110 = 0 _g20 = all_cols3.length while(_g110 < _g20) i10 = _g110 _g110+=1 sql_all_cols3 += "," if i10 > 0 sql_all_cols3 += @alt.get_quoted_column_name(all_cols3[i10]) end end sql_key_match2 = "" begin _g111 = 0 _g21 = key_cols.length while(_g111 < _g21) i11 = _g111 _g111+=1 sql_key_match2 += " AND " if i11 > 0 n = common.get_quoted_column_name(key_cols[i11]) sql_key_match2 += _hx_str(sql_table1) + "." + _hx_str(n) + " IS " + _hx_str(sql_table2) + "." + _hx_str(n) end end sql_key_match3 = "" if @alt != nil _g112 = 0 _g22 = key_cols.length while(_g112 < _g22) i12 = _g112 _g112+=1 sql_key_match3 += " AND " if i12 > 0 n1 = common.get_quoted_column_name(key_cols[i12]) sql_key_match3 += _hx_str(sql_table1) + "." + _hx_str(n1) + " IS " + _hx_str(sql_table3) + "." + _hx_str(n1) end end sql_data_mismatch = "" begin _g113 = 0 _g23 = data_common_cols.length while(_g113 < _g23) i13 = _g113 _g113+=1 sql_data_mismatch += " OR " if i13 > 0 n2 = common.get_quoted_column_name(data_common_cols[i13]) sql_data_mismatch += _hx_str(sql_table1) + "." + _hx_str(n2) + " IS NOT " + _hx_str(sql_table2) + "." + _hx_str(n2) end end begin _g114 = 0 _g24 = all_cols2.length while(_g114 < _g24) i14 = _g114 _g114+=1 key5 = all_cols2[i14] if !present1.include?(key5) sql_data_mismatch += " OR " if sql_data_mismatch != "" n3 = common.get_quoted_column_name(key5) sql_data_mismatch += _hx_str(sql_table2) + "." + _hx_str(n3) + " IS NOT NULL" end end end if @alt != nil begin _g115 = 0 _g25 = data_common_cols.length while(_g115 < _g25) i15 = _g115 _g115+=1 sql_data_mismatch += " OR " if sql_data_mismatch.length > 0 n4 = common.get_quoted_column_name(data_common_cols[i15]) sql_data_mismatch += _hx_str(sql_table1) + "." + _hx_str(n4) + " IS NOT " + _hx_str(sql_table3) + "." + _hx_str(n4) end end begin _g116 = 0 _g26 = all_cols3.length while(_g116 < _g26) i16 = _g116 _g116+=1 key6 = all_cols3[i16] if !present1.include?(key6) sql_data_mismatch += " OR " if sql_data_mismatch != "" n5 = common.get_quoted_column_name(key6) sql_data_mismatch += _hx_str(sql_table3) + "." + _hx_str(n5) + " IS NOT NULL" end end end end sql_dbl_cols = "" dbl_cols = [] begin _g117 = 0 _g27 = all_cols1.length while(_g117 < _g27) i17 = _g117 _g117+=1 sql_dbl_cols += "," if sql_dbl_cols != "" buf = "__coopy_" + _hx_str(i17) n6 = common.get_quoted_column_name(all_cols1[i17]) sql_dbl_cols += _hx_str(sql_table1) + "." + _hx_str(n6) + " AS " + _hx_str(buf) dbl_cols.push(buf) end end begin _g118 = 0 _g28 = all_cols2.length while(_g118 < _g28) i18 = _g118 _g118+=1 sql_dbl_cols += "," if sql_dbl_cols != "" buf1 = "__coopy_" + _hx_str(i18) + "b" n7 = common.get_quoted_column_name(all_cols2[i18]) sql_dbl_cols += _hx_str(sql_table2) + "." + _hx_str(n7) + " AS " + _hx_str(buf1) dbl_cols.push(buf1) end end if @alt != nil _g119 = 0 _g29 = all_cols3.length while(_g119 < _g29) i19 = _g119 _g119+=1 sql_dbl_cols += "," if sql_dbl_cols != "" buf2 = "__coopy_" + _hx_str(i19) + "c" n8 = common.get_quoted_column_name(all_cols3[i19]) sql_dbl_cols += _hx_str(sql_table3) + "." + _hx_str(n8) + " AS " + _hx_str(buf2) dbl_cols.push(buf2) end end sql_order = "" begin _g120 = 0 _g30 = key_cols.length while(_g120 < _g30) i20 = _g120 _g120+=1 sql_order += "," if i20 > 0 n9 = common.get_quoted_column_name(key_cols[i20]) sql_order += n9 end end rowid = "-3" rowid1 = "-3" rowid2 = "-3" rowid3 = "-3" if rowid_name != nil rowid = rowid_name rowid1 = _hx_str(sql_table1) + "." + _hx_str(rowid_name) if @local != nil rowid2 = _hx_str(sql_table2) + "." + _hx_str(rowid_name) if @remote != nil rowid3 = _hx_str(sql_table3) + "." + _hx_str(rowid_name) if @alt != nil end @at0 = 1 @at1 = 1 @at2 = 1 @diff_ct = 0 if @remote != nil sql_inserts = "SELECT DISTINCT 0 AS __coopy_code, NULL, " + _hx_str(rowid) + " AS rowid, NULL, " + _hx_str(sql_all_cols2) + " FROM " + _hx_str(sql_table2) sql_inserts += " WHERE NOT EXISTS (SELECT 1 FROM " + _hx_str(sql_table1) + _hx_str(self.where(sql_key_match2)) + ")" if @local != nil sql_inserts_order = ["__coopy_code","NULL","rowid","NULL"].concat(all_cols2) self.link_query(sql_inserts,sql_inserts_order) end if @alt != nil sql_inserts1 = "SELECT DISTINCT 1 AS __coopy_code, NULL, NULL, " + _hx_str(rowid) + " AS rowid, " + _hx_str(sql_all_cols3) + " FROM " + _hx_str(sql_table3) sql_inserts1 += " WHERE NOT EXISTS (SELECT 1 FROM " + _hx_str(sql_table1) + _hx_str(self.where(sql_key_match3)) + ")" if @local != nil sql_inserts_order1 = ["__coopy_code","NULL","NULL","rowid"].concat(all_cols3) self.link_query(sql_inserts1,sql_inserts_order1) end if @local != nil && @remote != nil sql_updates = "SELECT DISTINCT 2 AS __coopy_code, " + _hx_str(rowid1) + " AS __coopy_rowid0, " + _hx_str(rowid2) + " AS __coopy_rowid1, " if @alt != nil sql_updates += _hx_str(rowid3) + " AS __coopy_rowid2," else sql_updates += " NULL," end sql_updates += _hx_str(sql_dbl_cols) + " FROM " + _hx_str(sql_table1) sql_updates += " INNER JOIN " + _hx_str(sql_table2) + " ON " + _hx_str(sql_key_match2) if sql_table1 != sql_table2 sql_updates += " INNER JOIN " + _hx_str(sql_table3) + " ON " + _hx_str(sql_key_match3) if @alt != nil && sql_table1 != sql_table3 sql_updates += self.where(sql_data_mismatch) sql_updates_order = ["__coopy_code","__coopy_rowid0","__coopy_rowid1","__coopy_rowid2"].concat(dbl_cols) self.link_query(sql_updates,sql_updates_order) end if @alt == nil if @local != nil sql_deletes = "SELECT DISTINCT 0 AS __coopy_code, " + _hx_str(rowid) + " AS rowid, NULL, NULL, " + _hx_str(sql_all_cols1) + " FROM " + _hx_str(sql_table1) sql_deletes += " WHERE NOT EXISTS (SELECT 1 FROM " + _hx_str(sql_table2) + _hx_str(self.where(sql_key_match2)) + ")" if @remote != nil sql_deletes_order = ["__coopy_code","rowid","NULL","NULL"].concat(all_cols1) self.link_query(sql_deletes,sql_deletes_order) end end if @alt != nil sql_deletes1 = "SELECT 2 AS __coopy_code, " + _hx_str(rowid1) + " AS __coopy_rowid0, " + _hx_str(rowid2) + " AS __coopy_rowid1, " sql_deletes1 += _hx_str(rowid3) + " AS __coopy_rowid2, " sql_deletes1 += sql_dbl_cols sql_deletes1 += " FROM " + _hx_str(sql_table1) sql_deletes1 += " LEFT OUTER JOIN " + _hx_str(sql_table2) + " ON " + _hx_str(sql_key_match2) if @remote != nil sql_deletes1 += " LEFT OUTER JOIN " + _hx_str(sql_table3) + " ON " + _hx_str(sql_key_match3) sql_deletes1 += " WHERE __coopy_rowid1 IS NULL OR __coopy_rowid2 IS NULL" sql_deletes_order1 = ["__coopy_code","__coopy_rowid0","__coopy_rowid1","__coopy_rowid2"].concat(dbl_cols) self.link_query(sql_deletes1,sql_deletes_order1) end @align.mark_identical if @diff_ct == 0 @align end |
#scan_columns(all_cols1, all_cols2, key_cols, present1, present2, align) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/lib/coopy/sql_compare.rb', line 192 def scan_columns(all_cols1,all_cols2,key_cols,present1,present2,align) align. = ::Coopy::Alignment.new begin _g1 = 0 _g = all_cols1.length while(_g1 < _g) i = _g1 _g1+=1 key = all_cols1[i] if present2.include?(key) align..link(i,present2[key]) else align..link(i,-1) end end end begin _g11 = 0 _g2 = all_cols2.length while(_g11 < _g2) i1 = _g11 _g11+=1 key1 = all_cols2[i1] align..link(-1,i1) if !present1.include?(key1) end end align..range(all_cols1.length,all_cols2.length) begin _g3 = 0 while(_g3 < key_cols.length) key2 = key_cols[_g3] _g3+=1 unit = ::Coopy::Unit.new(present1[key2],present2[key2]) align.add_index_columns(unit) end end end |
#validate_schema ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lib/coopy/sql_compare.rb', line 65 def validate_schema all_cols1 = [] key_cols1 = [] access_error = false pk_missing = false if @local != nil all_cols1 = @local.get_column_names key_cols1 = @local.get_primary_key access_error = true if all_cols1.length == 0 pk_missing = true if key_cols1.length == 0 end all_cols2 = [] key_cols2 = [] if @remote != nil all_cols2 = @remote.get_column_names key_cols2 = @remote.get_primary_key access_error = true if all_cols2.length == 0 pk_missing = true if key_cols2.length == 0 end all_cols3 = all_cols2 key_cols3 = key_cols2 if @alt != nil all_cols3 = @alt.get_column_names key_cols3 = @alt.get_primary_key access_error = true if all_cols3.length == 0 pk_missing = true if key_cols3.length == 0 end raise hx_raise("Error accessing SQL table") if access_error raise hx_raise("sql diff not possible when primary key not available") if pk_missing pk_change = false if @local != nil && @remote != nil pk_change = true if !self.equal_array(key_cols1,key_cols2) end if @local != nil && @alt != nil pk_change = true if !self.equal_array(key_cols1,key_cols3) end raise hx_raise("sql diff not possible when primary key changes") if pk_change true end |