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

Commit

Permalink
Added new method database class truncate() as an alias of flush(true)…
Browse files Browse the repository at this point in the history
…. Added REGEX on query operators. (and more testing)
  • Loading branch information
timothymarois committed Aug 6, 2017
1 parent 0523914 commit 7067fc7
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

### 08/06/2017 - 1.0.5
* Added new method database class `truncate()` as an alias of `flush(true)`
* Added `REGEX` as new query operator. Uses `preg_match($regex, $fieldValue)`

### 08/05/2017 - 1.0.4
* Added `first()` (if you want to only return the first array of the query result)
* Ability to use Queries without needing `where()`, can now use queries to find all and order results
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ echo $db->get($userId)->createdAt();
// in this case, tags might come back as an array ["php","html","javascript"]
$user_tags = $db->get($userId)->field('tags');

// or if "tags" is nested in the user data, such as aboutme->tags
$user_tags = $db->get($userId)->field('aboutme.tags');
// or if "tags" is nested in the user data, such as about[tags]
$user_tags = $db->get($userId)->field('about.tags');

// and of course you can do this as well for getting "tags"
$user = $db->get($userId);
$user_tags = $user->tags;
$user_tags = $user->aboutme->tags;
$user_tags = $user->about['tags'];
```


Expand Down Expand Up @@ -197,10 +197,11 @@ Here is a list of methods you can use on the database class.
|Method|Details|
|---|---|
|`get()` | Refer to [get()](https://github.com/tmarois/Filebase#3-get-and-methods) |
|`findAll()` | Returns all Documents in database |
|`findAll()` | Returns all documents in database |
|`count()` | Number of documents in database |
|`flush(true)` | Deletes all documents |
|`flush(true)` | Deletes all documents. |
|`flushCache()` | Clears all the cache |
|`truncate()` | Deletes all documents. Alias of `flush(true)` |
|`query()` | Refer to the [Queries](https://github.com/tmarois/Filebase#8-queries) |

Examples
Expand Down Expand Up @@ -330,7 +331,7 @@ $users = $db->query()->where('status.language.english','=','blocked')->limit(10)
// Query LIKE Example: how about find all users that have a gmail account?
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->results();

// OrderBy Example: From the above query, what if you want to order the results by nested array (profile name?)
// OrderBy Example: From the above query, what if you want to order the results by nested array
$usersWithGmail = $db->query()
->where('email','LIKE','@gmail.com')
->orderBy('profile.name', 'ASC')
Expand All @@ -348,6 +349,9 @@ $user = $db->query()->orderBy('page_views', 'DESC')->first();
// print out the user name
echo $user['name'];

// What about regex search? Finds emails within a field
$users = $db->query()->where('email','REGEX','/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,4}\b/i')->results();


```

Expand Down Expand Up @@ -383,6 +387,7 @@ These methods execute the query and return results *(do not try to use them toge
|`IN` |Checks if the value is within a array|
|`LIKE` |case-insensitive regex expression search|
|`NOT LIKE` |case-insensitive regex expression search (opposite)|
|`REGEX` |Regex search|


## (9) Caching
Expand Down
15 changes: 15 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ public function delete(Document $document)
}


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

/**
* truncate
*
* Alias for flush(true)
*
* @return @see flush
*/
public function truncate()
{
return $this->flush(true);
}


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


Expand Down
3 changes: 2 additions & 1 deletion src/Predicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Predicate
'IN',
'NOT',
'LIKE',
'NOT LIKE'
'NOT LIKE',
'REGEX'
];


Expand Down
2 changes: 2 additions & 0 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ public function match($document, $field, $operator, $value)
return true;
case (strtoupper($operator) === 'IN' && in_array($value, (array) $d_value)):
return true;
case (strtoupper($operator) === 'REGEX' && preg_match($value, $d_value)):
return true;
default:
return false;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ public function testDatabaseFlushTrue()
}


public function testDatabaseTruncate()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/test_delete'
]);

$db->flush(true);

$db->get('test1')->set(['key'=>'value'])->save();
$db->get('test2')->set(['key'=>'value'])->save();

$this->assertEquals(true, $db->truncate());

$test = $db->get('test2');
$this->assertEquals(null, $test->key);
}


public function testDatabaseFlushFalse()
{
$this->expectException(\Exception::class);
Expand Down
32 changes: 32 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,38 @@ public function testSave()
//--------------------------------------------------------------------


/**
* testSetIdGetId()
*
* TEST CASE:
* - Set and Get Id
*
*/
public function testSetIdGetId()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/data_rename',
'cache' => false
]);

// save data
$doc = $db->get('name_1')->save(['key'=>'value']);
$this->assertEquals('name_1', $doc->getId());

// delete existing doc so its not duplicated
// object still exist, but file has been removed
$doc->delete();
$this->assertEquals('name_1', $doc->getId());

// change id and save (new file is created)
$doc->setId('name_2')->save();
$this->assertEquals('name_2', $doc->getId());
}


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


/**
* testSetValue()
*
Expand Down
56 changes: 54 additions & 2 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function testWhereCountAllGreaterLessCompare()


/**
* testWhereLikeRegex()
* testWhereLike()
*
* TEST CASE:
* - Creates a bunch of items with the same information
Expand All @@ -196,7 +196,7 @@ public function testWhereCountAllGreaterLessCompare()
* Comparisons used "LIKE", "NOT LIKE", "=="
*
*/
public function testWhereLikeRegex()
public function testWhereLike()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_like',
Expand Down Expand Up @@ -249,6 +249,58 @@ public function testWhereLikeRegex()
//--------------------------------------------------------------------


/**
* testWhereRegex()
*
* TEST CASE:
* - Testing the use of regex
*
* Comparisons used "REGEX"
*
*/
public function testWhereRegex()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_regex',
'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 (with bad email)
$user = $db->get(uniqid());
$user->name = 'Leo Ash';
$user->email = 'example@emailcom';
$user->save();

$count = $db->count();

// this should find match with regex loose expression
$query1 = $db->query()->where('name','REGEX','/leo/i')->results();
$query2 = $db->query()->where('name','REGEX','/leo\sash/i')->results();

// finds all the emails in a field
$query3 = $db->query()->where('email','REGEX','/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,4}\b/i')->results();

$this->assertEquals(1, count($query1));
$this->assertEquals(1, count($query2));
$this->assertEquals(10, count($query3));

$db->flush(true);
}


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


/**
* testLimitOffset()
*
Expand Down

0 comments on commit 7067fc7

Please sign in to comment.