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 (#110)
This change allows to use an array as single argument for #exec and #query and #scalar methods. Before it was shadowed by the *args splat overload.
This commit is contained in:
parent
af6d837bcd
commit
b3898ae2a2
9 changed files with 168 additions and 88 deletions
|
@ -227,14 +227,14 @@ describe DB do
|
|||
db.query "query", 1, "string" { }
|
||||
db.query("query", Bytes.new(4)) { }
|
||||
db.query("query", 1, "string", FooValue.new(5)) { }
|
||||
db.query "query", [1, "string", FooValue.new(5)] { }
|
||||
db.query "query", args: [1, "string", FooValue.new(5)] { }
|
||||
|
||||
db.query("query").close
|
||||
db.query("query", 1).close
|
||||
db.query("query", 1, "string").close
|
||||
db.query("query", Bytes.new(4)).close
|
||||
db.query("query", 1, "string", FooValue.new(5)).close
|
||||
db.query("query", [1, "string", FooValue.new(5)]).close
|
||||
db.query("query", args: [1, "string", FooValue.new(5)]).close
|
||||
end
|
||||
|
||||
DB.open("bar://host") do |db|
|
||||
|
@ -244,14 +244,14 @@ describe DB do
|
|||
db.query "query", 1, "string" { }
|
||||
db.query("query", Bytes.new(4)) { }
|
||||
db.query("query", 1, "string", BarValue.new(5)) { }
|
||||
db.query "query", [1, "string", BarValue.new(5)] { }
|
||||
db.query "query", args: [1, "string", BarValue.new(5)] { }
|
||||
|
||||
db.query("query").close
|
||||
db.query("query", 1).close
|
||||
db.query("query", 1, "string").close
|
||||
db.query("query", Bytes.new(4)).close
|
||||
db.query("query", 1, "string", BarValue.new(5)).close
|
||||
db.query("query", [1, "string", BarValue.new(5)]).close
|
||||
db.query("query", args: [1, "string", BarValue.new(5)]).close
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -263,7 +263,7 @@ describe DB do
|
|||
db.exec("query", 1, "string")
|
||||
db.exec("query", Bytes.new(4))
|
||||
db.exec("query", 1, "string", FooValue.new(5))
|
||||
db.exec("query", [1, "string", FooValue.new(5)])
|
||||
db.exec("query", args: [1, "string", FooValue.new(5)])
|
||||
end
|
||||
|
||||
DB.open("bar://host") do |db|
|
||||
|
@ -273,20 +273,20 @@ describe DB do
|
|||
db.exec("query", 1, "string")
|
||||
db.exec("query", Bytes.new(4))
|
||||
db.exec("query", 1, "string", BarValue.new(5))
|
||||
db.exec("query", [1, "string", BarValue.new(5)])
|
||||
db.exec("query", args: [1, "string", BarValue.new(5)])
|
||||
end
|
||||
end
|
||||
|
||||
it "Foo and Bar drivers should not implement each other params" do
|
||||
DB.open("foo://host") do |db|
|
||||
expect_raises Exception, "FooDriver::FooStatement does not support BarValue params" do
|
||||
db.exec("query", [BarValue.new(5)])
|
||||
db.exec("query", args: [BarValue.new(5)])
|
||||
end
|
||||
end
|
||||
|
||||
DB.open("bar://host") do |db|
|
||||
expect_raises Exception, "BarDriver::BarStatement does not support FooValue params" do
|
||||
db.exec("query", [FooValue.new(5)])
|
||||
db.exec("query", args: [FooValue.new(5)])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -43,10 +43,37 @@ 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 "allows no arguments" do
|
||||
with_dummy_connection do |cnn|
|
||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||
stmt.query
|
||||
stmt.params.should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "concatenate arguments" do
|
||||
with_dummy_connection do |cnn|
|
||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||
stmt.query 1, 2, args: ["a", [1, nil]]
|
||||
stmt.params[0].should eq(1)
|
||||
stmt.params[1].should eq(2)
|
||||
stmt.params[2].should eq("a")
|
||||
stmt.params[3].should eq([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,16 +90,43 @@ 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)
|
||||
end
|
||||
end
|
||||
|
||||
it "allows no arguments" do
|
||||
with_dummy_connection do |cnn|
|
||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||
stmt.exec
|
||||
stmt.params.should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "concatenate arguments" do
|
||||
with_dummy_connection do |cnn|
|
||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||
stmt.exec 1, 2, args: ["a", [1, nil]]
|
||||
stmt.params[0].should eq(1)
|
||||
stmt.params[1].should eq(2)
|
||||
stmt.params[2].should eq("a")
|
||||
stmt.params[3].should eq([1, nil])
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize positional params in scalar" do
|
||||
with_dummy_connection do |cnn|
|
||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue