From 863ebca9cd02a292f6f646e23bf51d37716aa12a Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Thu, 6 Oct 2011 13:24:17 +0200 Subject: [PATCH] fixes: OR... --- lib/smql_to_ar/condition_types.rb | 12 +++++++----- lib/smql_to_ar/query_builder.rb | 9 ++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/smql_to_ar/condition_types.rb b/lib/smql_to_ar/condition_types.rb index 1ab5b0f..117f629 100644 --- a/lib/smql_to_ar/condition_types.rb +++ b/lib/smql_to_ar/condition_types.rb @@ -159,12 +159,13 @@ class SmqlToAR builder.where values.keys.collect {|vid| self.class::Where % [ col, vid.to_s ] } end else + b2 = SmqlToAR::And.new builder values.keys.each do |vid| - builder.where @cols.collect {|col| + b2.where SmqlToAR::Or[ *@cols.collect {|col| col.joins builder, table col = builder.column table+col.path, col.col self.class::Where % [ col, vid.to_s ] - } + }] end end self @@ -272,7 +273,7 @@ class SmqlToAR def initialize model, cols, val super model, cols, val[1] # sub: model, subquery, sub(condition) - @cols.each {|col, sub| sub[ 1...1] = SmqlToAR.new( col.relation, val[0]).parse } + @cols.each {|col, sub| sub[ 1..-1] = SmqlToAR.new( col.relation, val[0]).parse, *sub[-1] } end def verify_column col @@ -283,6 +284,7 @@ class SmqlToAR @cols.each do |col, sub| t = table+col.to_a builder.sub_joins t, col, *sub[0..1] + #ap sub: sub[2..-1] sub[2..-1].each &it.build( builder, t) end self @@ -386,7 +388,7 @@ class SmqlToAR def build builder, table raise_unless 1 == table.length, RootOnlyFunctionError.new( table) - builder.limit @args + builder.limit = Array.wrap(@args).first.to_i end end @@ -396,7 +398,7 @@ class SmqlToAR def build builder, table raise_unless 1 == table.length, RootOnlyFunctionError.new( table) - builder.offset @args + builder.offset = Array.wrap(@args).first.to_i end end diff --git a/lib/smql_to_ar/query_builder.rb b/lib/smql_to_ar/query_builder.rb index 9123fb3..e38eb82 100644 --- a/lib/smql_to_ar/query_builder.rb +++ b/lib/smql_to_ar/query_builder.rb @@ -79,7 +79,7 @@ class SmqlToAR end def build_join orig, pretable, table, prekey, key - " LEFT OUTER JOIN #{orig} AS #{quote_table_name table} ON #{column pretable, prekey} = #{column table, key} " + " LEFT JOIN #{orig} AS #{quote_table_name table} ON #{column pretable, prekey} = #{column table, key} " end def sub_joins table, col, model, query @@ -226,18 +226,21 @@ class SmqlToAR end def default() SmqlToAR::And end def default_new( parent) default.new self, parent, false end + def collect_build_where + collect {|x| x.respond_to?( :build_where) ? x.build_where : x.to_s } + end end class And < SubBuilder def default; SmqlToAR::Or; end def build_where - join ' AND ' + collect_build_where.join ' AND ' end end class Or < SubBuilder def build_where - join ' OR ' + collect_build_where.join ' OR ' end end end