2016-07-07 18:50:09 +00:00
|
|
|
require "http/params"
|
2016-09-12 17:35:21 +00:00
|
|
|
require "weak_ref"
|
2016-07-07 18:50:09 +00:00
|
|
|
|
2016-01-29 19:13:01 +00:00
|
|
|
module DB
|
|
|
|
# Acts as an entry point for database access.
|
2016-07-07 18:50:09 +00:00
|
|
|
# Connections are managed by a pool.
|
|
|
|
# The connection pool can be configured from URI parameters:
|
|
|
|
#
|
|
|
|
# - initial_pool_size (default 1)
|
2016-10-22 00:49:16 +00:00
|
|
|
# - max_pool_size (default 0 = unlimited)
|
2016-07-07 18:50:09 +00:00
|
|
|
# - max_idle_pool_size (default 1)
|
|
|
|
# - checkout_timeout (default 5.0)
|
2016-08-31 20:32:01 +00:00
|
|
|
# - retry_attempts (default 1)
|
|
|
|
# - retry_delay (in seconds, default 1.0)
|
2016-01-30 22:46:43 +00:00
|
|
|
#
|
2016-12-04 18:14:43 +00:00
|
|
|
# When querying a database prepared statements are used by default.
|
|
|
|
# This can be changed from the `prepared_statements` URI parameter:
|
|
|
|
#
|
|
|
|
# - prepared_statements = `true`|`false` (default `true`)
|
|
|
|
#
|
2016-01-31 22:40:02 +00:00
|
|
|
# It should be created from DB module. See `DB#open`.
|
|
|
|
#
|
2016-12-04 18:14:43 +00:00
|
|
|
# Refer to `QueryMethods` and `SessionMethods` for documentation about querying the database.
|
2016-01-29 19:13:01 +00:00
|
|
|
class Database
|
2016-12-04 18:14:43 +00:00
|
|
|
include SessionMethods(Database, PoolStatement)
|
2016-12-03 18:56:03 +00:00
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# :nodoc:
|
2016-02-03 22:30:51 +00:00
|
|
|
getter driver
|
2016-08-29 16:14:47 +00:00
|
|
|
# :nodoc:
|
|
|
|
getter pool
|
2016-01-31 22:40:02 +00:00
|
|
|
|
2016-02-03 22:30:51 +00:00
|
|
|
# Returns the uri with the connection settings to the database
|
2016-02-03 21:29:09 +00:00
|
|
|
getter uri
|
2016-01-29 19:13:01 +00:00
|
|
|
|
2016-12-03 01:09:27 +00:00
|
|
|
getter? prepared_statements : Bool
|
|
|
|
|
2016-07-07 18:50:09 +00:00
|
|
|
@pool : Pool(Connection)
|
2016-08-18 03:55:43 +00:00
|
|
|
@setup_connection : Connection -> Nil
|
2016-12-03 19:03:50 +00:00
|
|
|
@statements_cache = StringKeyCache(PoolPreparedStatement).new
|
2016-06-16 15:14:57 +00:00
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# :nodoc:
|
2016-06-16 15:14:57 +00:00
|
|
|
def initialize(@driver : Driver, @uri : URI)
|
2016-08-30 18:22:27 +00:00
|
|
|
params = HTTP::Params.parse(uri.query || "")
|
2016-12-03 01:09:27 +00:00
|
|
|
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
|
2016-07-07 18:50:09 +00:00
|
|
|
pool_options = @driver.connection_pool_options(params)
|
|
|
|
|
2016-08-18 03:55:43 +00:00
|
|
|
@setup_connection = ->(conn : Connection) {}
|
2016-07-07 18:50:09 +00:00
|
|
|
@pool = uninitialized Pool(Connection) # in order to use self in the factory proc
|
2016-08-18 03:55:43 +00:00
|
|
|
@pool = Pool.new(**pool_options) {
|
|
|
|
conn = @driver.build_connection(self).as(Connection)
|
|
|
|
@setup_connection.call conn
|
|
|
|
conn
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_connection(&proc : Connection -> Nil)
|
|
|
|
@setup_connection = proc
|
|
|
|
@pool.each_resource do |conn|
|
|
|
|
@setup_connection.call conn
|
|
|
|
end
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 22:40:02 +00:00
|
|
|
# Closes all connection to the database.
|
2016-01-30 22:46:43 +00:00
|
|
|
def close
|
2016-08-30 19:20:18 +00:00
|
|
|
@statements_cache.each_value &.close
|
|
|
|
@statements_cache.clear
|
|
|
|
|
2016-07-07 18:50:09 +00:00
|
|
|
@pool.close
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-12-03 18:56:03 +00:00
|
|
|
# :nodoc:
|
|
|
|
def fetch_or_build_prepared_statement(query)
|
|
|
|
@statements_cache.fetch(query) { build_prepared_statement(query) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def build_prepared_statement(query)
|
2016-12-03 19:03:50 +00:00
|
|
|
PoolPreparedStatement.new(self, query)
|
2016-12-03 18:56:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def build_unprepared_statement(query)
|
|
|
|
PoolUnpreparedStatement.new(self, query)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-02-03 19:57:54 +00:00
|
|
|
# :nodoc:
|
2016-09-12 17:35:21 +00:00
|
|
|
def checkout_some(candidates : Enumerable(WeakRef(Connection))) : {Connection, Bool}
|
2016-08-29 19:56:34 +00:00
|
|
|
@pool.checkout_some candidates
|
2016-02-03 22:30:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def return_to_pool(connection)
|
2016-07-07 18:50:09 +00:00
|
|
|
@pool.release connection
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-06-23 20:27:30 +00:00
|
|
|
# yields a connection from the pool
|
|
|
|
# the connection is returned to the pool after
|
|
|
|
# when the block ends
|
|
|
|
def using_connection
|
2016-08-29 19:56:34 +00:00
|
|
|
connection = @pool.checkout
|
2016-07-11 15:56:40 +00:00
|
|
|
begin
|
|
|
|
yield connection
|
|
|
|
ensure
|
|
|
|
return_to_pool connection
|
|
|
|
end
|
2016-06-23 20:27:30 +00:00
|
|
|
end
|
|
|
|
|
2016-12-14 15:13:46 +00:00
|
|
|
# yields a `Transaction` from a connection of the pool
|
|
|
|
# Refer to `BeginTransaction#transaction` for documentation.
|
2016-11-16 02:46:11 +00:00
|
|
|
def transaction
|
|
|
|
using_connection do |cnn|
|
|
|
|
cnn.transaction do |tx|
|
|
|
|
yield tx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-31 20:32:01 +00:00
|
|
|
# :nodoc:
|
|
|
|
def retry
|
|
|
|
@pool.retry do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
end
|