mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
add statement cache in connection.
hide methods from api
This commit is contained in:
parent
562d5076bf
commit
fa111fd698
5 changed files with 34 additions and 6 deletions
|
@ -21,7 +21,7 @@ class DummyDriver < DB::Driver
|
|||
@@connections.try &.clear
|
||||
end
|
||||
|
||||
def prepare(query)
|
||||
def build_statement(query)
|
||||
DummyStatement.new(self, query)
|
||||
end
|
||||
|
||||
|
@ -30,6 +30,7 @@ class DummyDriver < DB::Driver
|
|||
end
|
||||
|
||||
protected def do_close
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -80,4 +80,15 @@ describe DB::Statement do
|
|||
stmt.closed?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
@ -9,17 +9,31 @@ module DB
|
|||
#
|
||||
# ### Note to implementors
|
||||
#
|
||||
# The connection must be initialized in `#initialize` and closed in `#perform_close`.
|
||||
# The connection must be initialized in `#initialize` and closed in `#do_close`.
|
||||
#
|
||||
# To allow quering override `#prepare` method in order to return a prepared `Statement`.
|
||||
# Also override `#last_insert_id` to allow safe access to the last inserted id through this connection.
|
||||
# Override `#build_statement` method in order to return a prepared `Statement` to allow querying.
|
||||
# See also `Statement` to define how the statements are executed.
|
||||
#
|
||||
abstract class Connection
|
||||
include Disposable
|
||||
include QueryMethods
|
||||
@statements_cache = {} of String => Statement
|
||||
|
||||
# :nodoc:
|
||||
abstract def prepare(query) : Statement
|
||||
def prepare(query) : Statement
|
||||
stmt = @statements_cache.fetch(query, nil)
|
||||
if stmt.is_a?(Nil)
|
||||
stmt = build_statement(query)
|
||||
@statements_cache[query] = stmt
|
||||
end
|
||||
|
||||
include QueryMethods
|
||||
stmt
|
||||
end
|
||||
|
||||
abstract def build_statement(query) : Statement
|
||||
|
||||
protected def do_close
|
||||
@statements_cache.clear
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,6 +15,7 @@ module DB
|
|||
abstract class ResultSet
|
||||
include Disposable
|
||||
|
||||
# :nodoc:
|
||||
getter statement
|
||||
|
||||
def initialize(@statement : Statement)
|
||||
|
|
|
@ -12,6 +12,7 @@ module DB
|
|||
abstract class Statement
|
||||
include Disposable
|
||||
|
||||
# :nodoc:
|
||||
getter connection
|
||||
|
||||
def initialize(@connection)
|
||||
|
|
Loading…
Reference in a new issue