Statement#exec and #query require named argument for array values

This change allows to use an array as single argument for #exec and
 #query methods. Before it was shadowed by the *args splat overload.
This commit is contained in:
Johannes Müller 2019-09-05 17:37:11 +02:00
parent dcd0af5ce8
commit d0d89c1e13
No known key found for this signature in database
GPG key ID: F0F349637AC5087A
3 changed files with 27 additions and 7 deletions

View file

@ -97,7 +97,7 @@ class DummyDriver < DB::Driver
property params
def initialize(connection, @query : String, @prepared : Bool)
@params = Hash(Int32 | String, DB::Any).new
@params = Hash(Int32 | String, DB::Any | Array(DB::Any)).new
super(connection)
raise DB::Error.new(query) if query == "syntax error"
end
@ -126,6 +126,10 @@ class DummyDriver < DB::Driver
@params[index] = value
end
private def set_param(index, value : Array)
@params[index] = value.map(&.as(DB::Any))
end
private def set_param(index, value)
raise "not implemented for #{value.class}"
end

View file

@ -43,10 +43,18 @@ describe DB::Statement do
end
end
it "should initialize positional params in query with array" do
it "accepts array as single argument" do
with_dummy_connection do |cnn|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
stmt.query ["a", 1, nil]
stmt.params[0].should eq(["a", 1, nil])
end
end
it "should initialize positional params in query with array" do
with_dummy_connection do |cnn|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
stmt.query args: ["a", 1, nil]
stmt.params[0].should eq("a")
stmt.params[1].should eq(1)
stmt.params[2].should eq(nil)
@ -63,10 +71,18 @@ describe DB::Statement do
end
end
it "should initialize positional params in exec with array" do
it "accepts array as single argument" do
with_dummy_connection do |cnn|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
stmt.exec ["a", 1, nil]
stmt.params[0].should eq(["a", 1, nil])
end
end
it "should initialize positional params in exec with array" do
with_dummy_connection do |cnn|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
stmt.exec args: ["a", 1, nil]
stmt.params[0].should eq("a")
stmt.params[1].should eq(1)
stmt.params[2].should eq(nil)

View file

@ -29,14 +29,14 @@ module DB
# See `QueryMethods#exec`
abstract def exec(*args) : ExecResult
# See `QueryMethods#exec`
abstract def exec(args : Array) : ExecResult
abstract def exec(*, args : Array) : ExecResult
# See `QueryMethods#query`
abstract def query : ResultSet
# See `QueryMethods#query`
abstract def query(*args) : ResultSet
# See `QueryMethods#query`
abstract def query(args : Array) : ResultSet
abstract def query(*, args : Array) : ResultSet
end
# Represents a query in a `Connection`.
@ -68,7 +68,7 @@ module DB
end
# See `QueryMethods#exec`
def exec(args : Array) : DB::ExecResult
def exec(*, args : Array) : DB::ExecResult
perform_exec_and_release(args)
end
@ -84,7 +84,7 @@ module DB
end
# See `QueryMethods#query`
def query(args : Array) : DB::ResultSet
def query(*, args : Array) : DB::ResultSet
perform_query_with_rescue args
end