mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Concatenate arguments
This commit is contained in:
parent
15d0e57eb0
commit
8e64eb82c1
4 changed files with 79 additions and 2 deletions
|
@ -51,6 +51,25 @@ describe DB::Statement do
|
|||
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)
|
||||
|
@ -89,6 +108,25 @@ describe DB::Statement do
|
|||
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)
|
||||
|
|
|
@ -181,6 +181,7 @@ end
|
|||
|
||||
require "./db/pool"
|
||||
require "./db/string_key_cache"
|
||||
require "./db/enumerable_concat"
|
||||
require "./db/query_methods"
|
||||
require "./db/session_methods"
|
||||
require "./db/disposable"
|
||||
|
|
38
src/db/enumerable_concat.cr
Normal file
38
src/db/enumerable_concat.cr
Normal file
|
@ -0,0 +1,38 @@
|
|||
module DB
|
||||
# :nodoc:
|
||||
struct EnumerableConcat(S, T, U)
|
||||
include Enumerable(S)
|
||||
|
||||
def initialize(@e1 : T, @e2 : U)
|
||||
end
|
||||
|
||||
def each
|
||||
if e1 = @e1
|
||||
@e1.each do |e|
|
||||
yield e
|
||||
end
|
||||
end
|
||||
if e2 = @e2
|
||||
e2.each do |e|
|
||||
yield e
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# returns given `e1 : T` an `Enumerable(T')` and `e2 : U` an `Enumerable(U') | Nil`
|
||||
# it retuns and `Enumerable(T' | U')` that enumerates the elements of `e1`
|
||||
# and, later, the elements of `e2`.
|
||||
def self.build(e1 : T, e2 : U)
|
||||
return e1 if e2.nil? || e2.empty?
|
||||
return e2 if e1.nil? || e1.empty?
|
||||
EnumerableConcat(Union(typeof(sample(e1)), typeof(sample(e2))), T, U).new(e1, e2)
|
||||
end
|
||||
|
||||
private def self.sample(c : Enumerable?)
|
||||
c.not_nil!.each do |e|
|
||||
return e
|
||||
end
|
||||
raise ""
|
||||
end
|
||||
end
|
||||
end
|
|
@ -65,7 +65,7 @@ module DB
|
|||
|
||||
# See `QueryMethods#exec`
|
||||
def exec(*t_args, args : Array? = nil) : DB::ExecResult
|
||||
perform_exec_and_release(args || t_args)
|
||||
perform_exec_and_release(EnumerableConcat.build(t_args, args))
|
||||
end
|
||||
|
||||
# See `QueryMethods#query`
|
||||
|
@ -75,7 +75,7 @@ module DB
|
|||
|
||||
# See `QueryMethods#query`
|
||||
def query(*t_args, args : Array? = nil) : DB::ResultSet
|
||||
perform_query_with_rescue(args || t_args)
|
||||
perform_query_with_rescue(EnumerableConcat.build(t_args, args))
|
||||
end
|
||||
|
||||
private def perform_exec_and_release(args : Enumerable) : ExecResult
|
||||
|
|
Loading…
Reference in a new issue