avoid caching statements in pool_statement

Only the connections where it was prepared need to be cached.
The connections already have a statement cache based on the query
This commit is contained in:
Brian J. Cardiff 2016-08-30 16:40:52 -03:00
parent f568e4506d
commit 9c9176608d

View file

@ -6,7 +6,8 @@ module DB
class PoolStatement
include StatementMethods
@statements = {} of Connection => Statement
# connections where the statement was prepared
@connections = Set(Connection).new
def initialize(@db : Database, @query : String)
# Prepares a statement on some connection
@ -24,7 +25,7 @@ module DB
# WHAT-IF the connection is busy? Should each statement be able to
# deallocate itself when the connection is free.
@statements.clear
@connections.clear
end
# See `QueryMethods#exec`
@ -58,16 +59,11 @@ module DB
end
# builds a statement over a real connection
# the conneciton and the stament is registered in `@statements`
# the conneciton is registered in `@connections`
private def get_statement : Statement
conn, existing = @db.checkout_some(@statements.keys)
if existing
@statements[conn]
else
stmt = conn.prepare @query
@statements[conn] = stmt
stmt
end
conn, existing = @db.checkout_some(@connections)
@connections << conn unless existing
conn.prepare(@query)
end
end
end