2016-01-28 23:31:35 +00:00
|
|
|
class DummyDriver < DB::Driver
|
|
|
|
def prepare(query)
|
|
|
|
DummyStatement.new(self, query.split.map { |r| r.split ',' })
|
|
|
|
end
|
|
|
|
|
|
|
|
class DummyStatement < DB::Statement
|
2016-01-29 22:21:48 +00:00
|
|
|
property! params
|
|
|
|
|
2016-01-28 23:31:35 +00:00
|
|
|
def initialize(driver, @items)
|
|
|
|
super(driver)
|
|
|
|
end
|
|
|
|
|
2016-01-29 20:13:05 +00:00
|
|
|
protected def add_parameter(index : Int32, value)
|
2016-01-29 22:21:48 +00:00
|
|
|
params[index] = value
|
2016-01-29 20:13:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected def add_parameter(name : String, value)
|
2016-01-29 22:21:48 +00:00
|
|
|
params[":#{name}"] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
protected def before_execute
|
|
|
|
@params = Hash(Int32 | String, DB::Any).new
|
2016-01-29 20:13:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected def execute
|
2016-01-28 23:31:35 +00:00
|
|
|
DummyResultSet.new self, @items.each
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DummyResultSet < DB::ResultSet
|
|
|
|
def initialize(statement, @iterator)
|
|
|
|
super(statement)
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:13:01 +00:00
|
|
|
def move_next
|
2016-01-28 23:31:35 +00:00
|
|
|
@iterator.next.tap do |n|
|
|
|
|
return false if n.is_a?(Iterator::Stop)
|
|
|
|
@values = n.each
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 00:57:00 +00:00
|
|
|
def column_count
|
|
|
|
2
|
|
|
|
end
|
|
|
|
|
|
|
|
def column_name(index)
|
|
|
|
"c#{index}"
|
|
|
|
end
|
|
|
|
|
2016-01-29 22:21:48 +00:00
|
|
|
private def read? : DB::Any?
|
2016-01-29 19:13:01 +00:00
|
|
|
n = @values.not_nil!.next
|
|
|
|
raise "end of row" if n.is_a?(Iterator::Stop)
|
|
|
|
return nil if n == "NULL"
|
2016-01-29 22:21:48 +00:00
|
|
|
|
|
|
|
if n == "?"
|
|
|
|
return @statement.params[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if n.starts_with?(":")
|
|
|
|
return @statement.params[n]
|
|
|
|
end
|
|
|
|
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : String.class)
|
|
|
|
read?.try &.to_s
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
2016-01-29 19:13:01 +00:00
|
|
|
def read?(t : Int32.class)
|
|
|
|
read?(String).try &.to_i32
|
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : Int64.class)
|
|
|
|
read?(String).try &.to_i64
|
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : Float32.class)
|
2016-01-29 22:21:48 +00:00
|
|
|
read?(String).try &.to_f32
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : Float64.class)
|
|
|
|
read?(String).try &.to_f64
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
2016-01-29 22:21:48 +00:00
|
|
|
|
|
|
|
def read?(t : Slice(UInt8).class)
|
|
|
|
value = read?
|
|
|
|
if value.is_a?(Nil)
|
|
|
|
value
|
|
|
|
elsif value.is_a?(String)
|
|
|
|
ary = value.bytes
|
|
|
|
Slice.new(ary.to_unsafe, ary.size)
|
|
|
|
else
|
|
|
|
value as Slice(UInt8)
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DB.register_driver "dummy", DummyDriver
|
|
|
|
|
|
|
|
def get_dummy
|
2016-01-29 19:13:01 +00:00
|
|
|
DB.open "dummy", {} of String => String
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|