Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Added the LIKE operator for regex expression search. Updated readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothymarois committed Aug 5, 2017
1 parent c93d878 commit 15939f2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

```


Expand Down Expand Up @@ -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()`
Expand All @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Predicate
'>=',
'<=',
'IN',
'NOT'
'NOT',
'LIKE'
];


Expand Down
2 changes: 2 additions & 0 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public function match($document, $field, $operator, $value)
return true;
case ($operator === '<=' && $d_value <= $value):
return true;
case (strtoupper($operator) === 'LIKE' && preg_match('/'.$value.'/is',$d_value)):
return true;
case (strtoupper($operator) === 'IN' && in_array($d_value, (array) $value)):
return true;
case (strtoupper($operator) === 'IN' && in_array($value, (array) $d_value)):
Expand Down
69 changes: 69 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


//--------------------------------------------------------------------





Expand Down

0 comments on commit 15939f2

Please sign in to comment.