ensure exec and scalar release connection

ensure exec, query can be executed with Enumerable(Any)
update db doc sample
This commit is contained in:
Brian J. Cardiff 2016-02-03 21:28:53 -03:00
parent d45427dfdd
commit 4ed7f28fe6
5 changed files with 64 additions and 7 deletions

View File

@ -69,4 +69,18 @@ describe DB do
end end
end end
end end
it "exec should return to pool" do
with_dummy do |db|
db.exec "foo"
db.exec "bar"
end
end
it "scalar should return to pool" do
with_dummy do |db|
db.scalar "foo"
db.scalar "bar"
end
end
end end

View File

@ -19,6 +19,16 @@ describe DB::Statement do
end end
end end
it "should initialize positional params in query with array" do
with_dummy do |db|
stmt = db.prepare("the query")
stmt.query ["a", 1, nil]
stmt.params[0].should eq("a")
stmt.params[1].should eq(1)
stmt.params[2].should eq(nil)
end
end
it "should initialize positional params in exec" do it "should initialize positional params in exec" do
with_dummy do |db| with_dummy do |db|
stmt = db.prepare("the query") stmt = db.prepare("the query")
@ -29,6 +39,16 @@ describe DB::Statement do
end end
end end
it "should initialize positional params in exec with array" do
with_dummy do |db|
stmt = db.prepare("the query")
stmt.exec ["a", 1, nil]
stmt.params[0].should eq("a")
stmt.params[1].should eq(1)
stmt.params[2].should eq(nil)
end
end
it "should initialize positional params in scalar" do it "should initialize positional params in scalar" do
with_dummy do |db| with_dummy do |db|
stmt = db.prepare("the query") stmt = db.prepare("the query")

View File

@ -14,14 +14,14 @@ require "uri"
# Assuming `crystal-sqlite3` is included a sqlite3 database can be opened with `#open`. # Assuming `crystal-sqlite3` is included a sqlite3 database can be opened with `#open`.
# #
# ``` # ```
# db = DB.open "sqlite3://%3Amemory%3A" # or sqlite3:///path/to/db/file.db # db = DB.open "sqlite3:%3Amemory%3A" # or sqlite3:./path/to/db/file.db
# db.close # db.close
# ``` # ```
# #
# If a block is given to `#open` the database is closed automatically # If a block is given to `#open` the database is closed automatically
# #
# ``` # ```
# DB.open "sqlite3://%3Amemory%3A" do |db| # DB.open "sqlite3:%3Amemory%3A" do |db|
# # work with db # # work with db
# end # db is closed # end # db is closed
# ``` # ```
@ -42,7 +42,11 @@ require "uri"
# DB.open "sqlite3://%3Amemory%3A" do |db| # DB.open "sqlite3://%3Amemory%3A" do |db|
# db.exec "create table contacts (name string, age integer)" # db.exec "create table contacts (name string, age integer)"
# db.exec "insert into contacts values (?, ?)", "John Doe", 30 # db.exec "insert into contacts values (?, ?)", "John Doe", 30
# db.exec "insert into contacts values (:name, :age)", {name: "Sarah", age: 33} #
# args = [] of DB::Any
# args << "Sarah"
# args << 33
# db.exec "insert into contacts values (?, ?)", args
# #
# puts "max age:" # puts "max age:"
# puts db.scalar "select max(age) from contacts" # => 33 # puts db.scalar "select max(age) from contacts" # => 33

View File

@ -22,8 +22,7 @@ module DB
end end
protected def do_close protected def do_close
cnn = statement.connection statement.release_connection
cnn.database.return_to_pool(cnn)
end end
# TODO add_next_result_set : Bool # TODO add_next_result_set : Bool

View File

@ -21,15 +21,24 @@ module DB
protected def do_close protected def do_close
end end
def release_connection
@connection.database.return_to_pool(@connection)
end
# See `QueryMethods#exec` # See `QueryMethods#exec`
def exec def exec
perform_exec(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn) perform_exec_and_release(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn)
end
# See `QueryMethods#exec`
def exec(args : Enumerable(Any))
perform_exec_and_release(args.to_a.to_unsafe.to_slice(args.size))
end end
# See `QueryMethods#exec` # See `QueryMethods#exec`
def exec(*args) def exec(*args)
# TODO better way to do it # TODO better way to do it
perform_exec(args.to_a.to_unsafe.to_slice(args.size)) perform_exec_and_release(args.to_a.to_unsafe.to_slice(args.size))
end end
# See `QueryMethods#scalar` # See `QueryMethods#scalar`
@ -81,11 +90,22 @@ module DB
perform_query(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn) perform_query(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn)
end end
private def perform_query(args : Enumerable(Any)) : ResultSet
# TODO better way to do it
perform_query(args.to_a.to_unsafe.to_slice(args.size))
end
private def perform_query(*args) : ResultSet private def perform_query(*args) : ResultSet
# TODO better way to do it # TODO better way to do it
perform_query(args.to_a.to_unsafe.to_slice(args.size)) perform_query(args.to_a.to_unsafe.to_slice(args.size))
end end
private def perform_exec_and_release(args : Slice(Any)) : ExecResult
perform_exec(args).tap do
release_connection
end
end
protected abstract def perform_query(args : Slice(Any)) : ResultSet protected abstract def perform_query(args : Slice(Any)) : ResultSet
protected abstract def perform_exec(args : Slice(Any)) : ExecResult protected abstract def perform_exec(args : Slice(Any)) : ExecResult
end end