From ee6f9467821722e363478bc06eac480bc482a074 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 25 Oct 2012 00:19:39 +0000 Subject: [PATCH] Fix out-of-order where/set statements --- classes/query_builder.php | 48 +++++++++++++++++++++++----------- tests/core/db_qb_test.php | 34 ++++++++++++++++++++++++ tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/classes/query_builder.php b/classes/query_builder.php index 1c13cef..01adc1a 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -115,7 +115,14 @@ class Query_Builder { * * @var array */ - private $values; + private $values = array(); + + /** + * Values to apply to where clauses in prepared statements + * + * @var array + */ + private $where_values = array(); /** * Value for limit string @@ -504,7 +511,7 @@ class Query_Builder { ); // Add to the values array - $this->values[] = $val; + $this->where_values[] = $val; return $this; } @@ -654,7 +661,7 @@ class Query_Builder { if (is_scalar($key) && is_scalar($val)) { $where[$key] = $val; - $this->values[] = $val; + $this->where_values[] = $val; } // Array or object, loop through and add to the where array elseif ( ! is_scalar($key)) @@ -662,7 +669,7 @@ class Query_Builder { foreach($key as $k => $v) { $where[$k] = $v; - $this->values[] = $v; + $this->where_values[] = $v; } } @@ -724,7 +731,7 @@ class Query_Builder { foreach($val as $v) { - $this->values[] = $v; + $this->where_values[] = $v; } $string = $key . " {$in} (".implode(',', $params).') '; @@ -1092,12 +1099,12 @@ class Query_Builder { // Do prepared statements for anything involving a "where" clause if ( ! empty($this->query_map) || ! empty($this->having_map)) { - $result = $this->prepare_execute($sql, $this->values); + $result = $this->_run($sql); } else { // Otherwise, a simple query will do. - $result = $this->query($sql); + $result = $this->query($sql); } // Reset for next query @@ -1163,7 +1170,7 @@ class Query_Builder { // Do prepared statements for anything involving a "where" clause if ( ! empty($this->query_map)) { - $result = $this->prepare_execute($sql, $this->values); + $result = $this->_run($sql); } else { @@ -1197,8 +1204,7 @@ class Query_Builder { } $sql = $this->_compile("insert", $table); - - $res = $this->prepare_execute($sql, $this->values); + $res = $this->_run($sql); $this->reset_query(); @@ -1223,8 +1229,7 @@ class Query_Builder { } $sql = $this->_compile('update', $table); - - $res = $this->prepare_execute($sql, $this->values); + $res = $this->_run($sql); $this->reset_query(); @@ -1251,8 +1256,7 @@ class Query_Builder { // Create the SQL and parameters $sql = $this->_compile("delete", $table); - - $res = $this->prepare_execute($sql, $this->values); + $res = $this->_run($sql); $this->reset_query(); @@ -1334,7 +1338,7 @@ class Query_Builder { * @param bool * @resturn string */ - protected function _get_compiled($type, $table, $reset) + protected function _get_compile($type, $table, $reset) { $sql = $this->_compile($type, $table); @@ -1388,6 +1392,20 @@ class Query_Builder { } } + // -------------------------------------------------------------------------- + + /** + * Executes the compiled query + * + * @param string $sql + * @return mixed + */ + private function _run($sql) + { + $vals = array_merge($this->values, (array) $this->where_values); + return $this->prepare_execute($sql, $vals); + } + // -------------------------------------------------------------------------- /** diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index c6db784..f0c5479 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -407,6 +407,40 @@ abstract class QBTest extends UnitTestCase { // -------------------------------------------------------------------------- + public function TestSetArrayUpdate() + { + if (empty($this->db)) return; + + $array = array( + 'id' => 4, + 'key' => 'gogle', + 'val' => 'non-word' + ); + + $query = $this->db->set($array) + ->where('id', 4) + ->update('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + // -------------------------------------------------------------------------- + + public function TestWhereSetUpdate() + { + if (empty($this->db)) return; + + $query = $this->db->where('id', 4) + ->set('id', 4) + ->set('key', 'gogle') + ->set('val', 'non-word') + ->update('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + // -------------------------------------------------------------------------- + public function TestDelete() { if (empty($this->db)) return; diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 9f26a950c6d5f24e26fe8e53bb30f239ae6fffb6..0bd4c45c02339cd7618514cfa88918e4bb5a7c33 100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