sqlitedriver

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 15 Imported by: 22

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

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) Exec

Exec executes the CREATE TABLE statement.

func (*CreateTableQuery) IfNotExists

func (q *CreateTableQuery) IfNotExists() *CreateTableQuery

IfNotExists adds the IF NOT EXISTS clause.

func (*CreateTableQuery) Temp

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) Exec

func (q *DeleteQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the DELETE (or soft-delete UPDATE).

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) Exec

func (q *DropTableQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the DROP TABLE statement.

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) Exec

func (q *InsertQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the 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.

func (*RawQuery) Exec

func (q *RawQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the raw query without returning rows.

func (*RawQuery) Scan

func (q *RawQuery) Scan(ctx context.Context, dest ...any) error

Scan executes the raw query and scans results into dest. dest can be:

  • *[]Model (slice pointer for multi-row)
  • *Model (struct pointer for single row)
  • scalar pointers (passed directly to row.Scan)

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) Limit

func (q *SelectQuery) Limit(n int) *SelectQuery

Limit sets LIMIT.

func (*SelectQuery) Offset

func (q *SelectQuery) Offset(n int) *SelectQuery

Offset sets OFFSET.

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

func Unwrap(db *grove.DB) *SqliteDB

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) BeginTx

func (db *SqliteDB) BeginTx(ctx context.Context, opts *driver.TxOptions) (driver.Tx, error)

BeginTx starts a new database transaction with the specified options.

func (*SqliteDB) BeginTxQuery

func (db *SqliteDB) BeginTxQuery(ctx context.Context, opts *driver.TxOptions) (*SqliteTx, error)

BeginTxQuery starts a new transaction and returns a SqliteTx that exposes query builder methods operating within that transaction.

func (*SqliteDB) Close

func (db *SqliteDB) Close() error

Close terminates all connections.

func (*SqliteDB) Dialect

func (db *SqliteDB) Dialect() driver.Dialect

Dialect returns the SQLite dialect.

func (*SqliteDB) Exec

func (db *SqliteDB) Exec(ctx context.Context, query string, args ...any) (driver.Result, error)

Exec executes a query that does not return rows (INSERT, UPDATE, DELETE, DDL) and returns a driver.Result.

func (*SqliteDB) GroveDelete

func (db *SqliteDB) GroveDelete(model any) any

GroveDelete is the adapter method for grove.DB.NewDelete().

func (*SqliteDB) GroveInsert

func (db *SqliteDB) GroveInsert(model any) any

GroveInsert is the adapter method for grove.DB.NewInsert().

func (*SqliteDB) GroveSelect

func (db *SqliteDB) GroveSelect(model ...any) any

GroveSelect is the adapter method for grove.DB.NewSelect().

func (*SqliteDB) GroveTx

func (db *SqliteDB) GroveTx(ctx context.Context, isolationLevel int, readOnly bool) (any, error)

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

func (db *SqliteDB) GroveUpdate(model any) any

GroveUpdate is the adapter method for grove.DB.NewUpdate().

func (*SqliteDB) Name

func (db *SqliteDB) Name() string

Name returns the driver identifier.

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) NewRaw

func (db *SqliteDB) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL query.

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

func (db *SqliteDB) Open(ctx context.Context, dsn string, opts ...driver.Option) error

Open parses the DSN, applies configuration options, opens the SQLite database, and configures WAL mode and foreign keys.

func (*SqliteDB) Ping

func (db *SqliteDB) Ping(ctx context.Context) error

Ping verifies that the database is reachable.

func (*SqliteDB) Prepare

func (db *SqliteDB) Prepare(ctx context.Context, query string) (driver.Stmt, error)

Prepare creates a prepared statement for repeated execution. If operating within a transaction, it delegates to the transaction's Prepare.

func (*SqliteDB) Query

func (db *SqliteDB) Query(ctx context.Context, query string, args ...any) (driver.Rows, error)

Query executes a query that returns rows and wraps the result in a driver.Rows.

func (*SqliteDB) QueryRow

func (db *SqliteDB) QueryRow(ctx context.Context, query string, args ...any) driver.Row

QueryRow executes a query expected to return at most one row.

func (*SqliteDB) SetHooks

func (db *SqliteDB) SetHooks(engine *hook.Engine)

SetHooks attaches a hook engine for lifecycle hooks (pre/post query and mutation). If engine is nil, hooks are disabled.

func (*SqliteDB) SupportsReturning

func (db *SqliteDB) SupportsReturning() bool

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):

  1. If opts.SQLType is set, it is returned verbatim.
  2. bool -> "INTEGER" (SQLite uses 0/1)
  3. int, int32 -> "INTEGER"
  4. int64 -> "INTEGER"
  5. int16 -> "INTEGER"
  6. int8 -> "INTEGER"
  7. float32 -> "REAL"
  8. float64 -> "REAL"
  9. string -> "TEXT" (or "TEXT" if Unique is set)
  10. time.Time -> "TEXT" (stored as RFC3339)
  11. *time.Time -> "TEXT" (stored as RFC3339)
  12. []byte -> "BLOB"
  13. map[string]any -> "TEXT" (JSON stored as text)
  14. 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) Commit

func (t *SqliteTx) Commit() error

Commit commits 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) NewRaw

func (t *SqliteTx) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL 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.

func (*SqliteTx) Rollback

func (t *SqliteTx) Rollback() error

Rollback rolls back the transaction. Safe to call after Commit.

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) Exec

func (q *UpdateQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the UPDATE.

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.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL