2016-02-26 01:37:10 +00:00
|
|
|
require "./spec_helper"
|
2016-01-30 22:46:43 +00:00
|
|
|
|
|
|
|
describe DB::Statement do
|
2016-11-29 23:14:07 +00:00
|
|
|
it "should build prepared statements" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
prepared = cnn.prepared("the query")
|
|
|
|
prepared.should be_a(DB::Statement)
|
|
|
|
prepared.as(DummyDriver::DummyStatement).prepared?.should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should build unprepared statements" do
|
2016-12-04 18:19:15 +00:00
|
|
|
with_dummy_connection("prepared_statements=false") do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
prepared = cnn.unprepared("the query")
|
|
|
|
prepared.should be_a(DB::Statement)
|
|
|
|
prepared.as(DummyDriver::DummyStatement).prepared?.should be_false
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-03 01:09:27 +00:00
|
|
|
describe "prepared_statements flag" do
|
|
|
|
it "should build prepared statements if true" do
|
2016-12-04 18:19:15 +00:00
|
|
|
with_dummy_connection("prepared_statements=true") do |cnn|
|
2016-12-03 01:09:27 +00:00
|
|
|
stmt = cnn.query("the query").statement
|
|
|
|
stmt.as(DummyDriver::DummyStatement).prepared?.should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should build unprepared statements if false" do
|
2016-12-04 18:19:15 +00:00
|
|
|
with_dummy_connection("prepared_statements=false") do |cnn|
|
2016-12-03 01:09:27 +00:00
|
|
|
stmt = cnn.query("the query").statement
|
|
|
|
stmt.as(DummyDriver::DummyStatement).prepared?.should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
it "should initialize positional params in query" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.query "a", 1, nil
|
2016-01-31 22:40:02 +00:00
|
|
|
stmt.params[0].should eq("a")
|
|
|
|
stmt.params[1].should eq(1)
|
|
|
|
stmt.params[2].should eq(nil)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-20 20:23:09 +00:00
|
|
|
it "accepts array as single argument" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
2016-02-04 00:28:53 +00:00
|
|
|
stmt.query ["a", 1, nil]
|
2019-09-20 20:23:09 +00:00
|
|
|
stmt.params[0].should eq(["a", 1, nil])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows no arguments" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
|
|
|
stmt.query
|
|
|
|
stmt.params.should be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "concatenate arguments" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
|
|
|
stmt.query 1, 2, args: ["a", [1, nil]]
|
|
|
|
stmt.params[0].should eq(1)
|
|
|
|
stmt.params[1].should eq(2)
|
|
|
|
stmt.params[2].should eq("a")
|
|
|
|
stmt.params[3].should eq([1, nil])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize positional params in query with array" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
|
|
|
stmt.query args: ["a", 1, nil]
|
2016-02-04 00:28:53 +00:00
|
|
|
stmt.params[0].should eq("a")
|
|
|
|
stmt.params[1].should eq(1)
|
|
|
|
stmt.params[2].should eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
it "should initialize positional params in exec" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.exec "a", 1, nil
|
2016-01-31 22:40:02 +00:00
|
|
|
stmt.params[0].should eq("a")
|
|
|
|
stmt.params[1].should eq(1)
|
|
|
|
stmt.params[2].should eq(nil)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-20 20:23:09 +00:00
|
|
|
it "accepts array as single argument" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
2016-02-04 00:28:53 +00:00
|
|
|
stmt.exec ["a", 1, nil]
|
2019-09-20 20:23:09 +00:00
|
|
|
stmt.params[0].should eq(["a", 1, nil])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize positional params in exec with array" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
|
|
|
stmt.exec args: ["a", 1, nil]
|
2016-02-04 00:28:53 +00:00
|
|
|
stmt.params[0].should eq("a")
|
|
|
|
stmt.params[1].should eq(1)
|
|
|
|
stmt.params[2].should eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-20 20:23:09 +00:00
|
|
|
it "allows no arguments" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
|
|
|
stmt.exec
|
|
|
|
stmt.params.should be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "concatenate arguments" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
|
|
|
stmt.exec 1, 2, args: ["a", [1, nil]]
|
|
|
|
stmt.params[0].should eq(1)
|
|
|
|
stmt.params[1].should eq(2)
|
|
|
|
stmt.params[2].should eq("a")
|
|
|
|
stmt.params[3].should eq([1, nil])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
it "should initialize positional params in scalar" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
2016-02-02 01:33:58 +00:00
|
|
|
stmt.scalar "a", 1, nil
|
2016-01-31 22:40:02 +00:00
|
|
|
stmt.params[0].should eq("a")
|
|
|
|
stmt.params[1].should eq(1)
|
|
|
|
stmt.params[2].should eq(nil)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "query with block should not close statement" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared "3,4 1,2"
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.query
|
|
|
|
stmt.closed?.should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-24 01:07:59 +00:00
|
|
|
it "closing connection should close statement" do
|
|
|
|
stmt = uninitialized DB::Statement
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared "3,4 1,2"
|
2016-06-24 01:07:59 +00:00
|
|
|
stmt.query
|
|
|
|
end
|
|
|
|
stmt.closed?.should be_true
|
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
it "query with block should not close statement" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared "3,4 1,2"
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.query do |rs|
|
|
|
|
end
|
2016-02-02 00:55:30 +00:00
|
|
|
stmt.closed?.should be_false
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
it "query should not close statement" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared "3,4 1,2"
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.query do |rs|
|
|
|
|
end
|
2016-02-02 00:55:30 +00:00
|
|
|
stmt.closed?.should be_false
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
it "scalar should not close statement" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared "3,4 1,2"
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.scalar
|
2016-02-02 00:55:30 +00:00
|
|
|
stmt.closed?.should be_false
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
it "exec should not close statement" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
stmt = cnn.prepared "3,4 1,2"
|
2016-01-30 22:46:43 +00:00
|
|
|
stmt.exec
|
2016-02-02 00:55:30 +00:00
|
|
|
stmt.closed?.should be_false
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
2016-02-03 21:46:37 +00:00
|
|
|
|
|
|
|
it "connection should cache statements by query" do
|
2016-08-29 19:50:37 +00:00
|
|
|
with_dummy_connection do |cnn|
|
2016-11-29 23:14:07 +00:00
|
|
|
rs = cnn.prepared.query "1, ?", 2
|
2016-02-03 21:46:37 +00:00
|
|
|
stmt = rs.statement
|
|
|
|
rs.close
|
|
|
|
|
2016-11-29 23:14:07 +00:00
|
|
|
rs = cnn.prepared.query "1, ?", 4
|
2016-02-03 21:46:37 +00:00
|
|
|
rs.statement.should be(stmt)
|
|
|
|
end
|
|
|
|
end
|
2016-08-29 04:23:20 +00:00
|
|
|
|
|
|
|
it "connection should be released if error occurs during exec" do
|
|
|
|
with_dummy do |db|
|
2017-12-22 12:12:07 +00:00
|
|
|
expect_raises DB::Error do
|
2016-08-29 04:23:20 +00:00
|
|
|
db.exec "raise"
|
|
|
|
end
|
2016-08-29 16:14:47 +00:00
|
|
|
DummyDriver::DummyConnection.connections.size.should eq(1)
|
|
|
|
db.pool.is_available?(DummyDriver::DummyConnection.connections.first)
|
2016-08-29 04:23:20 +00:00
|
|
|
end
|
|
|
|
end
|
2020-02-17 21:04:23 +00:00
|
|
|
|
|
|
|
it "raises NoResultsError for scalar" do
|
|
|
|
with_dummy_connection do |cnn|
|
|
|
|
stmt = cnn.prepared ""
|
|
|
|
expect_raises DB::NoResultsError do
|
|
|
|
stmt.scalar "SELECT LIMIT 0"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|