Skip to content

Commit

Permalink
fixed :part and :plus conflicts with :or
Browse files Browse the repository at this point in the history
  • Loading branch information
ethn committed May 28, 2011
1 parent 1e86b47 commit 13ef41b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
19 changes: 10 additions & 9 deletions lib/wql.rb
Expand Up @@ -32,7 +32,7 @@ class Wql
:referential => %w{ link_to linked_to_by refer_to referred_to_by include included_by },
:special => %w{ or match complete not count and },
:ignore => %w{ prepend append },
:pass => %w{ cond:0 cond:1 cond:2 cond:3 }
:pass => %w{ cond }
}.inject({}) {|h,pair| pair[1].each {|v| h[v.to_sym]=pair[0] }; h }
# put into form: { :content=>:basic, :left=>:relational, etc.. }

Expand Down Expand Up @@ -87,7 +87,7 @@ def sql
end

def run
#Rails.logger.info "query: #{query.inspect}\n\n sql: #{sql}"
#warn "query: #{query.inspect}\n\n sql: #{sql}"
rows = ActiveRecord::Base.connection.select_all( sql )
case (query[:return] || :card).to_sym
when :card
Expand Down Expand Up @@ -291,7 +291,7 @@ def merge(spec)
when :referential; self.refspec(key, spec.delete(key))
when :ignore; spec.delete(key)
when :pass; # for :cond ie. raw sql condition to be ANDed
else raise("Invalid attribute #{key}") unless key.to_s.match(/(type|id)\:\d+/)
else raise("Invalid attribute #{key}") unless key.to_s.match(/(type|id|cond)\:\d+/)
end
end

Expand Down Expand Up @@ -325,12 +325,12 @@ def match(val)
v.split(/\s+/).map{ |x| %{#{f} #{cxn.match(quote("[[:<:]]#{x}[[:>:]]"))}} }.join(" AND ")
end.join(" OR ") + ')'
end
merge :'cond:3'=>SqlCond.new(cond)
merge field(:cond)=>SqlCond.new(cond)
end

def complete(val)
no_plus_card = (val=~/\+/ ? '' : "and tag_id is null") #FIXME -- this should really be more nuanced -- it breaks down after one plus
merge :'cond:2' => SqlCond.new(" lower(name) LIKE lower(#{quote(val.to_s+'%')}) #{no_plus_card}")
merge field(:cond) => SqlCond.new(" lower(name) LIKE lower(#{quote(val.to_s+'%')}) #{no_plus_card}")
end

def field(name)
Expand All @@ -351,9 +351,8 @@ def or(val)

def subcondition(val, args={})
args = { :return=>:condition, :_parent=>self }.merge(args)
key = "cond:#{args.keys.length}".to_sym
cardspec = CardSpec.build( args )
merge key => cardspec.merge(val)
merge field(:cond) => cardspec.merge(val)
self.sql.joins += cardspec.sql.joins
self.sql.relevance_fields += cardspec.sql.relevance_fields
end
Expand All @@ -371,7 +370,8 @@ def right(val)
end

def part(val)
merge :or=>{ :tag_id => val.clone, :trunk_id => val }
subval = { :tag_id => val.clone, :trunk_id => val }
subcondition(subval, :join=>:or)
end

def right_plus(val)
Expand All @@ -387,10 +387,11 @@ def left_plus(val)
def plus(val)
#warn "GOT PLUS: #{val}"
part_spec, connection_spec = val.is_a?(Array) ? val : [ val, {} ]
merge :or=>{
subval = {
field(:id) => subspec(connection_spec, :return=>'trunk_id', :tag_id=>part_spec.clone),
field(:id) => subspec(connection_spec, :return=>'tag_id', :trunk_id=>part_spec)
}
subcondition(subval, :join=>:or)
end

def edited_by(val)
Expand Down
12 changes: 8 additions & 4 deletions spec/lib/wql_spec.rb
Expand Up @@ -58,7 +58,7 @@

describe "not" do
it "should exclude cards matching not criteria" do
s = Wql.new(:plus=>"A", :not=>{:plus=>"A+B"}).run.plot(:name).sort.should==%w{ B D E F }
Wql.new(:plus=>"A", :not=>{:plus=>"A+B"}).run.plot(:name).sort.should==%w{ B D E F }
end
end

Expand Down Expand Up @@ -307,8 +307,12 @@
results.map(&:name).sort.should == ['A+C','Z']
end
end



describe "or" do
it "should work with :plus" do
Wql.new(:plus=>"A", :or=>{:name=>'B', :match=>'K'}).run.plot(:name).sort.should==%w{ B }
end
end

describe "offset" do
it "should not break count" do
Expand Down Expand Up @@ -361,7 +365,7 @@
it "should find plus cards for _self" do
Wql.new( :plus=>"_self", :context=>"A" ).run.plot(:name).sort.should == A_JOINEES
end

it "should find plus cards for _left" do
# this test fails in mysql when running the full suite
# (although not when running the individual test )
Expand Down

0 comments on commit 13ef41b

Please sign in to comment.