test-tnb.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. include_once('../adodb.inc.php');
  3. include_once('adodb-replicate.inc.php');
  4. set_time_limit(0);
  5. function IndexFilter($dtable, $idxname,$flds,$options)
  6. {
  7. if (strlen($idxname) > 28) $idxname = substr($idxname,0,24).rand(1000,9999);
  8. return $idxname;
  9. }
  10. function SelFilter($table, &$arr, $delfirst)
  11. {
  12. return true;
  13. }
  14. function updatefilter($table, $fld, $val)
  15. {
  16. return "nvl($fld, $val)";
  17. }
  18. function FieldFilter(&$fld,$mode)
  19. {
  20. $uf = strtoupper($fld);
  21. switch($uf) {
  22. case 'SIZEFLD':
  23. return 'Size';
  24. case 'GROUPFLD':
  25. return 'Group';
  26. case 'GROUP':
  27. if ($mode == 'SELECT') $fld = '"Group"';
  28. return 'GroupFld';
  29. case 'SIZE':
  30. if ($mode == 'SELECT') $fld = '"Size"';
  31. return 'SizeFld';
  32. }
  33. return $fld;
  34. }
  35. function ParseTable(&$table, &$pkey)
  36. {
  37. $table = trim($table);
  38. if (strlen($table) == 0) return false;
  39. if (strpos($table, '#') !== false) {
  40. $at = strpos($table, '#');
  41. $table = trim(substr($table,0,$at));
  42. if (strlen($table) == 0) return false;
  43. }
  44. $tabarr = explode(',',$table);
  45. if (sizeof($tabarr) == 1) {
  46. $table = $tabarr[0];
  47. $pkey = '';
  48. echo "No primary key for $table **** **** <br>";
  49. } else {
  50. $table = trim($tabarr[0]);
  51. $pkey = trim($tabarr[1]);
  52. if (strpos($pkey,' ') !== false) echo "Bad PKEY for $table $pkey<br>";
  53. }
  54. return true;
  55. }
  56. global $TARR;
  57. function TableStats($rep, $table, $pkey)
  58. {
  59. global $TARR;
  60. if (empty($TARR)) $TARR = array();
  61. $cnt = $rep->connSrc->GetOne("select count(*) from $table");
  62. if (isset($TARR[$table])) echo "<h1>Table $table repeated twice</h1>";
  63. $TARR[$table] = $cnt;
  64. if ($pkey) {
  65. $ok = $rep->connSrc->SelectLimit("select $pkey from $table",1);
  66. if (!$ok) echo "<h1>$table: $pkey does not exist</h1>";
  67. } else
  68. echo "<h1>$table: no primary key</h1>";
  69. }
  70. function CreateTable($rep, $table)
  71. {
  72. ## CREATE TABLE
  73. #$DB2->Execute("drop table $table");
  74. $rep->execute = true;
  75. $ok = $rep->CopyTableStruct($table);
  76. if ($ok) echo "Table Created<br>\n";
  77. else {
  78. echo "<hr>Error: Cannot Create Table<hr>\n";
  79. }
  80. flush();@ob_flush();
  81. }
  82. function CopyData($rep, $table, $pkey)
  83. {
  84. $dtable = $table;
  85. $rep->execute = true;
  86. $rep->deleteFirst = true;
  87. $secs = time();
  88. $rows = $rep->ReplicateData($table,$dtable,array($pkey));
  89. $secs = time() - $secs;
  90. if (!$rows || !$rows[0] || !$rows[1] || $rows[1] != $rows[2]+$rows[3]) {
  91. echo "<hr>Error: "; var_dump($rows); echo " (secs=$secs) <hr>\n";
  92. } else
  93. echo date('H:i:s'),': ',$rows[1]," record(s) copied, ",$rows[2]," inserted, ",$rows[3]," updated (secs=$secs)<br>\n";
  94. flush();@ob_flush();
  95. }
  96. function MergeDataJohnTest($rep, $table, $pkey)
  97. {
  98. $rep->SwapDBs();
  99. $dtable = $table;
  100. $rep->oracleSequence = 'LGBSEQUENCE';
  101. # $rep->MergeSrcSetup($table, array($pkey),'UpdatedOn','CopiedFlag');
  102. if (strpos($rep->connDest->databaseType,'mssql') !== false) { # oracle ==> mssql
  103. $ignoreflds = array($pkey);
  104. $ignoreflds[] = 'MSSQL_ID';
  105. $set = 'MSSQL_ID=nvl($INSERT_ID,MSSQL_ID)';
  106. $pkeyarr = array(array($pkey),false,array('MSSQL_ID'));# array('MSSQL_ID', 'ORA_ID'));
  107. } else { # mssql ==> oracle
  108. $ignoreflds = array($pkey);
  109. $ignoreflds[] = 'ORA_ID';
  110. $set = '';
  111. #$set = 'ORA_ID=isnull($INSERT_ID,ORA_ID)';
  112. $pkeyarr = array(array($pkey),array('MSSQL_ID'));
  113. }
  114. $rep->execute = true;
  115. #$rep->updateFirst = false;
  116. $ok = $rep->Merge($table, $dtable, $pkeyarr, $ignoreflds, $set, 'UpdatedOn','CopiedFlag',array('Y','N','P','='), 'CopyDate');
  117. var_dump($ok);
  118. #$rep->connSrc->Execute("update JohnTest set name='Apple' where id=4");
  119. }
  120. $DB = ADONewConnection('odbtp');
  121. #$ok = $DB->Connect('localhost','root','','northwind');
  122. $ok = $DB->Connect('192.168.0.1','DRIVER={SQL Server};SERVER=(local);UID=sa;PWD=natsoft;DATABASE=OIR;','','');
  123. $DB->_bindInputArray = false;
  124. $DB2 = ADONewConnection('oci8');
  125. $ok2 = $DB2->Connect('192.168.0.2','tnb','natsoft','RAPTOR','');
  126. if (!$ok || !$ok2) die("Failed connection DB=$ok DB2=$ok2<br>");
  127. $tables =
  128. "
  129. JohnTest,id
  130. ";
  131. # net* are ERMS, need last updated field from LGBnet
  132. # tblRep* are tables insert or update from Juris, need last updated field also
  133. # The rest are lookup tables, can copy all from LGBnet
  134. $tablesOrig =
  135. "
  136. SysVoltSubLevel,id
  137. # Lookup table for Restoration Details screen
  138. sysefi,ID # (not identity)
  139. sysgenkva,ID #(not identity)
  140. sysrestoredby,ID #(not identity)
  141. # Sel* table added on 24 Oct
  142. SELSGManufacturer,ID
  143. SelABCCondSizeLV,ID
  144. SelABCCondSizeMV,ID
  145. SelArchingHornSize,ID
  146. SelBallastSize,ID
  147. SelBallastType,ID
  148. SelBatteryType,ID #(not identity)
  149. SelBreakerCapacity,ID
  150. SelBreakerType,ID #(not identity)
  151. SelCBreakerManuf,ID
  152. SelCTRatio,ID #(not identity)
  153. SelCableBrand,ID
  154. SelCableSize,ID
  155. SelCableSizeLV,ID # (not identity)
  156. SelCapacitorSize,ID
  157. SelCapacitorType,ID
  158. SelColourCode,ID
  159. SelCombineSealingChamberSize,ID
  160. SelConductorBrand,ID
  161. SelConductorSize4,ID
  162. SelConductorSizeLV,ID
  163. SelConductorSizeMV,ID
  164. SelContactorSize,ID
  165. SelContractor,ID
  166. SelCoverType,ID
  167. SelCraddleSize,ID
  168. SelDeadEndClampBrand,ID
  169. SelDeadEndClampSize,ID
  170. SelDevTermination,ID
  171. SelFPManuf,ID
  172. SelFPillarRating,ID
  173. SelFalseTrue,ID
  174. SelFuseManuf,ID
  175. SelFuseType,ID
  176. SelIPCBrand,ID
  177. SelIPCSize,ID
  178. SelIgnitorSize,ID
  179. SelIgnitorType,ID
  180. SelInsulatorBrand,ID
  181. SelJoint,ID
  182. SelJointBrand,ID
  183. SelJunctionBoxBrand,ID
  184. SelLVBoardBrand,ID
  185. SelLVBoardSize,ID
  186. SelLVOHManuf,ID
  187. SelLVVoltage,ID
  188. SelLightningArresterBrand,ID
  189. SelLightningShieldwireSize,ID
  190. SelLineTapSize,ID
  191. SelLocation,ID
  192. SelMVVoltage,ID
  193. SelMidSpanConnectorsSize,ID
  194. SelMidSpanJointSize,ID
  195. SelNERManuf,ID
  196. SelNERType,ID
  197. SelNLinkSize,ID
  198. SelPVCCondSizeLV,ID
  199. SelPoleBrand,ID
  200. SelPoleConcreteSize,ID
  201. SelPoleSize,ID
  202. SelPoleSpunConcreteSize,ID
  203. SelPoleSteelSize,ID
  204. SelPoleType,ID
  205. SelPoleWoodSize,ID
  206. SelPorcelainFuseSize,ID
  207. SelRatedFaultCurrentBreaker,ID
  208. SelRatedVoltageSG,ID #(not identity)
  209. SelRelayType,ID # (not identity)
  210. SelResistanceValue,ID
  211. SelSGEquipmentType,ID # (not identity)
  212. SelSGInsulationType,ID # (not identity)
  213. SelSGManufacturer,ID
  214. SelStayInsulatorSize,ID
  215. SelSuspensionClampBrand,ID
  216. SelSuspensionClampSize,ID
  217. SelTSwitchType,ID
  218. SelTowerType,ID
  219. SelTransformerCapacity,ID
  220. SelTransformerManuf,ID
  221. SelTransformerType,ID #(not identity)
  222. SelTypeOfArchingHorn,ID
  223. SelTypeOfCable,ID #(not identity)
  224. SelTypeOfConductor,ID # (not identity)
  225. SelTypeOfInsulationCB,ID # (not identity)
  226. SelTypeOfMidSpanJoint,ID
  227. SelTypeOfSTJoint,ID
  228. SelTypeSTCable,ID
  229. SelUGVoltage,ID # (not identity)
  230. SelVoltageInOut,ID
  231. SelWireSize,ID
  232. SelWireType,ID
  233. SelWonpieceBrand,ID
  234. #
  235. # Net* tables added on 24 Oct
  236. NetArchingHorn,Idx
  237. NetBatteryBank,Idx # identity, FunctLocation Pri
  238. NetBiMetal,Idx
  239. NetBoxFuse,Idx
  240. NetCable,Idx # identity, FunctLocation Pri
  241. NetCapacitorBank,Idx # identity, FunctLocation Pri
  242. NetCircuitBreaker,Idx # identity, FunctLocation Pri
  243. NetCombineSealingChamber,Idx
  244. NetCommunication,Idx
  245. NetCompInfras,Idx
  246. NetControl,Idx
  247. NetCraddle,Idx
  248. NetDeadEndClamp,Idx
  249. NetEarthing,Idx
  250. NetFaultIndicator,Idx
  251. NetFeederPillar,Idx # identity, FunctLocation Pri
  252. NetGenCable,Idx # identity , FunctLocation Not Null
  253. NetGenerator,Idx
  254. NetGrid,Idx
  255. NetHVOverhead,Idx #identity, FunctLocation Pri
  256. NetHVUnderground,Idx #identity, FunctLocation Pri
  257. NetIPC,Idx
  258. NetInductorBank,Idx
  259. NetInsulator,Idx
  260. NetJoint,Idx
  261. NetJunctionBox,Idx
  262. NetLVDB,Idx #identity, FunctLocation Pri
  263. NetLVOverhead,Idx
  264. NetLVUnderground,Idx # identity, FunctLocation Not Null
  265. NetLightningArrester,Idx
  266. NetLineTap,Idx
  267. NetMidSpanConnectors,Idx
  268. NetMidSpanJoint,Idx
  269. NetNER,Idx # identity , FunctLocation Pri
  270. NetOilPump,Idx
  271. NetOtherComponent,Idx
  272. NetPole,Idx
  273. NetRMU,Idx # identity, FunctLocation Pri
  274. NetStreetLight,Idx
  275. NetStrucSupp,Idx
  276. NetSuspensionClamp,Idx
  277. NetSwitchGear,Idx # identity, FunctLocation Pri
  278. NetTermination,Idx
  279. NetTransition,Idx
  280. NetWonpiece,Idx
  281. #
  282. # comment1
  283. SelMVFuseType,ID
  284. selFuseSize,ID
  285. netRelay,Idx # identity, FunctLocation Pri
  286. SysListVolt,ID
  287. sysVoltLevel,ID_SVL
  288. sysRestoration,ID_SRE
  289. sysRepairMethod,ID_SRM # (not identity)
  290. sysInterruptionType,ID_SIN
  291. netTransformer,Idx # identity, FunctLocation Pri
  292. #
  293. #
  294. sysComponent,ID_SC
  295. sysCodecibs #-- no idea, UpdatedOn(the only column is unique),Ermscode,Cibscode is unique but got null value
  296. sysCodeno,id
  297. sysProtection,ID_SP
  298. sysEquipment,ID_SEQ
  299. sysAddress #-- no idea, ID_SAD(might be auto gen No)
  300. sysWeather,ID_SW
  301. sysEnvironment,ID_SE
  302. sysPhase,ID_SPH
  303. sysFailureCause,ID_SFC
  304. sysFailureMode,ID_SFM
  305. SysSchOutageMode,ID_SSM
  306. SysOutageType,ID_SOT
  307. SysInstallation,ID_SI
  308. SysInstallationCat,ID_SIC
  309. SysInstallationType,ID_SIT
  310. SysFaultCategory,ID_SF #(not identity)
  311. SysResponsible,ID_SR
  312. SysProtectionOperation,ID_SPO #(not identity)
  313. netCodename,CodeNo #(not identity)
  314. netSubstation,Idx #identity, FunctLocation Pri
  315. netLvFeeder,Idx # identity, FunctLocation Pri
  316. #
  317. #
  318. tblReport,ReportNo
  319. tblRepRestoration,ID_RR
  320. tblRepResdetail,ID_RRD
  321. tblRepFailureMode,ID_RFM
  322. tblRepFailureCause,ID_RFC
  323. tblRepRepairMethod,ReportNo # (not identity)
  324. tblInterruptionType,ID_TIN
  325. tblProtType,ID_PT #--capital letter
  326. tblRepProtection,ID_RP
  327. tblRepComponent,ID_RC
  328. tblRepWeather,ID_RW
  329. tblRepEnvironment,ID_RE
  330. tblRepSubstation,ID_RSS
  331. tblInstallationType,ID_TIT
  332. tblInstallationCat,ID_TIC
  333. tblFailureCause,ID_TFC
  334. tblFailureMode,ID_TFM
  335. tblProtection,ID_TP
  336. tblComponent,ID_TC
  337. tblProtdetail,Id # (Id)--capital letter for I
  338. tblInstallation,ID_TI
  339. #
  340. ";
  341. $tables = explode("\n",$tables);
  342. $rep = new ADODB_Replicate($DB,$DB2);
  343. $rep->fieldFilter = 'FieldFilter';
  344. $rep->selFilter = 'SELFILTER';
  345. $rep->indexFilter = 'IndexFilter';
  346. if (1) {
  347. $rep->debug = 1;
  348. $DB->debug=1;
  349. $DB2->debug=1;
  350. }
  351. # $rep->SwapDBs();
  352. $cnt = sizeof($tables);
  353. foreach($tables as $k => $table) {
  354. $pkey = '';
  355. if (!ParseTable($table, $pkey)) continue;
  356. #######################
  357. $kcnt = $k+1;
  358. echo "<h1>($kcnt/$cnt) $table -- $pkey</h1>\n";
  359. flush();@ob_flush();
  360. CreateTable($rep,$table);
  361. # COPY DATA
  362. TableStats($rep, $table, $pkey);
  363. if ($table == 'JohnTest') MergeDataJohnTest($rep, $table, $pkey);
  364. else CopyData($rep, $table, $pkey);
  365. }
  366. if (!empty($TARR)) {
  367. ksort($TARR);
  368. adodb_pr($TARR);
  369. asort($TARR);
  370. adodb_pr($TARR);
  371. }
  372. echo "<hr>",date('H:i:s'),": Done</hr>";