Skip to content

Commit af5b285

Browse files
committed
Fieldnames (keys) of Key-Value-style where and having now can contain dots and will handles correctly in most cases. In case the key already contains a backtick or a paranthesis the key will not be untouched.
1 parent 0c776b6 commit af5b285

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Builder/Internal/ConditionBuilder.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Kir\MySQL\Builder\Internal;
33

4+
use Kir\MySQL\Builder\Expr\OptionalExpression;
45
use Kir\MySQL\Database;
56
use Kir\MySQL\Builder;
67

@@ -21,10 +22,11 @@ public static function build(Database $db, string $query, array $conditions, str
2122
foreach($conditions as [$expression, $arguments]) {
2223
if(is_array($expression)) {
2324
foreach($expression as $key => $value) {
25+
$key = self::formatKey($key);
2426
if($value === null) {
25-
$arr = self::buildCondition($arr, "ISNULL(`{$key}`)", [$value], $db);
27+
$arr = self::buildCondition($arr, "ISNULL({$key})", [$value], $db);
2628
} else {
27-
$arr = self::buildCondition($arr, "`{$key}`=?", [$value], $db);
29+
$arr = self::buildCondition($arr, "{$key}=?", [$value], $db);
2830
}
2931
}
3032
} else {
@@ -47,4 +49,20 @@ private static function buildCondition(array $conditions, string $expression, ar
4749
$conditions[] = "\t({$expr})";
4850
return $conditions;
4951
}
52+
53+
/**
54+
* @param string $key
55+
* @return string
56+
*/
57+
private static function formatKey(string $key): string {
58+
if(strpos($key, '`') !== false || strpos($key, '(') !== false) {
59+
return $key;
60+
}
61+
$keyParts = explode('.', $key);
62+
$fn = static function (string $part) {
63+
return "`{$part}`";
64+
};
65+
$enclosedKeyParts = array_map($fn, $keyParts);
66+
return implode('.', $enclosedKeyParts);
67+
}
5068
}

tests/Builder/SelectTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public function testWhereAsObject(): void {
117117
$str = $this->select()
118118
->field('a')
119119
->from('t', 'test')
120-
->where((object) ['field1' => 1, 'field2' => 'aaa', 'field3' => null])
120+
->where((object) ['field1' => 1, 't.field2' => 'aaa', '`t`.`field3`' => null])
121121
->asString();
122-
self::assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`=1)\n\tAND\n\t(`field2`='aaa')\n\tAND\n\t(ISNULL(`field3`))\n", $str);
122+
self::assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`=1)\n\tAND\n\t(`t`.`field2`='aaa')\n\tAND\n\t(ISNULL(`t`.`field3`))\n", $str);
123123
}
124124

125125
public function testWhereAsEmptyObject(): void {
@@ -135,9 +135,9 @@ public function testWhereAsArray(): void {
135135
$str = $this->select()
136136
->field('a')
137137
->from('t', 'test')
138-
->where(['field1' => 1, 'field2' => 'aaa', 'field3' => null])
138+
->where(['field1' => 1, 't.field2' => 'aaa', '`t`.`field3`' => null])
139139
->asString();
140-
self::assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`=1)\n\tAND\n\t(`field2`='aaa')\n\tAND\n\t(ISNULL(`field3`))\n", $str);
140+
self::assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(`field1`=1)\n\tAND\n\t(`t`.`field2`='aaa')\n\tAND\n\t(ISNULL(`t`.`field3`))\n", $str);
141141
}
142142

143143
public function testWhereAsEmptyArray(): void {

0 commit comments

Comments
 (0)