#5 Make sure queries (insert and query) work with any type

This commit is contained in:
Brian J. Cardiff 2016-06-22 00:44:16 -03:00
parent c5d7778a0d
commit 11e24e1c65
2 changed files with 42 additions and 2 deletions

View file

@ -66,12 +66,21 @@ class FooDriver < DB::Driver
class FooStatement < DB::Statement class FooStatement < DB::Statement
protected def perform_query(args : Enumerable) : DB::ResultSet protected def perform_query(args : Enumerable) : DB::ResultSet
args.each { |arg| process_arg arg }
FooResultSet.new(self, FooDriver.fake_row) FooResultSet.new(self, FooDriver.fake_row)
end end
protected def perform_exec(args : Enumerable) : DB::ExecResult protected def perform_exec(args : Enumerable) : DB::ExecResult
args.each { |arg| process_arg arg }
DB::ExecResult.new 0i64, 0i64 DB::ExecResult.new 0i64, 0i64
end end
private def process_arg(value : FooDriver::Any)
end
private def process_arg(value)
raise "#{self.class} does not support #{value.class} params"
end
end end
class FooResultSet < DB::ResultSet class FooResultSet < DB::ResultSet
@ -120,12 +129,21 @@ class BarDriver < DB::Driver
class BarStatement < DB::Statement class BarStatement < DB::Statement
protected def perform_query(args : Enumerable) : DB::ResultSet protected def perform_query(args : Enumerable) : DB::ResultSet
args.each { |arg| process_arg arg }
BarResultSet.new(self, BarDriver.fake_row) BarResultSet.new(self, BarDriver.fake_row)
end end
protected def perform_exec(args : Enumerable) : DB::ExecResult protected def perform_exec(args : Enumerable) : DB::ExecResult
args.each { |arg| process_arg arg }
DB::ExecResult.new 0i64, 0i64 DB::ExecResult.new 0i64, 0i64
end end
private def process_arg(value : BarDriver::Any)
end
private def process_arg(value)
raise "#{self.class} does not support #{value.class} params"
end
end end
class BarResultSet < DB::ResultSet class BarResultSet < DB::ResultSet
@ -232,7 +250,7 @@ describe DB do
db.query "query", 1, "string" { } db.query "query", 1, "string" { }
db.query("query", Slice(UInt8).new(4)) { } db.query("query", Slice(UInt8).new(4)) { }
db.query("query", 1, "string", BarValue.new(5)) { } db.query("query", 1, "string", BarValue.new(5)) { }
db.query "query", [1, "string", FooValue.new(5)] { } db.query "query", [1, "string", BarValue.new(5)] { }
db.query("query").close db.query("query").close
db.query("query", 1).close db.query("query", 1).close
@ -264,4 +282,18 @@ describe DB do
db.exec("query", [1, "string", BarValue.new(5)]) db.exec("query", [1, "string", BarValue.new(5)])
end end
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)])
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)])
end
end
end
end end

View file

@ -55,10 +55,18 @@ class DummyDriver < DB::Driver
private def set_params(args) private def set_params(args)
@params.clear @params.clear
args.each_with_index do |arg, index| args.each_with_index do |arg, index|
@params[index] = arg.as(DB::Any) set_param(index, arg)
end end
end end
private def set_param(index, value : DB::Any)
@params[index] = value
end
private def set_param(index, value)
raise "not implemented for #{value.class}"
end
protected def do_close protected def do_close
super super
end end