mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
allow exec to receive any object arguments
This commit is contained in:
parent
d4b2046a65
commit
76e3a35d59
4 changed files with 34 additions and 12 deletions
|
@ -72,8 +72,8 @@ class FooDriver < DB::Driver
|
|||
GenericResultSet(Any).new(self, FooDriver.fake_row)
|
||||
end
|
||||
|
||||
protected def perform_exec(args : Slice(DB::Any)) : DB::ExecResult
|
||||
raise "Not implemented"
|
||||
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
||||
DB::ExecResult.new 0, 0i64
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -114,8 +114,8 @@ class BarDriver < DB::Driver
|
|||
GenericResultSet(Any).new(self, BarDriver.fake_row)
|
||||
end
|
||||
|
||||
protected def perform_exec(args : Slice(DB::Any)) : DB::ExecResult
|
||||
raise "Not implemented"
|
||||
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
||||
DB::ExecResult.new 0, 0i64
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -193,4 +193,26 @@ describe DB do
|
|||
db.query("query", [1, "string", BarValue.new(5)]).close
|
||||
end
|
||||
end
|
||||
|
||||
it "allow custom types to be used as arguments for exec" do
|
||||
DB.open("foo://host") do |db|
|
||||
FooDriver.fake_row = [1, "string"] of FooDriver::Any
|
||||
db.exec("query")
|
||||
db.exec("query", 1)
|
||||
db.exec("query", 1, "string")
|
||||
db.exec("query", Slice(UInt8).new(4))
|
||||
db.exec("query", 1, "string", FooValue.new(5))
|
||||
db.exec("query", [1, "string", FooValue.new(5)])
|
||||
end
|
||||
|
||||
DB.open("bar://host") do |db|
|
||||
BarDriver.fake_row = [1, "string"] of BarDriver::Any
|
||||
db.exec("query")
|
||||
db.exec("query", 1)
|
||||
db.exec("query", 1, "string")
|
||||
db.exec("query", Slice(UInt8).new(4))
|
||||
db.exec("query", 1, "string", BarValue.new(5))
|
||||
db.exec("query", [1, "string", BarValue.new(5)])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -47,7 +47,7 @@ class DummyDriver < DB::Driver
|
|||
DummyResultSet.new self, @query
|
||||
end
|
||||
|
||||
protected def perform_exec(args : Slice(DB::Any))
|
||||
protected def perform_exec(args : Enumerable)
|
||||
set_params args
|
||||
DB::ExecResult.new 0, 0_i64
|
||||
end
|
||||
|
|
|
@ -40,7 +40,7 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
# Performs the `query` discarding any response
|
||||
# Performs the `query` and returns an `ExecResult`
|
||||
def exec(query, *args)
|
||||
prepare(query).exec(*args)
|
||||
end
|
||||
|
|
|
@ -27,18 +27,18 @@ module DB
|
|||
|
||||
# See `QueryMethods#exec`
|
||||
def exec
|
||||
perform_exec_and_release(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn)
|
||||
perform_exec_and_release(Slice(Any).new(0))
|
||||
end
|
||||
|
||||
# See `QueryMethods#exec`
|
||||
def exec(args : Enumerable(Any))
|
||||
perform_exec_and_release(args.to_a.to_unsafe.to_slice(args.size))
|
||||
def exec(args : Array)
|
||||
perform_exec_and_release(args)
|
||||
end
|
||||
|
||||
# See `QueryMethods#exec`
|
||||
def exec(*args)
|
||||
# TODO better way to do it
|
||||
perform_exec_and_release(args.to_a.to_unsafe.to_slice(args.size))
|
||||
perform_exec_and_release(args)
|
||||
end
|
||||
|
||||
# See `QueryMethods#scalar`
|
||||
|
@ -96,13 +96,13 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
private def perform_exec_and_release(args : Slice(Any)) : ExecResult
|
||||
private def perform_exec_and_release(args : Enumerable) : ExecResult
|
||||
perform_exec(args).tap do
|
||||
release_connection
|
||||
end
|
||||
end
|
||||
|
||||
protected abstract def perform_query(args : Enumerable) : ResultSet
|
||||
protected abstract def perform_exec(args : Slice(Any)) : ExecResult
|
||||
protected abstract def perform_exec(args : Enumerable) : ExecResult
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue