Documentation
¶
Overview ¶
Package driver defines the interfaces that every database backend must implement to work with Grove. It is deliberately thin: the core types are interfaces so that concrete drivers (PostgreSQL, MySQL, SQLite, MongoDB, ...) live in their own packages and depend on this contract without pulling in any specific database library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConnAcquirer ¶
type ConnAcquirer interface {
// AcquireConn acquires a dedicated connection from the pool.
// The returned DedicatedConn provides Exec/Query/QueryRow methods
// that are guaranteed to run on the same underlying connection.
// The caller MUST call Release() when done.
AcquireConn(ctx context.Context) (DedicatedConn, error)
}
ConnAcquirer is an optional interface that pool-based drivers implement to provide a dedicated connection from the pool. This is needed for operations that require session-level state (e.g., advisory locks) to remain on a single connection across multiple queries.
type DedicatedConn ¶
type DedicatedConn interface {
Exec(ctx context.Context, query string, args ...any) (Result, error)
Query(ctx context.Context, query string, args ...any) (Rows, error)
QueryRow(ctx context.Context, query string, args ...any) Row
Release()
}
DedicatedConn represents a single, dedicated database connection acquired from a pool. All operations execute on the same underlying connection, making it safe for session-level state like advisory locks.
type Dialect ¶
type Dialect interface {
// Name returns the dialect name (e.g., "pg", "mysql", "sqlite").
Name() string
// Quote quotes an identifier (table or column name) using the dialect's
// quoting convention. For example, PostgreSQL uses double quotes while
// MySQL uses backticks.
Quote(ident string) string
// Placeholder returns the nth parameter placeholder for prepared
// statements. n is 1-indexed.
// Examples:
// PostgreSQL -> "$1", "$2", ...
// MySQL -> "?", "?", ...
Placeholder(n int) string
// GoToDBType maps a Go reflect.Type to the appropriate database-native
// column type string, taking field options (e.g., explicit SQLType) into
// account.
GoToDBType(goType reflect.Type, opts schema.FieldOptions) string
// AppendBytes appends a byte-escaped representation of v to the byte
// slice b and returns the extended slice. The encoding is
// dialect-specific (e.g., hex encoding for PostgreSQL bytea).
AppendBytes(b []byte, v []byte) []byte
// AppendTime appends a time value formatted for the dialect to b and
// returns the extended slice.
AppendTime(b []byte, t time.Time) []byte
}
Dialect encapsulates database-specific syntax rules. Each concrete driver provides its own Dialect implementation so that the query builder can emit correct SQL (or equivalent) for the target database.
type Driver ¶
type Driver interface {
// Name returns the driver identifier (e.g., "pg", "mysql", "mongo").
Name() string
// Open initializes a connection using the given DSN.
Open(ctx context.Context, dsn string, opts ...Option) error
// Close terminates all connections.
Close() error
// Dialect returns the driver's SQL/query dialect.
Dialect() Dialect
// Ping checks connectivity.
Ping(ctx context.Context) error
// BeginTx starts a transaction.
BeginTx(ctx context.Context, opts *TxOptions) (Tx, error)
// Exec executes a query that doesn't return rows.
Exec(ctx context.Context, query string, args ...any) (Result, error)
// Query executes a query that returns rows.
Query(ctx context.Context, query string, args ...any) (Rows, error)
// QueryRow executes a query that returns at most one row.
QueryRow(ctx context.Context, query string, args ...any) Row
// SupportsReturning indicates if INSERT...RETURNING is supported.
SupportsReturning() bool
}
Driver is the core interface every database backend implements.
type DriverOptions ¶
type DriverOptions struct {
PoolSize int
MinConns int32
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
HealthCheckPeriod time.Duration
QueryTimeout time.Duration
Logger log.Logger
Extra map[string]any // driver-specific options
}
DriverOptions holds driver-level configuration.
func ApplyOptions ¶
func ApplyOptions(opts []Option) *DriverOptions
ApplyOptions folds the given options onto a set of default DriverOptions and returns the resulting configuration.
func DefaultDriverOptions ¶
func DefaultDriverOptions() *DriverOptions
DefaultDriverOptions returns a DriverOptions with sensible defaults.
type IsolationLevel ¶
type IsolationLevel int
IsolationLevel represents a SQL transaction isolation level.
const ( // LevelDefault uses the database's default isolation level. LevelDefault IsolationLevel = iota // LevelReadUncommitted allows reading uncommitted changes from other // transactions. LevelReadUncommitted // LevelReadCommitted only sees data committed before the query began. LevelReadCommitted // LevelRepeatableRead ensures that re-reading the same row within a // transaction yields the same data. LevelRepeatableRead // LevelSerializable is the strictest level; transactions execute as if // they were serialized one after another. LevelSerializable )
func (IsolationLevel) String ¶
func (l IsolationLevel) String() string
String returns a human-readable name for the isolation level.
type Option ¶
type Option func(*DriverOptions)
Option configures a driver during Open.
func WithHealthCheckPeriod ¶
WithHealthCheckPeriod returns an Option that sets the interval between automatic health checks on idle connections. On serverless databases like Neon, each health check costs bandwidth; consider setting this to 5m+.
func WithLogger ¶
WithLogger returns an Option that sets the structured logger.
func WithMaxConnIdleTime ¶
WithMaxConnIdleTime returns an Option that sets the maximum time a connection can sit idle before it is closed.
func WithMaxConnLifetime ¶
WithMaxConnLifetime returns an Option that sets the maximum lifetime of a connection. Connections older than this duration are closed and replaced.
func WithMinConns ¶
WithMinConns returns an Option that sets the minimum number of connections in the pool. Connections above this count may be closed when idle.
func WithPoolSize ¶
WithPoolSize returns an Option that sets the connection pool size.
func WithQueryTimeout ¶
WithQueryTimeout returns an Option that sets the default query timeout.
type Preparer ¶
Preparer is an optional interface for drivers that support prepared statements. Used for efficient bulk inserts via prepared-statement loops.
type Result ¶
type Result interface {
// RowsAffected returns the number of rows affected by the statement.
RowsAffected() (int64, error)
// LastInsertId returns the last auto-generated ID (if supported by the
// driver). Drivers that do not support auto-increment IDs (e.g.,
// PostgreSQL without RETURNING) may return 0 and an error.
LastInsertId() (int64, error)
}
Result represents the outcome of an exec query (INSERT, UPDATE, DELETE). It mirrors the standard database/sql.Result interface so that concrete drivers can return the native result directly or wrap it.
type Row ¶
Row represents a single row result from QueryRow. If the query returns no rows, Scan will return an appropriate error (e.g., sql.ErrNoRows).
type Rows ¶
type Rows interface {
// Next advances to the next row. It returns false when no more rows are
// available or an error occurred during iteration.
Next() bool
// Scan copies the current row's columns into dest. The number of dest
// values must match the number of columns in the result set.
Scan(dest ...any) error
// Columns returns the column names of the result set.
Columns() ([]string, error)
// Close closes the rows iterator, releasing any held resources.
Close() error
// Err returns any error encountered during iteration (other than
// io.EOF). It should be checked after the Next loop completes.
Err() error
}
Rows represents a result set from a query. Callers must call Close when finished iterating, typically via a defer.
type Stmt ¶
Stmt is a prepared statement that can be executed multiple times with different arguments. It must be closed when no longer needed.
type StreamCapable ¶
type StreamCapable interface {
// SupportsStreaming returns true if the driver supports server-side cursors
// or equivalent streaming mechanisms.
SupportsStreaming() bool
// SupportsCDC returns true if the driver supports change data capture
// (e.g., PG logical replication, Mongo change streams, MySQL binlog).
SupportsCDC() bool
}
StreamCapable is an optional interface that drivers implement to indicate support for streaming/cursor-based result iteration. Drivers that don't support streaming simply don't implement this.
type Tx ¶
type Tx interface {
// Exec executes a query within the transaction.
Exec(ctx context.Context, query string, args ...any) (Result, error)
// Query executes a query that returns rows within the transaction.
Query(ctx context.Context, query string, args ...any) (Rows, error)
// QueryRow executes a query that returns a single row within the
// transaction.
QueryRow(ctx context.Context, query string, args ...any) Row
// Commit commits the transaction. After Commit returns successfully,
// all changes made within the transaction are durable.
Commit() error
// Rollback rolls back the transaction, discarding all changes.
// Rollback is safe to call after Commit; it will be a no-op if the
// transaction has already been committed.
Rollback() error
}
Tx represents a database transaction. All queries executed through a Tx participate in the same underlying database transaction and share its isolation guarantees.
type TxOptions ¶
type TxOptions struct {
// IsolationLevel sets the isolation level for the transaction.
IsolationLevel IsolationLevel
// ReadOnly marks the transaction as read-only when true. The database
// may use this hint for optimisation.
ReadOnly bool
}
TxOptions holds transaction configuration.