-
Notifications
You must be signed in to change notification settings - Fork 1
feat: PostgreSQL database/sql.Driver + Connector implementation #129
Copy link
Copy link
Open
Labels
area/driverDatabase/cache driver infrastructureDatabase/cache driver infrastructuredriver/postgresPostgreSQL driverPostgreSQL driver
Milestone
Description
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:
- Handler goroutine calls
db.QueryContext(ctx, sql, args...) - Driver encodes PG wire message, writes via event loop's
writeFn - Creates
pendingQuerywithdoneCh chan queryResult - Blocks on
<-doneCh(orctx.Done()for cancellation) - 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=mydbTransactions
Begin()sendsBEGINvia simple queryCommit()/Rollback()send respective commands- Connection is exclusively held during transaction (standard
database/sqlbehavior) - Isolation levels via
BeginTxwithTxOptions
Cancellation
ctx.Done()triggers PostgreSQL cancel protocol: connect to same host, sendCancelRequestwith PID + secret key- Cancel connection is separate from the query connection
Acceptance Criteria
-
DriverandConnectorinterfaces fully implemented -
ConnwithPrepare,Close,Begin - Context-aware variants:
PrepareContext,BeginTx,QueryContext,ExecContext -
StmtandRowsimplementations - 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.Registerwithceleris-postgresdriver 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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area/driverDatabase/cache driver infrastructureDatabase/cache driver infrastructuredriver/postgresPostgreSQL driverPostgreSQL driver