Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support parameterized raw expressions in inserts/updates/criteria values #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Pixie/QueryBuilder/Adapters/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
48 changes: 46 additions & 2 deletions tests/Pixie/QueryBuilderBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down