Skip to content

Commit 6578f51

Browse files
authored
Merge pull request #20 from jr-cologne/where-clause-update
Add support for custom comparison operators in where clause
2 parents 42df986 + 1eae47f commit 6578f51

File tree

7 files changed

+130
-19
lines changed

7 files changed

+130
-19
lines changed

README.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ if ($db->table('users')->delete([
200200
}
201201
```
202202

203-
### Custom Logical Operators in Where Clause
203+
### Custom Where Clauses
204+
205+
#### Custom Logical Operators in Where Clause
204206

205207
Since the release of [version 2.3](https://github.com/jr-cologne/db-class/releases/tag/v2.3.0), a where clause can also have custom logical operators.
206208

@@ -214,6 +216,28 @@ $data = $db->table('users')->select('*', [
214216
])->retrieve();
215217
```
216218

219+
#### Custom Comparison Operators in Where Clause
220+
221+
Since the release of [version 2.4](https://github.com/jr-cologne/db-class/releases/tag/v2.4.0), a where clause can also have custom comparison operators.
222+
223+
This is how a where clause with custom comparison operators could look like when retrieving data from a database:
224+
225+
```php
226+
$data = $db->table('users')->select('*', [
227+
[
228+
'id',
229+
'>=',
230+
1
231+
],
232+
'username' => 'test',
233+
[
234+
'password',
235+
'!=',
236+
'test123'
237+
]
238+
])->retrieve();
239+
```
240+
217241
### Using PDO's functionality
218242

219243
Since the database class is extending PDO, you can use the whole functionality of PDO with this class as well.

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jr-cologne/db-class",
33
"type": "library",
4-
"version": "2.3.0",
4+
"version": "2.4.0",
55
"description": "A simple database class with PHP, PDO and a query builder.",
66
"keywords": [
77
"database",
@@ -35,7 +35,7 @@
3535
],
3636
"support": {
3737
"issues": "https://github.com/jr-cologne/db-class/issues",
38-
"source": "https://github.com/jr-cologne/db-class/tree/v2.3.0",
38+
"source": "https://github.com/jr-cologne/db-class/tree/v2.4.0",
3939
"docs": "https://github.com/jr-cologne/db-class/blob/README.md"
4040
}
4141
}

src/DB.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2018 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.3.0
16+
* @version v2.4.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*
@@ -383,6 +383,13 @@ public function getPDOException() : PDOException {
383383
*/
384384
protected function formatWhereData(array $where) : array {
385385
foreach ($where as $column => $value) {
386+
if (is_numeric($column) && is_array($value)) {
387+
$where_data['where_' . $value[0]] = $value[2];
388+
389+
next($where);
390+
continue;
391+
}
392+
386393
if (is_numeric($column)) {
387394
next($where);
388395
continue;

src/Exceptions/UnsupportedKeywordException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2018 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.3.0
16+
* @version v2.4.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*

src/QueryBuilder.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2018 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.3.0
16+
* @version v2.4.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*
@@ -183,16 +183,23 @@ protected function formatWhere(array $where) : string {
183183
$where_clause = '';
184184

185185
foreach ($where as $column => $value) {
186-
if (is_numeric($column)) {
187-
$where_clause .= " {$value} ";
188-
next($where);
189-
continue;
186+
$logical_operator = null;
187+
188+
if (is_numeric($column) && is_array($value)) {
189+
$where_clause .= "`{$value[0]}` {$value[1]} :where_{$value[0]}";
190+
} else if (is_numeric($column) && is_string($value)) {
191+
$logical_operator = $value;
192+
} else {
193+
$where_clause .= "`{$column}` = :where_{$column}";
190194
}
191195

192-
$where_clause .= "`{$column}` = :where_{$column}";
196+
$next_value = next($where);
197+
$next_key = key($where);
193198

194-
if ( next($where) !== false && !is_null(key($where)) && !is_numeric(key($where)) ) {
195-
$where_clause .= ' && ';
199+
if ( $next_value !== false && !is_null($next_key) && (!is_numeric($next_key) || !is_string($next_value)) ) {
200+
$logical_operator = $logical_operator ?? '&&';
201+
202+
$where_clause .= " {$logical_operator} ";
196203
}
197204
}
198205

tests/DBTest.php

+78-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2018 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.3.0
16+
* @version v2.4.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*
@@ -119,11 +119,34 @@ public function test_retrieve_method_returns_data_based_on_where_clause() {
119119
);
120120
}
121121

122-
public function test_retrieve_method_returns_data_based_on_where_clause_with_custom_operator() {
122+
public function test_retrieve_method_returns_data_based_on_where_clause_with_custom_logical_operator() {
123123
$this->assertEquals('John',
124124
$this->getDBConnection()
125125
->table('users')
126-
->select('username', [ 'id' => 1, '||', 'username' => 'John', '&&', 'password' => 'ilovecats123' ])
126+
->select('username', [
127+
'id' => 1,
128+
'||',
129+
'username' => 'John',
130+
'&&',
131+
'password' => 'ilovecats123'
132+
])
133+
->retrieve('first')['username']
134+
);
135+
}
136+
137+
public function test_retrieve_method_returns_data_based_on_where_clause_with_custom_comparison_operator() {
138+
$this->assertEquals('John',
139+
$this->getDBConnection()
140+
->table('users')
141+
->select('username', [
142+
[
143+
'id',
144+
'!=',
145+
2
146+
],
147+
'username' => 'John',
148+
'password' => 'ilovecats123'
149+
])
127150
->retrieve('first')['username']
128151
);
129152
}
@@ -347,7 +370,7 @@ public function test_update_method_updates_data_in_database() {
347370
);
348371
}
349372

350-
public function test_update_method_updates_data_in_database_based_on_custom_where_clause() {
373+
public function test_update_method_updates_data_in_database_based_on_where_clause_with_custom_logical_operator() {
351374
$this->getDBConnection()
352375
->table('users')
353376
->update([
@@ -367,6 +390,30 @@ public function test_update_method_updates_data_in_database_based_on_custom_wher
367390
);
368391
}
369392

393+
public function test_update_method_updates_data_in_database_based_on_where_clause_with_custom_comparison_operator() {
394+
$this->getDBConnection()
395+
->table('users')
396+
->update([
397+
'username' => 'Johnny'
398+
], [
399+
'username' => 'John',
400+
'||',
401+
[
402+
'id',
403+
'=',
404+
1
405+
],
406+
'password' => 'ilovecats123'
407+
]);
408+
409+
$this->assertEquals('Johnny',
410+
$this->getDBConnection()
411+
->table('users')
412+
->select('username', [ 'username' => 'Johnny' ])
413+
->retrieve('first')['username'] ?? null
414+
);
415+
}
416+
370417
public function test_update_method_without_where_clause_updates_all_data_in_database() {
371418
$this->getDBConnection()
372419
->table('users')
@@ -417,7 +464,7 @@ public function test_delete_method_deletes_data_in_database() {
417464
);
418465
}
419466

420-
public function test_delete_method_deletes_data_in_database_based_on_custom_where_clause() {
467+
public function test_delete_method_deletes_data_in_database_based_on_where_clause_with_custom_logical_operator() {
421468
$this->getDBConnection()
422469
->table('users')
423470
->delete([
@@ -435,6 +482,32 @@ public function test_delete_method_deletes_data_in_database_based_on_custom_wher
435482
);
436483
}
437484

485+
public function test_delete_method_deletes_data_in_database_based_on_where_clause_with_custom_comparison_operator() {
486+
$this->getDBConnection()
487+
->table('users')
488+
->delete([
489+
[
490+
'id',
491+
'>=',
492+
1
493+
],
494+
'username' => 'John',
495+
'AND',
496+
[
497+
'password',
498+
'!=',
499+
'test123'
500+
]
501+
]);
502+
503+
$this->assertCount(1,
504+
$this->getDBConnection()
505+
->table('users')
506+
->select('*')
507+
->retrieve()
508+
);
509+
}
510+
438511
public function test_delete_method_without_where_clause_deletes_all_data_in_database() {
439512
$this->getDBConnection()
440513
->table('users')

tests/DatabaseTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2018 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.3.0
16+
* @version v2.4.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*

0 commit comments

Comments
 (0)