Documentation
¶
Overview ¶
Package sqlitedriver provides a SQLite driver for the Grove ORM, built on top of database/sql with modernc.org/sqlite (pure Go, no CGo).
Index ¶
- type CreateTableQuery
- func (q *CreateTableQuery) Build() (string, []any, error)
- func (q *CreateTableQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *CreateTableQuery) IfNotExists() *CreateTableQuery
- func (q *CreateTableQuery) Temp() *CreateTableQuery
- func (q *CreateTableQuery) WithForeignKey(fk string) *CreateTableQuery
- type DeleteQuery
- func (q *DeleteQuery) Build() (string, []any, error)
- func (q *DeleteQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *DeleteQuery) ForceDelete() *DeleteQuery
- func (q *DeleteQuery) Returning(columns ...string) *DeleteQuery
- func (q *DeleteQuery) Scan(ctx context.Context, dest ...any) error
- func (q *DeleteQuery) Where(query string, args ...any) *DeleteQuery
- func (q *DeleteQuery) WhereOr(query string, args ...any) *DeleteQuery
- func (q *DeleteQuery) WherePK() *DeleteQuery
- type DropTableQuery
- type InsertQuery
- func (q *InsertQuery) Build() (string, []any, error)
- func (q *InsertQuery) Column(columns ...string) *InsertQuery
- func (q *InsertQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *InsertQuery) MultiRow() *InsertQuery
- func (q *InsertQuery) OnConflict(clause string) *InsertQuery
- func (q *InsertQuery) Returning(columns ...string) *InsertQuery
- func (q *InsertQuery) Scan(ctx context.Context, dest ...any) error
- func (q *InsertQuery) Set(expr string, args ...any) *InsertQuery
- func (q *InsertQuery) Value(values ...any) *InsertQuery
- type RawQuery
- type SelectQuery
- func (q *SelectQuery) Build() (string, []any, error)
- func (q *SelectQuery) BuildCount() (string, []any, error)
- func (q *SelectQuery) Column(columns ...string) *SelectQuery
- func (q *SelectQuery) ColumnExpr(expr string, args ...any) *SelectQuery
- func (q *SelectQuery) Count(ctx context.Context) (int64, error)
- func (q *SelectQuery) GroupExpr(expr string) *SelectQuery
- func (q *SelectQuery) Having(query string, args ...any) *SelectQuery
- func (q *SelectQuery) Join(joinType, table, on string, args ...any) *SelectQuery
- func (q *SelectQuery) Limit(n int) *SelectQuery
- func (q *SelectQuery) Offset(n int) *SelectQuery
- func (q *SelectQuery) OrderExpr(expr string) *SelectQuery
- func (q *SelectQuery) Relation(name string) *SelectQuery
- func (q *SelectQuery) Scan(ctx context.Context, dest ...any) error
- func (q *SelectQuery) TableExpr(expr string, args ...any) *SelectQuery
- func (q *SelectQuery) Where(query string, args ...any) *SelectQuery
- func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery
- func (q *SelectQuery) WherePK() *SelectQuery
- func (q *SelectQuery) WithDeleted() *SelectQuery
- type SqliteDB
- func (db *SqliteDB) BeginTx(ctx context.Context, opts *driver.TxOptions) (driver.Tx, error)
- func (db *SqliteDB) BeginTxQuery(ctx context.Context, opts *driver.TxOptions) (*SqliteTx, error)
- func (db *SqliteDB) Close() error
- func (db *SqliteDB) Dialect() driver.Dialect
- func (db *SqliteDB) Exec(ctx context.Context, query string, args ...any) (driver.Result, error)
- func (db *SqliteDB) GroveDelete(model any) any
- func (db *SqliteDB) GroveInsert(model any) any
- func (db *SqliteDB) GroveSelect(model ...any) any
- func (db *SqliteDB) GroveTx(ctx context.Context, isolationLevel int, readOnly bool) (any, error)
- func (db *SqliteDB) GroveUpdate(model any) any
- func (db *SqliteDB) Name() string
- func (db *SqliteDB) NewCreateTable(model any) *CreateTableQuery
- func (db *SqliteDB) NewDelete(model any) *DeleteQuery
- func (db *SqliteDB) NewDropTable(model any) *DropTableQuery
- func (db *SqliteDB) NewInsert(model any) *InsertQuery
- func (db *SqliteDB) NewRaw(query string, args ...any) *RawQuery
- func (db *SqliteDB) NewSelect(model ...any) *SelectQuery
- func (db *SqliteDB) NewUpdate(model any) *UpdateQuery
- func (db *SqliteDB) Open(ctx context.Context, dsn string, opts ...driver.Option) error
- func (db *SqliteDB) Ping(ctx context.Context) error
- func (db *SqliteDB) Prepare(ctx context.Context, query string) (driver.Stmt, error)
- func (db *SqliteDB) Query(ctx context.Context, query string, args ...any) (driver.Rows, error)
- func (db *SqliteDB) QueryRow(ctx context.Context, query string, args ...any) driver.Row
- func (db *SqliteDB) SetHooks(engine *hook.Engine)
- func (db *SqliteDB) SupportsReturning() bool
- type SqliteDialect
- func (d *SqliteDialect) AppendBytes(b []byte, v []byte) []byte
- func (d *SqliteDialect) AppendTime(b []byte, t time.Time) []byte
- func (d *SqliteDialect) GoToDBType(goType reflect.Type, opts schema.FieldOptions) string
- func (d *SqliteDialect) Name() string
- func (d *SqliteDialect) Placeholder(n int) string
- func (d *SqliteDialect) Quote(ident string) string
- type SqliteTx
- func (t *SqliteTx) Commit() error
- func (t *SqliteTx) NewDelete(model any) *DeleteQuery
- func (t *SqliteTx) NewInsert(model any) *InsertQuery
- func (t *SqliteTx) NewRaw(query string, args ...any) *RawQuery
- func (t *SqliteTx) NewSelect(model ...any) *SelectQuery
- func (t *SqliteTx) NewUpdate(model any) *UpdateQuery
- func (t *SqliteTx) Rollback() error
- type UpdateQuery
- func (q *UpdateQuery) Build() (string, []any, error)
- func (q *UpdateQuery) Column(columns ...string) *UpdateQuery
- func (q *UpdateQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *UpdateQuery) OmitZero() *UpdateQuery
- func (q *UpdateQuery) Returning(columns ...string) *UpdateQuery
- func (q *UpdateQuery) Scan(ctx context.Context, dest ...any) error
- func (q *UpdateQuery) Set(expr string, args ...any) *UpdateQuery
- func (q *UpdateQuery) Where(query string, args ...any) *UpdateQuery
- func (q *UpdateQuery) WherePK() *UpdateQuery
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CreateTableQuery ¶
type CreateTableQuery struct {
// contains filtered or unexported fields
}
CreateTableQuery builds SQLite CREATE TABLE statements.
func (*CreateTableQuery) Build ¶
func (q *CreateTableQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*CreateTableQuery) IfNotExists ¶
func (q *CreateTableQuery) IfNotExists() *CreateTableQuery
IfNotExists adds the IF NOT EXISTS clause.
func (*CreateTableQuery) Temp ¶
func (q *CreateTableQuery) Temp() *CreateTableQuery
Temp marks the table as TEMPORARY.
func (*CreateTableQuery) WithForeignKey ¶
func (q *CreateTableQuery) WithForeignKey(fk string) *CreateTableQuery
WithForeignKey adds a raw foreign key constraint string. Example: "(user_id) REFERENCES users(id) ON DELETE CASCADE"
type DeleteQuery ¶
type DeleteQuery struct {
// contains filtered or unexported fields
}
DeleteQuery builds SQLite DELETE statements.
func (*DeleteQuery) Build ¶
func (q *DeleteQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*DeleteQuery) ForceDelete ¶
func (q *DeleteQuery) ForceDelete() *DeleteQuery
ForceDelete bypasses soft delete, performing a real DELETE even if the model has a soft_delete field.
func (*DeleteQuery) Returning ¶
func (q *DeleteQuery) Returning(columns ...string) *DeleteQuery
Returning adds RETURNING columns.
func (*DeleteQuery) Scan ¶
func (q *DeleteQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the DELETE with RETURNING and scans results into dest.
func (*DeleteQuery) Where ¶
func (q *DeleteQuery) Where(query string, args ...any) *DeleteQuery
Where adds a WHERE clause.
func (*DeleteQuery) WhereOr ¶
func (q *DeleteQuery) WhereOr(query string, args ...any) *DeleteQuery
WhereOr adds an OR WHERE clause.
func (*DeleteQuery) WherePK ¶
func (q *DeleteQuery) WherePK() *DeleteQuery
WherePK adds WHERE pk = ? using model's primary key values.
type DropTableQuery ¶
type DropTableQuery struct {
// contains filtered or unexported fields
}
DropTableQuery builds SQLite DROP TABLE statements.
func (*DropTableQuery) Build ¶
func (q *DropTableQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*DropTableQuery) IfExists ¶
func (q *DropTableQuery) IfExists() *DropTableQuery
IfExists adds the IF EXISTS clause.
type InsertQuery ¶
type InsertQuery struct {
// contains filtered or unexported fields
}
InsertQuery builds SQLite INSERT statements.
func (*InsertQuery) Build ¶
func (q *InsertQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*InsertQuery) Column ¶
func (q *InsertQuery) Column(columns ...string) *InsertQuery
Column specifies which columns to insert.
func (*InsertQuery) MultiRow ¶
func (q *InsertQuery) MultiRow() *InsertQuery
MultiRow forces the insert to use a single multi-row VALUES statement instead of a prepared statement loop. This may be preferred for small batches where single-statement atomicity matters.
func (*InsertQuery) OnConflict ¶
func (q *InsertQuery) OnConflict(clause string) *InsertQuery
OnConflict adds an ON CONFLICT clause (e.g., "(email) DO UPDATE").
func (*InsertQuery) Returning ¶
func (q *InsertQuery) Returning(columns ...string) *InsertQuery
Returning adds RETURNING columns.
func (*InsertQuery) Scan ¶
func (q *InsertQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the INSERT with RETURNING and scans results into dest.
func (*InsertQuery) Set ¶
func (q *InsertQuery) Set(expr string, args ...any) *InsertQuery
Set adds a SET expression for ON CONFLICT DO UPDATE.
func (*InsertQuery) Value ¶
func (q *InsertQuery) Value(values ...any) *InsertQuery
Value adds explicit values (for manual inserts without model data).
type RawQuery ¶
type RawQuery struct {
// contains filtered or unexported fields
}
RawQuery executes arbitrary SQL with optional model scanning.
type SelectQuery ¶
type SelectQuery struct {
// contains filtered or unexported fields
}
SelectQuery builds SQLite SELECT statements.
func (*SelectQuery) Build ¶
func (q *SelectQuery) Build() (string, []any, error)
Build generates the SQL string and args without executing.
func (*SelectQuery) BuildCount ¶
func (q *SelectQuery) BuildCount() (string, []any, error)
BuildCount generates a SELECT COUNT(*) query string and args.
func (*SelectQuery) Column ¶
func (q *SelectQuery) Column(columns ...string) *SelectQuery
Column adds specific columns to select. If not called, selects all fields.
func (*SelectQuery) ColumnExpr ¶
func (q *SelectQuery) ColumnExpr(expr string, args ...any) *SelectQuery
ColumnExpr adds a raw column expression.
func (*SelectQuery) Count ¶
func (q *SelectQuery) Count(ctx context.Context) (int64, error)
Count executes a SELECT COUNT(*) and returns the count.
func (*SelectQuery) GroupExpr ¶
func (q *SelectQuery) GroupExpr(expr string) *SelectQuery
GroupExpr adds GROUP BY expression.
func (*SelectQuery) Having ¶
func (q *SelectQuery) Having(query string, args ...any) *SelectQuery
Having adds HAVING clause.
func (*SelectQuery) Join ¶
func (q *SelectQuery) Join(joinType, table, on string, args ...any) *SelectQuery
Join adds a JOIN clause.
func (*SelectQuery) OrderExpr ¶
func (q *SelectQuery) OrderExpr(expr string) *SelectQuery
OrderExpr adds ORDER BY expression.
func (*SelectQuery) Relation ¶
func (q *SelectQuery) Relation(name string) *SelectQuery
Relation marks a relation for eager loading.
func (*SelectQuery) Scan ¶
func (q *SelectQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the query and scans results into the model.
func (*SelectQuery) TableExpr ¶
func (q *SelectQuery) TableExpr(expr string, args ...any) *SelectQuery
TableExpr sets the FROM clause to a raw SQL expression instead of deriving it from the model's table name. This is useful for queries against functions, CTEs, or subqueries, e.g.:
db.NewSelect().TableExpr("some_view AS v")
func (*SelectQuery) Where ¶
func (q *SelectQuery) Where(query string, args ...any) *SelectQuery
Where adds an AND WHERE clause.
func (*SelectQuery) WhereOr ¶
func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery
WhereOr adds an OR WHERE clause.
func (*SelectQuery) WherePK ¶
func (q *SelectQuery) WherePK() *SelectQuery
WherePK adds WHERE conditions for the model's primary key fields. The user must have set the model so that PKFields are available. It generates conditions like "table"."pk_col" = ? using placeholders.
func (*SelectQuery) WithDeleted ¶
func (q *SelectQuery) WithDeleted() *SelectQuery
WithDeleted includes soft-deleted rows in the result set. By default, models with a soft_delete field automatically filter out rows where the soft delete column is not NULL.
type SqliteDB ¶
type SqliteDB struct {
// contains filtered or unexported fields
}
SqliteDB implements driver.Driver for SQLite using database/sql with the modernc.org/sqlite pure-Go driver. Call New() to create an instance and then Open() to establish the database connection.
When txConn is set (by SqliteTx), Exec/Query/QueryRow route through the transaction instead of the pool.
func New ¶
func New() *SqliteDB
New creates a new unconnected SqliteDB. Call Open to establish a connection.
func Unwrap ¶
Unwrap extracts the underlying *SqliteDB from a *grove.DB handle. This allows access to SQLite-specific query builders and features.
sdb := sqlitedriver.Unwrap(db) // returns *sqlitedriver.SqliteDB
sdb.NewSelect(&users).Where("email LIKE ?", "%@test.com").Scan(ctx)
Panics if the driver is not a *SqliteDB.
func (*SqliteDB) BeginTxQuery ¶
BeginTxQuery starts a new transaction and returns a SqliteTx that exposes query builder methods operating within that transaction.
func (*SqliteDB) Exec ¶
Exec executes a query that does not return rows (INSERT, UPDATE, DELETE, DDL) and returns a driver.Result.
func (*SqliteDB) GroveDelete ¶
GroveDelete is the adapter method for grove.DB.NewDelete().
func (*SqliteDB) GroveInsert ¶
GroveInsert is the adapter method for grove.DB.NewInsert().
func (*SqliteDB) GroveSelect ¶
GroveSelect is the adapter method for grove.DB.NewSelect().
func (*SqliteDB) GroveTx ¶
GroveTx is the adapter method for grove.DB.BeginTx(). It bridges the grove package's generic transaction interface with SqliteDB's typed BeginTx. The isolationLevel parameter maps to driver.IsolationLevel constants.
func (*SqliteDB) GroveUpdate ¶
GroveUpdate is the adapter method for grove.DB.NewUpdate().
func (*SqliteDB) NewCreateTable ¶
func (db *SqliteDB) NewCreateTable(model any) *CreateTableQuery
NewCreateTable creates a CREATE TABLE query for the given model.
func (*SqliteDB) NewDelete ¶
func (db *SqliteDB) NewDelete(model any) *DeleteQuery
NewDelete creates a DELETE query.
func (*SqliteDB) NewDropTable ¶
func (db *SqliteDB) NewDropTable(model any) *DropTableQuery
NewDropTable creates a DROP TABLE query for the given model.
func (*SqliteDB) NewInsert ¶
func (db *SqliteDB) NewInsert(model any) *InsertQuery
NewInsert creates an INSERT query. model can be a struct pointer or a pointer to a slice (for bulk insert).
func (*SqliteDB) NewSelect ¶
func (db *SqliteDB) NewSelect(model ...any) *SelectQuery
NewSelect creates a new SELECT query. model can be:
- *[]User (slice pointer for multi-row)
- *User (struct pointer for single row)
- (*User)(nil) (nil pointer for table reference without binding)
func (*SqliteDB) NewUpdate ¶
func (db *SqliteDB) NewUpdate(model any) *UpdateQuery
NewUpdate creates an UPDATE query.
func (*SqliteDB) Open ¶
Open parses the DSN, applies configuration options, opens the SQLite database, and configures WAL mode and foreign keys.
func (*SqliteDB) Prepare ¶
Prepare creates a prepared statement for repeated execution. If operating within a transaction, it delegates to the transaction's Prepare.
func (*SqliteDB) Query ¶
Query executes a query that returns rows and wraps the result in a driver.Rows.
func (*SqliteDB) SetHooks ¶
SetHooks attaches a hook engine for lifecycle hooks (pre/post query and mutation). If engine is nil, hooks are disabled.
func (*SqliteDB) SupportsReturning ¶
SupportsReturning returns true because SQLite 3.35+ supports INSERT ... RETURNING.
type SqliteDialect ¶
type SqliteDialect struct{}
SqliteDialect implements driver.Dialect for SQLite.
func (*SqliteDialect) AppendBytes ¶
func (d *SqliteDialect) AppendBytes(b []byte, v []byte) []byte
AppendBytes appends a hex-encoded SQLite blob literal to b and returns the extended slice. The format is: X'<hex>'
func (*SqliteDialect) AppendTime ¶
func (d *SqliteDialect) AppendTime(b []byte, t time.Time) []byte
AppendTime appends a time value formatted as RFC3339 (wrapped in single quotes) to b and returns the extended slice.
func (*SqliteDialect) GoToDBType ¶
func (d *SqliteDialect) GoToDBType(goType reflect.Type, opts schema.FieldOptions) string
GoToDBType maps a Go reflect.Type to the appropriate SQLite column type string, taking field options into account.
Mapping rules (in order of precedence):
- If opts.SQLType is set, it is returned verbatim.
- bool -> "INTEGER" (SQLite uses 0/1)
- int, int32 -> "INTEGER"
- int64 -> "INTEGER"
- int16 -> "INTEGER"
- int8 -> "INTEGER"
- float32 -> "REAL"
- float64 -> "REAL"
- string -> "TEXT" (or "TEXT" if Unique is set)
- time.Time -> "TEXT" (stored as RFC3339)
- *time.Time -> "TEXT" (stored as RFC3339)
- []byte -> "BLOB"
- map[string]any -> "TEXT" (JSON stored as text)
- default -> "TEXT"
func (*SqliteDialect) Name ¶
func (d *SqliteDialect) Name() string
Name returns the dialect identifier.
func (*SqliteDialect) Placeholder ¶
func (d *SqliteDialect) Placeholder(n int) string
Placeholder returns the SQLite parameter placeholder. SQLite uses ? for all positional parameters regardless of position.
func (*SqliteDialect) Quote ¶
func (d *SqliteDialect) Quote(ident string) string
Quote wraps an identifier in double quotes, escaping any embedded double quotes by doubling them. This follows the standard SQL quoting convention for identifiers that SQLite supports.
type SqliteTx ¶
type SqliteTx struct {
// contains filtered or unexported fields
}
SqliteTx wraps a driver.Tx and exposes query builder methods. Queries created from a SqliteTx execute within the transaction.
func (*SqliteTx) NewDelete ¶
func (t *SqliteTx) NewDelete(model any) *DeleteQuery
NewDelete creates a DELETE query that executes within the transaction.
func (*SqliteTx) NewInsert ¶
func (t *SqliteTx) NewInsert(model any) *InsertQuery
NewInsert creates an INSERT query that executes within the transaction.
func (*SqliteTx) NewSelect ¶
func (t *SqliteTx) NewSelect(model ...any) *SelectQuery
NewSelect creates a SELECT query that executes within the transaction.
func (*SqliteTx) NewUpdate ¶
func (t *SqliteTx) NewUpdate(model any) *UpdateQuery
NewUpdate creates an UPDATE query that executes within the transaction.
type UpdateQuery ¶
type UpdateQuery struct {
// contains filtered or unexported fields
}
UpdateQuery builds SQLite UPDATE statements.
func (*UpdateQuery) Build ¶
func (q *UpdateQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*UpdateQuery) Column ¶
func (q *UpdateQuery) Column(columns ...string) *UpdateQuery
Column limits which columns to update from the model.
func (*UpdateQuery) OmitZero ¶
func (q *UpdateQuery) OmitZero() *UpdateQuery
OmitZero skips fields with zero values when building SET from model.
func (*UpdateQuery) Returning ¶
func (q *UpdateQuery) Returning(columns ...string) *UpdateQuery
Returning adds RETURNING columns.
func (*UpdateQuery) Scan ¶
func (q *UpdateQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the UPDATE with RETURNING and scans results into dest.
func (*UpdateQuery) Set ¶
func (q *UpdateQuery) Set(expr string, args ...any) *UpdateQuery
Set adds a raw SET expression (e.g., "name = ?", "Alice").
func (*UpdateQuery) Where ¶
func (q *UpdateQuery) Where(query string, args ...any) *UpdateQuery
Where adds a WHERE clause.
func (*UpdateQuery) WherePK ¶
func (q *UpdateQuery) WherePK() *UpdateQuery
WherePK adds WHERE pk = ? using model's primary key values.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package sqlitemigrate provides a SQLite-specific migration executor for the Grove migration system.
|
Package sqlitemigrate provides a SQLite-specific migration executor for the Grove migration system. |