mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
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:
parent
dcd0af5ce8
commit
d0d89c1e13
3 changed files with 27 additions and 7 deletions
|
@ -97,7 +97,7 @@ class DummyDriver < DB::Driver
|
||||||
property params
|
property params
|
||||||
|
|
||||||
def initialize(connection, @query : String, @prepared : Bool)
|
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)
|
super(connection)
|
||||||
raise DB::Error.new(query) if query == "syntax error"
|
raise DB::Error.new(query) if query == "syntax error"
|
||||||
end
|
end
|
||||||
|
@ -126,6 +126,10 @@ class DummyDriver < DB::Driver
|
||||||
@params[index] = value
|
@params[index] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def set_param(index, value : Array)
|
||||||
|
@params[index] = value.map(&.as(DB::Any))
|
||||||
|
end
|
||||||
|
|
||||||
private def set_param(index, value)
|
private def set_param(index, value)
|
||||||
raise "not implemented for #{value.class}"
|
raise "not implemented for #{value.class}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,10 +43,18 @@ describe DB::Statement do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should initialize positional params in query with array" do
|
it "accepts array as single argument" do
|
||||||
with_dummy_connection do |cnn|
|
with_dummy_connection do |cnn|
|
||||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||||
stmt.query ["a", 1, nil]
|
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[0].should eq("a")
|
||||||
stmt.params[1].should eq(1)
|
stmt.params[1].should eq(1)
|
||||||
stmt.params[2].should eq(nil)
|
stmt.params[2].should eq(nil)
|
||||||
|
@ -63,10 +71,18 @@ describe DB::Statement do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should initialize positional params in exec with array" do
|
it "accepts array as single argument" do
|
||||||
with_dummy_connection do |cnn|
|
with_dummy_connection do |cnn|
|
||||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||||
stmt.exec ["a", 1, nil]
|
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[0].should eq("a")
|
||||||
stmt.params[1].should eq(1)
|
stmt.params[1].should eq(1)
|
||||||
stmt.params[2].should eq(nil)
|
stmt.params[2].should eq(nil)
|
||||||
|
|
|
@ -29,14 +29,14 @@ module DB
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
abstract def exec(*args) : ExecResult
|
abstract def exec(*args) : ExecResult
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
abstract def exec(args : Array) : ExecResult
|
abstract def exec(*, args : Array) : ExecResult
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
abstract def query : ResultSet
|
abstract def query : ResultSet
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
abstract def query(*args) : ResultSet
|
abstract def query(*args) : ResultSet
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
abstract def query(args : Array) : ResultSet
|
abstract def query(*, args : Array) : ResultSet
|
||||||
end
|
end
|
||||||
|
|
||||||
# Represents a query in a `Connection`.
|
# Represents a query in a `Connection`.
|
||||||
|
@ -68,7 +68,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#exec`
|
# See `QueryMethods#exec`
|
||||||
def exec(args : Array) : DB::ExecResult
|
def exec(*, args : Array) : DB::ExecResult
|
||||||
perform_exec_and_release(args)
|
perform_exec_and_release(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# See `QueryMethods#query`
|
# See `QueryMethods#query`
|
||||||
def query(args : Array) : DB::ResultSet
|
def query(*, args : Array) : DB::ResultSet
|
||||||
perform_query_with_rescue args
|
perform_query_with_rescue args
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue