Guest-side database access over the db.* host calls (abiv1 db.proto), two surfaces sharing one injected transport: - database/sql driver registered as "bnwasm" (driver.go) — QueryContext/ ExecContext/BeginTx over db.query/db.exec/db.tx_*; named args rejected. - plugin.Pool (pool.go) handing out a pgx.Tx-shaped value (tx.go), so the pgx-flavored sqlc DBTX every current plugin generates against (sql_package: pgx/v5) is satisfied with no source edits. This is the primary path: their DBTX needs pgconn.CommandTag/pgx.Rows/pgx.Row, which database/sql cannot produce. DbValue↔Go mapping (dbvalue.go) covers all 11 oneof arms both directions; DbError surfaces as *pgconn.PgError (SQLSTATE preserved for errors.As); nested tx/savepoints rejected with a clear error (no fleet plugin uses them). The scan contract is pinned in the exported DbValueFixtures table (dbvalue_fixtures.go) that the WO-WZ-007 host executor mirrors. Tests: driver_test.go (fake host — every DbValue variant round-trips with correct scan types, exec rows-affected, ordered host-call assertions for tx commit/rollback sequences, post-rollback autocommit carries no handle) and sqlcgen_test.go (vendored sqlc-style Queries + WithTx run against the fake host). Native + wasip1 builds green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package bnwasm
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
// Pool implements plugin.Pool (the pgx-flavored interface plugins bind their
|
|
// sqlc DBTX to) over the db.* host calls. It is also itself a valid DBTX: its
|
|
// Exec/Query/QueryRow run in autocommit (tx_handle 0), so a plugin can pass the
|
|
// Pool directly to sqlc's New(db) for non-transactional queries, exactly as it
|
|
// passes a *pgxpool.Pool today.
|
|
type Pool struct {
|
|
t Transport
|
|
}
|
|
|
|
// NewPool returns a Pool bound to transport t. A nil t (native build / DESCRIBE
|
|
// probe) yields a Pool whose every operation fails cleanly with errNoHost.
|
|
func NewPool(t Transport) *Pool { return &Pool{t: t} }
|
|
|
|
// Begin opens a host-side transaction and returns a pgx.Tx bound to its handle.
|
|
func (p *Pool) Begin(ctx context.Context) (pgx.Tx, error) {
|
|
handle, err := p.t.txBegin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Tx{t: p.t, handle: handle}, nil
|
|
}
|
|
|
|
// Exec runs a statement in autocommit and returns its command tag.
|
|
func (p *Pool) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
|
|
return p.t.exec(ctx, sql, 0, args)
|
|
}
|
|
|
|
// Query runs a rows-returning statement in autocommit.
|
|
func (p *Pool) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
|
|
resp, err := p.t.query(ctx, sql, 0, args)
|
|
if err != nil {
|
|
return errRows(err), err
|
|
}
|
|
return newPgxRows(resp), nil
|
|
}
|
|
|
|
// QueryRow runs a rows-returning statement in autocommit and returns the first
|
|
// row; any error is deferred to Row.Scan, matching pgx.
|
|
func (p *Pool) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
|
|
resp, err := p.t.query(ctx, sql, 0, args)
|
|
if err != nil {
|
|
return &pgxRow{err: err}
|
|
}
|
|
return &pgxRow{rows: newPgxRows(resp)}
|
|
}
|
|
|
|
// errRows is a closed, empty pgx.Rows carrying err, so a Query caller that
|
|
// ignores the returned error and iterates still terminates and reports it.
|
|
func errRows(err error) *pgxRows {
|
|
return &pgxRows{idx: -1, err: err, closed: true}
|
|
}
|