Skip to content

Commit

Permalink
Merge 94efd25 into fd764fb
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosenarruzza committed Nov 3, 2020
2 parents fd764fb + 94efd25 commit fb5fa0e
Show file tree
Hide file tree
Showing 29 changed files with 416 additions and 87 deletions.
11 changes: 10 additions & 1 deletion lib/rasti/db/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def collection_name
end

def collection_attributes
@collection_attributes ||= model.attributes - relations.keys
@collection_attributes ||= model.attributes - relations.keys - computed_attributes.keys
end

def primary_key
Expand Down Expand Up @@ -47,6 +47,10 @@ def queries
@queries ||= Hash::Indifferent.new
end

def computed_attributes
@computed_attributes ||= Hash::Indifferent.new
end

private

def set_collection_name(collection_name)
Expand Down Expand Up @@ -91,6 +95,11 @@ def query(name, lambda=nil, &block)
end
end

def computed_attribute(name, &block)
raise "Computed Attribute #{name} already exists" if computed_attributes.key? name
computed_attributes[name] = block.call
end

end

def initialize(environment)
Expand Down
22 changes: 22 additions & 0 deletions lib/rasti/db/computed_attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Rasti
module DB
class ComputedAttribute

attr_reader :identifier

def initialize(identifier, &join)
@identifier = identifier
@join = join
end

def apply_join(dataset)
join ? join.call(dataset) : dataset
end

private

attr_reader :join

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Rasti
module DB
module NQL
module Nodes
class Field < Treetop::Runtime::SyntaxNode
class Attribute < Treetop::Runtime::SyntaxNode

def identifier
def identifier(collection_class)
tables.empty? ? Sequel[column.to_sym] : Sequel[tables.join('__').to_sym][column.to_sym]
end

Expand All @@ -16,6 +16,10 @@ def column
_column.text_value
end

def computed_attributes
[]
end

end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rasti/db/nql/nodes/binary_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def dependency_tables
values.flat_map(&:dependency_tables)
end

def computed_attributes
left.computed_attributes | right.computed_attributes
end

def values
@values ||= values_for(left) + values_for(right)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rasti/db/nql/nodes/comparisons/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module Comparisons
class Base < Treetop::Runtime::SyntaxNode

def dependency_tables
field.tables.empty? ? [] : [field.tables.join('.')]
attribute.tables.empty? ? [] : [attribute.tables.join('.')]
end

def computed_attributes
attribute.computed_attributes
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class Equal < Base

def filter_condition
{ field.identifier => argument.value }
def filter_condition(collection_class)
{ attribute.identifier(collection_class) => argument.value }
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/greater_than.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class GreaterThan < Base

def filter_condition
field.identifier > argument.value
def filter_condition(collection_class)
attribute.identifier(collection_class) > argument.value
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/greater_than_or_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class GreaterThanOrEqual < Base

def filter_condition
field.identifier >= argument.value
def filter_condition(collection_class)
attribute.identifier(collection_class) >= argument.value
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class Include < Base

def filter_condition
Sequel.ilike(field.identifier, "%#{argument.value}%")
def filter_condition(collection_class)
Sequel.ilike(attribute.identifier(collection_class), "%#{argument.value}%")
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/less_than.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class LessThan < Base

def filter_condition
field.identifier < argument.value
def filter_condition(collection_class)
attribute.identifier(collection_class) < argument.value
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/less_than_or_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class LessThanOrEqual < Base

def filter_condition
field.identifier <= argument.value
def filter_condition(collection_class)
attribute.identifier(collection_class) <= argument.value
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class Like < Base

def filter_condition
Sequel.ilike(field.identifier, argument.value)
def filter_condition(collection_class)
Sequel.ilike(attribute.identifier(collection_class), argument.value)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/not_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class NotEqual < Base

def filter_condition
Sequel.negate(field.identifier => argument.value)
def filter_condition(collection_class)
Sequel.negate(attribute.identifier(collection_class) => argument.value)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/comparisons/not_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Nodes
module Comparisons
class NotInclude < Base

def filter_condition
~ Sequel.ilike(field.identifier, "%#{argument.value}%")
def filter_condition(collection_class)
~ Sequel.ilike(attribute.identifier(collection_class), "%#{argument.value}%")
end

end
Expand Down
29 changes: 29 additions & 0 deletions lib/rasti/db/nql/nodes/computed_attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Rasti
module DB
module NQL
module Nodes
class ComputedAttribute < Treetop::Runtime::SyntaxNode

def identifier(collection_class)
collection_class.computed_attributes[computed_attribute].identifier
end

def computed_attributes
[computed_attribute]
end

def tables
[]
end

private

def computed_attribute
name.text_value.to_sym
end

end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/conjunction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module NQL
module Nodes
class Conjunction < BinaryNode

def filter_condition
Sequel.&(*values.map(&:filter_condition))
def filter_condition(collection_class)
Sequel.&(*values.map { |value| value.filter_condition(collection_class) } )
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/nql/nodes/disjunction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module NQL
module Nodes
class Disjunction < BinaryNode

def filter_condition
Sequel.|(*values.map(&:filter_condition))
def filter_condition(collection_class)
Sequel.|(*values.map { |value| value.filter_condition(collection_class) } )
end

end
Expand Down
8 changes: 6 additions & 2 deletions lib/rasti/db/nql/nodes/parenthesis_sentence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ def dependency_tables
sentence.dependency_tables
end

def filter_condition
sentence.filter_condition
def computed_attributes
sentence.computed_attributes
end

def filter_condition(collection_class)
sentence.filter_condition collection_class
end

end
Expand Down
8 changes: 6 additions & 2 deletions lib/rasti/db/nql/nodes/sentence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ def dependency_tables
proposition.dependency_tables
end

def filter_condition
proposition.filter_condition
def computed_attributes
proposition.computed_attributes
end

def filter_condition(collection_class)
proposition.filter_condition collection_class
end

end
Expand Down
Loading

0 comments on commit fb5fa0e

Please sign in to comment.