mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
code review notes
close / do_close in result_set avoid closing statements remove named arguments refactor positioned arguments query
This commit is contained in:
parent
fd804dd592
commit
d01da912f7
10 changed files with 86 additions and 189 deletions
|
@ -23,7 +23,7 @@ describe DB do
|
|||
cnn.not_nil!.closed?.should be_true
|
||||
end
|
||||
|
||||
it "query should close statement" do
|
||||
it "query should close result_set" do
|
||||
with_witness do |w|
|
||||
with_dummy do |db|
|
||||
db.query "1,2" do
|
||||
|
@ -31,7 +31,7 @@ describe DB do
|
|||
end
|
||||
|
||||
w.check
|
||||
db.connection.last_statement.closed?.should be_true
|
||||
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -39,14 +39,14 @@ describe DB do
|
|||
it "exec should close statement" do
|
||||
with_dummy do |db|
|
||||
db.exec ""
|
||||
db.connection.last_statement.closed?.should be_true
|
||||
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "scalar should close statement" do
|
||||
with_dummy do |db|
|
||||
db.scalar "1"
|
||||
db.connection.last_statement.closed?.should be_true
|
||||
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -54,7 +54,6 @@ describe DB do
|
|||
with_dummy do |db|
|
||||
db.exec ""
|
||||
DummyDriver::DummyResultSet.last_result_set.executed?.should be_true
|
||||
db.connection.last_statement.closed?.should be_true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,10 +6,8 @@ class DummyDriver < DB::Driver
|
|||
end
|
||||
|
||||
class DummyConnection < DB::Connection
|
||||
getter! last_statement
|
||||
|
||||
def prepare(query)
|
||||
@last_statement = DummyStatement.new(self, query.split.map { |r| r.split ',' })
|
||||
DummyStatement.new(self, query)
|
||||
end
|
||||
|
||||
def last_insert_id : Int64
|
||||
|
@ -21,32 +19,27 @@ class DummyDriver < DB::Driver
|
|||
end
|
||||
|
||||
class DummyStatement < DB::Statement
|
||||
property! params
|
||||
property params
|
||||
|
||||
def initialize(driver, @items)
|
||||
def initialize(driver, @query)
|
||||
@params = Hash(Int32 | String, DB::Any).new
|
||||
super(driver)
|
||||
end
|
||||
|
||||
protected def begin_parameters
|
||||
@params = Hash(Int32 | String, DB::Any?).new
|
||||
end
|
||||
|
||||
protected def add_parameter(index : Int32, value)
|
||||
params[index] = value
|
||||
end
|
||||
|
||||
protected def add_parameter(name : String, value)
|
||||
params[":#{name}"] = value
|
||||
end
|
||||
|
||||
protected def perform
|
||||
DummyResultSet.new self, @items.each
|
||||
protected def perform(args : Slice(DB::Any))
|
||||
@params.clear
|
||||
args.each_with_index do |arg, index|
|
||||
@params[index] = arg
|
||||
end
|
||||
DummyResultSet.new self, @query
|
||||
end
|
||||
end
|
||||
|
||||
class DummyResultSet < DB::ResultSet
|
||||
def initialize(statement, @iterator)
|
||||
def initialize(statement, query)
|
||||
super(statement)
|
||||
@iterator = query.split.map { |r| r.split(',') }.to_a.each
|
||||
|
||||
@executed = false
|
||||
@@last_result_set = self
|
||||
end
|
||||
|
@ -89,10 +82,6 @@ class DummyDriver < DB::Driver
|
|||
return @statement.params[0]
|
||||
end
|
||||
|
||||
if n.starts_with?(":")
|
||||
return @statement.params[n]
|
||||
end
|
||||
|
||||
return n
|
||||
end
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ describe DummyDriver do
|
|||
end
|
||||
end
|
||||
|
||||
it "should query with block shuold executes always" do
|
||||
it "should query with block should executes always" do
|
||||
with_witness do |w|
|
||||
with_dummy do |db|
|
||||
db.query "" do |rs|
|
||||
|
@ -47,7 +47,9 @@ describe DummyDriver do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should query with block should executes always" do
|
||||
with_witness do |w|
|
||||
with_dummy do |db|
|
||||
db.query "lorem ipsum" do |rs|
|
||||
|
@ -136,28 +138,6 @@ describe DummyDriver do
|
|||
db.scalar(typeof({{value}}), "?", {{value}}).should eq({{value}})
|
||||
end
|
||||
end
|
||||
|
||||
it "should set arguments by symbol for {{value.id}}" do
|
||||
with_dummy do |db|
|
||||
db.query ":once :twice", {once: {{value}}, twice: {{value + value}} } do |rs|
|
||||
rs.move_next.should be_true
|
||||
rs.read(typeof({{value}})).should eq({{value}})
|
||||
rs.move_next.should be_true
|
||||
rs.read(typeof({{value}})).should eq({{value + value}})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should set arguments by string for {{value.id}}" do
|
||||
with_dummy do |db|
|
||||
db.query ":once :twice", {"once": {{value}}, "twice": {{value + value}} } do |rs|
|
||||
rs.move_next.should be_true
|
||||
rs.read(typeof({{value}})).should eq({{value}})
|
||||
rs.move_next.should be_true
|
||||
rs.read(typeof({{value}})).should eq({{value + value}})
|
||||
end
|
||||
end
|
||||
end
|
||||
{% end %}
|
||||
|
||||
it "executes and selects blob" do
|
||||
|
|
|
@ -19,26 +19,6 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
it "should initialize symbol named params in query" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.query({a: "a", b: 1, c: nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize string named params in query" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.query({"a": "a", "b": 1, "c": nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize positional params in exec" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
|
@ -49,26 +29,6 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
it "should initialize symbol named params in exec" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.exec({a: "a", b: 1, c: nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize string named params in exec" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.exec({"a": "a", "b": 1, "c": nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize positional params in scalar" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
|
@ -79,26 +39,6 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
it "should initialize symbol named params in scalar" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.scalar(String, {a: "a", b: 1, c: nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize string named params in scalar" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.scalar(String, {"a": "a", "b": 1, "c": nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize positional params in scalar?" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
|
@ -109,26 +49,6 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
it "should initialize symbol named params in scalar?" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.scalar?(String, {a: "a", b: 1, c: nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize string named params in scalar?" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare("the query")
|
||||
stmt.scalar?(String, {"a": "a", "b": 1, "c": nil})
|
||||
stmt.params[":a"].should eq("a")
|
||||
stmt.params[":b"].should eq(1)
|
||||
stmt.params[":c"].should eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "query with block should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
|
@ -137,45 +57,45 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
it "query with block should close statement" do
|
||||
it "query with block should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
stmt.query do |rs|
|
||||
end
|
||||
stmt.closed?.should be_true
|
||||
stmt.closed?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "query should close statement" do
|
||||
it "query should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
stmt.query do |rs|
|
||||
end
|
||||
stmt.closed?.should be_true
|
||||
stmt.closed?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "scalar should close statement" do
|
||||
it "scalar should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
stmt.scalar
|
||||
stmt.closed?.should be_true
|
||||
stmt.closed?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "scalar should close statement" do
|
||||
it "scalar should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
stmt.scalar?
|
||||
stmt.closed?.should be_true
|
||||
stmt.closed?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "exec should close statement" do
|
||||
it "exec should not close statement" do
|
||||
with_dummy do |db|
|
||||
stmt = db.prepare "3,4 1,2"
|
||||
stmt.exec
|
||||
stmt.closed?.should be_true
|
||||
stmt.closed?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue