lib/ustate/query_string.treetop in ustate-client-0.0.2 vs lib/ustate/query_string.treetop in ustate-client-0.0.3
- old
+ new
@@ -3,10 +3,16 @@
# (state =~ "foo" or state == "bar") and service == "foo"
# Binding order proceeds from loosest to tightest.
rule or
first:and rest:(space 'or' space and)* {
+ def query
+ rest.elements.map { |x| x.and }.inject(first.query) do |a, sub|
+ Query::Or.new a, sub.query
+ end
+ end
+
def sql
rest.elements.map { |x| x.and }.
inject(first.sql) do |a, sub|
a | sub.sql
end
@@ -14,10 +20,16 @@
}
end
rule and
first:primary rest:(space 'and' space primary)* {
+ def query
+ rest.elements.map { |x| x.primary }.inject(first.query) do |a, sub|
+ Query::And.new a, sub.query
+ end
+ end
+
def sql
rest.elements.map { |x| x.primary }.
inject(first.sql) do |a, sub|
a & sub.sql
end
@@ -25,10 +37,14 @@
}
end
rule primary
'(' space? x:or space? ')' {
+ def query
+ x.query
+ end
+
def sql
x.sql
end
}
/
@@ -39,25 +55,37 @@
equals / not_equals / approximately
end
rule approximately
field space? '=~' space? string {
+ def query
+ Query::Approximately.new field.sql, string.sql
+ end
+
def sql
Sequel::SQL::StringExpression.like field.sql, string.sql
end
}
end
rule not_equals
field space? '!=' space? value {
+ def query
+ Query::NotEquals.new field.sql, value.sql
+ end
+
def sql
Sequel::SQL::BooleanExpression.from_value_pairs({field.sql => value.sql}, :AND, true)
end
}
end
rule equals
- field space? ('=' / '==') space? value {
+ field space? ('==' / '=') space? value {
+ def query
+ Query::Equals.new field.sql, value.sql
+ end
+
def sql
Sequel::SQL::BooleanExpression.from_value_pairs field.sql => value.sql
end
}
end