From b2da4f3f6efe4cdc9ee72f8f158a9bdadf7312cc Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Thu, 23 Jun 2016 22:19:53 -0300 Subject: [PATCH] allow scalar to return custom types --- spec/custom_drivers_types_spec.cr | 7 +++++++ src/db/query_methods.cr | 2 +- src/db/statement.cr | 20 +------------------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/spec/custom_drivers_types_spec.cr b/spec/custom_drivers_types_spec.cr index 2929d0f..7cbe924 100644 --- a/spec/custom_drivers_types_spec.cr +++ b/spec/custom_drivers_types_spec.cr @@ -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| diff --git a/src/db/query_methods.cr b/src/db/query_methods.cr index 517eee9..ba71a7f 100644 --- a/src/db/query_methods.cr +++ b/src/db/query_methods.cr @@ -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 diff --git a/src/db/statement.cr b/src/db/statement.cr index 0991019..b919052 100644 --- a/src/db/statement.cr +++ b/src/db/statement.cr @@ -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