class DB::Database


Acts as an entry point for database access. Connections are managed by a pool. The connection pool can be configured from URI parameters:

When querying a database prepared statements are used by default. This can be changed from the prepared_statements URI parameter:

It should be created from DB module. See DB#open.

Refer to QueryMethods and SessionMethods for documentation about querying the database.

Included Modules

Defined in:


Instance Method Summary

Instance methods inherited from module DB::ConnectionContext

discard(connection : Connection) discard, prepared_statements? : Bool prepared_statements?, release(connection : Connection) release, uri : URI uri

Instance methods inherited from module DB::SessionMethods(DB::Database, DB::PoolStatement)

build(query) : Stmt build, build_unprepared_statement(query) : Stmt build_unprepared_statement, fetch_or_build_prepared_statement(query) : Stmt fetch_or_build_prepared_statement, prepared(query)
, prepared_statements? : Bool prepared_statements?, unprepared(query)

Instance methods inherited from module DB::QueryMethods

exec(query, *args) exec, query(query, *args)
query(query, *args, &block)
, query_all(query, *args, &block : ResultSet -> U) : Array(U) forall U
query_all(query, *args, as types : Tuple)
query_all(query, *args, as type : Class)
, query_each(query, *args, &block) query_each, query_one(query, *args, &block : ResultSet -> U) : U forall U
query_one(query, *args, as types : Tuple)
query_one(query, *args, as type : Class)
, query_one?(query, *args, &block : ResultSet -> U) : U? forall U
query_one?(query, *args, as types : Tuple)
query_one?(query, *args, as type : Class)
, scalar(query, *args) scalar

Instance Method Detail

def checkout #

returns a connection from the pool the returned connection must be returned to the pool by explictly calling Connection#release

[View source]
def close #

Closes all connection to the database.

[View source]
def prepared_statements? : Bool #

[View source]
def setup_connection(&proc : Connection -> Nil) #

[View source]
def transaction(&block) #

yields a Transaction from a connection of the pool Refer to BeginTransaction#transaction for documentation.

[View source]
def uri : URI #

Returns the uri with the connection settings to the database

[View source]
def using_connection(&block) #

yields a connection from the pool the connection is returned to the pool when the block ends

[View source]