From 2ec999e5986f08c34afde95f674e1e00002b2dff Mon Sep 17 00:00:00 2001 From: Muhammad Fikri Date: Tue, 22 Dec 2015 12:57:27 +0700 Subject: [PATCH 1/5] add manual transaction and make sure only one transaction active --- .../QueryBuilder/QueryBuilderHandler.php | 67 ++++++++++++++++--- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/src/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pixie/QueryBuilder/QueryBuilderHandler.php index 7f89796..f9dfa06 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; } /** From 18d7d4cf64a65254714fe3b2b7b57a9038bdff8b Mon Sep 17 00:00:00 2001 From: Muhammad Fikri Date: Tue, 22 Dec 2015 12:59:37 +0700 Subject: [PATCH 2/5] fix indentation --- src/Pixie/QueryBuilder/QueryBuilderHandler.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pixie/QueryBuilder/QueryBuilderHandler.php index f9dfa06..23334e0 100644 --- a/src/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -823,13 +823,13 @@ public function commit() */ public function rollback() { - if ($this->transactions == 1) { - $this->transactions = 0; - $this->pdo->rollBack(); - } - else { - --$this->transactions; - } + if ($this->transactions == 1) { + $this->transactions = 0; + $this->pdo->rollBack(); + } + else { + --$this->transactions; + } } /** From 2e996ba352759147d7abb606c25ba63294a1a9be Mon Sep 17 00:00:00 2001 From: Muhammad Fikri Date: Fri, 15 Jan 2016 00:35:25 +0700 Subject: [PATCH 3/5] ondulicate --- .../QueryBuilder/Adapters/BaseAdapter.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php index a6277df..b7ff6e0 100644 --- a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -233,10 +233,42 @@ private function getUpdateStatement($data) $bindings[] = $value; } } + + if (isset($statements['onduplicate'])) { + if (count($statements['onduplicate']) < 1) { + throw new Exception('No data given.', 4); + } + list($updateStatement, $updateBindings) = $this->getUpdateStatement($statements['onduplicate']); + $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement; + $bindings = array_merge($bindings, $updateBindings); + } $statement = trim($statement, ','); return array($statement, $bindings); } + + /** + * Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements + * + * @param array $data + * + * @return array + */ + private function getUpdateStatement($data) + { + $bindings = array(); + $statement = ''; + foreach ($data as $key => $value) { + if ($value instanceof Raw) { + $statement .= $this->wrapSanitizer($key) . '=' . $value . ','; + } else { + $statement .= $this->wrapSanitizer($key) . '=?,'; + $bindings[] = $value; + } + } + $statement = trim($statement, ','); + return array($statement, $bindings); + } /** * Build update query From 67754d513c2380e042c62074d87f9baf247c3ace Mon Sep 17 00:00:00 2001 From: Muhammad Fikri Date: Fri, 15 Jan 2016 00:56:50 +0700 Subject: [PATCH 4/5] fix duplicate function --- .../QueryBuilder/Adapters/BaseAdapter.php | 34 ------------------- 1 file changed, 34 deletions(-) diff --git a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php index b7ff6e0..a45677e 100644 --- a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -212,40 +212,6 @@ public function replace($statements, array $data) { return $this->doInsert($statements, $data, 'REPLACE'); } - - /** - * Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements - * - * @param array $data - * - * @return array - */ - private function getUpdateStatement($data) - { - $bindings = array(); - $statement = ''; - - foreach ($data as $key => $value) { - if ($value instanceof Raw) { - $statement .= $this->wrapSanitizer($key) . '=' . $value . ','; - } else { - $statement .= $this->wrapSanitizer($key) . '=?,'; - $bindings[] = $value; - } - } - - if (isset($statements['onduplicate'])) { - if (count($statements['onduplicate']) < 1) { - throw new Exception('No data given.', 4); - } - list($updateStatement, $updateBindings) = $this->getUpdateStatement($statements['onduplicate']); - $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement; - $bindings = array_merge($bindings, $updateBindings); - } - - $statement = trim($statement, ','); - return array($statement, $bindings); - } /** * Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements From 80788a24bfdb87b818e610b88da870874b0c4caa Mon Sep 17 00:00:00 2001 From: Muhammad Fikri Date: Fri, 15 Jan 2016 04:01:31 +0700 Subject: [PATCH 5/5] fast fix: space raw where --- src/Pixie/QueryBuilder/Adapters/BaseAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php index a45677e..98cb96b 100644 --- a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -404,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