class
CQL::Postgres::EngineSource
- CQL::Postgres::EngineSource
- CQL::ConnectionSource
- Reference
- Object
Overview
Runs a CQL::Schema on an AsyncEngine's pool: models, queries, migrations,
schema.transaction and schema.with_connection use the engine's
fiber-safe pool, statement cache and health checks without using(engine).
SOURCE = CQL::Postgres::EngineSource.connect("postgres://app@db/app?pool_size=10")
AppDB = CQL::Schema.define(:app, "postgres://app@db/app", CQL::Adapter::Postgres, source: SOURCE) do
table :users do
primary :id, Int64, auto_increment: true
text :name
end
end
SOURCE.engine.pipeline([AppDB.query.from(:users).select(:name)]) # the engine's own API stays reachable
A connection that cannot be opened raises DB::ConnectionRefused and a saturated pool DB::PoolTimeout (the
engine itself raises the underlying IO::Error and CQL::Postgres::PoolTimeout, kept as cause). crystal-db's
pool raises DB::ConnectionRefused only for the first connect and DB::PoolRetryAttemptsExceeded for a lazy one,
so code rescuing that class will not match here. A closed pool raises CQL::Postgres::PoolClosed. The engine
does not retry connects.
Closing the source closes the engine for every schema that shares it.
Defined in:
postgres/engine_source.crConstructors
- .connect(url : String, adapter : CQL::Adapter | Nil = nil, setup : Proc(Connection, Nil) | Nil = nil, reset : String | Nil = nil) : EngineSource
- .new(engine : AsyncEngine)
Instance Method Summary
-
#adapter : CQL::Adapter | Nil
The adapter this source speaks, when it is bound to one (a
Schemarefuses a different one). - #close : Nil
-
#database_uri(uri : String) : String
The URL without the engine's pool options (
pool_size,max_queries, ...), for code that opens a second connection fromSchema#urithrough crystal-db (SchemaDump). - #engine : AsyncEngine
- #pool_stats : PoolStats
-
#transaction(& : DB::Transaction -> _)
Runs the block in a transaction on one connection: committed when the block returns, rolled back when it raises.
-
#using_connection(& : DB::Connection -> _)
Lends one connection to the block and takes it back afterwards.
Instance methods inherited from class CQL::ConnectionSource
adapter : Adapter | Nil
adapter,
close : Nil
close,
database_uri(uri : String) : String
database_uri,
pool_stats : PoolStats
pool_stats,
transaction(& : DB::Transaction -> _)
transaction,
using_connection(& : DB::Connection -> _)
using_connection
Constructor Detail
Instance Method Detail
The adapter this source speaks, when it is bound to one (a Schema refuses a different one).
The URL without the engine's pool options (pool_size, max_queries, ...), for code that opens a second
connection from Schema#uri through crystal-db (SchemaDump). Connection settings (sslmode,
max_protocol_version, binary_params, ...) stay: that driver honours them too. It must name the same server
as the engine.
Runs the block in a transaction on one connection: committed when the
block returns, rolled back when it raises. DB::Rollback rolls back
quietly and makes this return nil.
Lends one connection to the block and takes it back afterwards.