Skip to content

Commit acf1f98

Browse files
feat: de-dupe where conditions and select expressions (#54)
1 parent ff9118d commit acf1f98

File tree

3 files changed

+61
-32
lines changed

3 files changed

+61
-32
lines changed

.vscode/settings.json

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
{
2-
"files.trimTrailingWhitespace": true,
3-
"intelephense.completion.insertUseDeclaration": false,
4-
"intelephense.environment.shortOpenTag": false,
5-
"intelephense.format.braces": "per",
6-
"intelephense.phpdoc.useFullyQualifiedNames": true,
7-
"php.suggest.basic": false,
8-
"prettier.enable": false,
9-
"files.exclude": {
10-
"composer.lock": true,
11-
".phpunit.result.cache": true
12-
},
13-
"search.exclude": {},
14-
"[php]": {
15-
"editor.formatOnSave": true,
16-
"editor.tabSize": 4,
17-
"editor.detectIndentation": false,
18-
"editor.insertSpaces": true,
19-
"editor.defaultFormatter": "Jota0222.multi-formatter",
20-
"multiFormatter.formatterList": [
21-
"bmewburn.vscode-intelephense-client",
22-
"junstyle.php-cs-fixer"
23-
]
24-
},
25-
"editor.codeActionsOnSave": {
26-
"source.fixAll.eslint": "explicit",
27-
"source.organizeImports": "never"
28-
}
29-
}
1+
{
2+
"files.trimTrailingWhitespace": true,
3+
"intelephense.completion.insertUseDeclaration": false,
4+
"intelephense.environment.shortOpenTag": false,
5+
"intelephense.format.braces": "per",
6+
"intelephense.phpdoc.useFullyQualifiedNames": true,
7+
"intelephense.inlayHint.parameterNames": false,
8+
"php.suggest.basic": false,
9+
"prettier.enable": false,
10+
"files.exclude": {
11+
"composer.lock": true,
12+
".phpunit.result.cache": true
13+
},
14+
"search.exclude": {},
15+
"[php]": {
16+
"editor.formatOnSave": true,
17+
"editor.tabSize": 4,
18+
"editor.detectIndentation": false,
19+
"editor.insertSpaces": true,
20+
"editor.defaultFormatter": "Jota0222.multi-formatter",
21+
"multiFormatter.formatterList": [
22+
"bmewburn.vscode-intelephense-client",
23+
"junstyle.php-cs-fixer"
24+
]
25+
},
26+
"editor.codeActionsOnSave": {
27+
"source.fixAll.eslint": "explicit",
28+
"source.organizeImports": "never"
29+
}
30+
}

src/Granada/ORM.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,9 @@ protected function _add_result_column($expr, $alias = null)
961961
$this->_result_columns = [$expr];
962962
$this->_using_default_result_columns = false;
963963
} else {
964-
$this->_result_columns[] = $expr;
964+
if (!in_array($expr, $this->_result_columns)) {
965+
$this->_result_columns[] = $expr;
966+
}
965967
}
966968

967969
return $this;
@@ -1245,10 +1247,15 @@ protected function _add_condition($type, $fragment, $values = [])
12451247
if (!is_array($values)) {
12461248
$values = [$values];
12471249
}
1248-
array_push($this->$conditions_class_property_name, [
1250+
$filter = [
12491251
self::CONDITION_FRAGMENT => $fragment,
12501252
self::CONDITION_VALUES => $values,
1251-
]);
1253+
];
1254+
if (in_array($filter, $this->$conditions_class_property_name)) {
1255+
// Condition already exists, de-dupe
1256+
return $this;
1257+
}
1258+
array_push($this->$conditions_class_property_name, $filter);
12521259

12531260
return $this;
12541261
}

tests/orm/ORMTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,25 @@ public function testSaveInsideLoop()
184184
$this->assertEquals($expected, ORM::get_last_query());
185185
}
186186
}
187+
188+
public function testDuplicateFilters()
189+
{
190+
$cars_query = ORM::for_table('car')
191+
->where('name', 'ABC')
192+
->where('name', 'ABC')
193+
->get_select_query();
194+
195+
$this->assertSame("SELECT * FROM `car` WHERE `name` = 'ABC'", $cars_query);
196+
}
197+
198+
public function testDuplicateSelect()
199+
{
200+
$cars_query = ORM::for_table('car')
201+
->select('id')
202+
->select('name')
203+
->select('id')
204+
->get_select_query();
205+
206+
$this->assertSame('SELECT `id`, `name` FROM `car`', $cars_query);
207+
}
187208
}

0 commit comments

Comments
 (0)