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 9f26a95..0bd4c45 100644 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