Documentation
¶
Overview ¶
Package sqlite is MyGo's SQLite plugin. SQLite's C amalgamation and a small C shim are compiled with Zig and loaded through purego: no cgo, Zig, Bun or compiler is needed at application run time.
Register the plugin only when the web frontend uses @mygo-plugins/sqlite:
mygo.Use(sqlite.Plugin)
The frontend uses open from @mygo-plugins/sqlite. It opens named databases in the app's user-data directory, executes parameterized SQL, queries rows and runs batches in transactions. Go and native UI apps call Open directly and close their own connections; no mygo.Use is needed.
Index ¶
- Variables
- func LibraryPath() (string, error)
- func Load() error
- func New(opts Options) mygo.Plugin
- type DB
- func (d *DB) Close() error
- func (d *DB) Execute(ctx context.Context, sql string, args ...any) (result Result, err error)
- func (d *DB) Query(ctx context.Context, sql string, args ...any) (rows Rows, err error)
- func (d *DB) Transaction(ctx context.Context, statements []Statement) (results []Result, err error)
- type Error
- type OpenOptions
- type Options
- type Result
- type Rows
- type Statement
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("sqlite: database is closed")
ErrClosed is returned when an operation uses a closed database.
var Plugin = New(Options{})
Plugin is the page plugin with default options. It loads the native library only when a page opens its first database.
Functions ¶
func LibraryPath ¶
LibraryPath finds the library named by MYGO_SQLITE_LIBRARY, the app's resources, or the executable's directory, then the CLI's SHA-256-checked cache. Unpackaged programs download a missing library into that cache; packaged apps require the library that mygo dev/build put into them.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a connection to the plugin's SQLite library. It is safe from any goroutine; operations serialize on the connection and may be canceled while waiting for it. Use Transaction for atomic groups of statements.
func Open ¶
Open opens path (or ":memory:") with the SQLite C library compiled by Zig, without cgo. It creates a missing file unless ReadOnly. The parent directory must already exist. Close the returned connection when done.
func (*DB) Close ¶
Close cancels in-flight operations, waits for their statements to be finalized, and closes the connection. Repeated calls are harmless.
func (*DB) Execute ¶
Execute runs exactly one statement with positional parameters. Supported values are nil, bool, integers, finite floats, strings, []byte and json.Number. SQL text may not contain NUL. Transaction control, ATTACH and VACUUM INTO are rejected; use Transaction for groups of statements.
func (*DB) Query ¶
Query runs exactly one statement and copies its rows. It also supports statements with RETURNING. For large results, select a bounded page.
func (*DB) Transaction ¶
Transaction runs statements under BEGIN IMMEDIATE, without interleaving other operations on this connection. Any error, including cancellation, rolls back the batch. Results are returned only after COMMIT succeeds.
type Error ¶
Error is an SQLite error. Code is the primary result code; ExtendedCode distinguishes such errors as unique, foreign-key and not-null constraints.
type OpenOptions ¶
type OpenOptions struct {
// ReadOnly opens an existing database without allowing writes.
ReadOnly bool
// BusyTimeout is how long an operation waits for another connection's
// lock: five seconds when zero, no wait when negative. Cancellation
// stops the wait early.
BusyTimeout time.Duration
}
OpenOptions configure a connection. Foreign keys are enabled by default.
type Options ¶
type Options struct {
// Directory holds the page's database files, mygo.PathUserData when
// empty. Pages may open file names here, or ":memory:", not paths.
Directory string
// BusyTimeout follows OpenOptions.BusyTimeout.
BusyTimeout time.Duration
}
Options configure the page plugin. Go's Open accepts its own path and OpenOptions and does not use these options.
type Result ¶
Result describes a statement. Changes counts directly affected rows; LastInsertID is SQLite's last inserted rowid on this connection.