class DB::Database
- DB::Database
- Reference
- Object
Overview
Acts as an entry point for database access.
Connections are managed by a pool.
Use DB#open
to create a Database
instance.
Refer to QueryMethods
and SessionMethods
for documentation about querying the database.
Database URI
Connection parameters are configured in a URI. The format is specified by the individual database drivers. See the reference book for examples.
The connection pool can be configured from URI parameters:
initial_pool_size
(default 1)max_pool_size
(default 0 = unlimited)max_idle_pool_size
(default 1)checkout_timeout
(default 5.0)retry_attempts
(default 1)retry_delay
(in seconds, default 1.0)
When querying a database, prepared statements are used by default.
This can be changed from the prepared_statements
URI parameter:
prepared_statements
(true, or false, default true)
Included Modules
Defined in:
db/database.crInstance Method Summary
-
#checkout
returns a connection from the pool the returned connection must be returned to the pool by explictly calling
Connection#release
-
#close
Closes all connection to the database.
-
#prepared_statements? : Bool
Return whether the statements should be prepared by default
-
#setup_connection(&proc : Connection -> Nil)
Run the specified block every time a new connection is established, yielding the new connection to the block.
-
#transaction(&)
yields a
Transaction
from a connection of the pool Refer toBeginTransaction#transaction
for documentation. -
#uri : URI
Returns the uri with the connection settings to the database
-
#using_connection(&)
yields a connection from the pool the connection is returned to the pool when the block ends
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 prepared, prepared_statements? : Bool prepared_statements?, unprepared(query)
unprepared unprepared
Instance methods inherited from module DB::QueryMethods(DB::PoolStatement)
exec(query, *args_, args : Array | Nil = nil)
exec,
query(query, *args_, args : Array | Nil = nil)query(query, *args_, args : Array | Nil = nil, &) query, query_all(query, *args_, args : Array | Nil = nil, &block : ResultSet -> U) : Array(U) forall U
query_all(query, *args_, args : Array | Nil = nil, as types : Tuple)
query_all(query, *args_, args : Array | Nil = nil, as types : NamedTuple)
query_all(query, *args_, args : Array | Nil = nil, as type : Class) query_all, query_each(query, *args_, args : Array | Nil = nil, &) query_each, query_one(query, *args_, args : Array | Nil = nil, &block : ResultSet -> U) : U forall U
query_one(query, *args_, args : Array | Nil = nil, as types : Tuple)
query_one(query, *args_, args : Array | Nil = nil, as types : NamedTuple)
query_one(query, *args_, args : Array | Nil = nil, as type : Class) query_one, query_one?(query, *args_, args : Array | Nil = nil, &block : ResultSet -> U) : U | Nil forall U
query_one?(query, *args_, args : Array | Nil = nil, as types : Tuple)
query_one?(query, *args_, args : Array | Nil = nil, as types : NamedTuple)
query_one?(query, *args_, args : Array | Nil = nil, as type : Class) query_one?, scalar(query, *args_, args : Array | Nil = nil) scalar
Instance Method Detail
returns a connection from the pool
the returned connection must be returned
to the pool by explictly calling Connection#release
Return whether the statements should be prepared by default
Run the specified block every time a new connection is established, yielding the new connection to the block.
db = DB.open(DB_URL)
db.setup_connection do |connection|
connection.exec "SET TIME ZONE 'America/New_York'"
end
yields a Transaction
from a connection of the pool
Refer to BeginTransaction#transaction
for documentation.
yields a connection from the pool the connection is returned to the pool when the block ends