From 3ecf3aee1f9cd39d57a97736d18db34efc475ec3 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 02:28:16 +0330 Subject: [PATCH 01/17] add check for predicates in documentload method for keep old code style --- src/QueryLogic.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/QueryLogic.php b/src/QueryLogic.php index e573d86..3e6ec1c 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -45,7 +45,10 @@ public function __construct(Database $database) private function loadDocuments() { $predicates = $this->predicate->get(); - + if (empty($predicates)) + { + $predicates = 'findAll'; + } if ($this->cache===false) { $this->documents = $this->database->findAll(true,false); @@ -81,7 +84,7 @@ public function run() } $this->loadDocuments(); - + if ($predicates !== 'findAll') { $this->documents = $this->filter($this->documents, $predicates); From 419f6fa4e5cdc543aa45a5911f59dde32577029a Mon Sep 17 00:00:00 2001 From: Timothy Marois Date: Sun, 24 Feb 2019 17:55:23 -0500 Subject: [PATCH 02/17] Updated changelog and minor comment block updates. --- CHANGELOG.md | 3 +++ src/Database.php | 2 +- src/QueryLogic.php | 7 ++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 026e4b2..856a4e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Change Log ========== +### 02/24/2019 - 1.0.24 +* Merged [Pull Request](https://github.com/filebase/Filebase/pull/50) Fixed [bug](https://github.com/filebase/Filebase/issues/41) returning unexpected results. + ### 02/24/2019 - 1.0.23 * Merged [Pull Request](https://github.com/filebase/Filebase/pull/49) Added support for order by multiple columns * Merged [Pull Request](https://github.com/filebase/Filebase/pull/46) Added ability to query document ids (internal id) diff --git a/src/Database.php b/src/Database.php index 559b8cb..40b189b 100644 --- a/src/Database.php +++ b/src/Database.php @@ -15,7 +15,7 @@ class Database * Stores the version of Filebase * use $db->getVersion() */ - const VERSION = '1.0.23'; + const VERSION = '1.0.24'; /** * $config diff --git a/src/QueryLogic.php b/src/QueryLogic.php index e573d86..0d97c4f 100644 --- a/src/QueryLogic.php +++ b/src/QueryLogic.php @@ -42,6 +42,10 @@ public function __construct(Database $database) } } + /** + * loadDocuments + * + */ private function loadDocuments() { $predicates = $this->predicate->get(); @@ -60,11 +64,12 @@ private function loadDocuments() $this->sort(); $this->offsetLimit(); - return $this; + return $this; } $this->documents = $this->database->findAll(true,false); return $this; } + /** * run * From d64408583d04f9aa0b52f6f3cdf0ec0d0d323ba5 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 03:51:33 +0330 Subject: [PATCH 03/17] check method exist to is_callable and add method get column to get database ... --- src/Database.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Database.php b/src/Database.php index 559b8cb..b29065e 100644 --- a/src/Database.php +++ b/src/Database.php @@ -422,11 +422,15 @@ public function __call($method,$args) return $this->$method(...$args); } - if(method_exists(Query::class,$method)) { - return (new Query($this))->$method(...$args); + if(is_callable([$obj=(new Query($this)),$method])) { + return $obj->$method(...$args); } throw new \BadMethodCallException("method {$method} not found on 'Database::class' and 'Query::class'"); } + public function getColumns() + { + return array_keys($this->first()); + } } From 3a21aa6304b63738d3df1e3996c320a813f3b47e Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 04:05:17 +0330 Subject: [PATCH 04/17] convert to lowercase --- src/Database.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Database.php b/src/Database.php index b29065e..4fdd8a7 100644 --- a/src/Database.php +++ b/src/Database.php @@ -430,7 +430,12 @@ public function __call($method,$args) } public function getColumns() { - return array_keys($this->first()); + $items=[]; + foreach(array_keys($this->first()) as $item) + { + $items[]=strtolower($item); + } + return $items; } } From f4123702b9acad76eb68bf9a63e1e44c93a07f16 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 04:05:41 +0330 Subject: [PATCH 05/17] test name_of_culomns --- tests/DatabaseTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index e187e71..9535700 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -338,4 +338,25 @@ public function test_must_return_array_on_select_an_culomn_from_cache() $this->assertInternalType('string', $result_from_cache[0]['email']); $db->flush(true); } + public function test_must_return_name_of_culomns() + { + $db = new \Filebase\Database([ + 'dir' => __DIR__.'/databases/saved', + 'cache' => true + ]); + + $db->flush(true); + + for ($x = 1; $x <= 10; $x++) + { + $user = $db->get(uniqid()); + $user->name = 'John'; + $user->email = 'john@example.com'; + $user->save(); + } + + $r=$db->first(); + $names=array_keys($r); + $this->assertEquals($names, $db->getColumns()); + } } From 24bdfbdc6874b5583e86058066213a7c760fbeb3 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 04:09:45 +0330 Subject: [PATCH 06/17] add call method for handle methods, add method "methodMatchs" --- src/Query.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Query.php b/src/Query.php index 3712a29..359ec78 100644 --- a/src/Query.php +++ b/src/Query.php @@ -246,4 +246,44 @@ public function delete($input = null) } } } + public function __call($method,$args) + { + if(method_exists($this,$method)) + { + return $method(...$args); + } + + if($name=$this->methodMatchs($method)) + { + $names=$this->database->getColumns(); + if(in_array($name,$names)) + { + if(count($args) == 1) + { + return $this->where($name,'==',$args[0]); + } + if(count($args) == 2) + { + return $this->where($name,$args[0],$args[1]); + } + throw new \InvalidArgumentException("input count must be 1 or 2 " . count($args) . "given"); + } + } + throw new \BadMethodCallException('method not found on '.__CLASS__); + + } + /** + * methodMatchs + * find where* with regex + */ + public function methodMatchs($method) + { + $patern='/^where(.*?)\s*$/is'; + preg_match($patern,$method,$result); + if(isset($result[1])) + { + return strtolower($result[1]); + } + return false; + } } From 2786f89275db5cd671b22951d4ec694874cf7434 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 04:09:55 +0330 Subject: [PATCH 07/17] add tests --- tests/QueryTest.php | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 1589e4a..59f6840 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -1261,4 +1261,66 @@ public function testWhereInUsingDocId() $db->flush(true); } + public function test_must_detect_and_return_culomn_with_regex() + { + $query=new Query(new Database); + $result=$query->methodMatchs('whereName'); + $this->assertEquals('name',$result); + } + public function test_must_call_method_on_regex_matched_culumn() + { + $db = new \Filebase\Database([ + 'dir' => __DIR__.'/databases/users_1', + 'cache' => false + ]); + + $db->flush(true); + + $user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'email@addres.com']); + $user2 = $db->get('obj2')->save(['name' => 'Jenny','email'=>'email@addres.com']); + $user3 = $db->get('obj3')->save(['name' => 'Cyrus','email'=>'email@addres.com']); + + // check with one input + $test1 = $db->query()->whereName('Bob')->first(); + $expected = ['name' => 'Bob','email'=>'email@addres.com']; + $this->assertEquals($expected, $test1); + + // check with two input + $test1 = $db->query()->whereEmail('==','email@addres.com')->first(); + $expected = ['name' => 'Bob','email'=>'email@addres.com']; + $this->assertEquals($expected, $test1); + + // check with two input withour query() + $test1 = $db->whereEmail('==','email@addres.com')->first(); + $expected = ['name' => 'Bob','email'=>'email@addres.com']; + $this->assertEquals($expected, $test1); + + $db->flush(true); + } + public function test_must_return_exeption_on_none_exist_method() + { + $db = new \Filebase\Database([ + 'dir' => __DIR__.'/databases/users_1', + 'cache' => false + ]); + + $db->flush(true); + + $user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'email@addres.com']); + $this->expectException(\BadMethodCallException::class); + $db->query()->wherenone('name')->first(); + } + public function test_must_return_exeption_on_empty_whereName_call_method() + { + $db = new \Filebase\Database([ + 'dir' => __DIR__.'/databases/users_1', + 'cache' => false + ]); + + $db->flush(true); + + $user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'email@addres.com']); + $this->expectException(\InvalidArgumentException::class); + $db->query()->whereName()->first(); + } } From 0bba8dfdc216b22e9f1b51203835e1dc51802472 Mon Sep 17 00:00:00 2001 From: faryar Date: Mon, 25 Feb 2019 04:40:54 +0330 Subject: [PATCH 08/17] add missed $this --- src/Query.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Query.php b/src/Query.php index 359ec78..9beaa0d 100644 --- a/src/Query.php +++ b/src/Query.php @@ -246,11 +246,14 @@ public function delete($input = null) } } } + /** + * call magic method + */ public function __call($method,$args) { if(method_exists($this,$method)) { - return $method(...$args); + return $this->$method(...$args); } if($name=$this->methodMatchs($method)) From f679fd5654a304ade8794e73dbd938ab9de7b5a7 Mon Sep 17 00:00:00 2001 From: faryar76 Date: Mon, 25 Feb 2019 20:09:49 +0330 Subject: [PATCH 09/17] doc block --- src/Database.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Database.php b/src/Database.php index 00448ac..71fc178 100644 --- a/src/Database.php +++ b/src/Database.php @@ -428,6 +428,9 @@ public function __call($method,$args) throw new \BadMethodCallException("method {$method} not found on 'Database::class' and 'Query::class'"); } + /** + * getColumns + */ public function getColumns() { $items=[]; From d0fd3f8391389c042205d37d4bdcf6813cfd447e Mon Sep 17 00:00:00 2001 From: faryar76 Date: Tue, 26 Feb 2019 01:25:28 +0330 Subject: [PATCH 10/17] replace regex patern and method name and test it --- src/Query.php | 16 ++++++---------- tests/QueryTest.php | 23 ++++++++++++++++------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/Query.php b/src/Query.php index 9beaa0d..4e6686e 100644 --- a/src/Query.php +++ b/src/Query.php @@ -256,7 +256,7 @@ public function __call($method,$args) return $this->$method(...$args); } - if($name=$this->methodMatchs($method)) + if($name=$this->sanatizeWhere($method)) { $names=$this->database->getColumns(); if(in_array($name,$names)) @@ -276,17 +276,13 @@ public function __call($method,$args) } /** - * methodMatchs + * sanatizeWhere * find where* with regex */ - public function methodMatchs($method) + function sanatizeWhere($method, $parameters=0) { - $patern='/^where(.*?)\s*$/is'; - preg_match($patern,$method,$result); - if(isset($result[1])) - { - return strtolower($result[1]); - } - return false; + $method = substr($method, 5); + $result=preg_replace('/(.)(?=[A-Z])/u', '$1'.'_', $method); + return strtolower($result); } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 59f6840..9214066 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -1264,8 +1264,12 @@ public function testWhereInUsingDocId() public function test_must_detect_and_return_culomn_with_regex() { $query=new Query(new Database); - $result=$query->methodMatchs('whereName'); + $result=$query->sanatizeWhere('whereName'); $this->assertEquals('name',$result); + + $result=$query->sanatizeWhere('whereUserProfileOwner'); + $this->assertEquals('user_profile_owner',$result); + } public function test_must_call_method_on_regex_matched_culumn() { @@ -1276,23 +1280,28 @@ public function test_must_call_method_on_regex_matched_culumn() $db->flush(true); - $user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'email@addres.com']); - $user2 = $db->get('obj2')->save(['name' => 'Jenny','email'=>'email@addres.com']); - $user3 = $db->get('obj3')->save(['name' => 'Cyrus','email'=>'email@addres.com']); + $user1 = $db->get('obj1')->save(['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']); + $user2 = $db->get('obj2')->save(['name' => 'Jenny','user_profile_is_some_query'=>'data','email'=>'email@addres.com']); + $user3 = $db->get('obj3')->save(['name' => 'Cyrus','user_profile_is_some_query'=>'data','email'=>'email@addres.com']); // check with one input $test1 = $db->query()->whereName('Bob')->first(); - $expected = ['name' => 'Bob','email'=>'email@addres.com']; + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; $this->assertEquals($expected, $test1); // check with two input $test1 = $db->query()->whereEmail('==','email@addres.com')->first(); - $expected = ['name' => 'Bob','email'=>'email@addres.com']; + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; $this->assertEquals($expected, $test1); // check with two input withour query() $test1 = $db->whereEmail('==','email@addres.com')->first(); - $expected = ['name' => 'Bob','email'=>'email@addres.com']; + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; + $this->assertEquals($expected, $test1); + + // check long query + $test1 = $db->whereUserProfileIsSomeQuery('==','data')->first(); + $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; $this->assertEquals($expected, $test1); $db->flush(true); From 34983f0e7017dfe3f0226e3de680fae84c9d29e8 Mon Sep 17 00:00:00 2001 From: faryar76 Date: Tue, 26 Feb 2019 01:27:03 +0330 Subject: [PATCH 11/17] remove uncoveraged method --- src/Query.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Query.php b/src/Query.php index 4e6686e..b6cbe2b 100644 --- a/src/Query.php +++ b/src/Query.php @@ -251,11 +251,6 @@ public function delete($input = null) */ public function __call($method,$args) { - if(method_exists($this,$method)) - { - return $this->$method(...$args); - } - if($name=$this->sanatizeWhere($method)) { $names=$this->database->getColumns(); From 4c0fda35161d1532b1fdd090b1aa86d18151cede Mon Sep 17 00:00:00 2001 From: faryar76 Date: Wed, 27 Feb 2019 01:27:40 +0330 Subject: [PATCH 12/17] add comment --- src/Query.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Query.php b/src/Query.php index b6cbe2b..f7445fc 100644 --- a/src/Query.php +++ b/src/Query.php @@ -251,6 +251,9 @@ public function delete($input = null) */ public function __call($method,$args) { + + // it will just hanle Dynamic where + // other exist methods will handle with system on call if($name=$this->sanatizeWhere($method)) { $names=$this->database->getColumns(); From ca5122d341991cf0b2aa5c5f10ec4a5526e999f3 Mon Sep 17 00:00:00 2001 From: faryar76 Date: Wed, 27 Feb 2019 01:29:57 +0330 Subject: [PATCH 13/17] refactor --- src/Query.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Query.php b/src/Query.php index f7445fc..ef31a6d 100644 --- a/src/Query.php +++ b/src/Query.php @@ -235,15 +235,12 @@ public function delete($input = null) foreach($items as $item) { if (is_object($input)) { - $condition = $input($item); - - if ($condition) { + if ($input($item)===true) { $item->delete(); } + continue; } - else { - $item->delete(); - } + $item->delete(); } } /** From c96b60483bdd03f2862f0c569cbcfc73eb3841f3 Mon Sep 17 00:00:00 2001 From: faryar76 Date: Wed, 27 Feb 2019 01:46:52 +0330 Subject: [PATCH 14/17] change Dynamic where method --- src/Query.php | 9 ++++++--- tests/QueryTest.php | 27 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Query.php b/src/Query.php index ef31a6d..a78f323 100644 --- a/src/Query.php +++ b/src/Query.php @@ -251,6 +251,7 @@ public function __call($method,$args) // it will just hanle Dynamic where // other exist methods will handle with system on call + if($name=$this->sanatizeWhere($method)) { $names=$this->database->getColumns(); @@ -276,8 +277,10 @@ public function __call($method,$args) */ function sanatizeWhere($method, $parameters=0) { - $method = substr($method, 5); - $result=preg_replace('/(.)(?=[A-Z])/u', '$1'.'_', $method); - return strtolower($result); + if(strpos('_',$method)) + { + return false; + } + return strtolower(substr($method, 5)); } } diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 9214066..7462cbc 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -1267,8 +1267,8 @@ public function test_must_detect_and_return_culomn_with_regex() $result=$query->sanatizeWhere('whereName'); $this->assertEquals('name',$result); - $result=$query->sanatizeWhere('whereUserProfileOwner'); - $this->assertEquals('user_profile_owner',$result); + $result=$query->sanatizeWhere('whereUser'); + $this->assertEquals('user',$result); } public function test_must_call_method_on_regex_matched_culumn() @@ -1299,14 +1299,10 @@ public function test_must_call_method_on_regex_matched_culumn() $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; $this->assertEquals($expected, $test1); - // check long query - $test1 = $db->whereUserProfileIsSomeQuery('==','data')->first(); - $expected = ['name' => 'Bob','user_profile_is_some_query'=>'data','email'=>'email@addres.com']; - $this->assertEquals($expected, $test1); - + $db->flush(true); } - public function test_must_return_exeption_on_none_exist_method() + public function test_must_return_exception_on_none_exist_method() { $db = new \Filebase\Database([ 'dir' => __DIR__.'/databases/users_1', @@ -1319,7 +1315,7 @@ public function test_must_return_exeption_on_none_exist_method() $this->expectException(\BadMethodCallException::class); $db->query()->wherenone('name')->first(); } - public function test_must_return_exeption_on_empty_whereName_call_method() + public function test_must_return_exception_on_empty_whereName_call_method() { $db = new \Filebase\Database([ 'dir' => __DIR__.'/databases/users_1', @@ -1332,4 +1328,17 @@ public function test_must_return_exeption_on_empty_whereName_call_method() $this->expectException(\InvalidArgumentException::class); $db->query()->whereName()->first(); } + public function test_must_return_exception_on_where_contain_underscore() + { + $db = new \Filebase\Database([ + 'dir' => __DIR__.'/databases/users_1', + 'cache' => false + ]); + + $db->flush(true); + + $user1 = $db->get('obj1')->save(['name' => 'Bob','email'=>'email@addres.com']); + $this->expectException(\BadMethodCallException::class); + $db->query()->where_Name('Bob')->first(); + } } From aa0b8bb877a8a6aca5eba64f475e7e2e30ac4f1c Mon Sep 17 00:00:00 2001 From: faryar76 Date: Wed, 27 Feb 2019 01:59:21 +0330 Subject: [PATCH 15/17] set defult "[]" for first and last on null result "php 5.6<" --- src/Query.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Query.php b/src/Query.php index a78f323..b45fd07 100644 --- a/src/Query.php +++ b/src/Query.php @@ -161,11 +161,19 @@ public function first( $data_only = true ) if ($data_only === true && empty($this->fields)) { $results = parent::run()->toArray(); - return current($results); + if(count($results) > 0) + { + return current($results); + } + return []; } $results = parent::run()->getDocuments(); - return current($results); + if(count($results) > 0) + { + return current($results); + } + return []; } /** @@ -179,10 +187,18 @@ public function last( $data_only = true ) if ($data_only === true && empty($this->fields)) { $results = parent::run()->toArray(); + if(count($results) > 0) + { + return end($results); + } return end($results); } $results = parent::run()->getDocuments(); + if(count($results) > 0) + { + return end($results); + } return end($results); } From 9606eb252e2faa00302fbcec80d00b3367704c43 Mon Sep 17 00:00:00 2001 From: faryar76 Date: Wed, 27 Feb 2019 02:04:57 +0330 Subject: [PATCH 16/17] fix --- src/Query.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Query.php b/src/Query.php index b45fd07..5317f88 100644 --- a/src/Query.php +++ b/src/Query.php @@ -191,7 +191,7 @@ public function last( $data_only = true ) { return end($results); } - return end($results); + return []; } $results = parent::run()->getDocuments(); @@ -199,7 +199,7 @@ public function last( $data_only = true ) { return end($results); } - return end($results); + return []; } /** From ec11c2f6453e0576df04af4419d45a0f2d5efdae Mon Sep 17 00:00:00 2001 From: faryar76 Date: Wed, 27 Feb 2019 02:16:25 +0330 Subject: [PATCH 17/17] redo changes --- src/Query.php | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/Query.php b/src/Query.php index 5317f88..a78f323 100644 --- a/src/Query.php +++ b/src/Query.php @@ -161,19 +161,11 @@ public function first( $data_only = true ) if ($data_only === true && empty($this->fields)) { $results = parent::run()->toArray(); - if(count($results) > 0) - { - return current($results); - } - return []; + return current($results); } $results = parent::run()->getDocuments(); - if(count($results) > 0) - { - return current($results); - } - return []; + return current($results); } /** @@ -187,19 +179,11 @@ public function last( $data_only = true ) if ($data_only === true && empty($this->fields)) { $results = parent::run()->toArray(); - if(count($results) > 0) - { - return end($results); - } - return []; + return end($results); } $results = parent::run()->getDocuments(); - if(count($results) > 0) - { - return end($results); - } - return []; + return end($results); } /**