Skip to content

Commit

Permalink
fix (Query): #wherish accepts Enumerable params
Browse files Browse the repository at this point in the history
Fixes #86
  • Loading branch information
vladfaust committed Apr 9, 2019
1 parent 0ddf13d commit f5b667f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/query/having_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ describe "Query#having" do

params.to_a.should eq [42, 43]
end

it "accepts both splat and enumerable params" do
q1 = Query(User).new.having("foo = ? AND bar = ?", 42, 43)
q2 = Query(User).new.having("foo = ? AND bar = ?", {42, 43})
q1.build.should eq q2.build
end
end

describe "shorthands" do
Expand Down
6 changes: 6 additions & 0 deletions spec/query/where_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe "Query#where" do

params.to_a.should eq [42, 43]
end

it "accepts both splat and enumerable params" do
q1 = Query(User).new.where("foo = ? AND bar = ?", 42, 43)
q2 = Query(User).new.where("foo = ? AND bar = ?", [42, 43])
q1.build.should eq q2.build
end
end

context "without params" do
Expand Down
7 changes: 6 additions & 1 deletion src/onyx-sql/query/wherish.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Onyx::SQL
# query = User.{{wherish.id}}("foo = ?", "bar").{{wherish.id}}("baz = ?", 42)
# query.build # => {"{{wherish.upcase.id}} (foo = ?) AND (baz = ?)", {"bar", 42}}
# ```
def {{wherish.id}}(clause : String, *params : DB::Any, or : Bool = false, not : Bool = false)
def {{wherish.id}}(clause : String, params : Enumerable(DB::Any), or : Bool = false, not : Bool = false)
ensure_{{wherish.id}} << {{wherish.camelcase.id}}.new(
clause: clause,
params: params.to_a.map(&.as(DB::Any)),
Expand All @@ -27,6 +27,11 @@ module Onyx::SQL
self
end

# ditto
def {{wherish.id}}(clause : String, *params : DB::Any, or : Bool = false, not : Bool = false)
{{wherish.id}}(clause, params.to_a, or, not)
end

# Add `{{wherish.upcase.id}}` *clause* without params.
#
# ```
Expand Down

0 comments on commit f5b667f

Please sign in to comment.