ciallo-driver-pg

    PostgreSQL v3 wire driver (SCRAM-SHA-256, LRU prepared statement cache, cancellation propagation, TLS, and JS Serverless bridge) for unmbt/ciallo-orm

    postgres
    postgresql
    pg
    orm
    driver
    scram
    ciallo
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    5 hours ago
    Downloads
    1

    #unmbt/ciallo-driver-pg

    PostgreSQL v3 wire driver for unmbt/ciallo-orm featuring:

    • Four-target pure wire protocol & SCRAM-SHA-256 package (unmbt/ciallo-driver-pg/proto)
    • Native/Wasm pooled TCP driver with optional TLS (moonbitlang/core/async/tls), per-connection LRU prepared statement cache (default 256), and out-of-band CancelRequest propagation on cancellation
    • JavaScript bridge (JsPgDriver) for pg, @neondatabase/serverless, and Cloudflare Hyperdrive

    PgDecoder

    Stateful streaming PG backend message decoder using a cursor buffer to avoid O(N^2) copies.

    ScramClient

    Client-side SCRAM-SHA-256 authentication state machine.

    PgTransport

    pub(open) trait PgTransport {
    async fn write_all(Self, payload : Bytes) -> Unit raise
    DataError

    async fn read_chunk(Self) -> Bytes? raise
    DataError

    async fn close(Self) -> Unit
    }

    Pluggable PostgreSQL byte-stream transport abstraction. Production Native/Wasm connections use TCP (with optional TLS upgrade); tests may inject MockPgTransport.

    MockPgTransport

    pub struct MockPgTransport {
    // private fields
    }

    Deterministic in-memory mock transport for unit testing PG Wire v3 handshakes, prepared statement caching, transactions, and cancellation propagation.

    MockPgTransport::MockPgTransport

    fn MockPgTransport::MockPgTransport(responses? : Array[Bytes], responder? : (Bytes) -> Array[Bytes]) -> MockPgTransport

    MockPgTransport::is_closed

    fn MockPgTransport::is_closed(self : MockPgTransport) -> Bool

    MockPgTransport::push_response

    fn MockPgTransport::push_response(self : MockPgTransport, chunk : Bytes) -> Unit

    MockPgTransport::set_responder

    fn MockPgTransport::set_responder(self : MockPgTransport, f : (Bytes) -> Array[Bytes]) -> Unit

    MockPgTransport::written_frames

    fn MockPgTransport::written_frames(self : MockPgTransport) -> Array[Bytes]

    PgConfig

    pub(all) struct PgConfig {
    host : String
    port : Int
    user : String
    password : String
    database : String
    ssl_mode : SslMode
    connect_timeout_ms : Int
    cancel_timeout_ms : Int
    statement_cache_size : Int
    pool :
    PoolConfig

    application_name : String
    } derive(Eq,
    Debug
    )

    PostgreSQL driver connection and pool configuration.

    PgConfig::PgConfig

    fn PgConfig::PgConfig(host? : String, port? : Int, user? : String, password? : String, database? : String, ssl_mode? : SslMode, connect_timeout_ms? : Int, cancel_timeout_ms? : Int, statement_cache_size? : Int, pool? :
    PoolConfig
    , application_name? : String) -> PgConfig

    PgConfig::from_url

    fn PgConfig::from_url(dsn : StringView) -> PgConfig raise
    DataError

    Parses a postgres:// or postgresql:// connection URL into PgConfig.

    PgConnection

    pub struct PgConnection {
    // private fields
    }

    Active PostgreSQL wire-protocol v3 connection with LRU statement cache.

    PgConnection::backend_key

    fn PgConnection::backend_key(self : PgConnection) -> (Int, Int)

    PgConnection::cached_statement_count

    fn PgConnection::cached_statement_count(self : PgConnection) -> Int

    PgConnection::close

    async fn PgConnection::close(self : PgConnection) -> Unit

    PgConnection::handshake

    async fn PgConnection::handshake(transport : &PgTransport, config : PgConfig, scram_nonce? : String) -> PgConnection raise
    DataError

    Performs the PG v3 Startup + Authentication (Cleartext / SCRAM-SHA-256) handshake.

    PgConnection::run_extended

    Runs a parameterized SQL query using PostgreSQL Extended Query Protocol with LRU prepared statement caching.

    PgConnection::run_simple

    async fn PgConnection::run_simple(self : PgConnection, sql : String) -> Unit raise
    DataError

    Runs a simple SQL command (e.g., BEGIN, COMMIT, ROLLBACK, SAVEPOINT).

    PgConnector

    pub struct PgConnector {
    open : async (PgConfig) -> &PgTransport raise
    DataError

    cancel : async (PgConfig, Int, Int) -> Unit
    }

    Transport factory & out-of-band cancel callback used by PgDriver.

    PgConnector::PgConnector

    fn PgConnector::PgConnector(open~ : async (PgConfig) -> &PgTransport raise
    DataError
    , cancel? : async (PgConfig, Int, Int) -> Unit) -> PgConnector

    PgDriver

    pub struct PgDriver {
    // private fields
    }

    Pooled PostgreSQL v3 SqlDriver with LRU prepared statement caching and cancellation propagation.

    PgDriver::PgDriver

    fn PgDriver::PgDriver(config : PgConfig) -> PgDriver

    Creates a PgDriver using the platform's default socket connector.

    PgDriver::close

    async fn PgDriver::close(self : PgDriver) -> Unit

    PgDriver::from_url

    fn PgDriver::from_url(dsn : StringView) -> PgDriver raise
    DataError

    Creates a PgDriver from a postgres:// / postgresql:// DSN URL.

    PgDriver::pool_stats

    PgDriver::with_connector

    fn PgDriver::with_connector(config : PgConfig, connector : PgConnector) -> PgDriver

    Creates a PgDriver with a custom PgConnector (for testing or custom TLS/Unix-socket transports).

    SslMode

    pub(all) enum SslMode {
    Disable
    Prefer
    Require
    } derive(Eq,
    Debug
    )

    PostgreSQL TLS negotiation mode.

    decode_backend_message

    Decodes one complete PostgreSQL backend message from buf, returning (message, bytes_consumed). Returns None if more bytes are needed.

    decode_sql_cell

    Decodes a PostgreSQL text-format column cell using its FieldDesc OID.

    decode_sql_row

    Decodes a DataRow using a prior RowDescription into a @orm_schema.SqlRow.

    default_pg_connector

    fn default_pg_connector() -> PgConnector

    encode_bind

    fn encode_bind(portal_name~ : String, stmt_name~ : String, params~ : Array[Bytes?]) -> Bytes

    Encodes an Extended Query Bind ('B') message using text parameter & result format codes.

    encode_cancel_request

    fn encode_cancel_request(process_id~ : Int, secret_key~ : Int) -> Bytes

    Encodes a PostgreSQL CancelRequest packet (16 bytes, code 80877102).

    encode_close

    fn encode_close(target~ : Byte, name~ : String) -> Bytes

    Encodes an Extended Query Close ('C') message (target: b'S' or b'P').

    encode_describe

    fn encode_describe(target~ : Byte, name~ : String) -> Bytes

    Encodes an Extended Query Describe ('D') message (target: b'S' for Statement or b'P' for Portal).

    encode_execute

    fn encode_execute(portal_name~ : String, max_rows? : Int) -> Bytes

    Encodes an Extended Query Execute ('E') message (max_rows = 0 means unlimited).

    encode_parse

    fn encode_parse(stmt_name~ : String, sql~ : String, param_oids? : Array[Int]) -> Bytes

    Encodes an Extended Query Parse ('P') message.

    encode_password_message

    fn encode_password_message(password : StringView) -> Bytes

    Encodes a PasswordMessage ('p') for cleartext or MD5 authentication.

    encode_sasl_initial_response

    fn encode_sasl_initial_response(mechanism : StringView, initial_data : BytesView) -> Bytes

    Encodes a SASLInitialResponse ('p') message for SCRAM-SHA-256.

    encode_sasl_response

    fn encode_sasl_response(data : BytesView) -> Bytes

    Encodes a SASLResponse ('p') message carrying client-final-message.

    encode_simple_query

    fn encode_simple_query(sql : StringView) -> Bytes

    Encodes a Simple Query ('Q') message.

    encode_sql_param

    fn encode_sql_param(val :
    SqlValue
    ) -> Bytes?

    Encodes a SqlValue into PostgreSQL text-format wire parameter bytes (None for SQL NULL).

    encode_ssl_request

    fn encode_ssl_request() -> Bytes

    Encodes a PostgreSQL SSLRequest negotiation packet (8 bytes).

    encode_startup

    fn encode_startup(user~ : String, database~ : String, params? : Array[(String, String)]) -> Bytes

    Encodes a PostgreSQL v3 StartupMessage (no leading type byte).

    encode_sync

    fn encode_sync() -> Bytes

    Encodes a Sync ('S') message.

    encode_terminate

    fn encode_terminate() -> Bytes

    Encodes a Terminate ('X') message.

    map_pg_error

    Maps PostgreSQL ErrorResponse SQLSTATE codes into structured @codec.DataError variants.

    parse_command_complete_rows

    fn parse_command_complete_rows(tag : StringView) -> Int64

    Extracts the rows-affected count from a PostgreSQL CommandComplete tag (e.g., "INSERT 0 1", "UPDATE 5", "DELETE 3", "SELECT 10").