///|
async fn _pool_quick_start(
host : String,
user : String,
database : String,
password : String,
) -> Unit {
@async.with_task_group(group => {
let config = @pgpool.Config::Config(
[@pgpool.ConnectionTarget(host~)],
user~,
dbname=database,
password~,
application_name="my-service",
pool=PoolConfig(4),
)
let (pool, executor) = @pgpool.Pool::create(config)
let task = group.spawn(no_wait=true, () => executor.run())
pool.ready()
let value : Int32 = pool
.query_one("select 1::int4 as value")
.get_name("value")
ignore(value)
pool.close()
task.wait()
})
}///|
async fn _session_example(pool : @pgpool.Pool) -> Int32 {
pool.with_session(session => {
session.batch_execute("set application_name = 'pool-example'")
session.query_one("select 42::int4 as value").get_name("value")
})
}///|
async fn _pool_transaction(pool : @pgpool.Pool) -> Unit {
pool.with_transaction(tx => {
ignore(tx.execute("update accounts set active = true"))
()
})
}///|
async fn _pool_serializable_transaction(pool : @pgpool.Pool) -> Unit {
pool.with_transaction(
_tx => (),
options=@pgpool.TransactionOptions(
isolation_level=@pgpool.IsolationLevel::Serializable,
),
)
}///|
fn _pool_config() -> @pgpool.PoolConfig raise {
PoolConfig(
8,
statement_cache_capacity=100,
timeouts=Timeouts(wait_ms=Some(500)),
queue_mode=@pgpool.QueueMode::fifo(),
recycling_method=@pgpool.RecyclingMethod::verified(),
)
}///|
async fn _cancellable(pool : @pgpool.Pool) -> Unit {
@async.with_task_group(group => {
ignore(
pool.with_session(session => {
session.run_cancellable((op, token) => {
group.spawn_bg(no_wait=true, () => {
@async.sleep(50)
token.cancel()
})
let value : Int32 = op
.query_one("select pg_sleep(5), 1::int4 as value")
.get_name("value")
ignore(value)
})
}),
) catch {
_ => ()
}
})
}pub(open) trait GenericClient {
async fn query_all(Self, String, params? : Array[&ToSql]) -> Array[Row]
async fn query_one(Self, String, params? : Array[&ToSql]) -> Row
async fn query_opt(Self, String, params? : Array[&ToSql]) -> Row?
async fn query_typed_all(Self, String, Array[Type], params? : Array[&ToSql]) -> Array[Row]
async fn execute(Self, String, params? : Array[&ToSql]) -> UInt64
async fn batch_execute(Self, String) -> Unit
async fn check_connection(Self) -> Unit
}pub suberror PoolError {
Closed
ExecutorAlreadyStarted
Timeout(TimeoutKind)
ScopeExpired
HookLeftOpenTransaction
InvalidConfig(String)
} derive(Eq, Debug)pub struct Config {
user : String
password : String?
dbname : String
options : String?
application_name : String
ssl_mode : SslMode?
ssl_root_cert : String?
channel_binding : ChannelBinding?
connect_timeout_ms : Int?
keepalives : Bool?
keepalives_idle_s : Int?
target_session_attrs : TargetSessionAttrs?
load_balance_hosts : LoadBalanceHosts?
pool : PoolConfig
// private fields
} derive(Eq)fn Config::Config(targets : Array[ConnectionTarget], user~ : String, dbname? : String, password? : String, ssl_mode? : SslMode, ssl_root_cert? : String, channel_binding? : ChannelBinding, application_name~ : String, options? : String, connect_timeout_ms? : Int, keepalives? : Bool, keepalives_idle_s? : Int, target_session_attrs? : TargetSessionAttrs, load_balance_hosts? : LoadBalanceHosts, pool~ : PoolConfig) -> Config raisefn ConnectionTarget::ConnectionTarget(host? : String, hostaddr? : String, port? : Int) -> ConnectionTargettype Connectortype Operationtype OperationCancelTokentype Poolimpl GenericClient for Poolasync fn[T] Pool::with_transaction(self : Pool, f : async (Transaction) -> T, options? : TransactionOptions) -> Tpub struct PoolConfig {
max_size : Int
timeouts : Timeouts
queue_mode : QueueMode
recycling_method : RecyclingMethod
statement_cache_capacity : Int
async_message_capacity : Int
} derive(Eq, Debug)fn PoolConfig::PoolConfig(max_size : Int, timeouts? : Timeouts, queue_mode? : QueueMode, recycling_method? : RecyclingMethod, statement_cache_capacity? : Int, async_message_capacity? : Int) -> PoolConfig raisetype PoolExecutorfn PoolOptions::PoolOptions(connector? : Connector, post_create? : async (Operation) -> Unit, pre_recycle? : async (Operation) -> Unit, post_recycle? : async (Operation) -> Unit) -> PoolOptionstype PreparedStatementasync fn PreparedStatement::query_all(self : PreparedStatement, params? : Array[&ToSql]) -> Array[Row]async fn[T] PreparedStatement::with_stream(self : PreparedStatement, params? : Array[&ToSql], f : async (RowStream) -> T) -> Ttype Sessionimpl GenericClient for Sessionasync fn[T] Session::run_cancellable(self : Session, f : async (Operation, OperationCancelToken) -> T) -> Tasync fn[T] Session::with_copy_out(self : Session, sql : String, f : async (CopyOutStream) -> T) -> Tasync fn[T] Session::with_prepared(self : Session, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Session::with_prepared_typed(self : Session, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Tasync fn[T] Session::with_simple_query(self : Session, sql : String, f : async (SimpleQueryStream) -> T) -> Tasync fn[T] Session::with_transaction(self : Session, f : async (Transaction) -> T, options? : TransactionOptions) -> Ttype Transactionimpl GenericClient for Transactionasync fn Transaction::query_all(self : Transaction, sql : String, params? : Array[&ToSql]) -> Array[Row]async fn Transaction::query_typed_all(self : Transaction, sql : String, param_types : Array[Type], params? : Array[&ToSql]) -> Array[Row]async fn[T] Transaction::with_prepared(self : Transaction, sql : String, f : async (PreparedStatement) -> T) -> Tasync fn[T] Transaction::with_prepared_typed(self : Transaction, sql : String, param_types : Array[Type], f : async (PreparedStatement) -> T) -> Tasync fn[T] Transaction::with_savepoint(self : Transaction, name : String, f : async (Transaction) -> T) -> Tasync fn[T] Transaction::with_stream(self : Transaction, sql : String, params? : Array[&ToSql], f : async (RowStream) -> T) -> Tasync fn[T] Transaction::with_typed_stream(self : Transaction, sql : String, param_types : Array[Type], params? : Array[&ToSql], f : async (RowStream) -> T) -> Tpub struct TransactionOptions {
isolation_level : IsolationLevel?
read_only : Bool?
deferrable : Bool?
} derive(Eq, Debug)fn TransactionOptions::TransactionOptions(isolation_level? : IsolationLevel, read_only? : Bool, deferrable? : Bool) -> TransactionOptionsInstall
Download zipA secure, easy-to-use PostgreSQL client library for MoonBit with an included connection pool.