allow query to receive any object arguments

This commit is contained in:
Brian J. Cardiff 2016-04-14 17:33:39 -03:00
parent 5b23114095
commit 3464a1d1cc
3 changed files with 56 additions and 16 deletions

View file

@ -68,7 +68,7 @@ class FooDriver < DB::Driver
end end
class FooStatement < DB::Statement class FooStatement < DB::Statement
protected def perform_query(args : Slice(DB::Any)) : DB::ResultSet protected def perform_query(args : Enumerable) : DB::ResultSet
GenericResultSet(Any).new(self, FooDriver.fake_row) GenericResultSet(Any).new(self, FooDriver.fake_row)
end end
@ -110,7 +110,7 @@ class BarDriver < DB::Driver
end end
class BarStatement < DB::Statement class BarStatement < DB::Statement
protected def perform_query(args : Slice(DB::Any)) : DB::ResultSet protected def perform_query(args : Enumerable) : DB::ResultSet
GenericResultSet(Any).new(self, BarDriver.fake_row) GenericResultSet(Any).new(self, BarDriver.fake_row)
end end
@ -157,4 +157,26 @@ describe DB do
end end
end end
end end
it "allow custom types to be used as arguments for query" do
DB.open("foo://host") do |db|
FooDriver.fake_row = [1, "string"] of FooDriver::Any
db.query "query" { }
db.query "query", 1 { }
db.query "query", 1, "string" { }
db.query("query", Slice(UInt8).new(4)) { }
db.query("query", 1, "string", FooValue.new(5)) { }
db.query "query", [1, "string", FooValue.new(5)] { }
end
DB.open("bar://host") do |db|
BarDriver.fake_row = [1, "string"] of BarDriver::Any
db.query "query" { }
db.query "query", 1 { }
db.query "query", 1, "string" { }
db.query("query", Slice(UInt8).new(4)) { }
db.query("query", 1, "string", BarValue.new(5)) { }
db.query "query", [1, "string", FooValue.new(5)] { }
end
end
end end

View file

@ -42,7 +42,7 @@ class DummyDriver < DB::Driver
super(connection) super(connection)
end end
protected def perform_query(args : Slice(DB::Any)) protected def perform_query(args : Enumerable)
set_params args set_params args
DummyResultSet.new self, @query DummyResultSet.new self, @query
end end

View file

@ -71,13 +71,13 @@ module DB
end end
# See `QueryMethods#query` # See `QueryMethods#query`
def query(*args) def query
perform_query *args perform_query Slice(Any).new(0)
end end
# See `QueryMethods#query` # See `QueryMethods#query`
def query(*args) def query
perform_query(*args).tap do |rs| perform_query(Slice(Any).new(0)).tap do |rs|
begin begin
yield rs yield rs
ensure ensure
@ -86,18 +86,36 @@ module DB
end end
end end
private def perform_query : ResultSet # See `QueryMethods#query`
perform_query(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn) def query(args : Array)
perform_query args
end end
private def perform_query(args : Enumerable(Any)) : ResultSet # See `QueryMethods#query`
# TODO better way to do it def query(args : Array)
perform_query(args.to_a.to_unsafe.to_slice(args.size)) perform_query(args).tap do |rs|
begin
yield rs
ensure
rs.close
end
end
end end
private def perform_query(*args) : ResultSet # See `QueryMethods#query`
# TODO better way to do it def query(*args)
perform_query(args.to_a.to_unsafe.to_slice(args.size)) perform_query args
end
# See `QueryMethods#query`
def query(*args)
perform_query(args).tap do |rs|
begin
yield rs
ensure
rs.close
end
end
end end
private def perform_exec_and_release(args : Slice(Any)) : ExecResult private def perform_exec_and_release(args : Slice(Any)) : ExecResult
@ -106,7 +124,7 @@ module DB
end end
end end
protected abstract def perform_query(args : Slice(Any)) : ResultSet protected abstract def perform_query(args : Enumerable) : ResultSet
protected abstract def perform_exec(args : Slice(Any)) : ExecResult protected abstract def perform_exec(args : Slice(Any)) : ExecResult
end end
end end