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>
132 lines
4.7 KiB
Go
132 lines
4.7 KiB
Go
package bnwasm
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
abiv1 "git.dev.alexdunmow.com/block/core/abi/v1"
|
|
"github.com/google/uuid"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
// FixtureTime is the fixed timestamp used by the shared DbValue fixtures, so
|
|
// both sides of the boundary compare against one deterministic value.
|
|
var FixtureTime = time.Date(2026, 7, 3, 12, 30, 0, 0, time.UTC)
|
|
|
|
// FixtureUUID is the fixed UUID used by the shared DbValue fixtures.
|
|
var FixtureUUID = uuid.MustParse("11111111-2222-3333-4444-555555555555")
|
|
|
|
// DbValueFixture is one entry in the canonical DbValue↔Go mapping table.
|
|
type DbValueFixture struct {
|
|
// Name identifies the DbValue variant.
|
|
Name string
|
|
// Value is the wire value the host would send back in a DbRowsResponse cell
|
|
// (and the guest would send as an argument for the non-NULL scalar cases).
|
|
Value *abiv1.DbValue
|
|
// NewDest returns a fresh pointer to the canonical Go scan destination type
|
|
// that plugin sqlc code binds for this variant.
|
|
NewDest func() any
|
|
// Want is the expected dereferenced value after scanning Value into
|
|
// NewDest(). Compared with reflect.DeepEqual (times via .Equal).
|
|
Want any
|
|
// DriverValue is the expected database/sql driver.Value for this variant,
|
|
// documenting the "bnwasm" driver's mapping (uuid/numeric→string,
|
|
// text[]→Postgres array literal).
|
|
DriverValue any
|
|
}
|
|
|
|
// DbValueFixtures is the authoritative DbValue↔Go mapping table. It is the
|
|
// shared contract WO-WZ-007 (the CMS host executor) MUST mirror: every variant
|
|
// the guest driver produces on scan, the host must be able to build on read,
|
|
// and vice-versa. Exported (non-test) precisely so the host's tests in the CMS
|
|
// module can import and assert against the same table rather than duplicating a
|
|
// drift-prone copy.
|
|
//
|
|
// Every oneof arm of abiv1.DbValue.Kind appears exactly once.
|
|
var DbValueFixtures = []DbValueFixture{
|
|
{
|
|
Name: "null",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_Null{Null: true}},
|
|
NewDest: func() any { return new(*string) }, // **string: NULL → (*string)(nil)
|
|
Want: (*string)(nil),
|
|
DriverValue: nil,
|
|
},
|
|
{
|
|
Name: "bool",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_BoolValue{BoolValue: true}},
|
|
NewDest: func() any { return new(bool) },
|
|
Want: true,
|
|
DriverValue: true,
|
|
},
|
|
{
|
|
Name: "int64",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_Int64Value{Int64Value: 42}},
|
|
NewDest: func() any { return new(int64) },
|
|
Want: int64(42),
|
|
DriverValue: int64(42),
|
|
},
|
|
{
|
|
Name: "int32", // narrowing coercion sqlc emits for int4 columns
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_Int64Value{Int64Value: 7}},
|
|
NewDest: func() any { return new(int32) },
|
|
Want: int32(7),
|
|
DriverValue: int64(7),
|
|
},
|
|
{
|
|
Name: "float64",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_Float64Value{Float64Value: 3.5}},
|
|
NewDest: func() any { return new(float64) },
|
|
Want: 3.5,
|
|
DriverValue: 3.5,
|
|
},
|
|
{
|
|
Name: "string",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_StringValue{StringValue: "hello"}},
|
|
NewDest: func() any { return new(string) },
|
|
Want: "hello",
|
|
DriverValue: "hello",
|
|
},
|
|
{
|
|
Name: "bytes",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_BytesValue{BytesValue: []byte{0x01, 0x02, 0x03}}},
|
|
NewDest: func() any { return new([]byte) },
|
|
Want: []byte{0x01, 0x02, 0x03},
|
|
DriverValue: []byte{0x01, 0x02, 0x03},
|
|
},
|
|
{
|
|
Name: "timestamp",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_TimestampValue{TimestampValue: timestamppb.New(FixtureTime)}},
|
|
NewDest: func() any { return new(time.Time) },
|
|
Want: FixtureTime,
|
|
DriverValue: FixtureTime,
|
|
},
|
|
{
|
|
Name: "uuid",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_UuidValue{UuidValue: FixtureUUID.String()}},
|
|
NewDest: func() any { return new(uuid.UUID) },
|
|
Want: FixtureUUID,
|
|
DriverValue: FixtureUUID.String(),
|
|
},
|
|
{
|
|
Name: "jsonb",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_JsonbValue{JsonbValue: []byte(`{"a":1}`)}},
|
|
NewDest: func() any { return new(json.RawMessage) },
|
|
Want: json.RawMessage(`{"a":1}`),
|
|
DriverValue: []byte(`{"a":1}`),
|
|
},
|
|
{
|
|
Name: "numeric",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_NumericValue{NumericValue: "123.45"}},
|
|
NewDest: func() any { return new(string) },
|
|
Want: "123.45",
|
|
DriverValue: "123.45",
|
|
},
|
|
{
|
|
Name: "text_array",
|
|
Value: &abiv1.DbValue{Kind: &abiv1.DbValue_TextArrayValue{TextArrayValue: &abiv1.TextArray{Values: []string{"a", "b"}}}},
|
|
NewDest: func() any { return new([]string) },
|
|
Want: []string{"a", "b"},
|
|
DriverValue: `{"a","b"}`,
|
|
},
|
|
}
|