column_count and column_names

close result_set and statements
main entry points for exec_non_query and exec_query closing them when ready
This commit is contained in:
Brian J. Cardiff 2016-01-29 21:57:00 -03:00
parent e50cc4ec73
commit caf2676aad
4 changed files with 56 additions and 0 deletions

View file

@ -40,6 +40,14 @@ class DummyDriver < DB::Driver
end
end
def column_count
2
end
def column_name(index)
"c#{index}"
end
private def read? : DB::Any?
n = @values.not_nil!.next
raise "end of row" if n.is_a?(Iterator::Stop)

View file

@ -9,12 +9,35 @@ module DB
@driver = @driver_class.new(@options)
end
# :nodoc:
def prepare(query)
@driver.prepare(query)
end
# :nodoc:
def exec(query, *args)
prepare(query).exec(*args)
end
def exec_non_query(query, *args)
exec_query(query) do |result_set|
result_set.move_next
end
end
# :nodoc:
def exec_query(query, *args)
result_set = exec(query, *args)
yield result_set
result_set.close
end
def exec_query_each(query, *args)
exec_query(query) do |result_set|
result_set.each do
yield result_set
end
end
end
end
end

View file

@ -11,8 +11,17 @@ module DB
end
end
def close
@statement.close
end
abstract def move_next : Bool
abstract def column_count : Int32
abstract def column_name(index : Int32) : String
# abstract def column_type(index : Int32)
# list datatypes that must be supported form the driver
# users will call read(String) or read?(String) for nillables
{% for t in DB::TYPES %}

View file

@ -3,6 +3,7 @@ module DB
getter driver
def initialize(@driver)
@closed = false
end
def exec(*args) : ResultSet
@ -32,9 +33,24 @@ module DB
protected def before_execute
end
# Closes this statement.
def close
raise "Statement already closed" if @closed
@closed = true
on_close
end
# Returns `true` if this statement is closed. See `#close`.
def closed?
@closed
end
# 1-based positional arguments
protected abstract def add_parameter(index : Int32, value)
protected abstract def add_parameter(name : String, value)
protected abstract def execute : ResultSet
protected def on_close
end
end
end