add prepared_statements option to database

* use ?prepared_statements=true|false on connection string (default: true)
* make inmutable state in database
* make mutable state in connection
* change Connection#build to use the current prepared_statements flag to build prepared or unprepared statements.
This commit is contained in:
Brian J. Cardiff 2016-12-02 22:09:27 -03:00
parent fe0ed55ef9
commit 9ef9d19d1d
6 changed files with 80 additions and 3 deletions

View file

@ -120,6 +120,15 @@ module DB
private def self.build_database(uri : URI)
Database.new(driver_class(uri.scheme).new, uri)
end
# :nodoc:
def self.fetch_bool(params : HTTP::Params, name, default : Bool)
if value = params[name]?
value.underscore == "true"
else
default
end
end
end
require "./db/pool"

View file

@ -24,15 +24,19 @@ module DB
# :nodoc:
getter database
@statements_cache = StringKeyCache(Statement).new
property? prepared_statements : Bool
def initialize(@database : Database)
@prepared_statements = @database.prepared_statements?
end
# :nodoc:
def build(query) : Statement
# TODO add flag for default statements kind.
# configured in database overridable by connection
fetch_or_build_prepared_statement(query)
if prepared_statements?
fetch_or_build_prepared_statement(query)
else
build_unprepared_statement(query)
end
end
# :nodoc:

View file

@ -25,6 +25,8 @@ module DB
# Returns the uri with the connection settings to the database
getter uri
getter? prepared_statements : Bool
@pool : Pool(Connection)
@setup_connection : Connection -> Nil
@statements_cache = StringKeyCache(PoolStatement).new
@ -32,6 +34,7 @@ module DB
# :nodoc:
def initialize(@driver : Driver, @uri : URI)
params = HTTP::Params.parse(uri.query || "")
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
pool_options = @driver.connection_pool_options(params)
@setup_connection = ->(conn : Connection) {}