shard-crystal-db/src/db/database.cr

74 lines
1.8 KiB
Crystal
Raw Normal View History

require "http/params"
module DB
# Acts as an entry point for database access.
# Connections are managed by a pool.
# The connection pool can be configured from URI parameters:
#
# - initial_pool_size (default 1)
# - max_pool_size (default 1)
# - max_idle_pool_size (default 1)
# - checkout_timeout (default 5.0)
#
# It should be created from DB module. See `DB#open`.
#
# Refer to `QueryMethods` for documentation about querying the database.
class Database
# :nodoc:
getter driver
# Returns the uri with the connection settings to the database
2016-02-03 21:29:09 +00:00
getter uri
@pool : Pool(Connection)
2016-06-16 15:14:57 +00:00
# :nodoc:
2016-06-16 15:14:57 +00:00
def initialize(@driver : Driver, @uri : URI)
# TODO: PR HTTP::Params.new -> HTTP::Params.new(Hash(String, Array(String)).new)
params = (query = uri.query) ? HTTP::Params.parse(query) : HTTP::Params.new(Hash(String, Array(String)).new)
pool_options = @driver.connection_pool_options(params)
@pool = uninitialized Pool(Connection) # in order to use self in the factory proc
@pool = Pool.new(->{ @driver.build_connection(self).as(Connection) }, **pool_options)
end
# Closes all connection to the database.
def close
@pool.close
end
# :nodoc:
def prepare(query)
conn = get_from_pool
begin
conn.prepare(query)
rescue ex
return_to_pool(conn)
raise ex
end
end
2016-02-03 19:57:54 +00:00
# :nodoc:
def get_from_pool
@pool.checkout
end
# :nodoc:
def return_to_pool(connection)
@pool.release connection
end
# yields a connection from the pool
# the connection is returned to the pool after
# when the block ends
def using_connection
connection = get_from_pool
yield connection
ensure
return_to_pool connection
end
include QueryMethods
end
end