leave a single scalar value returns DB::Any

This commit is contained in:
Brian J. Cardiff 2016-02-01 22:33:58 -03:00
parent d01da912f7
commit 67fe5c9aae
5 changed files with 39 additions and 84 deletions

View File

@ -36,6 +36,8 @@ class DummyDriver < DB::Driver
end
class DummyResultSet < DB::ResultSet
@@next_column_type = String
def initialize(statement, query)
super(statement)
@iterator = query.split.map { |r| r.split(',') }.to_a.each
@ -70,7 +72,11 @@ class DummyDriver < DB::Driver
end
def column_type(index : Int32)
String
@@next_column_type
end
def self.next_column_type=(value)
@@next_column_type = value
end
private def read? : DB::Any?

View File

@ -110,32 +110,25 @@ describe DummyDriver do
end
end
it "should get Int32 scalars by default" do
it "should get Nil scalars" do
with_dummy do |db|
db.scalar("1").should be_a(Int32)
db.scalar?("1").should be_a(Int32)
db.scalar?("NULL").should be_nil
end
end
it "should get String scalars" do
with_dummy do |db|
db.scalar(String, "foo").should eq("foo")
DummyDriver::DummyResultSet.next_column_type = Nil
db.scalar("NULL").should be_nil
end
end
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
it "numeric scalars of type of {{value.id}} should return value or nil" do
with_dummy do |db|
db.scalar(typeof({{value}}), "#{{{value}}}").should eq({{value}})
db.scalar?(typeof({{value}}), "#{{{value}}}").should eq({{value}})
db.scalar?(typeof({{value}}), "NULL").should be_nil
DummyDriver::DummyResultSet.next_column_type = typeof({{value}})
db.scalar("#{{{value}}}").should eq({{value}})
end
end
it "should set positional arguments for {{value.id}}" do
with_dummy do |db|
db.scalar(typeof({{value}}), "?", {{value}}).should eq({{value}})
DummyDriver::DummyResultSet.next_column_type = typeof({{value}})
db.scalar("?", {{value}}).should eq({{value}})
end
end
{% end %}
@ -144,7 +137,8 @@ describe DummyDriver do
with_dummy do |db|
ary = UInt8[0x53, 0x51, 0x4C]
slice = Slice.new(ary.to_unsafe, ary.size)
db.scalar(Slice(UInt8), "?", slice).to_a.should eq(ary)
DummyDriver::DummyResultSet.next_column_type = typeof(slice)
(db.scalar("?", slice) as Slice(UInt8)).to_a.should eq(ary)
end
end
end

View File

@ -32,17 +32,7 @@ describe DB::Statement do
it "should initialize positional params in scalar" do
with_dummy do |db|
stmt = db.prepare("the query")
stmt.scalar String, "a", 1, nil
stmt.params[0].should eq("a")
stmt.params[1].should eq(1)
stmt.params[2].should eq(nil)
end
end
it "should initialize positional params in scalar?" do
with_dummy do |db|
stmt = db.prepare("the query")
stmt.scalar? String, "a", 1, nil
stmt.scalar "a", 1, nil
stmt.params[0].should eq("a")
stmt.params[1].should eq(1)
stmt.params[2].should eq(nil)
@ -83,14 +73,6 @@ describe DB::Statement do
end
end
it "scalar should not close statement" do
with_dummy do |db|
stmt = db.prepare "3,4 1,2"
stmt.scalar?
stmt.closed?.should be_false
end
end
it "exec should not close statement" do
with_dummy do |db|
stmt = db.prepare "3,4 1,2"

View File

@ -40,36 +40,12 @@ module DB
prepare(query).exec(*args)
end
# Performs the `query` and returns a single scalar `Int32` value
# Performs the `query` and returns a single scalar `DB::Any` value
# puts db.scalar("SELECT MAX(name)") as String # => (a String)
def scalar(query, *args)
prepare(query).scalar(*args)
end
# TODO remove scalar? make it always nillable. raise if 0-rows raise +1-rows
# Performs the `query` and returns a single scalar value of type `t`.
# `t` must be any of the allowed `DB::Any` types.
#
# ```
# puts db.scalar(String, "SELECT MAX(name)") # => (a String)
# ```
def scalar(t, query, *args)
prepare(query).scalar(t, *args)
end
# Performs the `query` and returns a single scalar `Int32 | Nil` value
def scalar?(query, *args)
prepare(query).scalar?(*args)
end
# Performs the `query` and returns a single scalar value of type `t` or `Nil`.
# `t` must be any of the allowed `DB::Any` types.
#
# ```
# puts db.scalar?(String, "SELECT MAX(name)") # => (a String | Nil)
# ```
def scalar?(t, query, *args)
prepare(query).scalar?(t, *args)
end
# TODO add query_row
end
end

View File

@ -26,30 +26,27 @@ module DB
# See `QueryMethods#scalar`
def scalar(*args)
scalar(Int32, *args)
end
# See `QueryMethods#scalar`. `t` must be in DB::TYPES
def scalar(t, *args)
query(*args) do |rs|
rs.each do
return rs.read(t)
end
end
raise "no results"
end
# See `QueryMethods#scalar?`
def scalar?(*args)
scalar?(Int32, *args)
end
# See `QueryMethods#scalar?`. `t` must be in DB::TYPES
def scalar?(t, *args)
query(*args) do |rs|
rs.each do
return rs.read?(t)
# return case rs.read?(rs.column_type(0)) # :-( Some day...
t = rs.column_type(0)
if t == String
return rs.read?(String)
elsif t == Int32
return rs.read?(Int32)
elsif t == Int64
return rs.read?(Int64)
elsif t == Float32
return rs.read?(Float32)
elsif t == Float64
return rs.read?(Float64)
elsif t == Slice(UInt8)
return rs.read?(Slice(UInt8))
elsif t == Nil
return rs.read?(Int32)
else
raise "not implemented for #{t} type"
end
end
end