This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the LIKE operator for regex expression search. Updated readme.
- Loading branch information
1 parent
c93d878
commit 15939f2
Showing
4 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,9 @@ $database = new \Filebase\Database([ | |
'dir' => 'path/to/database/dir' | ||
]); | ||
|
||
// in this example, you would replace user_name with the actual user name. | ||
// It would technically be stored as user_name.json | ||
$item = $database->get('user_name'); | ||
// in this example, you would search an extra user name | ||
// It would technically be stored as user_name.json in the directories | ||
$item = $database->get('kingslayer'); | ||
|
||
// display property values | ||
echo $item->first_name; | ||
|
@@ -46,10 +46,17 @@ echo $item->email; | |
|
||
// change existing or add new properties | ||
$item->email = '[email protected]'; | ||
$item->tags = ['php','developer','html5']; | ||
|
||
// need to save? thats easy! | ||
$item->save(); | ||
|
||
// Need to find all the users that have a tag for "php" ? | ||
$users = $db->query()->where('tag','IN','php')->results(); | ||
|
||
// Need to search for all the users who use @yahoo.com email addresses? | ||
$users = $db->query()->where('email','LIKE','@yahoo.com')->results(); | ||
|
||
``` | ||
|
||
|
||
|
@@ -295,20 +302,22 @@ If caching is enabled, queries will use `findAll()` and then cache results for t | |
```php | ||
// Simple (equal to) Query | ||
// return all the users that are blocked. | ||
$users = $db->query() | ||
->where(['status' => 'blocked']) | ||
->results(); | ||
$users = $db->query()->where(['status' => 'blocked'])->results(); | ||
|
||
// Stackable WHERE clauses | ||
// return all the users who are blocked, | ||
// AND have "php" within the tag array | ||
$users = $db->query() | ||
->where('status','=','blocked') | ||
->where('tag','IN','php') | ||
->andWhere('tag','IN','php') | ||
->results(); | ||
|
||
// You can also use `.` dot delimiter to use on nested keys | ||
$users = $db->query()->where('status.language.english','=','blocked')->results(); | ||
|
||
// how about find all users that have a gmail account? | ||
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->results(); | ||
|
||
``` | ||
|
||
To run the query use `results()` | ||
|
@@ -335,6 +344,7 @@ To run the query use `results()` | |
|`<` |Less than| | ||
|`<=` |Less than or equal| | ||
|`IN` |Checks if the value is within a array| | ||
|`LIKE` |case-insensitive regex expression search| | ||
|
||
|
||
## (9) Caching | ||
|
@@ -359,4 +369,3 @@ Accepting contributions and feedback. Send in any issues and pull requests. | |
- Indexing (single document filtering, applied with all `save()` actions from validation closure) | ||
- Internal validations..security etc. | ||
- Cache driver (to use on other services like memcached, redis etc) | ||
- Query "LIKE" operator, using regex format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ class Predicate | |
'>=', | ||
'<=', | ||
'IN', | ||
'NOT' | ||
'NOT', | ||
'LIKE' | ||
]; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,6 +186,75 @@ public function testWhereCountAllGreaterLessCompare() | |
//-------------------------------------------------------------------- | ||
|
||
|
||
/** | ||
* testWhereLikeRegex() | ||
* | ||
* TEST CASE: | ||
* - Creates 10 items in database with ["pages" = 5] | ||
* - Counts the total items in the database | ||
* | ||
* FIRST TEST: Greater Than | ||
* - Should match "10" | ||
* | ||
* SECOND TEST: Less Than | ||
* - Should match "10" | ||
* | ||
* THIRD TEST: Less/Greater than "no match" | ||
* - Should match "0" | ||
* | ||
* Comparisons used ">=", ">", "<=", "<" | ||
* | ||
*/ | ||
public function testWhereLikeRegex() | ||
{ | ||
$db = new \Filebase\Database([ | ||
'dir' => __DIR__.'/databases/users_like', | ||
'cache' => false | ||
]); | ||
|
||
$db->flush(true); | ||
|
||
for ($x = 1; $x <= 10; $x++) | ||
{ | ||
$user = $db->get(uniqid()); | ||
$user->name = 'John Ellot'; | ||
$user->email = '[email protected]'; | ||
$user->save(); | ||
} | ||
|
||
// the needle | ||
$user = $db->get(uniqid()); | ||
$user->name = 'Timothy Marois'; | ||
$user->email = '[email protected]'; | ||
$user->save(); | ||
|
||
$count = $db->count(); | ||
|
||
// should return exact match | ||
$query1 = $db->query()->where('name','==','Timothy Marois')->results(); | ||
// this should fail to find anything | ||
$query2 = $db->query()->where('name','==','timothy marois')->results(); | ||
|
||
// this should find match with regex loose expression | ||
$query3 = $db->query()->where('name','LIKE','timothy marois')->results(); | ||
// this should find match by looking for loose expression on "timothy" | ||
$query4 = $db->query()->where('name','LIKE','timothy')->results(); | ||
// this should find all teh users that have an email address using "@email.com" | ||
$query5 = $db->query()->where('email','LIKE','@email.com')->results(); | ||
|
||
$this->assertEquals(1, count($query1)); | ||
$this->assertEquals(0, count($query2)); | ||
$this->assertEquals(1, count($query3)); | ||
$this->assertEquals(1, count($query4)); | ||
$this->assertEquals(1, count($query5)); | ||
|
||
$db->flush(true); | ||
} | ||
|
||
|
||
//-------------------------------------------------------------------- | ||
|
||
|
||
|
||
|
||
|
||
|