diff --git a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php index 5b1e0a4..91462ec 100644 --- a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -141,6 +141,7 @@ private function doInsert($statements, array $data, $type) $keys[] = $key; if ($value instanceof Raw) { $values[] = (string) $value; + $bindings = array_merge($bindings, $value->getBindings()); } else { $values[] = '?'; $bindings[] = $value; @@ -226,6 +227,7 @@ private function getUpdateStatement($data) foreach ($data as $key => $value) { if ($value instanceof Raw) { $statement .= $this->wrapSanitizer($key) . '=' . $value . ','; + $bindings = array_merge($bindings, $value->getBindings()); } else { $statement .= $this->wrapSanitizer($key) . '=?,'; $bindings[] = $value; @@ -408,6 +410,9 @@ protected function buildCriteria($statements, $bindValues = true) } } elseif ($value instanceof Raw) { $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value "; + if ($bindValues) { + $bindings = array_merge($bindings, $value->getBindings()); + } } else { // Usual where like criteria diff --git a/tests/Pixie/QueryBuilderBehaviorTest.php b/tests/Pixie/QueryBuilderBehaviorTest.php index e1c3a88..9b82958 100644 --- a/tests/Pixie/QueryBuilderBehaviorTest.php +++ b/tests/Pixie/QueryBuilderBehaviorTest.php @@ -73,10 +73,11 @@ public function testRawStatementsWithinCriteria() ->where('simple', 'criteria') ->where($this->builder->raw('RAW')) ->where($this->builder->raw('PARAMETERIZED_ONE(?)', 'foo')) - ->where($this->builder->raw('PARAMETERIZED_SEVERAL(?, ?, ?)', array(1, '2', 'foo'))); + ->where($this->builder->raw('PARAMETERIZED_SEVERAL(?, ?, ?)', array(1, '2', 'foo'))) + ->where('raw_parameterized_value', '=', $this->builder->raw('FUNC(?)', 'parm')); $this->assertEquals( - "SELECT * FROM `cb_my_table` WHERE `simple` = 'criteria' AND RAW AND PARAMETERIZED_ONE('foo') AND PARAMETERIZED_SEVERAL(1, '2', 'foo')", + "SELECT * FROM `cb_my_table` WHERE `simple` = 'criteria' AND RAW AND PARAMETERIZED_ONE('foo') AND PARAMETERIZED_SEVERAL(1, '2', 'foo') AND `raw_parameterized_value` = FUNC('parm')", $query->getQuery()->getRawSql() ); } @@ -191,6 +192,16 @@ public function testInsertQuery() , $builder->getQuery('insert', $data)->getRawSql()); } + public function testInsertQueryWithRawParams() + { + $builder = $this->builder->from('my_table'); + $data = array('key' => 'Name', + 'value' => $this->builder->raw('UPPER(?)', 'Sana'),); + + $this->assertEquals("INSERT INTO `cb_my_table` (`key`,`value`) VALUES ('Name',UPPER('Sana'))" + , $builder->getQuery('insert', $data)->getRawSql()); + } + public function testInsertIgnoreQuery() { $builder = $this->builder->from('my_table'); @@ -201,6 +212,16 @@ public function testInsertIgnoreQuery() , $builder->getQuery('insertignore', $data)->getRawSql()); } + public function testInsertIgnoreQueryWithRawParams() + { + $builder = $this->builder->from('my_table'); + $data = array('key' => 'Name', + 'value' => $this->builder->raw('UPPER(?)', 'Sana'),); + + $this->assertEquals("INSERT IGNORE INTO `cb_my_table` (`key`,`value`) VALUES ('Name',UPPER('Sana'))" + , $builder->getQuery('insertignore', $data)->getRawSql()); + } + public function testReplaceQuery() { $builder = $this->builder->from('my_table'); @@ -211,6 +232,16 @@ public function testReplaceQuery() , $builder->getQuery('replace', $data)->getRawSql()); } + public function testReplaceQueryWithRawParams() + { + $builder = $this->builder->from('my_table'); + $data = array('key' => 'Name', + 'value' => $this->builder->raw('UPPER(?)', 'Sana'),); + + $this->assertEquals("REPLACE INTO `cb_my_table` (`key`,`value`) VALUES ('Name',UPPER('Sana'))" + , $builder->getQuery('replace', $data)->getRawSql()); + } + public function testInsertOnDuplicateKeyUpdateQuery() { $builder = $this->builder; @@ -240,6 +271,19 @@ public function testUpdateQuery() , $builder->getQuery('update', $data)->getRawSql()); } + public function testUpdateQueryWithRawParams() + { + $builder = $this->builder->table('my_table')->where('value', 'Sana'); + + $data = array( + 'key' => 'Sana', + 'value' => $this->builder->raw('UPPER(?)', 'Amrin'), + ); + + $this->assertEquals("UPDATE `cb_my_table` SET `key`='Sana',`value`=UPPER('Amrin') WHERE `value` = 'Sana'" + , $builder->getQuery('update', $data)->getRawSql()); + } + public function testDeleteQuery() { $this->builder = new QueryBuilder\QueryBuilderHandler($this->mockConnection);