Documentation
¶
Overview ¶
Package sqlite is a SQLite driver for database/sql that needs no cgo.
It registers itself as "sqlite":
db, err := sql.Open("sqlite", "file:app.db?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)")
modernc.org/sqlite and github.com/glebarez/go-sqlite register the same name, so a program that imports one of them as well panics at start. Use sql.OpenDB(sqlite.NewConnector(dsn)) to open a database without the registered name.
The data source name ¶
A file name, ":memory:", or a URI ("file:app.db?mode=ro"), which SQLite reads itself (https://sqlite.org/uri.html). These parameters are the driver's and SQLite does not see them:
- _pragma=name(value): a PRAGMA run on every new connection, in order. It may be given more than once.
- _txlock=deferred, immediate or exclusive: how BEGIN starts a transaction (deferred by default). A read-only transaction always starts deferred.
- _time_format=sqlite: how a time.Time is written. The only format, and the default, is "2006-01-02 15:04:05.999999999-07:00".
A connection waits 5 seconds for a lock unless a _pragma sets busy_timeout.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Aggregate ¶
Aggregate is the state of one group of an aggregate function written in Go. Step runs for each row of the group, with arguments as a Func gets them, then Final once, with a result as a Func gives it. An error or a panic in either is the error of the statement.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector opens connections to one database.
func NewConnector ¶
NewConnector parses dsn, for sql.OpenDB. Its connections get what Register gave, then opts.
type Driver ¶
type Driver struct{}
Driver is the database/sql driver.
type Func ¶
Func is a SQL function written in Go.
Its arguments are copies, each an int64, float64, string, []byte or nil, so it may keep them. It returns a value database/sql can bind (an integer, float, bool, string, []byte, time.Time, nil or a driver.Valuer), or an error, which becomes the error of the statement. A panic in it is recovered and becomes the error of the statement too: the connection stays good.
It may run on any connection of its connector, and from several goroutines at once. It must not use the database: it runs in the middle of a statement. It may also run more than once for the same row: SQLite does not promise how often it calls a function, and group commit runs a batch again when a failure of one statement ended its transaction. Keep side effects out of it, or make them safe to repeat.
type FuncFlag ¶
type FuncFlag int
FuncFlag tells SQLite how a function behaves.
const ( // Deterministic: the same arguments always give the same result, so // SQLite may compute it once, and use it in indexes and generated // columns. Deterministic FuncFlag = csqlite.FuncDeterministic // DirectOnly: the function may be called only from SQL the program // runs, never from a trigger, view, index or generated column of the // schema. Give it to a function with side effects, so that a database // file someone else made cannot call it. DirectOnly FuncFlag = csqlite.FuncDirectOnly // Innocuous: the function has no side effects and reveals nothing, so // it is safe wherever SQL comes from. Innocuous FuncFlag = csqlite.FuncInnocuous )
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
An Option adds something to the connections of a connector: give it to NewConnector, or to Register for the connectors sql.Open makes.
func Join ¶
Join makes one Option of several, applied in order: how a package of functions hands them over.
func WithAggregate ¶
WithAggregate makes an aggregate function name of nArg arguments: for each group, newAggregate makes a state. A group of no rows gets one too, for Final alone.
func WithCollation ¶
WithCollation makes cmp the collation name, for COLLATE name in queries and in the schema. cmp returns a negative number, 0 or a positive number as a sorts before, with or after b, and must be a total order. A panic in cmp fails the statement with the panic, and SQLite undoes it as it does when memory runs out: it rolls back the statement, or the whole transaction. When the whole transaction is gone, the next statements of it and its Commit fail. The connection stays good.
func WithFunction ¶
WithFunction makes fn the SQL function name of nArg arguments (-1 for any number) on every connection of the connector. A later option of the same name and nArg replaces an earlier one.
Example ¶
//go:build ((linux || darwin) && (amd64 || arm64)) || (windows && amd64)
package main
import (
"database/sql"
"database/sql/driver"
"fmt"
"strings"
"github.com/liustack/sqlite"
)
type longest struct{ s string }
func (l *longest) Step(args []driver.Value) error {
if s, ok := args[0].(string); ok && len(s) > len(l.s) {
l.s = s
}
return nil
}
func (l *longest) Final() (driver.Value, error) { return l.s, nil }
func main() {
reverse := func(args []driver.Value) (driver.Value, error) {
s, _ := args[0].(string)
r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r), nil
}
c, err := sqlite.NewConnector(":memory:",
sqlite.WithFunction("reverse", 1, reverse, sqlite.Deterministic),
sqlite.WithAggregate("longest", 1, func() sqlite.Aggregate { return &longest{} }),
sqlite.WithCollation("nocase_go", func(a, b string) int {
return strings.Compare(strings.ToLower(a), strings.ToLower(b))
}))
if err != nil {
panic(err)
}
db := sql.OpenDB(c)
defer db.Close()
var r, l, first string
err = db.QueryRow(`
WITH w(word) AS (VALUES ('Banana'), ('apple'), ('cherry'))
SELECT reverse('stressed'), longest(word),
(SELECT word FROM w ORDER BY word COLLATE nocase_go LIMIT 1)
FROM w`).Scan(&r, &l, &first)
if err != nil {
panic(err)
}
fmt.Println(r, l, first)
}
Output: desserts Banana apple
Directories
¶
| Path | Synopsis |
|---|---|
|
ext
|
|
|
hash
Package hash gives SQLite hash functions:
|
Package hash gives SQLite hash functions: |
|
internal/arg
Package arg reads the arguments of the functions of ext/.
|
Package arg reads the arguments of the functions of ext/. |
|
internal/sqltest
Package sqltest runs the queries of the tests of ext/.
|
Package sqltest runs the queries of the tests of ext/. |
|
regexp
Package regexp gives SQLite the REGEXP operator and regexp functions, with Go's regexp syntax (RE2: no backreferences, and time linear in the text).
|
Package regexp gives SQLite the REGEXP operator and regexp functions, with Go's regexp syntax (RE2: no backreferences, and time linear in the text). |
|
stats
Package stats gives SQLite the variance and the standard deviation as aggregate functions:
|
Package stats gives SQLite the variance and the standard deviation as aggregate functions: |
|
uuid
Package uuid gives SQLite the functions of SQLite's own uuid extension:
|
Package uuid gives SQLite the functions of SQLite's own uuid extension: |
|
internal
|
|
|
goasm/gen/cmd/engine
command
|
|
|
goasm/gen/cmd/gen
command
|
|
|
vfs
Package vfs is the operating system layer of SQLite, in Go: the files, locks and shared memory that os_unix.c gives a normal build.
|
Package vfs is the operating system layer of SQLite, in Go: the files, locks and shared memory that os_unix.c gives a normal build. |