Skip to content

Commit 42df986

Browse files
authored
Merge pull request #19 from jr-cologne/where-clause-update
Add support for custom logical operators in where clause
2 parents 2180a8d + 6015ce9 commit 42df986

File tree

7 files changed

+80
-8
lines changed

7 files changed

+80
-8
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ if ($db->table('users')->delete([
200200
}
201201
```
202202

203+
### Custom Logical Operators in Where Clause
204+
205+
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.
206+
207+
This is how a where clause with custom logical operators could look like when retrieving data from a database:
208+
209+
```php
210+
$data = $db->table('users')->select('*', [
211+
'id' => 1,
212+
'||',
213+
'username' => 'test'
214+
])->retrieve();
215+
```
216+
203217
### Using PDO's functionality
204218

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

composer.json

Lines changed: 2 additions & 2 deletions
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.2.0",
4+
"version": "2.3.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.2.0",
38+
"source": "https://github.com/jr-cologne/db-class/tree/v2.3.0",
3939
"docs": "https://github.com/jr-cologne/db-class/blob/README.md"
4040
}
4141
}

src/DB.php

Lines changed: 6 additions & 1 deletion
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.2.0
16+
* @version v2.3.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,11 @@ public function getPDOException() : PDOException {
383383
*/
384384
protected function formatWhereData(array $where) : array {
385385
foreach ($where as $column => $value) {
386+
if (is_numeric($column)) {
387+
next($where);
388+
continue;
389+
}
390+
386391
$where_data['where_' . $column] = $value;
387392
}
388393

src/Exceptions/UnsupportedKeywordException.php

Lines changed: 1 addition & 1 deletion
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.2.0
16+
* @version v2.3.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

Lines changed: 8 additions & 2 deletions
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.2.0
16+
* @version v2.3.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,9 +183,15 @@ 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;
190+
}
191+
186192
$where_clause .= "`{$column}` = :where_{$column}";
187193

188-
if (next($where) !== false) {
194+
if ( next($where) !== false && !is_null(key($where)) && !is_numeric(key($where)) ) {
189195
$where_clause .= ' && ';
190196
}
191197
}

tests/DBTest.php

Lines changed: 48 additions & 1 deletion
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.2.0
16+
* @version v2.3.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,6 +119,15 @@ 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() {
123+
$this->assertEquals('John',
124+
$this->getDBConnection()
125+
->table('users')
126+
->select('username', [ 'id' => 1, '||', 'username' => 'John', '&&', 'password' => 'ilovecats123' ])
127+
->retrieve('first')['username']
128+
);
129+
}
130+
122131
public function test_retrieve_method_returns_data_based_on_fetch_mode() {
123132
$this->assertInternalType('object',
124133
$this->getDBConnection()
@@ -338,6 +347,26 @@ public function test_update_method_updates_data_in_database() {
338347
);
339348
}
340349

350+
public function test_update_method_updates_data_in_database_based_on_custom_where_clause() {
351+
$this->getDBConnection()
352+
->table('users')
353+
->update([
354+
'username' => 'Johnny'
355+
], [
356+
'username' => 'John',
357+
'||',
358+
'id' => 1,
359+
'password' => 'ilovecats123'
360+
]);
361+
362+
$this->assertEquals('Johnny',
363+
$this->getDBConnection()
364+
->table('users')
365+
->select('username', [ 'username' => 'Johnny' ])
366+
->retrieve('first')['username'] ?? null
367+
);
368+
}
369+
341370
public function test_update_method_without_where_clause_updates_all_data_in_database() {
342371
$this->getDBConnection()
343372
->table('users')
@@ -388,6 +417,24 @@ public function test_delete_method_deletes_data_in_database() {
388417
);
389418
}
390419

420+
public function test_delete_method_deletes_data_in_database_based_on_custom_where_clause() {
421+
$this->getDBConnection()
422+
->table('users')
423+
->delete([
424+
'id' => 1,
425+
'username' => 'John',
426+
'AND',
427+
'password' => 'ilovecats123'
428+
]);
429+
430+
$this->assertCount(1,
431+
$this->getDBConnection()
432+
->table('users')
433+
->select('*')
434+
->retrieve()
435+
);
436+
}
437+
391438
public function test_delete_method_without_where_clause_deletes_all_data_in_database() {
392439
$this->getDBConnection()
393440
->table('users')

tests/DatabaseTestCase.php

Lines changed: 1 addition & 1 deletion
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.2.0
16+
* @version v2.3.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)