Code Duplication    Length = 58-58 lines in 2 locations

src/Fwlib/Db/DbDiff.php 2 locations

@@ 483-540 (lines=58) @@
480
     *
481
     * @return  array
482
     */
483
    protected function generateCommitSql()
484
    {
485
        $sqlArray = [];
486
        $db = $this->db;
487
488
        foreach ($this->diff as $table => $rowArray) {
489
            foreach ((array)$rowArray as $index => $row) {
490
                $sqlConfig = [];
491
492
                switch ($row['mode']) {
493
                    case 'INSERT':
494
                        $sqlConfig['INSERT'] = $table;
495
496
                        $columnArray = $row['pk'] + $row['column'];
497
                        foreach ($columnArray as $k => $v) {
498
                            $sqlConfig['VALUES'][$k] = $v['new'];
499
                        }
500
501
                        break;
502
503
                    case 'DELETE':
504
                        $sqlConfig['DELETE'] = $table;
505
                        // Limit row count to 1 for safety
506
                        $sqlConfig['LIMIT'] = 1;
507
508
                        foreach ($row['pk'] as $k => $v) {
509
                            $sqlConfig['WHERE'][] = $k . ' = ' .
510
                                $db->quoteValue($table, $k, $v['old']);
511
                        }
512
513
                        break;
514
515
                    case 'UPDATE':
516
                        $sqlConfig['UPDATE'] = $table;
517
                        // Limit row count to 1 for safety
518
                        $sqlConfig['LIMIT'] = 1;
519
520
                        foreach ($row['column'] as $k => $v) {
521
                            $sqlConfig['SET'][$k] = $v['new'];
522
                        }
523
524
                        foreach ($row['pk'] as $k => $v) {
525
                            $sqlConfig['WHERE'][] = $k . ' = ' .
526
                                $db->quoteValue($table, $k, $v['new']);
527
                        }
528
529
                        break;
530
531
                    default:
532
                        throw new \Exception("Invalid mode {$row['mode']}");
533
                }
534
535
                $sqlArray[] = $db->generateSql($sqlConfig);
536
            }
537
        }
538
539
        return $sqlArray;
540
    }
541
542
543
    /**
@@ 548-605 (lines=58) @@
545
     *
546
     * @return  array
547
     */
548
    protected function generateRollbackSql()
549
    {
550
        $sqlArray = [];
551
        $db = $this->db;
552
553
        foreach ($this->diff as $table => $rowArray) {
554
            foreach ((array)$rowArray as $index => $row) {
555
                $sqlConfig = [];
556
557
                switch ($row['mode']) {
558
                    case 'INSERT':
559
                        $sqlConfig['DELETE'] = $table;
560
                        // Limit row count to 1 for safety
561
                        $sqlConfig['LIMIT'] = 1;
562
563
                        foreach ($row['pk'] as $k => $v) {
564
                            $sqlConfig['WHERE'][] = $k . ' = ' .
565
                                $db->quoteValue($table, $k, $v['new']);
566
                        }
567
568
                        break;
569
570
                    case 'DELETE':
571
                        $sqlConfig['INSERT'] = $table;
572
573
                        $columnArray = $row['pk'] + $row['column'];
574
                        foreach ($columnArray as $k => $v) {
575
                            $sqlConfig['VALUES'][$k] = $v['old'];
576
                        }
577
578
                        break;
579
580
                    case 'UPDATE':
581
                        $sqlConfig['UPDATE'] = $table;
582
                        // Limit row count to 1 for safety
583
                        $sqlConfig['LIMIT'] = 1;
584
585
                        foreach ($row['column'] as $k => $v) {
586
                            $sqlConfig['SET'][$k] = $v['old'];
587
                        }
588
589
                        foreach ($row['pk'] as $k => $v) {
590
                            $sqlConfig['WHERE'][] = $k . ' = ' .
591
                                $db->quoteValue($table, $k, $v['old']);
592
                        }
593
594
                        break;
595
596
                    default:
597
                        throw new \Exception("Invalid mode {$row['mode']}");
598
                }
599
600
                $sqlArray[] = $db->generateSql($sqlConfig);
601
            }
602
        }
603
604
        return $sqlArray;
605
    }
606
607
608
    /**