Skip to content

Commit 7228765

Browse files
authored
Merge pull request #133 from skipperbent/v4-raw-bindings
[BUGFIX] Fixed raw bindings not set correctly + optimisations
2 parents 93bb6e8 + 97280c3 commit 7228765

3 files changed

Lines changed: 34 additions & 27 deletions

File tree

src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function __construct(Connection $connection)
8686
*
8787
* @return string
8888
*/
89-
protected function arrayStr(array $pieces, string $glue = ',', bool $wrapSanitizer = true): string
89+
protected function arrayStr(array $pieces, string $glue = ', ', bool $wrapSanitizer = true): string
9090
{
9191
$str = '';
9292
foreach ($pieces as $key => $piece) {
@@ -379,11 +379,7 @@ public function delete(array $statements, array $columns = null): array
379379
$columnsQuery = '';
380380

381381
if($columns !== null) {
382-
foreach($columns as $key => $column) {
383-
$columns[$key] = $this->wrapSanitizer($column);
384-
}
385-
386-
$columnsQuery = implode(', ', $columns);
382+
$columnsQuery = $this->arrayStr($columns);
387383
}
388384

389385
// WHERE
@@ -426,6 +422,7 @@ private function doInsert(array $statements, array $data, $type): array
426422
$keys[] = $key;
427423
if ($value instanceof Raw) {
428424
$values[] = (string)$value;
425+
$bindings += $value->getBindings();
429426
} else {
430427
$values[] = '?';
431428
$bindings[] = $value;
@@ -437,7 +434,7 @@ private function doInsert(array $statements, array $data, $type): array
437434
$this->wrapSanitizer($table),
438435
'(' . $this->arrayStr($keys) . ')',
439436
'VALUES',
440-
'(' . $this->arrayStr($values, ',', false) . ')',
437+
'(' . $this->arrayStr($values, ', ', false) . ')',
441438
];
442439

443440
if (isset($statements['onduplicate']) === true) {
@@ -467,21 +464,22 @@ private function doInsert(array $statements, array $data, $type): array
467464
private function getUpdateStatement(array $data): array
468465
{
469466
$bindings = [];
470-
$statement = '';
467+
$statements = [];
471468

472469
foreach ($data as $key => $value) {
473470

474-
$statement .= $this->wrapSanitizer($key) . '=';
471+
$statement = $this->wrapSanitizer($key) . ' = ';
475472

476473
if ($value instanceof Raw) {
477-
$statement .= $value . ',';
474+
$statements[] = $statement . $value;
475+
$bindings += $value->getBindings();
478476
} else {
479-
$statement .= '?,';
480-
$bindings[] = $value;
477+
$statements[] = $statement . '?';
478+
$bindings[] = $value;
481479
}
482480
}
483481

484-
$statement = trim($statement, ',');
482+
$statement = trim($this->arrayStr($statements, ', ', false));
485483

486484
return [$statement, $bindings];
487485
}

src/Pecee/Pixie/QueryBuilder/Adapters/Sqlserver.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ public function delete(array $statements, array $columns = null): array
146146
$columnsQuery = '';
147147

148148
if($columns !== null) {
149-
foreach($columns as $key => $column) {
150-
$columns[$key] = $this->wrapSanitizer($column);
151-
}
152-
153-
$columnsQuery = implode(', ', $columns);
149+
$columnsQuery = $this->arrayStr($columns);
154150
}
155151

156152
// WHERE

tests/Pecee/Pixie/QueryBuilderBehaviorTest.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testInsertIgnoreQuery()
7878
'value' => 'Sana',
7979
];
8080

81-
$this->assertEquals("INSERT IGNORE INTO `cb_my_table` (`key`,`value`) VALUES ('Name','Sana')"
81+
$this->assertEquals("INSERT IGNORE INTO `cb_my_table` (`key`, `value`) VALUES ('Name', 'Sana')"
8282
, $builder->getQuery('insertignore', $data)->getRawSql());
8383
}
8484

@@ -94,7 +94,7 @@ public function testInsertOnDuplicateKeyUpdateQuery()
9494
'counter' => 2,
9595
];
9696
$builder->from('my_table')->onDuplicateKeyUpdate($dataUpdate);
97-
$this->assertEquals("INSERT INTO `cb_my_table` (`name`,`counter`) VALUES ('Sana',1) ON DUPLICATE KEY UPDATE `name`='Sana',`counter`=2"
97+
$this->assertEquals("INSERT INTO `cb_my_table` (`name`, `counter`) VALUES ('Sana', 1) ON DUPLICATE KEY UPDATE `name` = 'Sana', `counter` = 2"
9898
, $builder->getQuery('insert', $data)->getRawSql());
9999
}
100100

@@ -106,7 +106,7 @@ public function testInsertQuery()
106106
'value' => 'Sana',
107107
];
108108

