code review notes

close / do_close in result_set
avoid closing statements
remove named arguments
refactor positioned arguments query
This commit is contained in:
Brian J. Cardiff 2016-02-01 21:55:30 -03:00
parent fd804dd592
commit d01da912f7
10 changed files with 86 additions and 189 deletions

View file

@ -15,15 +15,17 @@ module DB
# Also override `#last_insert_id` to allow safe access to the last inserted id through this connection.
#
abstract class Connection
getter connection_string
# TODO add IDLE status, for connection ppool management.
def initialize(@connection_string)
@closed = false
getter connection_string # TODO Remove
@closed = false
def initialize(@connection_string) # TODO Remove
end
# Closes this connection.
def close
raise "Connection already closed" if @closed
raise "Connection already closed" if @closed # TODO make it no fail if closed
@closed = true
perform_close
end
@ -44,8 +46,9 @@ module DB
include QueryMethods
# Returns the last inserted id through this connection.
abstract def last_insert_id : Int64
abstract def last_insert_id : Int64 # TODO move to ExecResult record. plano. with last_rows. eagerly askit.
protected abstract def perform_close
protected abstract def perform_close # TODO do_close
end
end

View file

@ -24,8 +24,7 @@ module DB
@connection.close
end
# Returns a `Connection` to the database.
# Useful if you need to ensure the statements are executed in the connection.
# :nodoc:
def connection
@connection
end

View file

@ -64,10 +64,11 @@ module DB
# method to be used as query parameters
TYPES = [String, Int32, Int64, Float32, Float64, Slice(UInt8)]
# See `DB::TYPES` in `DB`
alias Any = String | Int32 | Int64 | Float32 | Float64 | Slice(UInt8)
# See `DB::TYPES` in `DB`. `Any` is a nillable version of the union of all types in `DB::TYPES`
alias Any = Nil | String | Int32 | Int64 | Float32 | Float64 | Slice(UInt8)
# :nodoc:
def self.driver_class(driver_name) # : Driver.class
@@drivers.not_nil![driver_name]
end

View file

@ -45,6 +45,8 @@ module DB
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.
#

View file

@ -13,12 +13,13 @@ module DB
# 4. Override `#column_count`, `#column_name`.
# 5. Override `#column_type`. It must return a type in `DB::TYPES`.
abstract class ResultSet
# :nodoc:
getter statement
def initialize(@statement : Statement)
end
# TODO add_next_result_set : Bool
# Iterates over all the rows
def each
while move_next
@ -26,9 +27,24 @@ module DB
end
end
# Closes the result set.
# Closes this result set.
def close
@statement.close
return if @closed
@closed = true
do_close
end
# Returns `true` if this result set is closed. See `#close`.
def closed?
@closed
end
# :nodoc:
def finalize
close
end
protected def do_close
end
# Ensures it executes the query
@ -65,5 +81,13 @@ module DB
read?({{t}}).not_nil!
end
{% end %}
# def read_blob
# yield ... io ....
# end
# def read_text
# yield ... io ....
# end
end
end

View file

@ -72,35 +72,20 @@ module DB
end
end
private def execute : ResultSet
perform(Slice(Any).new(0))
end
private def execute(*args) : ResultSet
execute args
end
private def execute(arg : Slice(UInt8))
begin_parameters
add_parameter 0, arg
perform
end
private def execute(args : Enumerable)
begin_parameters
args.each_with_index do |arg, index|
if arg.is_a?(Hash)
arg.each do |key, value|
add_parameter key.to_s, value
end
else
add_parameter index, arg
end
end
perform
# TODO better way to do it
perform(args.to_a.to_unsafe.to_slice(args.size))
end
# Closes this statement.
def close
raise "Statement already closed" if @closed
return if @closed # make it work if closed
@closed = true
on_close
do_close
end
# Returns `true` if this statement is closed. See `#close`.
@ -108,19 +93,14 @@ module DB
@closed
end
# # :nodoc:
# def finalize
# close unless closed?
# end
# 0-based positional arguments
protected def begin_parameters
# :nodoc:
def finalize
close
end
protected abstract def add_parameter(index : Int32, value)
protected abstract def add_parameter(name : String, value)
protected abstract def perform : ResultSet
protected def on_close
protected abstract def perform(args : Slice(Any)) : ResultSet
protected def do_close
end
end
end