Skip to content

feat: PostgreSQL database/sql.Driver + Connector implementation #129

@FumingPower3925

Description

@FumingPower3925

Summary

Implement the database/sql Driver and Connector interfaces, providing the standard Go database access API on top of the celeris PostgreSQL wire protocol.

Design

Package: driver/postgres/

Interfaces to Implement

// database/sql/driver interfaces
type Driver interface {
    Open(name string) (Conn, error)
}

type Connector interface {
    Connect(ctx context.Context) (Conn, error)
    Driver() Driver
}

type Conn interface {
    Prepare(query string) (Stmt, error)
    Close() error
    Begin() (Tx, error)
}

// Extended interfaces for context support
type ConnPrepareContext interface {
    PrepareContext(ctx context.Context, query string) (Stmt, error)
}

type ConnBeginTx interface {
    BeginTx(ctx context.Context, opts TxOptions) (Tx, error)
}

type QueryerContext interface {
    QueryContext(ctx context.Context, query string, args []NamedValue) (Rows, error)
}

type ExecerContext interface {
    ExecContext(ctx context.Context, query string, args []NamedValue) (Result, error)
}

Blocking API Bridge

database/sql is synchronous but the celeris event loop is async. The bridge:

  1. Handler goroutine calls db.QueryContext(ctx, sql, args...)
  2. Driver encodes PG wire message, writes via event loop's writeFn
  3. Creates pendingQuery with doneCh chan queryResult
  4. Blocks on <-doneCh (or ctx.Done() for cancellation)
  5. Event loop receives PG response, parses via protocol state machine, sends result on doneCh
type pgConn struct {
    fd       int
    state    *PGState        // protocol state machine
    pending  chan *request    // requests queued from handler goroutine
    writeFn  func([]byte)    // event loop write
}

Registration

func init() {
    sql.Register("celeris-postgres", &Driver{})
}

// DSN format
// postgres://user:pass@host:port/dbname?sslmode=disable
// or key=value format: host=localhost port=5432 user=app dbname=mydb

Transactions

  • Begin() sends BEGIN via simple query
  • Commit() / Rollback() send respective commands
  • Connection is exclusively held during transaction (standard database/sql behavior)
  • Isolation levels via BeginTx with TxOptions

Cancellation

  • ctx.Done() triggers PostgreSQL cancel protocol: connect to same host, send CancelRequest with PID + secret key
  • Cancel connection is separate from the query connection

Acceptance Criteria

  • Driver and Connector interfaces fully implemented
  • Conn with Prepare, Close, Begin
  • Context-aware variants: PrepareContext, BeginTx, QueryContext, ExecContext
  • Stmt and Rows implementations
  • Blocking bridge with completion channels
  • DSN parsing (both URL and key=value formats)
  • Transaction support with isolation levels
  • Context cancellation triggers PG cancel protocol
  • sql.Register with celeris-postgres driver name
  • Integration test: sql.Open → query → scan → close

Dependencies

  • Depends on 125 (simple query protocol)
  • Depends on 126 (extended query / prepared statements)
  • Depends on 127 (type encoding/decoding)

Metadata

Metadata

Labels

area/driverDatabase/cache driver infrastructuredriver/postgresPostgreSQL driver

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions