Deal with unexpected ResultSet#read types during runtime.

This commit is contained in:
Brian J. Cardiff 2017-04-06 15:28:20 -03:00
parent 545d758ae0
commit bb3203301b
2 changed files with 8 additions and 3 deletions

View file

@ -197,7 +197,7 @@ describe DB do
FooDriver.fake_row = [1] of FooDriver::Any FooDriver.fake_row = [1] of FooDriver::Any
db.query "query" do |rs| db.query "query" do |rs|
rs.move_next rs.move_next
expect_raises(TypeCastError) do expect_raises(Exception, "FooResultSet#read returned a Int32. A BarValue was expected.") do
w.check w.check
rs.read(BarValue) rs.read(BarValue)
end end
@ -210,7 +210,7 @@ describe DB do
BarDriver.fake_row = [1] of BarDriver::Any BarDriver.fake_row = [1] of BarDriver::Any
db.query "query" do |rs| db.query "query" do |rs|
rs.move_next rs.move_next
expect_raises(TypeCastError) do expect_raises(Exception, "BarResultSet#read returned a Int32. A FooValue was expected.") do
w.check w.check
rs.read(FooValue) rs.read(FooValue)
end end

View file

@ -76,7 +76,12 @@ module DB
# Reads the next column value as a **type** # Reads the next column value as a **type**
def read(type : T.class) : T forall T def read(type : T.class) : T forall T
read.as(T) value = read
if value.is_a?(T)
value
else
raise "#{self.class}#read returned a #{value.class}. A #{T} was expected."
end
end end
# Reads the next columns and returns a tuple of the values. # Reads the next columns and returns a tuple of the values.