From 63c941e33b93faa4c4c1019c7dcd3f8851cee212 Mon Sep 17 00:00:00 2001 From: Kiril Grancharov Date: Thu, 14 Apr 2016 16:16:56 +0300 Subject: [PATCH 1/4] Adding postgre spesific operators for jsonb type. --- src/Illuminate/Database/Query/Builder.php | 4 ++-- src/Illuminate/Database/Query/Grammars/Grammar.php | 10 ++++++++++ .../Database/Query/Grammars/PostgresGrammar.php | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 390c14296878..ec80f4208d5e 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -505,7 +505,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' // If the given operator is not found in the list of valid operators we will // assume that the developer is just short-cutting the '=' operators and // we will set the operators to '=' and set the values appropriately. - if (! in_array(strtolower($operator), $this->operators, true)) { + if (! in_array(strtolower($operator), $this->operators, true) && ! in_array(strtolower($operator), $this->grammar->getOperators(), true)) { list($value, $operator) = [$operator, '=']; } @@ -579,7 +579,7 @@ public function orWhere($column, $operator = null, $value = null) */ protected function invalidOperatorAndValue($operator, $value) { - $isOperator = in_array($operator, $this->operators); + $isOperator = in_array($operator, $this->operators) && in_array($operator, $this->grammar->getOperators()); return $isOperator && $operator != '=' && is_null($value); } diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index fbfc8404b519..ee41123bfd01 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -837,4 +837,14 @@ protected function removeLeadingBoolean($value) { return preg_replace('/and |or /i', '', $value, 1); } + + /** + * Get the gramar operators + * + * @return array + */ + public function getOperators() + { + return $this->operators; + } } diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index cc9d4eee30ac..db6a47adc0a8 100755 --- a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php @@ -16,6 +16,7 @@ class PostgresGrammar extends Grammar '=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike', '&', '|', '#', '<<', '>>', + '@>','<@','?','?|','?&','||','-','-','#-', ]; /** From 135cb84e4f62c035e0baaac57fe8c8a9860f899c Mon Sep 17 00:00:00 2001 From: Kiril Grancharov Date: Thu, 14 Apr 2016 17:42:40 +0300 Subject: [PATCH 2/4] fixed some bugs with the testing and code style --- src/Illuminate/Database/Query/Grammars/Grammar.php | 4 ++-- src/Illuminate/Database/Query/Grammars/PostgresGrammar.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index ee41123bfd01..87e90c8092df 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -839,12 +839,12 @@ protected function removeLeadingBoolean($value) } /** - * Get the gramar operators + * Get the gramar operators. * * @return array */ public function getOperators() { - return $this->operators; + return isset($this->operators)?$this->operators:[]; } } diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index db6a47adc0a8..d1b69d4cd838 100755 --- a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php @@ -16,7 +16,7 @@ class PostgresGrammar extends Grammar '=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike', '&', '|', '#', '<<', '>>', - '@>','<@','?','?|','?&','||','-','-','#-', + '@>', '<@', '?', '?|', '?&', '||', '-', '-', '#-', ]; /** From bd54aeec58f37bab78325ac7985e7247387777ab Mon Sep 17 00:00:00 2001 From: Kiril Grancharov Date: Thu, 14 Apr 2016 17:50:31 +0300 Subject: [PATCH 3/4] again fixing coding style --- src/Illuminate/Database/Query/Grammars/Grammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 87e90c8092df..7f3167df370f 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -845,6 +845,6 @@ protected function removeLeadingBoolean($value) */ public function getOperators() { - return isset($this->operators)?$this->operators:[]; + return isset($this->operators) ? $this->operators : []; } } From 0932c666192c7d27017e006c66abd66cc9eacced Mon Sep 17 00:00:00 2001 From: Kiril Grancharov Date: Mon, 18 Apr 2016 11:28:57 +0300 Subject: [PATCH 4/4] Added simple test to check postgres where operators and fixed other failed tests --- src/Illuminate/Database/Query/Builder.php | 2 +- tests/Database/DatabaseEloquentBuilderTest.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index ec80f4208d5e..ffbf88b1281b 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -579,7 +579,7 @@ public function orWhere($column, $operator = null, $value = null) */ protected function invalidOperatorAndValue($operator, $value) { - $isOperator = in_array($operator, $this->operators) && in_array($operator, $this->grammar->getOperators()); + $isOperator = in_array($operator, $this->operators); return $isOperator && $operator != '=' && is_null($value); } diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 8c983aaa8aa4..41e0499f6760 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -438,6 +438,14 @@ public function testSimpleWhere() $this->assertEquals($result, $builder); } + public function testPostgresOperatorsWhere() + { + $builder = $this->getBuilder(); + $builder->getQuery()->shouldReceive('where')->once()->with('foo', '@>', 'bar'); + $result = $builder->where('foo', '@>', 'bar'); + $this->assertEquals($result, $builder); + } + public function testDeleteOverride() { $builder = $this->getBuilder();