/usr/share/php/adodb/replicate/adodb-replicate.inc.php is in libphp-adodb 5.20.3-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 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 32 33 34 35 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 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 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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 229 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 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 | <?php
define('ADODB_REPLICATE',1.2);
include_once(ADODB_DIR.'/adodb-datadict.inc.php');
/*
1.2 9 June 2009
Minor patches
1.1 8 June 2009
Added $lastUpdateFld to replicatedata
Added $rep->compat. If compat set to 1.0, then $lastUpdateFld not used during MergeData.
1.0 Apr 2009
Added support for MFFA
0.9 ? 2008
First release
Note: this code assumes that comments such as / * * / ar`e allowed which works with:
Note: this code assumes that comments such as / * * / are allowed which works with:
mssql, postgresql, oracle, mssql
Replication engine to
- copy table structures and data from different databases (e.g. mysql to oracle)
for replication purposes
- generate CREATE TABLE, CREATE INDEX, INSERT ... for installation scripts
Table Structure copying includes
- fields and limited subset of types
- optional default values
- indexes
- but not constraints
Two modes of data copy:
ReplicateData
- Copy from src to dest, with update of status of copy back to src,
with configurable src SELECT where clause
MergeData
- Copy from src to dest based on last mod date field and/or copied flag field
Default settings are
- do not execute, generate sql ($rep->execute = false)
- do not delete records in dest table first ($rep->deleteFirst = false).
if $rep->deleteFirst is true and primary keys are defined,
then no deletion will occur unless *INSERTONLY* is defined in pkey array
- only commit once at the end of every ReplicateData ($rep->commitReplicate = true)
- do not autocommit every x records processed ($rep->commitRecs = -1)
- even if error occurs on one record, continue copying remaining records ($rep->neverAbort = true)
- debugging turned off ($rep->debug = false)
*/
class ADODB_Replicate {
var $connSrc;
var $connDest;
var $connSrc2 = false;
var $connDest2 = false;
var $ddSrc;
var $ddDest;
var $execute = false;
var $debug = false;
var $deleteFirst = false;
var $commitReplicate = true; // commit at end of replicatedata
var $commitRecs = -1; // only commit at end of ReplicateData()
var $selFilter = false;
var $fieldFilter = false;
var $indexFilter = false;
var $updateFilter = false;
var $insertFilter = false;
var $updateSrcFn = false;
var $limitRecs = false;
var $neverAbort = true;
var $copyTableDefaults = false; // turn off because functions defined as defaults will not work when copied
var $errHandler = false; // name of error handler function, if used.
var $htmlSpecialChars = true; // if execute false, then output with htmlspecialchars enabled.
// Will autoconfigure itself. No need to modify
var $trgSuffix = '_mrgTr';
var $idxSuffix = '_mrgidx';
var $trLogic = '1 = 1';
var $datesAreTimeStamps = false;
var $oracleSequence = false;
var $readUncommitted = false; // read without obeying shared locks for fast select (mssql)
var $compat = false;
// connSrc2 and connDest2 are only required if the db driver
// does not allow updates back to src db in first connection (the select connection),
// so we need 2nd connection
function __construct($connSrc, $connDest, $connSrc2=false, $connDest2=false)
{
if (strpos($connSrc->databaseType,'odbtp') !== false) {
$connSrc->_bindInputArray = false; # bug in odbtp, binding fails
}
if (strpos($connDest->databaseType,'odbtp') !== false) {
$connDest->_bindInputArray = false; # bug in odbtp, binding fails
}
$this->connSrc = $connSrc;
$this->connDest = $connDest;
$this->connSrc2 = ($connSrc2) ? $connSrc2 : $connSrc;
$this->connDest2 = ($connDest2) ? $connDest2 : $connDest;
$this->ddSrc = NewDataDictionary($connSrc);
$this->ddDest = NewDataDictionary($connDest);
$this->htmlSpecialChars = isset($_SERVER['HTTP_HOST']);
}
function ExecSQL($sql)
{
if (!is_array($sql)) $sql[] = $sql;
$ret = true;
foreach($sql as $s)
if (!$this->execute) echo "<pre>",$s.";\n</pre>";
else {
$ok = $this->connDest->Execute($s);
if (!$ok)
if ($this->neverAbort) $ret = false;
else return false;
}
return $ret;
}
/*
We assume replication between $table and $desttable only works if the field names and types match for both tables.
Also $table and desttable can have different names.
*/
function CopyTableStruct($table,$desttable='')
{
$sql = $this->CopyTableStructSQL($table,$desttable);
if (empty($sql)) return false;
return $this->ExecSQL($sql);
}
function RunFieldFilter(&$fld, $mode = '')
{
if ($this->fieldFilter) {
$fn = $this->fieldFilter;
return $fn($fld, $mode);
} else
return $fld;
}
function RunUpdateFilter($table, $fld, $val)
{
if ($this->updateFilter) {
$fn = $this->updateFilter;
return $fn($table, $fld, $val);
} else
return $val;
}
function RunInsertFilter($table, $fld, &$val)
{
if ($this->insertFilter) {
$fn = $this->insertFilter;
return $fn($table, $fld, $val);
} else
return $fld;
}
/*
$mode = INS or UPD
The lastUpdateFld holds the field that counts the number of updates or the date of last mod. This ensures that
if the rec was modified after replicatedata retrieves the data but before we update back the src record,
we don't set the copiedflag='Y' yet.
*/
function RunUpdateSrcFn($srcdb, $table, $fldoffsets, $row, $where, $mode, $dest_insertid=null, $lastUpdateFld='')
{
if (!$this->updateSrcFn) return;
$bindarr = array();
foreach($fldoffsets as $k) {
$bindarr[$k] = $row[$k];
}
$last = sizeof($row);
if ($lastUpdateFld && $row[$last-1]) {
$ds = $row[$last-1];
if (strpos($ds,':') !== false) $s = $srcdb->DBTimeStamp($ds);
else $s = $srcdb->qstr($ds);
$where = "WHERE $lastUpdateFld = $s and $where";
} else
$where = "WHERE $where";
$fn = $this->updateSrcFn;
if (is_array($fn)) {
if (sizeof($fn) == 1) $set = reset($fn);
else $set = @$fn[$mode];
if ($set) {
if (strlen($dest_insertid) == 0) $dest_insertid = 'null';
$set = str_replace('$INSERT_ID',$dest_insertid,$set);
$sql = "UPDATE $table SET $set $where ";
$ok = $srcdb->Execute($sql,$bindarr);
if (!$ok) {
echo $srcdb->ErrorMsg(),"<br>\n";
die();
}
}
} else $fn($srcdb, $table, $row, $where, $bindarr, $mode, $dest_insertid);
}
function CopyTableStructSQL($table, $desttable='',$dropdest =false)
{
if (!$desttable) {
$desttable = $table;
$prefixidx = '';
} else
$prefixidx = $desttable;
$conn = $this->connSrc;
$types = $conn->MetaColumns($table);
if (!$types) {
echo "$table does not exist in source db<br>\n";
return array();
}
if (!$dropdest && $this->connDest->MetaColumns($desttable)) {
echo "$desttable already exists in dest db<br>\n";
return array();
}
if ($this->debug) var_dump($types);
$sa = array();
$idxcols = array();
foreach($types as $name => $t) {
$s = '';
$mt = $this->ddSrc->MetaType($t->type);
$len = $t->max_length;
$fldname = $this->RunFieldFilter($t->name,'TABLE');
if (!$fldname) continue;
$s .= $fldname . ' '.$mt;
if (isset($t->scale)) $precision = '.'.$t->scale;
else $precision = '';
if ($mt == 'C' or $mt == 'X') $s .= "($len)";
else if ($mt == 'N' && $precision) $s .= "($len$precision)";
if ($mt == 'R') $idxcols[] = $fldname;
if ($this->copyTableDefaults) {
if (isset($t->default_value)) {
$v = $t->default_value;
if ($mt == 'C' or $mt == 'X') $v = $this->connDest->qstr($v); // might not work as this could be function
$s .= ' DEFAULT '.$v;
}
}
$sa[] = $s;
}
$s = implode(",\n",$sa);
// dump adodb intermediate data dictionary format
if ($this->debug) echo '<pre>'.$s.'</pre>';
$sqla = $this->ddDest->CreateTableSQL($desttable,$s);
/*
if ($idxcols) {
$idxoptions = array('UNIQUE'=>1);
$sqla2 = $this->ddDest->_IndexSQL($table.'_'.$fldname.'_SERIAL', $desttable, $idxcols,$idxoptions);
$sqla = array_merge($sqla,$sqla2);
}*/
$idxs = $conn->MetaIndexes($table);
if ($idxs)
foreach($idxs as $name => $iarr) {
$idxoptions = array();
$fldnames = array();
if(!empty($iarr['unique'])) {
$idxoptions['UNIQUE'] = 1;
}
foreach($iarr['columns'] as $fld) {
$fldnames[] = $this->RunFieldFilter($fld,'TABLE');
}
$idxname = $prefixidx.str_replace($table,$desttable,$name);
if (!empty($this->indexFilter)) {
$fn = $this->indexFilter;
$idxname = $fn($desttable,$idxname,$fldnames,$idxoptions);
}
$sqla2 = $this->ddDest->_IndexSQL($idxname, $desttable, $fldnames,$idxoptions);
$sqla = array_merge($sqla,$sqla2);
}
return $sqla;
}
function _clearcache()
{
}
function _concat($v)
{
return $this->connDest->concat("' ","chr(".ord($v).")","'");
}
function fixupbinary($v)
{
return str_replace(
array("\r","\n"),
array($this->_concat("\r"),$this->_concat("\n")),
$v );
}
function SwapDBs()
{
$o = $this->connSrc;
$this->connSrc = $this->connDest;
$this->connDest = $o;
$o = $this->connSrc2;
$this->connSrc2 = $this->connDest2;
$this->connDest2 = $o;
$o = $this->ddSrc;
$this->ddSrc = $this->ddDest;
$this->ddDest = $o;
}
/*
// if no uniqflds defined, then all desttable recs will be deleted before insert
// $where clause must include the WHERE word if used
// if $this->commitRecs is set to a +ve value, then it will autocommit every $this->commitRecs records
// -- this should never be done with 7x24 db's
Returns an array:
$arr[0] = true if no error, false if error
$arr[1] = number of recs processed
$arr[2] = number of successful inserts
$arr[3] = number of successful updates
ReplicateData() params:
$table = src table name
$desttable = dest table name, leave blank to use src table name
$uniqflds = array() = an array. If set, then inserts and updates will occur. eg. array('PK1', 'PK2');
To prevent updates to desttable (allow only to src table), add '*INSERTONLY*' or '*ONLYINSERT*' to array.
Sometimes you are replicating a src table with an autoinc primary key.
You sometimes create recs in the dest table. The dest table has to retrieve the
src table's autoinc key (stored in a 2nd field) so you can match the two tables.
To define this, and the uniqflds contains nested arrays. Copying from autoinc table to other table:
array(array($destpkey), array($destfld_holds_src_autoinc_pkey))
Copying from normal table to autoinc table:
array(array($destpkey), array(), array($srcfld_holds_dest_autoinc_pkey))
$where = where clause for SELECT from $table $where. Include the WHERE reserved word in beginning.
You can put ORDER BY at the end also
$ignoreflds = array(), list of fields to ignore. e.g. array('FLD1',FLD2');
$dstCopyDateFld = date field on $desttable to update with current date
$extraflds allows you to add additional flds to insert/update. Format
array(fldname => $fldval)
$fldval itself can be an array or a string. If an array, then
$extraflds = array($fldname => array($insertval, $updateval))
Thus we have the following behaviours:
a. Delete all data in $desttable then insert from src $table
$rep->execute = true;
$rep->ReplicateData($table, $desttable)
b. Update $desttable if record exists (based on $uniqflds), otherwise insert.
$rep->execute = true;
$rep->ReplicateData($table, $desttable, $array($pkey1, $pkey2))
c. Select from src $table all data modified since a date. Then update $desttable
if record exists (based on $uniqflds), otherwise insert
$rep->execute = true;
$rep->ReplicateData($table, $desttable, array($pkey1, $pkey2), "WHERE update_datetime_fld > $LAST_REFRESH")
d. Insert all records into $desttable modified after a certain id (or time) in src $table:
$rep->execute = true;
$rep->ReplicateData($table, $desttable, false, "WHERE id_fld > $LAST_ID_SAVED", true);
For (a) to (d), returns array: array($boolean_ok_fail, $no_recs_selected_from_src_db, $no_recs_inserted, $no_recs_updated);
e. Generate sample SQL:
$rep->execute = false;
$rep->ReplicateData(....);
This returns $array, which contains:
$array['SEL'] = select stmt from src db
$array['UPD'] = update stmt to dest db
$array['INS'] = insert stmt to dest db
Error-handling
==============
Default is never abort if error occurs. You can set $rep->neverAbort = false; to force replication to abort if an error occurs.
Value Filtering
========
Sometimes you might need to modify/massage the data before the code works. Assume that the value used for True and False is
'T' and 'F' in src DB, but is 'Y' and 'N' in dest DB for field[2] in select stmt. You can do this by
$rep->filterSelect = 'filter';
$rep->ReplicateData(...);
function filter($table,& $fields, $deleteFirst)
{
if ($table == 'SOMETABLE') {
if ($fields[2] == 'T') $fields[2] = 'Y';
else if ($fields[2] == 'F') $fields[2] = 'N';
}
}
We pass in $deleteFirst as that determines the order of the fields (which are numeric-based):
TRUE: the order of fields matches the src table order
FALSE: the order of fields is all non-primary key fields first, followed by primary key fields. This is because it needs
to match the UPDATE statement, which is UPDATE $table SET f2 = ?, f3 = ? ... WHERE f1 = ?
Name Filtering
=========
Sometimes field names that are legal in one RDBMS can be illegal in another.
We allow you to handle this using a field filter.
Also if you don't want to replicate certain fields, just return false.
$rep->fieldFilter = 'ffilter';
function ffilter(&$fld,$mode)
{
$uf = strtoupper($fld);
switch($uf) {
case 'GROUP':
if ($mode == 'SELECT') $fld = '"Group"';
return 'GroupFld';
case 'PRIVATEFLD': # do not replicate
return false;
}
return $fld;
}
UPDATE FILTERING
================
Sometimes, when we want to update
UPDATE table SET fld = val WHERE ....
we want to modify val. To do so, define
$rep->updateFilter = 'ufilter';
function ufilter($table, $fld, $val)
{
return "nvl($fld, $val)";
}
Sending back audit info back to src Table
=========================================
Use $rep->updateSrcFn. This can be an array of strings, or the name of a php function to call.
If an array of strings is defined, then it will perform an update statement...
UPDATE srctable SET $string WHERE ....
With $string set to the array you define. If a new record was inserted into desttable, then the
'INS' string is used ($INSERT_ID will be replaced with the real INSERT_ID, if any),
and if an update then use the 'UPD' string.
array(
'INS' => 'insertid = $INSERT_ID, copieddate=getdate(), copied = 1',
'UPD' => 'copieddate=getdate(), copied = 1'
)
If a single string array is defined, then it will be used for both insert and update.
array('copieddate=getdate(), copied = 1')
Note that the where clause is automatically defined by the system.
If $rep->updateSrcFn is a PHP function name, then it will be called with the following params:
$fn($srcConnection, $tableName, $row, $where, $bindarr, $mode, $dest_insertid)
$srcConnection - source db connection
$tableName - source tablename
$row - array holding records updated into dest
$where - where clause to be used (uses bind vars)
$bindarr - array holding bind variables for where clause
$mode - INS or UPD
$dest_insertid - when mode=INS, then the insert_id is stored here.
oracle mssql
---> insert
mssqlid <--- insert_id
----> update with mssqlid
<---- update with mssqlid
TODO: add src pkey and dest pkey for updates. Also sql stmt needs to be tuned, so dest pkey, src pkey
*/
function ReplicateData($table, $desttable = '', $uniqflds = array(), $where = '',$ignore_flds = array(),
$dstCopyDateFld='', $extraflds = array(), $lastUpdateFld = '')
{
if (is_array($where)) {
$wheresrc = $where[0];
$wheredest = $where[1];
} else {
$wheresrc = $wheredest = $where;
}
$dstCopyDateName = $dstCopyDateFld;
$dstCopyDateFld = strtoupper($dstCopyDateFld);
$this->_clearcache();
if (is_string($uniqflds) && strlen($uniqflds)) $uniqflds = array($uniqflds);
if (!$desttable) $desttable = $table;
$uniq = array();
if ($uniqflds) {
if (is_array(reset($uniqflds))) {
/*
primary key of src and dest tables differ. This means when we perform the select stmts
we retrieve both keys. Then any insert statement will have to ignore one array element.
Any update statement will need to use a different where clause
*/
$destuniqflds = $uniqflds[0];
if (sizeof($uniqflds)>1 && $uniqflds[1]) // srckey field name in dest table
$srcuniqflds = $uniqflds[1];
else
$srcuniqflds = array();
if (sizeof($uniqflds)>2)
$srcPKDest = reset($uniqflds[2]);
} else {
$destuniqflds = $uniqflds;
$srcuniqflds = array();
}
$onlyInsert = false;
foreach($destuniqflds as $k => $u) {
if ($u == '*INSERTONLY*' || $u == '*ONLYINSERT*') {
$onlyInsert = true;
continue;
}
$uniq[strtoupper($u)] = $k;
}
$deleteFirst = $this->deleteFirst;
} else {
$deleteFirst = true;
}
if ($deleteFirst) $onlyInsert = true;
if ($ignore_flds) {
foreach($ignore_flds as $u) {
$ignoreflds[strtoupper($u)] = 1;
}
} else
$ignoreflds = array();
$src = $this->connSrc;
$dest = $this->connDest;
$src2 = $this->connSrc2;
$dest->noNullStrings = false;
$src->noNullStrings = false;
$src2->noNullStrings = false;
if ($src === $dest) $this->execute = false;
$types = $src->MetaColumns($table);
if (!$types) {
echo "Source $table does not exist<br>\n";
return array();
}
$dtypes = $dest->MetaColumns($desttable);
if (!$dtypes) {
echo "Destination $desttable does not exist<br>\n";
return array();
}
$sa = array();
$selflds = array();
$wheref = array();
$wheres = array();
$srcwheref = array();
$fldoffsets = array();
$k = 0;
foreach($types as $name => $t) {
$name2 = strtoupper($this->RunFieldFilter($name,'SELECT'));
// handle quotes
if ($name2 && $name2[0] == '"' && $name2[strlen($name2)-1] == '"') $name22 = substr($name2,1,strlen($name2)-2);
elseif ($name2 && $name2[0] == '`' && $name2[strlen($name2)-1] == '`') $name22 = substr($name2,1,strlen($name2)-2);
else $name22 = $name2;
//else $name22 = $name2; // this causes problem for quotes strip above
if (!isset($dtypes[($name22)]) || !$name2) {
if ($this->debug) echo " Skipping $name ==> $name2 as not in destination $desttable<br>";
continue;
}
if ($name2 == $dstCopyDateFld) {
$dstCopyDateName = $t->name;
continue;
}
$fld = $t->name;
$fldval = $t->name;
$mt = $src->MetaType($t->type);
if ($this->datesAreTimeStamps && $mt == 'D') $mt = 'T';
if ($mt == 'D') $fldval = $dest->DBDate($fldval);
elseif ($mt == 'T') $fldval = $dest->DBTimeStamp($fldval);
$ufld = strtoupper($fld);
if (isset($ignoreflds[($name2)]) && !isset($uniq[$ufld])) {
continue;
}
if ($this->debug) echo " field=$fld type=$mt fldval=$fldval<br>";
if (!isset($uniq[$ufld])) {
$selfld = $fld;
$fld = $this->RunFieldFilter($selfld,'SELECT');
$selflds[] = $selfld;
$p = $dest->Param($k);
if ($mt == 'D') $p = $dest->DBDate($p, true);
else if ($mt == 'T') $p = $dest->DBTimeStamp($p, true);
# UPDATES
$sets[] = "$fld = ".$this->RunUpdateFilter($desttable, $fld, $p);
# INSERTS
$insflds[] = $this->RunInsertFilter($desttable,$fld, $p); $params[] = $p;
$k++;
} else {
$fld = $this->RunFieldFilter($fld);
$wheref[] = $fld;
if (!empty($srcuniqflds)) $srcwheref[] = $srcuniqflds[$uniq[$ufld]];
if ($mt == 'C') { # normally we don't include the primary key in the insert if it is numeric, but ok if varchar
$insertpkey = true;
}
}
}
foreach($extraflds as $fld => $evals) {
if (!is_array($evals)) $evals = array($evals, $evals);
$insflds[] = $this->RunInsertFilter($desttable,$fld, $p); $params[] = $evals[0];
$sets[] = "$fld = ".$evals[1];
}
if ($dstCopyDateFld) {
$sets[] = "$dstCopyDateName = ".$dest->sysTimeStamp;
$insflds[] = $this->RunInsertFilter($desttable,$dstCopyDateName, $p); $params[] = $dest->sysTimeStamp;
}
if (!empty($srcPKDest)) {
$selflds[] = $srcPKDest;
$fldoffsets = array($k+1);
}
foreach($wheref as $uu => $fld) {
$p = $dest->Param($k);
$sp = $src->Param($k);
if (!empty($srcuniqflds)) {
if ($uu > 1) die("Only one primary key for srcuniqflds allowed currently");
$destsrckey = reset($srcuniqflds);
$wheres[] = reset($srcuniqflds).' = '.$p;
$insflds[] = $this->RunInsertFilter($desttable,$destsrckey, $p);
$params[] = $p;
} else {
$wheres[] = $fld.' = '.$p;
if (!isset($ignoreflds[strtoupper($fld)]) || !empty($insertpkey)) {
$insflds[] = $this->RunInsertFilter($desttable,$fld, $p);
$params[] = $p;
}
}
$selflds[] = $fld;
$srcwheres[] = $fld.' = '.$sp;
$fldoffsets[] = $k;
$k++;
}
if (!empty($srcPKDest)) {
$fldoffsets = array($k);
$srcwheres = array($fld.'='.$src->Param($k));
$k++;
}
if ($lastUpdateFld) {
$selflds[] = $lastUpdateFld;
} else
$selflds[] = 'null as Z55_DUMMY_LA5TUPD';
$insfldss = implode(', ', $insflds);
$fldss = implode(', ', $selflds);
$setss = implode(', ', $sets);
$paramss = implode(', ', $params);
$wheress = implode(' AND ', $wheres);
if (isset($srcwheres))
$srcwheress = implode(' AND ',$srcwheres);
$seltable = $table;
if ($this->readUncommitted && strpos($src->databaseType,'mssql')) $seltable .= ' with (NOLOCK)';
$sa['SEL'] = "SELECT $fldss FROM $seltable $wheresrc";
$sa['INS'] = "INSERT INTO $desttable ($insfldss) VALUES ($paramss) /**INS**/";
$sa['UPD'] = "UPDATE $desttable SET $setss WHERE $wheress /**UPD**/";
$DB1 = "/* <font color=green> Source DB - sample sql in case you need to adapt code\n\n";
$DB2 = "/* <font color=green> Dest DB - sample sql in case you need to adapt code\n\n";
if (!$this->execute) echo '/*<style>
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
</style><pre>*/
';
if ($deleteFirst && $this->deleteFirst) {
$where = preg_replace('/[ \n\r\t]+order[ \n\r\t]+by.*$/i', '', $where);
$sql = "DELETE FROM $desttable $wheredest\n";
if (!$this->execute) echo $DB2,'</font>*/',$sql,"\n";
else $dest->Execute($sql);
}
global $ADODB_COUNTRECS;
$err = false;
$savemode = $src->setFetchMode(ADODB_FETCH_NUM);
$ADODB_COUNTRECS = false;
if (!$this->execute) {
echo $DB1,$sa['SEL'],"</font>\n*/\n\n";
echo $DB2,$sa['INS'],"</font>\n*/\n\n";
$suffix = ($onlyInsert) ? ' PRIMKEY=?' : '';
echo $DB2,$sa['UPD'],"$suffix</font>\n*/\n\n";
$rs = $src->Execute($sa['SEL']);
$cnt = 1;
$upd = 0;
$ins = 0;
$sqlarr = explode('?',$sa['INS']);
$nparams = sizeof($sqlarr)-1;
$useQmark = $dest && ($dest->dataProvider != 'oci8');
while ($rs && !$rs->EOF) {
if ($useQmark) {
$sql = ''; $i = 0;
$arr = array_reverse($rs->fields);
//Use each() instead of foreach to reduce memory usage -mikefedyk
while(list(, $v) = each($arr)) {
$sql .= $sqlarr[$i];
// from Ron Baldwin <ron.baldwin#sourceprose.com>
// Only quote string types
$typ = gettype($v);
if ($typ == 'string')
//New memory copy of input created here -mikefedyk
$sql .= $dest->qstr($v);
else if ($typ == 'double')
$sql .= str_replace(',','.',$v); // locales fix so 1.1 does not get converted to 1,1
else if ($typ == 'boolean')
$sql .= $v ? $dest->true : $dest->false;
else if ($typ == 'object') {
if (method_exists($v, '__toString')) $sql .= $dest->qstr($v->__toString());
else $sql .= $dest->qstr((string) $v);
} else if ($v === null)
$sql .= 'NULL';
else
$sql .= $v;
$i += 1;
if ($i == $nparams) break;
} // while
if (isset($sqlarr[$i])) {
$sql .= $sqlarr[$i];
}
$INS = $sql;
} else {
$INS = $sa['INS'];
$arr = array_reverse($rs->fields);
foreach($arr as $k => $v) { // only works on oracle currently
$k = sizeof($arr)-$k-1;
$v = str_replace(":","%~%COLON%!%",$v);
$INS = str_replace(':'.$k,$this->fixupbinary($dest->qstr($v)),$INS);
}
$INS = str_replace("%~%COLON%!%",":",$INS);
if ($this->htmlSpecialChars) $INS = htmlspecialchars($INS);
}
echo "-- $cnt\n",$INS,";\n\n";
$cnt += 1;
$ins += 1;
$rs->MoveNext();
}
$src->setFetchMode($savemode);
return $sa;
} else {
$saved = $src->debug;
#$src->debug=1;
if ($this->limitRecs>100)
$rs = $src->SelectLimit($sa['SEL'],$this->limitRecs);
else
$rs = $src->Execute($sa['SEL']);
$src->debug = $saved;
if (!$rs) {
if ($this->errHandler) $this->_doerr('SEL',array());
return array(0,0,0,0);
}
if ($this->commitReplicate || $commitRecs > 0) {
$dest->BeginTrans();
if ($this->updateSrcFn) $src2->BeginTrans();
}
if ($this->updateSrcFn && strpos($src2->databaseType,'mssql') !== false) {
# problem is writers interfere with readers in mssql
$rs = $src->_rs2rs($rs);
}
$cnt = 0;
$upd = 0;
$ins = 0;
$sizeofrow = sizeof($selflds);
$fn = $this->selFilter;
$commitRecs = $this->commitRecs;
$saved = $dest->debug;
if ($this->deleteFirst) $onlyInsert = true;
while ($origrow = $rs->FetchRow()) {
if ($dest->debug) {flush(); @ob_flush();}
if ($fn) {
if (!$fn($desttable, $origrow, $deleteFirst, $this, $selflds)) continue;
}
$doinsert = true;
$row = array_slice($origrow,0,$sizeofrow-1);
if (!$onlyInsert) {
$doinsert = false;
$upderr = false;
if (isset($srcPKDest)) {
if (is_null($origrow[$sizeofrow-3])) {
$doinsert = true;
$upderr = true;
}
}
if (!$upderr && !$dest->Execute($sa['UPD'],$row)) {
$err = true;
$upderr = true;
if ($this->errHandler) $this->_doerr('UPD',$row);
if (!$this->neverAbort) break;
}
if ($upderr || $dest->Affected_Rows() == 0) {
$doinsert = true;
} else {
if (!empty($uniqflds)) $this->RunUpdateSrcFn($src2, $table, $fldoffsets, $origrow, $srcwheress, 'UPD', null, $lastUpdateFld);
$upd += 1;
}
}
if ($doinsert) {
$inserr = false;
if (isset($srcPKDest)) {
$row = array_slice($origrow,0,$sizeofrow-2);
}
if (! $dest->Execute($sa['INS'],$row)) {
$err = true;
$inserr = true;
if ($this->errHandler) $this->_doerr('INS',$row);
if ($this->neverAbort) continue;
else break;
} else {
if ($dest->dataProvider == 'oci8') {
if ($this->oracleSequence) $lastid = $dest->GetOne("select ".$this->oracleSequence.".currVal from dual");
else $lastid = 'null';
} else {
$lastid = $dest->Insert_ID();
}
if (!$inserr && !empty($uniqflds)) {
$this->RunUpdateSrcFn($src2, $table, $fldoffsets, $origrow, $srcwheress, 'INS', $lastid,$lastUpdateFld);
}
$ins += 1;
}
}
$cnt += 1;
if ($commitRecs > 0 && ($cnt % $commitRecs) == 0) {
$dest->CommitTrans();
$dest->BeginTrans();
if ($this->updateSrcFn) {
$src2->CommitTrans();
$src2->BeginTrans();
}
}
} // while
if ($this->commitReplicate || $commitRecs > 0) {
if (!$this->neverAbort && $err) {
$dest->RollbackTrans();
if ($this->updateSrcFn) $src2->RollbackTrans();
} else {
$dest->CommitTrans();
if ($this->updateSrcFn) $src2->CommitTrans();
}
}
}
if ($cnt != $ins + $upd) echo "<p>ERROR: $cnt != INS $ins + UPD $upd</p>";
$src->setFetchMode($savemode);
return array(!$err, $cnt, $ins, $upd);
}
// trigger support only for sql server and oracle
// need to add
function MergeSrcSetup($srcTable, $pkeys, $srcUpdateDateFld, $srcCopyDateFld, $srcCopyFlagFld,
$srcCopyFlagType='C(1)', $srcCopyFlagVals = array('Y','N','P','='))
{
$sqla = array();
$src = $this->connSrc;
$idx = $srcTable.'_mrgIdx';
$cols = $src->MetaColumns($srcTable);
#adodb_pr($cols);
if (!isset($cols[strtoupper($srcUpdateDateFld)])) {
$sqla = $this->ddSrc->AddColumnSQL($srcTable, "$srcUpdateDateFld TS DEFTIMESTAMP");
foreach($sqla as $sql) $src->Execute($sql);
}
if ($srcCopyDateFld && !isset($cols[strtoupper($srcCopyDateFld)])) {
$sqla = $this->ddSrc->AddColumnSQL($srcTable, "$srcCopyDateFld TS DEFTIMESTAMP");
foreach($sqla as $sql) $src->Execute($sql);
}
$sysdate = $src->sysTimeStamp;
$arrv0 = $src->qstr($srcCopyFlagVals[0]);
$arrv1 = $src->qstr($srcCopyFlagVals[1]);
$arrv2 = $src->qstr($srcCopyFlagVals[2]);
$arrv3 = $src->qstr($srcCopyFlagVals[3]);
if ($srcCopyFlagFld && !isset($cols[strtoupper($srcCopyFlagFld)])) {
$sqla = $this->ddSrc->AddColumnSQL($srcTable, "$srcCopyFlagFld $srcCopyFlagType DEFAULT $arrv1");
foreach($sqla as $sql) $src->Execute($sql);
}
$sqla = array();
$name = "{$srcTable}_mrgTr";
if (is_array($pkeys) && strpos($src->databaseType,'mssql') !== false) {
$pk = reset($pkeys);
#$sqla[] = "DROP TRIGGER $name";
$sqltr = "
TRIGGER $name
ON $srcTable /* for data replication and merge */
AFTER UPDATE
AS
UPDATE $srcTable
SET
$srcUpdateDateFld = case when I.$srcCopyFlagFld = $arrv2 or I.$srcCopyFlagFld = $arrv3 then I.$srcUpdateDateFld
else $sysdate end,
$srcCopyFlagFld = case
when I.$srcCopyFlagFld = $arrv2 then $arrv0
when I.$srcCopyFlagFld = $arrv3 then D.$srcCopyFlagFld
else $arrv1 end
FROM $srcTable S Join Inserted AS I on I.$pk = S.$pk
JOIN Deleted as D ON I.$pk = D.$pk
WHERE I.$srcCopyFlagFld = D.$srcCopyFlagFld or I.$srcCopyFlagFld = $arrv2
or I.$srcCopyFlagFld = $arrv3 or I.$srcCopyFlagFld is null
";
$sqla[] = 'CREATE '.$sqltr; // first if does not exists
$sqla[] = 'ALTER '.$sqltr; // second if it already exists
} else if (strpos($src->databaseType,'oci') !== false) {
if (strlen($srcTable)>22) $tableidx = substr($srcTable,0,16).substr(crc32($srcTable),6);
else $tableidx = $srcTable;
$name = $tableidx.$this->trgSuffix;
$idx = $tableidx.$this->idxSuffix;
$sqla[] = "
CREATE OR REPLACE TRIGGER $name /* for data replication and merge */
BEFORE UPDATE ON $srcTable REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
if :new.$srcCopyFlagFld = $arrv2 then
:new.$srcCopyFlagFld := $arrv0;
elsif :new.$srcCopyFlagFld = $arrv3 then
:new.$srcCopyFlagFld := :old.$srcCopyFlagFld;
elsif :old.$srcCopyFlagFld = :new.$srcCopyFlagFld or :new.$srcCopyFlagFld is null then
if $this->trLogic then
:new.$srcUpdateDateFld := $sysdate;
:new.$srcCopyFlagFld := $arrv1;
end if;
end if;
END;
";
}
foreach($sqla as $sql) $src->Execute($sql);
if ($srcCopyFlagFld) $srcCopyFlagFld .= ', ';
$src->Execute("CREATE INDEX {$idx} on $srcTable ($srcCopyFlagFld$srcUpdateDateFld)");
}
/*
Perform Merge by copying all data modified from src to dest
then update src copied flag if present.
Returns array taken from ReplicateData:
Returns an array:
$arr[0] = true if no error, false if error
$arr[1] = number of recs processed
$arr[2] = number of successful inserts
$arr[3] = number of successful updates
$srcTable = src table
$dstTable = dest table
$pkeys = primary keys array. if empty, then only inserts will occur
$srcignoreflds = ignore these flds (must be upper cased)
$setsrc = updateSrcFn string
$srcUpdateDateFld = field in src with the last update date
$srcCopyFlagFld = false = optional field that holds the copied indicator
$flagvals=array('Y','N','P','=') = array of values indicating array(copied, not copied).
Null is assumed to mean not copied. The 3rd value 'P' indicates that we want to force 'Y', bypassing
default trigger behaviour to reset the COPIED='N' when the record is replicated from other side.
The last value '=' is don't change copyflag.
$srcCopyDateFld = field that holds last copy date in src table, which will be updated on Merge()
$dstCopyDateFld = field that holds last copy date in dst table, which will be updated on Merge()
$defaultDestRaiseErrorFn = The adodb raiseErrorFn handler. Default is to not raise an error.
Just output error message to stdout
*/
function Merge($srcTable, $dstTable, $pkeys, $srcignoreflds, $setsrc,
$srcUpdateDateFld,
$srcCopyFlagFld, $flagvals=array('Y','N','P','='),
$srcCopyDateFld = false,
$dstCopyDateFld = false,
$whereClauses = '',
$orderBy = '', # MUST INCLUDE THE "ORDER BY" suffix
$copyDoneFlagIdx = 3,
$defaultDestRaiseErrorFn = '')
{
$src = $this->connSrc;
$dest = $this->connDest;
$time = $src->Time();
$delfirst = $this->deleteFirst;
$upd = $this->updateSrcFn;
$this->deleteFirst = false;
//$this->updateFirst = true;
$srcignoreflds[] = $srcUpdateDateFld;
$srcignoreflds[] = $srcCopyFlagFld;
$srcignoreflds[] = $srcCopyDateFld;
if (empty($whereClauses)) $whereClauses = '1=1';
$where = " WHERE ($whereClauses) and ($srcCopyFlagFld = ".$src->qstr($flagvals[1]).')';
if ($orderBy) $where .= ' '.$orderBy;
else $where .= ' ORDER BY '.$srcUpdateDateFld;
if ($setsrc) $set[] = $setsrc;
else $set = array();
if ($srcCopyFlagFld) $set[] = "$srcCopyFlagFld = ".$src->qstr($flagvals[2]);
if ($srcCopyDateFld) $set[]= "$srcCopyDateFld = ".$src->sysTimeStamp;
if ($set) $this->updateSrcFn = array(implode(', ',$set));
else $this->updateSrcFn = '';
$extra[$srcCopyFlagFld] = array($dest->qstr($flagvals[0]),$dest->qstr($flagvals[$copyDoneFlagIdx]));
$saveraise = $dest->raiseErrorFn;
$dest->raiseErrorFn = '';
if ($this->compat && $this->compat == 1.0) $srcUpdateDateFld = '';
$arr = $this->ReplicateData($srcTable, $dstTable, $pkeys, $where, $srcignoreflds,
$dstCopyDateFld,$extra,$srcUpdateDateFld);
$dest->raiseErrorFn = $saveraise;
$this->updateSrcFn = $upd;
$this->deleteFirst = $delfirst;
return $arr;
}
/*
If doing a 2 way merge, then call
$rep->Merge()
to save without modifying the COPIEDFLAG ('=').
Then can the following to set the COPIEDFLAG to 'P' which forces the COPIEDFLAG = 'Y'
$rep->MergeDone()
*/
function MergeDone($srcTable, $dstTable, $pkeys, $srcignoreflds, $setsrc,
$srcUpdateDateFld,
$srcCopyFlagFld, $flagvals=array('Y','N','P','='),
$srcCopyDateFld = false,
$dstCopyDateFld = false,
$whereClauses = '',
$orderBy = '', # MUST INCLUDE THE "ORDER BY" suffix
$copyDoneFlagIdx = 2,
$defaultDestRaiseErrorFn = '')
{
return $this->Merge($srcTable, $dstTable, $pkeys, $srcignoreflds, $setsrc,
$srcUpdateDateFld,
$srcCopyFlagFld, $flagvals,
$srcCopyDateFld,
$dstCopyDateFld,
$whereClauses,
$orderBy, # MUST INCLUDE THE "ORDER BY" suffix
$copyDoneFlagIdx,
$defaultDestRaiseErrorFn);
}
function _doerr($reason, $selflds)
{
$fn = $this->errHandler;
if ($fn) $fn($this, $reason, $selflds); // set $this->neverAbort to true or false as required inside $fn
}
}
|