2016-01-30 22:46:43 +00:00
|
|
|
require "spec"
|
|
|
|
require "db"
|
|
|
|
require "./dummy_driver"
|
|
|
|
|
|
|
|
describe DB::Statement do
|
|
|
|
it "should prepare statements" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.prepare("the query").should be_a(DB::Statement)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should initialize positional params in query" do
|
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare("the query")
|
|
|
|
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
|
|
|
|
|
|
|
|
it "should initialize positional params in exec" do
|
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare("the query")
|
|
|
|
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
|
|
|
|
|
|
|
|
it "should initialize positional params in scalar" do
|
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare("the query")
|
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
|
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare "3,4 1,2"
|
|
|
|
stmt.query
|
|
|
|
stmt.closed?.should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
it "query with block should not close statement" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare "3,4 1,2"
|
|
|
|
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-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare "3,4 1,2"
|
|
|
|
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-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare "3,4 1,2"
|
|
|
|
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-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
stmt = db.prepare "3,4 1,2"
|
|
|
|
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
|
|
|
|
with_dummy do |db|
|
|
|
|
rs = db.query "1, ?", 2
|
|
|
|
stmt = rs.statement
|
|
|
|
rs.close
|
|
|
|
|
|
|
|
rs = db.query "1, ?", 4
|
|
|
|
rs.statement.should be(stmt)
|
|
|
|
end
|
|
|
|
end
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|