forked from anykeyh/clear
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yacine Petitprez
committed
Jun 29, 2018
1 parent
70a57dd
commit 7d33a3a
Showing
9 changed files
with
192 additions
and
51 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
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
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
require "./node" | ||
|
||
class Clear::Expression::Node::Literal < Clear::Expression::Node | ||
def initialize(@lit : AvailableLiteral) | ||
getter value : AvailableLiteral | ||
|
||
def initialize(@value : AvailableLiteral) | ||
end | ||
|
||
def resolve : String | ||
Clear::Expression[@lit] | ||
Clear::Expression[@value] | ||
end | ||
end |
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require "./jsonb" | ||
|
||
class Clear::Expression::Node::JSONB::Field < Clear::Expression::Node | ||
include Clear::SQL::JSONB | ||
|
||
getter field : Node | ||
getter key : String | ||
|
||
def initialize(@field, @key) | ||
end | ||
|
||
def resolve | ||
jsonb_resolve(@field.resolve, jsonb_k2a(key)) | ||
end | ||
|
||
def ==(value : Clear::Expression::Node) | ||
super(value) # << Keep same for node which are not literal value | ||
end | ||
|
||
def ==(value : _) # << For other type, literalize and use smart JSONB equality | ||
Clear::Expression::Node::JSONB::Equality.new(field.resolve, jsonb_k2h(key, value)) | ||
end | ||
end | ||
|
||
# Define a __value contains?__ operation between a jsonb column and a json hash | ||
class Clear::Expression::Node::JSONB::Equality < Clear::Expression::Node | ||
include Clear::SQL::JSONB | ||
|
||
getter jsonb_field : String | ||
getter value : JSONBHash | ||
|
||
def initialize(@jsonb_field, @value) | ||
end | ||
|
||
def resolve | ||
{@jsonb_field, Clear::Expression[@value.to_json]}.join(" @> ") | ||
end | ||
|
||
# In case of AND with another JSON equality test | ||
# we merge both expression in only one ! | ||
def &(other : self) | ||
if (other.jsonb_field == jsonb_field) | ||
Clear::Expression::Node::JSONB::Equality.new(jsonb_field, | ||
Clear::Util.hash_union(value, other.value) | ||
) | ||
else | ||
super(other) | ||
end | ||
end | ||
end | ||
|
||
class Clear::Expression::Node::Variable < Clear::Expression::Node | ||
def jsonb_key_exists?(key : String) | ||
Clear::Expression::Node::DoubleOperator.new(self, Clear::Expression::Node::Literal.new(key), "?") | ||
end | ||
|
||
def jsonb_any_key_exists?(keys : Array(String)) | ||
Clear::Expression::Node::DoubleOperator.new(self, | ||
{"array[", keys.join(", "), "]"}.join | ||
,"?|") | ||
end | ||
|
||
def jsonb_all_keys_exists?(keys : Array(String)) | ||
Clear::Expression::Node::DoubleOperator.new(self, | ||
{"array[", keys.join(", "), "]"}.join | ||
,"?&") | ||
end | ||
|
||
def jsonb(key : String) | ||
Clear::Expression::Node::JSONB::Field.new(self, key) | ||
end | ||
end |
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