add Connection#last_inserted_id

extract QueryMethods modules and use it in Database and Connection
"add" finalize methods, but they cause GC warnings
This commit is contained in:
Brian J. Cardiff 2016-01-31 14:01:52 -03:00
parent 1fb0b8a82b
commit 8c0313a306
6 changed files with 60 additions and 36 deletions

View file

@ -12,6 +12,10 @@ class DummyDriver < DB::Driver
@last_statement = DummyStatement.new(self, query.split.map { |r| r.split ',' })
end
def last_insert_id : Int64
0
end
def perform_close
end
end

View file

@ -18,8 +18,17 @@ module DB
@closed
end
# # :nodoc:
# def finalize
# close unless closed?
# end
abstract def prepare(query) : Statement
include QueryMethods
abstract def last_insert_id : Int64
protected abstract def perform_close
end
end

View file

@ -31,39 +31,6 @@ module DB
connection.prepare(query)
end
def query(query, *args)
prepare(query).query(*args)
end
def query(query, *args)
# CHECK prepare(query).query(*args, &block)
query(query, *args).tap do |rs|
begin
yield rs
ensure
rs.close
end
end
end
def exec(query, *args)
prepare(query).exec(*args)
end
def scalar(query, *args)
prepare(query).scalar(*args)
end
def scalar(t, query, *args)
prepare(query).scalar(t, *args)
end
def scalar?(query, *args)
prepare(query).scalar?(*args)
end
def scalar?(t, query, *args)
prepare(query).scalar?(t, *args)
end
include QueryMethods
end
end

View file

@ -24,6 +24,7 @@ module DB
end
end
require "./query_methods"
require "./database"
require "./driver"
require "./connection"

38
src/db/query_methods.cr Normal file
View file

@ -0,0 +1,38 @@
module DB
module QueryMethods
def query(query, *args)
prepare(query).query(*args)
end
def query(query, *args)
# CHECK prepare(query).query(*args, &block)
query(query, *args).tap do |rs|
begin
yield rs
ensure
rs.close
end
end
end
def exec(query, *args)
prepare(query).exec(*args)
end
def scalar(query, *args)
prepare(query).scalar(*args)
end
def scalar(t, query, *args)
prepare(query).scalar(t, *args)
end
def scalar?(query, *args)
prepare(query).scalar?(*args)
end
def scalar?(t, query, *args)
prepare(query).scalar?(t, *args)
end
end
end

View file

@ -24,7 +24,7 @@ module DB
end
end
raise "unreachable"
raise "no results"
end
def scalar?(*args)
@ -39,7 +39,7 @@ module DB
end
end
raise "unreachable"
raise "no results"
end
def query(*args)
@ -92,6 +92,11 @@ module DB
@closed
end
# # :nodoc:
# def finalize
# close unless closed?
# end
# 1-based positional arguments
protected def begin_parameters
end