allow scalar to return custom types

This commit is contained in:
Brian J. Cardiff 2016-06-23 22:19:53 -03:00
parent 34ae9d5775
commit b2da4f3f6e
3 changed files with 9 additions and 20 deletions

View file

@ -197,6 +197,13 @@ describe DB do
end
end
it "drivers should return custom values as scalar" do
DB.open("foo://host") do |db|
FooDriver.fake_row = [FooValue.new(3), FooValue.new(99)] of FooDriver::Any
db.scalar("query").as(FooValue).value.should eq(3)
end
end
it "Foo and Bar drivers should not implement each other read" do
with_witness do |w|
DB.open("foo://host") do |db|

View file

@ -4,7 +4,7 @@ module DB
#
# Three kind of statements can be performed:
# 1. `#exec` waits no record response from the database. An `ExecResult` is returned.
# 2. `#scalar` reads a single value of the response. A `DB::Any` is returned.
# 2. `#scalar` reads a single value of the response. A union of possible values is returned.
# 3. `#query` returns a `ResultSet` that allows iteration over the rows in the response and column information.
#
# Arguments can be passed by position

View file

@ -45,25 +45,7 @@ module DB
def scalar(*args)
query(*args) do |rs|
rs.each do
# return case rs.read?(rs.column_type(0)) # :-( Some day...
case rs.column_type(0)
when String.class
return rs.read?(String)
when Int32.class
return rs.read?(Int32)
when Int64.class
return rs.read?(Int64)
when Float32.class
return rs.read?(Float32)
when Float64.class
return rs.read?(Float64)
when Bytes.class
return rs.read?(Bytes)
when Nil.class
return rs.read?(Int32)
else
raise "not implemented for #{rs.column_type(0)} type"
end
return rs.read?(rs.column_type(0))
end
end