Pure-MoonBit PostgreSQL wire-protocol driver implementing @moondb.Driver — no C, like asyncpg.
Dependencies
moon add Lfan-ke/moon-postgresasync fn run() -> Unit raise {
let conn = @moon_postgres.PgConn::connect(
"127.0.0.1", 5432, "postgres", "postgres", "test",
)
conn.execute("CREATE TABLE hero (id int, name text)", []) |> ignore
// '?' placeholders are translated to '$1','$2' and bound out-of-band
// (never spliced into the SQL) — injection-safe to the wire.
conn.execute("INSERT INTO hero VALUES (?, ?)", [
@moondb.Int(1), @moondb.Text("Boromir"),
]) |> ignore
let rows = conn.query("SELECT id, name FROM hero WHERE id = ?", [@moondb.Int(1)])
println(rows[0].text_by("name")) // Boromir
conn.begin()
conn.execute("UPDATE hero SET name = ? WHERE id = ?", [
@moondb.Text("Faramir"), @moondb.Int(1),
]) |> ignore
conn.commit()
conn.close()
}sequenceDiagram
participant C as PgConn (client)
participant S as PostgreSQL
C->>S: StartupMessage (user, database, client_encoding)
S-->>C: Authentication (Ok / MD5 / cleartext)
C->>S: PasswordMessage (md5… token) %% if requested
S-->>C: AuthenticationOk
S-->>C: ParameterStatus*, BackendKeyData, ReadyForQuery
Note over C,S: simple query (no params)
C->>S: Query 'Q'
S-->>C: RowDescription 'T', DataRow 'D'*, CommandComplete 'C', ReadyForQuery 'Z'
Note over C,S: extended query (bound params, ?→$n)
C->>S: Parse 'P', Bind 'B', Describe 'D', Execute 'E', Sync 'S'
S-->>C: ParseComplete, BindComplete, RowDescription, DataRow*, CommandComplete, ReadyForQuerypub struct PgDriver {
host : String
port : Int
user : String
password : String
database : String
}fn base64_decode(s : String) -> Bytesfn base64_encode(data : Bytes) -> Stringfn build_describe_portal() -> Bytesfn build_execute() -> Bytesfn build_parse(sql : String) -> Bytesfn build_password(token : String) -> Bytesfn build_query(sql : String) -> Bytesfn build_sasl_initial(mechanism : String, client_first : String) -> Bytesfn build_sasl_response(client_final : String) -> Bytesfn build_startup(user : String, database : String) -> Bytesfn build_sync() -> Bytesfn build_terminate() -> Bytesfn concat_bytes(a : Bytes, b : Bytes) -> Bytesfn count_placeholders(sql : String) -> Intfn decode_bytea(s : String) -> Bytesfn hex_lower(data : Bytes) -> Stringfn md5(msg : Bytes) -> Bytesfn parse_double(s : String) -> Double?fn parse_int(s : String) -> Int?fn parse_int64(s : String) -> Int64?fn pbkdf2_sha256(password : Bytes, salt : Bytes, iterations : Int) -> Bytesfn pg_md5_password(user : String, password : String, salt : Bytes) -> Stringfn scram_client_final(password : Bytes, client_first_bare : String, server_first : String) -> (String, Bytes) raise DbErrorfn scram_client_proof(salted : Bytes, auth_message : Bytes) -> Bytesfn scram_server_signature(salted : Bytes, auth_message : Bytes) -> Bytesfn translate_placeholders(sql : String) -> Stringfn utf8_decode(data : Bytes) -> StringPure-MoonBit PostgreSQL wire-protocol driver implementing @moondb.Driver — no C, like asyncpg.
Dependencies