| @@ 109-145 (lines=37) @@ | ||
| 106 | * |
|
| 107 | * @return DbDiff |
|
| 108 | */ |
|
| 109 | public function commit() |
|
| 110 | { |
|
| 111 | if (empty($this->diff)) { |
|
| 112 | throw new \Exception('No diff data'); |
|
| 113 | } |
|
| 114 | ||
| 115 | if ($this->isCommitted()) { |
|
| 116 | throw new \Exception( |
|
| 117 | 'Committed DbDiff can\'t commit again' |
|
| 118 | ); |
|
| 119 | } |
|
| 120 | ||
| 121 | $sqlArray = $this->generateCommitSql(); |
|
| 122 | ||
| 123 | $this->db->BeginTrans(); |
|
| 124 | ||
| 125 | try { |
|
| 126 | foreach ((array)$sqlArray as $sql) { |
|
| 127 | $this->db->execute($sql); |
|
| 128 | ||
| 129 | if (0 != $this->db->getErrorCode()) { |
|
| 130 | throw new \Exception($this->db->getErrorMessage()); |
|
| 131 | } |
|
| 132 | } |
|
| 133 | ||
| 134 | $this->db->CommitTrans(); |
|
| 135 | $this->rowCount = count($sqlArray); |
|
| 136 | $this->executeStatus = 100; |
|
| 137 | ||
| 138 | } catch (\Exception $e) { |
|
| 139 | $this->db->RollbackTrans(); |
|
| 140 | ||
| 141 | throw new \Exception($e->getMessage()); |
|
| 142 | } |
|
| 143 | ||
| 144 | return $this; |
|
| 145 | } |
|
| 146 | ||
| 147 | ||
| 148 | /** |
|
| @@ 763-799 (lines=37) @@ | ||
| 760 | * |
|
| 761 | * @return DbDiff |
|
| 762 | */ |
|
| 763 | public function rollback() |
|
| 764 | { |
|
| 765 | if (empty($this->diff)) { |
|
| 766 | throw new \Exception('No diff data'); |
|
| 767 | } |
|
| 768 | ||
| 769 | if ($this->isRollbacked()) { |
|
| 770 | throw new \Exception( |
|
| 771 | 'Rollbacked DbDiff can\'t rollback again' |
|
| 772 | ); |
|
| 773 | } |
|
| 774 | ||
| 775 | $sqlArray = $this->generateRollbackSql(); |
|
| 776 | ||
| 777 | $this->db->BeginTrans(); |
|
| 778 | ||
| 779 | try { |
|
| 780 | foreach ((array)$sqlArray as $sql) { |
|
| 781 | $this->db->execute($sql); |
|
| 782 | ||
| 783 | if (0 != $this->db->getErrorCode()) { |
|
| 784 | throw new \Exception($this->db->getErrorMessage()); |
|
| 785 | } |
|
| 786 | } |
|
| 787 | ||
| 788 | $this->db->CommitTrans(); |
|
| 789 | // Rollback operate doesn't change $rowCount |
|
| 790 | $this->executeStatus = -100; |
|
| 791 | ||
| 792 | } catch (\Exception $e) { |
|
| 793 | $this->db->RollbackTrans(); |
|
| 794 | ||
| 795 | throw new \Exception($e->getMessage()); |
|
| 796 | } |
|
| 797 | ||
| 798 | return $this; |
|
| 799 | } |
|
| 800 | } |
|
| 801 | ||