@@ 290-376 (lines=87) @@ | ||
287 | * @return int Rows modified, < 0 when error. |
|
288 | * @see DbDiff() |
|
289 | */ |
|
290 | public function DbDiffCommit (array &$ar_diff, Adodb $db = NULL) { |
|
291 | // Condition check |
|
292 | if (empty($ar_diff) || empty($ar_diff['diff'])) |
|
293 | // No diff data |
|
294 | return -2; |
|
295 | if (0 > $ar_diff['code']) |
|
296 | // Previous op not successful |
|
297 | return -3; |
|
298 | if (100 == $ar_diff['flag']) |
|
299 | // Already committed |
|
300 | return -4; |
|
301 | ||
302 | if (is_null($db)) |
|
303 | $db = $this->oDb; |
|
304 | ||
305 | // Generate sql |
|
306 | $ar_sql_all = array(); |
|
307 | foreach ($ar_diff['diff'] as $tbl => $ar_rows) { |
|
308 | if (empty($ar_rows)) |
|
309 | continue; |
|
310 | foreach ($ar_rows as $i_row => $row) { |
|
311 | $ar_sql = array(); |
|
312 | switch ($row['mode']) { |
|
313 | case 'INSERT': |
|
314 | $ar_sql['INSERT'] = $tbl; |
|
315 | $ar_col = $row['pk'] + $row['col']; // Sure not empty |
|
316 | foreach ($ar_col as $k => $v) |
|
317 | $ar_sql['VALUES'][$k] = $v['new']; |
|
318 | break; |
|
319 | case 'DELETE': |
|
320 | $ar_sql['DELETE'] = $tbl; |
|
321 | foreach ($row['pk'] as $k => $v) |
|
322 | $ar_sql['WHERE'][] = $k . ' = ' |
|
323 | . $db->QuoteValue($tbl, $k, $v['old']); |
|
324 | // Limit rowcount to 1 for safety |
|
325 | $ar_sql['LIMIT'] = 1; |
|
326 | break; |
|
327 | case 'UPDATE': |
|
328 | $ar_sql['UPDATE'] = $tbl; |
|
329 | foreach ($row['col'] as $k => $v) |
|
330 | $ar_sql['SET'][$k] = $v['new']; |
|
331 | foreach ($row['pk'] as $k => $v) |
|
332 | $ar_sql['WHERE'][] = $k . ' = ' |
|
333 | . $db->QuoteValue($tbl, $k, $v['new']); |
|
334 | // Limit rowcount to 1 for safety |
|
335 | $ar_sql['LIMIT'] = 1; |
|
336 | break; |
|
337 | } |
|
338 | ||
339 | if (!empty($ar_sql)) { |
|
340 | $ar_sql_all[] = $db->GenSql($ar_sql); |
|
341 | } |
|
342 | } |
|
343 | } |
|
344 | ||
345 | // Execute sql |
|
346 | $i_cnt = 0; |
|
347 | if (!empty($ar_sql_all)) { |
|
348 | $b_error = false; |
|
349 | $db->BeginTrans(); |
|
350 | while (!$b_error && !empty($ar_sql_all)) { |
|
351 | $db->Execute(array_shift($ar_sql_all)); |
|
352 | if (0 != $db->ErrorNo()) { |
|
353 | $b_error = true; |
|
354 | $this->Log('DbDiffCommit error ' . $db->ErrorNo() |
|
355 | . ' : ' . $db->ErrorMsg()); |
|
356 | } |
|
357 | else |
|
358 | $i_cnt += $db->Affected_Rows(); |
|
359 | } |
|
360 | ||
361 | if ($b_error) { |
|
362 | $ar_diff['code'] = abs($db->ErrorNo()) * -1; |
|
363 | $ar_diff['msg'] = $db->ErrorMsg(); |
|
364 | $db->RollbackTrans(); |
|
365 | return -1; |
|
366 | } |
|
367 | else { |
|
368 | $db->CommitTrans(); |
|
369 | // Modify diff info |
|
370 | $ar_diff['code'] = $i_cnt; |
|
371 | $ar_diff['flag'] = 100; |
|
372 | return $i_cnt; |
|
373 | } |
|
374 | } |
|
375 | return $i_cnt; |
|
376 | } // end of func DbDiffCommit |
|
377 | ||
378 | ||
379 | /** |
|
@@ 410-496 (lines=87) @@ | ||
407 | * @return int Rows modified, < 0 when error. |
|
408 | * @see DbDiff() |
|
409 | */ |
|
410 | public function DbDiffRollback (array &$ar_diff, Adodb $db = NULL) { |
|
411 | // Condition check |
|
412 | if (empty($ar_diff) || empty($ar_diff['diff'])) |
|
413 | // No diff data |
|
414 | return -2; |
|
415 | if (0 > $ar_diff['code']) |
|
416 | // Previous op not successful |
|
417 | return -3; |
|
418 | if (100 != $ar_diff['flag']) |
|
419 | // Not committed |
|
420 | return -4; |
|
421 | ||
422 | if (is_null($db)) |
|
423 | $db = $this->oDb; |
|
424 | ||
425 | // Generate sql |
|
426 | $ar_sql_all = array(); |
|
427 | foreach ($ar_diff['diff'] as $tbl => $ar_rows) { |
|
428 | if (empty($ar_rows)) |
|
429 | continue; |
|
430 | foreach ($ar_rows as $i_row => $row) { |
|
431 | $ar_sql = array(); |
|
432 | switch ($row['mode']) { |
|
433 | case 'INSERT': |
|
434 | $ar_sql['DELETE'] = $tbl; |
|
435 | foreach ($row['pk'] as $k => $v) |
|
436 | $ar_sql['WHERE'][] = $k . ' = ' |
|
437 | . $db->QuoteValue($tbl, $k, $v['new']); |
|
438 | // Limit rowcount to 1 for safety |
|
439 | $ar_sql['LIMIT'] = 1; |
|
440 | break; |
|
441 | case 'DELETE': |
|
442 | $ar_sql['INSERT'] = $tbl; |
|
443 | $ar_col = $row['pk'] + $row['col']; // Sure not empty |
|
444 | foreach ($ar_col as $k => $v) |
|
445 | $ar_sql['VALUES'][$k] = $v['old']; |
|
446 | break; |
|
447 | case 'UPDATE': |
|
448 | $ar_sql['UPDATE'] = $tbl; |
|
449 | foreach ($row['col'] as $k => $v) |
|
450 | $ar_sql['SET'][$k] = $v['old']; |
|
451 | foreach ($row['pk'] as $k => $v) |
|
452 | $ar_sql['WHERE'][] = $k . ' = ' |
|
453 | . $db->QuoteValue($tbl, $k, $v['old']); |
|
454 | // Limit rowcount to 1 for safety |
|
455 | $ar_sql['LIMIT'] = 1; |
|
456 | break; |
|
457 | } |
|
458 | ||
459 | if (!empty($ar_sql)) { |
|
460 | $ar_sql_all[] = $db->GenSql($ar_sql); |
|
461 | } |
|
462 | } |
|
463 | } |
|
464 | ||
465 | // Execute sql |
|
466 | $i_cnt = 0; |
|
467 | if (!empty($ar_sql_all)) { |
|
468 | $b_error = false; |
|
469 | $db->BeginTrans(); |
|
470 | while (!$b_error && !empty($ar_sql_all)) { |
|
471 | $db->Execute(array_shift($ar_sql_all)); |
|
472 | if (0 != $db->ErrorNo()) { |
|
473 | $b_error = true; |
|
474 | $this->Log('DbDiffRollback error ' . $db->ErrorNo() |
|
475 | . ' : ' . $db->ErrorMsg()); |
|
476 | } |
|
477 | else |
|
478 | $i_cnt += $db->Affected_Rows(); |
|
479 | } |
|
480 | ||
481 | if ($b_error) { |
|
482 | $ar_diff['code'] = abs($db->ErrorNo()) * -1; |
|
483 | $ar_diff['msg'] = $db->ErrorMsg(); |
|
484 | $db->RollbackTrans(); |
|
485 | return -1; |
|
486 | } |
|
487 | else { |
|
488 | $db->CommitTrans(); |
|
489 | // Modify diff info |
|
490 | $ar_diff['code'] = $i_cnt; |
|
491 | $ar_diff['flag'] = -100; |
|
492 | return $i_cnt; |
|
493 | } |
|
494 | } |
|
495 | return $i_cnt; |
|
496 | } // end of func DbDiffRollback |
|
497 | ||
498 | ||
499 | /** |