From 928c1517dc0e31354a733905f935e938cdef587e Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Fri, 19 Feb 2016 18:42:30 -0300 Subject: [PATCH] update to 0.12 features clarify expected protocol with ResultSet in docs minor refactors suggested --- src/db/connection.cr | 5 +---- src/db/db.cr | 2 +- src/db/driver.cr | 3 --- src/db/result_set.cr | 8 ++++++++ src/db/statement.cr | 18 +++++++++--------- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/db/connection.cr b/src/db/connection.cr index 5980cb9..ddd61a9 100644 --- a/src/db/connection.cr +++ b/src/db/connection.cr @@ -28,10 +28,7 @@ module DB # :nodoc: def prepare(query) : Statement stmt = @statements_cache.fetch(query, nil) - if stmt.is_a?(Nil) - stmt = build_statement(query) - @statements_cache[query] = stmt - end + stmt = @statements_cache[query] = build_statement(query) unless stmt stmt end diff --git a/src/db/db.cr b/src/db/db.cr index abbdf2c..149e367 100644 --- a/src/db/db.cr +++ b/src/db/db.cr @@ -79,7 +79,7 @@ module DB record ExecResult, rows_affected, last_insert_id # :nodoc: - def self.driver_class(driver_name) # : Driver.class + def self.driver_class(driver_name) : Driver.class @@drivers.not_nil![driver_name] end diff --git a/src/db/driver.cr b/src/db/driver.cr index 35e5713..49fc6e7 100644 --- a/src/db/driver.cr +++ b/src/db/driver.cr @@ -26,9 +26,6 @@ module DB # Refer to `Connection`, `Statement` and `ResultSet` for further # driver implementation instructions. abstract class Driver - def initialize - end - abstract def build_connection(db : Database) : Connection end end diff --git a/src/db/result_set.cr b/src/db/result_set.cr index 3bb4aa6..8dbaa54 100644 --- a/src/db/result_set.cr +++ b/src/db/result_set.cr @@ -4,6 +4,14 @@ module DB # See `DB` for a complete sample. # # Each `#read` call consumes the result and moves to the next column. + # Each column must be read in order. + # At any moment a `#move_next` can be invoked, meaning to skip the + # remaining, or even all the columns, in the current row. + # Also it is not mandatory to consume the whole `ResultSet`, hence an iteration + # through `#each` or `#move_next` can be stopped. + # + # **Note:** depending on how the `ResultSet` was obtained it might be mandatory an + # explicit call to `#close`. Check `QueryMethods#query`. # # ### Note to implementors # diff --git a/src/db/statement.cr b/src/db/statement.cr index b4b777e..175c162 100644 --- a/src/db/statement.cr +++ b/src/db/statement.cr @@ -46,23 +46,23 @@ module DB query(*args) do |rs| rs.each do # return case rs.read?(rs.column_type(0)) # :-( Some day... - t = rs.column_type(0) - if t == String + case rs.column_type(0) + when String.class return rs.read?(String) - elsif t == Int32 + when Int32.class return rs.read?(Int32) - elsif t == Int64 + when Int64.class return rs.read?(Int64) - elsif t == Float32 + when Float32.class return rs.read?(Float32) - elsif t == Float64 + when Float64.class return rs.read?(Float64) - elsif t == Slice(UInt8) + when Slice(UInt8).class return rs.read?(Slice(UInt8)) - elsif t == Nil + when Nil.class return rs.read?(Int32) else - raise "not implemented for #{t} type" + raise "not implemented for #{rs.column_type(0)} type" end end end