allow exec to receive any object arguments

This commit is contained in:
Brian J. Cardiff 2016-04-15 01:40:27 -03:00
parent d4b2046a65
commit 76e3a35d59
4 changed files with 34 additions and 12 deletions

View file

@ -72,8 +72,8 @@ class FooDriver < DB::Driver
GenericResultSet(Any).new(self, FooDriver.fake_row) GenericResultSet(Any).new(self, FooDriver.fake_row)
end end
protected def perform_exec(args : Slice(DB::Any)) : DB::ExecResult protected def perform_exec(args : Enumerable) : DB::ExecResult
raise "Not implemented" DB::ExecResult.new 0, 0i64
end end
end end
end end
@ -114,8 +114,8 @@ class BarDriver < DB::Driver
GenericResultSet(Any).new(self, BarDriver.fake_row) GenericResultSet(Any).new(self, BarDriver.fake_row)
end end
protected def perform_exec(args : Slice(DB::Any)) : DB::ExecResult protected def perform_exec(args : Enumerable) : DB::ExecResult
raise "Not implemented" DB::ExecResult.new 0, 0i64
end end
end end
end end
@ -193,4 +193,26 @@ describe DB do
db.query("query", [1, "string", BarValue.new(5)]).close db.query("query", [1, "string", BarValue.new(5)]).close
end end
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 end

View file

@ -47,7 +47,7 @@ class DummyDriver < DB::Driver
DummyResultSet.new self, @query DummyResultSet.new self, @query
end end
protected def perform_exec(args : Slice(DB::Any)) protected def perform_exec(args : Enumerable)
set_params args set_params args
DB::ExecResult.new 0, 0_i64 DB::ExecResult.new 0, 0_i64
end end

View file

@ -40,7 +40,7 @@ module DB
end end
end end
# Performs the `query` discarding any response # Performs the `query` and returns an `ExecResult`
def exec(query, *args) def exec(query, *args)
prepare(query).exec(*args) prepare(query).exec(*args)
end end

View file

@ -27,18 +27,18 @@ module DB
# See `QueryMethods#exec` # See `QueryMethods#exec`
def 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 end
# See `QueryMethods#exec` # See `QueryMethods#exec`
def exec(args : Enumerable(Any)) def exec(args : Array)
perform_exec_and_release(args.to_a.to_unsafe.to_slice(args.size)) perform_exec_and_release(args)
end end
# See `QueryMethods#exec` # See `QueryMethods#exec`
def exec(*args) def exec(*args)
# TODO better way to do it # 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 end
# See `QueryMethods#scalar` # See `QueryMethods#scalar`
@ -96,13 +96,13 @@ module DB
end end
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 perform_exec(args).tap do
release_connection release_connection
end end
end end
protected abstract def perform_query(args : Enumerable) : ResultSet 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
end end