mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
leave a single scalar value returns DB::Any
This commit is contained in:
parent
d01da912f7
commit
67fe5c9aae
5 changed files with 39 additions and 84 deletions
|
@ -36,6 +36,8 @@ class DummyDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyResultSet < DB::ResultSet
|
class DummyResultSet < DB::ResultSet
|
||||||
|
@@next_column_type = String
|
||||||
|
|
||||||
def initialize(statement, query)
|
def initialize(statement, query)
|
||||||
super(statement)
|
super(statement)
|
||||||
@iterator = query.split.map { |r| r.split(',') }.to_a.each
|
@iterator = query.split.map { |r| r.split(',') }.to_a.each
|
||||||
|
@ -70,7 +72,11 @@ class DummyDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
def column_type(index : Int32)
|
def column_type(index : Int32)
|
||||||
String
|
@@next_column_type
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.next_column_type=(value)
|
||||||
|
@@next_column_type = value
|
||||||
end
|
end
|
||||||
|
|
||||||
private def read? : DB::Any?
|
private def read? : DB::Any?
|
||||||
|
|
|
@ -110,32 +110,25 @@ describe DummyDriver do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should get Int32 scalars by default" do
|
it "should get Nil scalars" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
db.scalar("1").should be_a(Int32)
|
DummyDriver::DummyResultSet.next_column_type = Nil
|
||||||
db.scalar?("1").should be_a(Int32)
|
db.scalar("NULL").should be_nil
|
||||||
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")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
{% 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
|
it "numeric scalars of type of {{value.id}} should return value or nil" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
db.scalar(typeof({{value}}), "#{{{value}}}").should eq({{value}})
|
DummyDriver::DummyResultSet.next_column_type = typeof({{value}})
|
||||||
db.scalar?(typeof({{value}}), "#{{{value}}}").should eq({{value}})
|
db.scalar("#{{{value}}}").should eq({{value}})
|
||||||
db.scalar?(typeof({{value}}), "NULL").should be_nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should set positional arguments for {{value.id}}" do
|
it "should set positional arguments for {{value.id}}" do
|
||||||
with_dummy do |db|
|
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
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
@ -144,7 +137,8 @@ describe DummyDriver do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
ary = UInt8[0x53, 0x51, 0x4C]
|
ary = UInt8[0x53, 0x51, 0x4C]
|
||||||
slice = Slice.new(ary.to_unsafe, ary.size)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,17 +32,7 @@ describe DB::Statement do
|
||||||
it "should initialize positional params in scalar" do
|
it "should initialize positional params in scalar" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
stmt = db.prepare("the query")
|
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)
|
|
||||||
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.params[0].should eq("a")
|
stmt.params[0].should eq("a")
|
||||||
stmt.params[1].should eq(1)
|
stmt.params[1].should eq(1)
|
||||||
stmt.params[2].should eq(nil)
|
stmt.params[2].should eq(nil)
|
||||||
|
@ -83,14 +73,6 @@ describe DB::Statement do
|
||||||
end
|
end
|
||||||
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
|
it "exec should not close statement" do
|
||||||
with_dummy do |db|
|
with_dummy do |db|
|
||||||
stmt = db.prepare "3,4 1,2"
|
stmt = db.prepare "3,4 1,2"
|
||||||
|
|
|
@ -40,36 +40,12 @@ module DB
|
||||||
prepare(query).exec(*args)
|
prepare(query).exec(*args)
|
||||||
end
|
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)
|
def scalar(query, *args)
|
||||||
prepare(query).scalar(*args)
|
prepare(query).scalar(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO remove scalar? make it always nillable. raise if 0-rows raise +1-rows
|
# TODO add query_row
|
||||||
|
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,30 +26,27 @@ module DB
|
||||||
|
|
||||||
# See `QueryMethods#scalar`
|
# See `QueryMethods#scalar`
|
||||||
def scalar(*args)
|
def scalar(*args)
|
||||||
scalar(Int32, *args)
|
|
||||||
end
|
|
||||||
|
|
||||||
# See `QueryMethods#scalar`. `t` must be in DB::TYPES
|
|
||||||
def scalar(t, *args)
|
|
||||||
query(*args) do |rs|
|
query(*args) do |rs|
|
||||||
rs.each do
|
rs.each do
|
||||||
return rs.read(t)
|
# return case rs.read?(rs.column_type(0)) # :-( Some day...
|
||||||
end
|
t = rs.column_type(0)
|
||||||
end
|
if t == String
|
||||||
|
return rs.read?(String)
|
||||||
raise "no results"
|
elsif t == Int32
|
||||||
end
|
return rs.read?(Int32)
|
||||||
|
elsif t == Int64
|
||||||
# See `QueryMethods#scalar?`
|
return rs.read?(Int64)
|
||||||
def scalar?(*args)
|
elsif t == Float32
|
||||||
scalar?(Int32, *args)
|
return rs.read?(Float32)
|
||||||
end
|
elsif t == Float64
|
||||||
|
return rs.read?(Float64)
|
||||||
# See `QueryMethods#scalar?`. `t` must be in DB::TYPES
|
elsif t == Slice(UInt8)
|
||||||
def scalar?(t, *args)
|
return rs.read?(Slice(UInt8))
|
||||||
query(*args) do |rs|
|
elsif t == Nil
|
||||||
rs.each do
|
return rs.read?(Int32)
|
||||||
return rs.read?(t)
|
else
|
||||||
|
raise "not implemented for #{t} type"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue