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"