diff --git a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php index a6277df..98cb96b 100644 --- a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -212,7 +212,7 @@ public function replace($statements, array $data) { return $this->doInsert($statements, $data, 'REPLACE'); } - + /** * Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements * @@ -224,7 +224,6 @@ private function getUpdateStatement($data) { $bindings = array(); $statement = ''; - foreach ($data as $key => $value) { if ($value instanceof Raw) { $statement .= $this->wrapSanitizer($key) . '=' . $value . ','; @@ -233,7 +232,6 @@ private function getUpdateStatement($data) $bindings[] = $value; } } - $statement = trim($statement, ','); return array($statement, $bindings); } @@ -406,7 +404,7 @@ protected function buildCriteria($statements, $bindValues = true) break; } } elseif ($value instanceof Raw) { - $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value"; + $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value "; } else { // Usual where like criteria diff --git a/src/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pixie/QueryBuilder/QueryBuilderHandler.php index 7f89796..23334e0 100644 --- a/src/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -48,6 +48,11 @@ class QueryBuilderHandler * @var array */ protected $fetchParameters = array(\PDO::FETCH_OBJ); + + /** + * @var integer + */ + protected $transactions = 0; /** * @param null|\Pixie\Connection $connection @@ -788,20 +793,66 @@ public function join($table, $key, $operator = null, $value = null, $type = 'inn return $this; } - - public function transaction(\Closure $callback) + + /** + * @return void + */ + public function beginTransaction() { - try { + ++$this->transactions; + + if ($this->transactions === 1) { $this->pdo->beginTransaction(); - - $callback($this); + } + } + + /** + * @return void + */ + public function commit() + { + if ($this->transactions === 1) { $this->pdo->commit(); + } + + --$this->transactions; + } + + /** + * @return void + */ + public function rollback() + { + if ($this->transactions == 1) { + $this->transactions = 0; + $this->pdo->rollBack(); + } + else { + --$this->transactions; + } + } + + /** + * @param \Closure $callback + * + * @return mixed + * + * @throws \Exception + */ + public function transaction(\Closure $callback) + { + try { + $this->beginTransaction(); - return true; + $result = $callback($this); + $this->commit(); } catch (\Exception $e) { - $this->pdo->rollBack(); - return false; + $this->rollback(); + + throw $e; } + + return $result; } /**