2016-12-04 18:14:43 +00:00
|
|
|
module DB
|
|
|
|
# Methods that are shared accross session like objects:
|
|
|
|
# - Database
|
|
|
|
# - Connection
|
|
|
|
#
|
|
|
|
# Classes that includes this module are able to execute
|
|
|
|
# queries and statements in both prepared and unprepared fashion.
|
|
|
|
#
|
|
|
|
# This module serves for dsl reuse over session like objects.
|
|
|
|
module SessionMethods(Session, Stmt)
|
2019-08-02 14:54:52 +00:00
|
|
|
include QueryMethods(Stmt)
|
2016-12-04 18:14:43 +00:00
|
|
|
|
|
|
|
# Returns whether by default the statements should
|
|
|
|
# be prepared or not.
|
|
|
|
abstract def prepared_statements? : Bool
|
|
|
|
|
2023-11-29 21:39:44 +00:00
|
|
|
abstract def prepared_statements_cache? : Bool
|
|
|
|
|
2016-12-04 18:14:43 +00:00
|
|
|
abstract def fetch_or_build_prepared_statement(query) : Stmt
|
|
|
|
|
|
|
|
abstract def build_unprepared_statement(query) : Stmt
|
|
|
|
|
|
|
|
def build(query) : Stmt
|
|
|
|
if prepared_statements?
|
2023-11-29 21:39:44 +00:00
|
|
|
stmt = fetch_or_build_prepared_statement(query)
|
|
|
|
|
|
|
|
# #build is a :nodoc: method used on QueryMethods where
|
|
|
|
# the statements are not exposed. As such if the cache
|
|
|
|
# is disabled we should auto_close the statement.
|
|
|
|
# When the statements are build explicitly the #prepared
|
|
|
|
# and #unprepared methods are used. In that case the
|
|
|
|
# statement is closed by the user explicitly also.
|
|
|
|
if !prepared_statements_cache?
|
|
|
|
stmt.auto_close = true if stmt.responds_to?(:auto_close=)
|
|
|
|
end
|
|
|
|
|
|
|
|
stmt
|
2016-12-04 18:14:43 +00:00
|
|
|
else
|
|
|
|
build_unprepared_statement(query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# dsl helper to build prepared statements
|
|
|
|
# returns a value that includes `QueryMethods`
|
|
|
|
def prepared
|
|
|
|
PreparedQuery(Session, Stmt).new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a prepared `Statement` that has not been executed yet.
|
|
|
|
def prepared(query)
|
|
|
|
prepared.build(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
# dsl helper to build unprepared statements
|
|
|
|
# returns a value that includes `QueryMethods`
|
|
|
|
def unprepared
|
|
|
|
UnpreparedQuery(Session, Stmt).new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an unprepared `Statement` that has not been executed yet.
|
|
|
|
def unprepared(query)
|
|
|
|
unprepared.build(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
struct PreparedQuery(Session, Stmt)
|
2019-08-02 14:54:52 +00:00
|
|
|
include QueryMethods(Stmt)
|
2016-12-04 18:14:43 +00:00
|
|
|
|
|
|
|
def initialize(@session : Session)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(query) : Stmt
|
|
|
|
@session.fetch_or_build_prepared_statement(query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
struct UnpreparedQuery(Session, Stmt)
|
2019-08-02 14:54:52 +00:00
|
|
|
include QueryMethods(Stmt)
|
2016-12-04 18:14:43 +00:00
|
|
|
|
|
|
|
def initialize(@session : Session)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(query) : Stmt
|
|
|
|
@session.build_unprepared_statement(query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|