109-
$this->assertEquals("INSERT INTO `cb_my_table` (`key`,`value`) VALUES ('Name','Sana')"
109+
$this->assertEquals("INSERT INTO `cb_my_table` (`key`, `value`) VALUES ('Name', 'Sana')"
110110
, $builder->getQuery('insert', $data)->getRawSql());
111111
}
112112

@@ -163,6 +163,19 @@ public function testRawStatementsWithinCriteria()
163163
);
164164
}
165165

166+
public function testRawStatementWithinStatement() {
167+
$query = $this->builder->from('my_table')
168+
->where('simple', '=', 'criteria')
169+
->update([
170+
'alias' => $this->builder->raw('?', ['2'])
171+
]);
172+
173+
$this->assertEquals(
174+
"UPDATE `cb_my_table` SET `alias` = '2' WHERE `simple` = 'criteria'",
175+
$this->builder->getLastQuery()->getRawSql()
176+
);
177+
}
178+
166179
public function testReplaceQuery()
167180
{
168181
$builder = $this->builder->from('my_table');
@@ -171,7 +184,7 @@ public function testReplaceQuery()
171184
'value' => 'Sana',
172185
];
173186

174-
$this->assertEquals("REPLACE INTO `cb_my_table` (`key`,`value`) VALUES ('Name','Sana')"
187+
$this->assertEquals("REPLACE INTO `cb_my_table` (`key`, `value`) VALUES ('Name', 'Sana')"
175188
, $builder->getQuery('replace', $data)->getRawSql());
176189
}
177190

@@ -322,7 +335,7 @@ public function testUpdateQuery()
322335
'value' => 'Amrin',
323336
];
324337

325-
$this->assertEquals("UPDATE `cb_my_table` SET `key`='Sana',`value`='Amrin' WHERE `value` = 'Sana'"
338+
$this->assertEquals("UPDATE `cb_my_table` SET `key` = 'Sana', `value` = 'Amrin' WHERE `value` = 'Sana'"
326339
, $builder->getQuery('update', $data)->getRawSql());
327340
}
328341

@@ -370,7 +383,7 @@ public function testUpdateAdvancedQuery()
370383
->update(['foo.status' => 1]);
371384

372385
$this->assertEquals(
373-
'UPDATE `cb_foo` LEFT JOIN `cb_bar` ON `cb_foo`.`id` = `cb_bar`.`id` SET `foo`.`status`=1 WHERE `cb_bar`.`id` = 1 GROUP BY `cb_foo`.`id` ORDER BY `cb_foo`.`id` ASC LIMIT 1 OFFSET 1',
386+
'UPDATE `cb_foo` LEFT JOIN `cb_bar` ON `cb_foo`.`id` = `cb_bar`.`id` SET `foo`.`status` = 1 WHERE `cb_bar`.`id` = 1 GROUP BY `cb_foo`.`id` ORDER BY `cb_foo`.`id` ASC LIMIT 1 OFFSET 1',
374387
$this->builder->getConnection()->getLastQuery()->getRawSql());
375388
}
376389

@@ -408,7 +421,7 @@ public function testJoinUsing()
408421

409422
$query = $this->builder->table('user')->joinUsing('user_data', ['user_id', 'image_id'])->where('user_id', '=', 1);
410423

411-
$this->assertEquals('SELECT * FROM `cb_user` JOIN `cb_user_data` USING (`user_id`,`image_id`) WHERE `user_id` = 1', $query->getQuery()->getRawSql());
424+
$this->assertEquals('SELECT * FROM `cb_user` JOIN `cb_user_data` USING (`user_id`, `image_id`) WHERE `user_id` = 1', $query->getQuery()->getRawSql());
412425

413426
}
414427

@@ -419,8 +432,8 @@ public function testJoinQueryBuilderUsing()
419432
$jb->using(['user_id', 'image_id']);
420433
})->where('user_id', '=', 1);
421434

422-
$this->assertEquals('SELECT * FROM `cb_user` JOIN `cb_user_data` USING (`user_id`,`image_id`) WHERE `user_id` = 1', $query->getQuery()->getRawSql());
435+
$this->assertEquals('SELECT * FROM `cb_user` JOIN `cb_user_data` USING (`user_id`, `image_id`) WHERE `user_id` = 1', $query->getQuery()->getRawSql());
423436

424437
}
425438

426-
}
439+
}

0 commit comments

Comments
 (0)