Skip to content

Commit e292fef

Browse files
committed
- It's not possible to pass a key-value-array to where- and having-conditions
1 parent 3ffd7e5 commit e292fef

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

src/Builder/Internal/ConditionBuilder.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,31 @@ public static function build(Database $db, $query, array $conditions, $token) {
1616
return $query;
1717
}
1818
$query .= "{$token}\n";
19-
$arr = array();
19+
$arr = [];
2020
foreach($conditions as $condition) {
2121
list($expression, $arguments) = $condition;
22-
$expr = $db->quoteExpression($expression, $arguments);
23-
$arr[] = "\t({$expr})";
22+
if(is_array($expression)) {
23+
foreach($expression as $key => $value) {
24+
$arr = self::buildCondition($arr, "`{$key}`=?", [$value], $db);
25+
}
26+
} else {
27+
$arr = self::buildCondition($arr, $expression, $arguments, $db);
28+
}
2429
}
2530
$query .= join("\n\tAND\n", $arr);
26-
return $query."\n";
31+
return "{$query}\n";
32+
}
33+
34+
/**
35+
* @param array $conditions
36+
* @param mixed $expression
37+
* @param mixed $arguments
38+
* @param Database $db
39+
* @return array
40+
*/
41+
private static function buildCondition(array $conditions, $expression, $arguments, Database $db) {
42+
$expr = $db->quoteExpression($expression, $arguments);
43+
$conditions[] = "\t({$expr})";
44+
return $conditions;
2745
}
2846
}

tests/Builder/DeleteTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function testWhere() {
3838
$this->assertEquals("DELETE FROM\n\ttravis_test.test1\nWHERE\n\t(field1='1')\n\tAND\n\t(field2 != field1)\n", $sql);
3939
}
4040

41+
public function testWhereViaArray() {
42+
$sql = TestDelete::create()
43+
->from('travis#test1')
44+
->where(['field1' => 1, 'field2' => 'aaa'])
45+
->asString();
46+
$this->assertEquals("DELETE FROM\n\ttravis_test.test1\nWHERE\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $sql);
47+
}
48+
4149
public function testDBExpr() {
4250
$sql = TestDelete::create()
4351
->from('travis#test1')

tests/Builder/SelectTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public function testWhere() {
6363
->where('a < ?', 1000)
6464
->asString();
6565
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(a < '1000')\n", $str);
66+
67+
$str = TestSelect::create()
68+
->field('a')
69+
->from('t', 'test')
70+
->where(['field1' => 1, 'field2' => 'aaa'])
71+
->asString();
72+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $str);
6673
}
6774

6875
public function testHaving() {
@@ -72,6 +79,13 @@ public function testHaving() {
7279
->having('a+1<2')
7380
->asString();
7481
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nHAVING\n\t(a+1<2)\n", $str);
82+
83+
$str = TestSelect::create()
84+
->field('a')
85+
->from('t', 'test')
86+
->having(['field1' => 1, 'field2' => 'aaa'])
87+
->asString();
88+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nHAVING\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $str);
7589
}
7690

7791
public function testDBExpr() {

tests/Builder/UpdateTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public function testWhere() {
9292
->where('field1=?', 2)
9393
->asString();
9494
$this->assertEquals("UPDATE\n\ttest1\nSET\n\t`field1`='1'\nWHERE\n\t(field1='2')\n", $sql);
95+
96+
$sql = TestUpdate::create()
97+
->table('test1')
98+
->set('field1', 1)
99+
->where(['field1' => 1, 'field2' => 'aaa'])
100+
->asString();
101+
$this->assertEquals("UPDATE\n\ttest1\nSET\n\t`field1`='1'\nWHERE\n\t(`field1`='1')\n\tAND\n\t(`field2`='aaa')\n", $sql);
95102
}
96103

97104
public function testOrder() {

0 commit comments

Comments
 (0)