mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add specs for prepared statements life cycle
This commit is contained in:
parent
89e32c6e0a
commit
ba2dcd4fe8
2 changed files with 42 additions and 0 deletions
|
@ -34,14 +34,20 @@ class DummyDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_prepared_statement(query) : DB::Statement
|
def build_prepared_statement(query) : DB::Statement
|
||||||
|
assert_not_closed!
|
||||||
|
|
||||||
DummyStatement.new(self, query, true)
|
DummyStatement.new(self, query, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_unprepared_statement(query) : DB::Statement
|
def build_unprepared_statement(query) : DB::Statement
|
||||||
|
assert_not_closed!
|
||||||
|
|
||||||
DummyStatement.new(self, query, false)
|
DummyStatement.new(self, query, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_insert_id : Int64
|
def last_insert_id : Int64
|
||||||
|
assert_not_closed!
|
||||||
|
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -54,12 +60,18 @@ class DummyDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_transaction
|
def create_transaction
|
||||||
|
assert_not_closed!
|
||||||
|
|
||||||
DummyTransaction.new(self)
|
DummyTransaction.new(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def do_close
|
protected def do_close
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def assert_not_closed!
|
||||||
|
raise "Statement is closed" if closed?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyTransaction < DB::TopLevelTransaction
|
class DummyTransaction < DB::TopLevelTransaction
|
||||||
|
@ -114,6 +126,8 @@ class DummyDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def perform_query(args : Enumerable) : DB::ResultSet
|
protected def perform_query(args : Enumerable) : DB::ResultSet
|
||||||
|
assert_not_closed!
|
||||||
|
|
||||||
Fiber.yield
|
Fiber.yield
|
||||||
@connection.as(DummyConnection).check
|
@connection.as(DummyConnection).check
|
||||||
set_params args
|
set_params args
|
||||||
|
@ -121,6 +135,8 @@ class DummyDriver < DB::Driver
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
||||||
|
assert_not_closed!
|
||||||
|
|
||||||
@connection.as(DummyConnection).check
|
@connection.as(DummyConnection).check
|
||||||
set_params args
|
set_params args
|
||||||
raise DB::Error.new("forced exception due to query") if command == "raise"
|
raise DB::Error.new("forced exception due to query") if command == "raise"
|
||||||
|
@ -153,6 +169,10 @@ class DummyDriver < DB::Driver
|
||||||
protected def do_close
|
protected def do_close
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def assert_not_closed!
|
||||||
|
raise "Statement is closed" if closed?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class DummyResultSet < DB::ResultSet
|
class DummyResultSet < DB::ResultSet
|
||||||
|
|
|
@ -43,6 +43,17 @@ describe DB::Statement do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should leave statements open to be reused if true" do
|
||||||
|
with_dummy_connection("prepared_statements=true&prepared_statements_cache=true") do |cnn|
|
||||||
|
rs = cnn.query("the query")
|
||||||
|
# do not close while iterating
|
||||||
|
rs.statement.closed?.should be_false
|
||||||
|
rs.close
|
||||||
|
# do not close to be reused
|
||||||
|
rs.statement.closed?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "should not reuse prepared statements if false" do
|
it "should not reuse prepared statements if false" do
|
||||||
with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn|
|
with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn|
|
||||||
stmt1 = cnn.query("the query").statement
|
stmt1 = cnn.query("the query").statement
|
||||||
|
@ -50,6 +61,17 @@ describe DB::Statement do
|
||||||
stmt1.object_id.should_not eq(stmt2.object_id)
|
stmt1.object_id.should_not eq(stmt2.object_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should close statements if false" do
|
||||||
|
with_dummy_connection("prepared_statements=true&prepared_statements_cache=false") do |cnn|
|
||||||
|
rs = cnn.query("the query")
|
||||||
|
# do not close while iterating
|
||||||
|
rs.statement.closed?.should be_false
|
||||||
|
rs.close
|
||||||
|
# do close after iterating
|
||||||
|
rs.statement.closed?.should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should initialize positional params in query" do
|
it "should initialize positional params in query" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue