Documentation
¶
Overview ¶
Package sqlite is a SQLite client, of the same shape as go-pg's: queries are bound to a connection, and objects are read and written by the Reader, Writer and Selector interfaces. An implementation, such as package ncruces, opens a database as a PoolConn.
Index ¶
- Constants
- func HTTPError(err error) error
- func NewDatabaseError(err error, kinds ...Err) error
- func SetVersion(ctx context.Context, conn Conn, version int64) error
- func Statement(sql string) string
- func Version(ctx context.Context, conn Conn) (int64, error)
- type Bind
- func (bind *Bind) Append(key string, value any) bool
- func (bind *Bind) Copy(pairs ...any) *Bind
- func (bind *Bind) Del(key string)
- func (bind *Bind) Expand(query string) (string, map[string]any)
- func (bind *Bind) Get(key string) any
- func (bind *Bind) Has(key string) bool
- func (bind *Bind) Join(key, sep string) string
- func (bind *Bind) MarshalJSON() ([]byte, error)
- func (bind *Bind) Query(name string) string
- func (bind *Bind) Replace(query string) string
- func (bind *Bind) Set(key string, value any) string
- func (bind *Bind) String() string
- func (bind *Bind) WithQueries(queries ...*Queries) *Bind
- type Conn
- type DatabaseError
- type Err
- type Fn
- type Function
- type ListReader
- type OffsetLimit
- type Op
- type Opt
- type Options
- type PoolConn
- type Pragma
- type Publisher
- type Queries
- type Reader
- type Row
- type Selector
- type Writer
Constants ¶
const ( // MaxChanges is the most rows of a table, of a transaction, which are // published as changes of each, rather than one change of the table MaxChanges = 1000 // DefaultReaders is the most connections which read at once DefaultReaders = 4 // DefaultBusyTimeout is how long, in milliseconds, a connection waits // for another which is writing DefaultBusyTimeout = "10000" // Memory is the path of a database in memory, which is one connection's, // so it's both written and read on one Memory = ":memory:" )
const (
// TraceSpanNameArg is the bind var which names a query's span
TraceSpanNameArg = "otelspan"
)
Variables ¶
This section is empty.
Functions ¶
func NewDatabaseError ¶ added in v1.1.1
NewDatabaseError returns an implementation's error, such as SQLite's, which is ErrDatabase, and the kinds of error it is, such as ErrConflict and ErrUniqueViolation
func SetVersion ¶ added in v1.1.1
SetVersion sets the version of the database's schema, such as within the transaction which migrates it.
Types ¶
type Bind ¶ added in v1.1.1
Bind represents a set of variables and arguments to be used in a query. The vars are substituted in the query string itself, as ${key}, while the args are passed as arguments to the query, as @key.
func NewBind ¶ added in v1.1.1
NewBind creates a new Bind object with the given name/value pairs. Returns nil if the number of arguments is not even.
func (*Bind) Append ¶ added in v1.1.1
Append appends a bind var to a list. Returns false if the key is not a list, or the value is not a list.
func (*Bind) Copy ¶ added in v1.1.1
Copy creates a copy of the bind object with additional name/value pairs.
func (*Bind) Expand ¶ added in v1.1.1
Expand returns the query with its ${key} substitutions replaced, and the vars, of one copy, which its @key arguments are bound to
func (*Bind) Join ¶ added in v1.1.1
Join joins a bind var with a separator when it is a []any and returns the result as a string. Returns an empty string if the key does not exist.
func (*Bind) MarshalJSON ¶ added in v1.1.1
func (*Bind) Query ¶ added in v1.1.1
Query looks up a named query, which was loaded via WithQueries, such as "index.object.get", sets the span name for OpenTelemetry tracing, and returns its SQL, whose ${key} substitutions are replaced once, when it's executed, so a value which has a $ in it, such as a path, isn't replaced again. It returns an empty string when there's no query of the name.
func (*Bind) Replace ¶ added in v1.1.1
Replace returns a query string with ${subtitution} replaced by the values:
- ${key} => value
- ${'key'} => 'value'
- ${"key"} => "value"
- ${1} => $1
Anything else is kept as it is, such as $key, @key and :key, which are SQLite's parameters, and are bound as arguments, rather than substituted.
func (*Bind) WithQueries ¶ added in v1.1.1
WithQueries returns a copy of the bind object, with the queries of each set, by their keys, which Query returns
type Conn ¶ added in v1.1.1
type Conn interface {
// Return a new connection with bound parameters
With(...any) Conn
// Return a new connection with bound queries
WithQueries(...*Queries) Conn
// Perform a transaction within a function, on the connection which
// writes, which is begun immediately, so it isn't upgraded to write
// later, when another transaction may be writing
Tx(context.Context, func(Conn) error) error
// Perform a bulk operation within a function, in a transaction, whose
// statements are prepared once
Bulk(context.Context, func(Conn) error) error
// Execute a query
Exec(context.Context, string) error
// Perform an insert
Insert(context.Context, Reader, Writer) error
// Perform an update
Update(context.Context, Reader, Selector, Writer) error
// Perform a delete
Delete(context.Context, Reader, Selector) error
// Perform a get, on a connection which reads, unless in a transaction
Get(context.Context, Reader, Selector) error
// Perform a list, on a connection which reads, unless in a transaction.
// If the reader is a ListReader, then the count of items is also
// calculated
List(context.Context, Reader, Selector) error
}
Conn is a connection, with bound parameters and queries.
type DatabaseError ¶ added in v1.1.1
type DatabaseError struct {
// contains filtered or unexported fields
}
DatabaseError is an implementation's error, which is ErrDatabase, and the kinds of error it is, such as ErrConflict and ErrUniqueViolation, with errors.Is
func (*DatabaseError) Error ¶ added in v1.1.1
func (e *DatabaseError) Error() string
Error returns the error's message
func (*DatabaseError) Is ¶ added in v1.1.1
func (e *DatabaseError) Is(target error) bool
Is returns true when the error is of the kind, or the implementation's error is
func (*DatabaseError) Unwrap ¶ added in v1.1.1
func (e *DatabaseError) Unwrap() error
Unwrap returns the implementation's error
type Err ¶ added in v1.1.1
type Err int
Err is a kind of error, which SQLite's errors are mapped to
type Fn ¶ added in v1.1.1
type Fn struct {
// contains filtered or unexported fields
}
Fn is a function, of its arguments, which SQL calls, which Func0, Func1, Func2, Func3 and FuncN make, of a Go function, of typed arguments and result
func Func0 ¶ added in v1.1.1
Func0 returns a function, of no arguments, and its result, which is set as values.Set sets it, such as a []string, which is JSON
func Func1 ¶ added in v1.1.1
Func1 returns a function, of one argument, which is scanned as values.Scan scans it, such as a time.Time, or a *int64, which is nil when it's NULL, and its result
type Function ¶ added in v1.1.1
type Function struct {
Name string
Args int // The number of arguments, or -1 for any
Deterministic bool // The same arguments always return the same result
// Fn is called with the arguments, which it scans, and returns its
// result, which is set as values.Set sets it, or an error
Fn func(args []values.Value) (any, error)
}
Function is a function, which SQL calls by its name, which an implementation creates on each connection
type ListReader ¶ added in v1.1.1
ListReader scans database rows and counts total results.
type OffsetLimit ¶ added in v1.1.1
type OffsetLimit struct {
Offset uint64 `json:"offset,omitempty" help:"Offset within the result set" jsonschema:"Offset for pagination" example:"0"`
Limit *uint64 `json:"limit,omitempty" help:"Number of items to return" jsonschema:"Limit for pagination" example:"100"`
}
func (*OffsetLimit) Bind ¶ added in v1.1.1
func (r *OffsetLimit) Bind(bind *Bind, max uint64)
Bind sets the offset and limit SQL fragment on the bind object.
func (*OffsetLimit) Clamp ¶ added in v1.1.1
func (r *OffsetLimit) Clamp(total uint64)
Clamp restricts the limit to the number of rows available after offset.
func (*OffsetLimit) Query ¶ added in v1.1.1
func (r *OffsetLimit) Query(params url.Values)
Query sets offset and limit query parameters on the provided map.
type Opt ¶ added in v1.1.1
Opt is an option of a database, which Open is given
func WithBroadcaster ¶ added in v1.1.1
WithBroadcaster publishes the rows which are inserted, updated and deleted, once they're committed, to a broadcaster, such as go-server's broadcaster.Broadcaster, as a change of each row: its action, the database, such as main, its table, and its rowid. Changes of the tables, when there are some, are published, or else of all of them, other than the shadow tables of virtual tables, such as fts5's. A row which is written more than once in a transaction is one change. A transaction of more than MaxChanges rows of a table is one change of the table, without a row. Writes by other processes, and of tables WITHOUT ROWID, aren't published.
Publish is called after the commit, before the writer is released, so it mustn't block, or query the database.
func WithFunction ¶ added in v1.1.1
WithFunction creates a function on each connection, which SQL calls by its name, of Func0, Func1, Func2, Func3 or FuncN, such as:
sqlite.WithFunction("double", true, sqlite.Func1(func(n int64) (int64, error) {
return n * 2, nil
}))
A deterministic function always returns the same result for the same arguments, so it can be used in indexes.
func WithMaxReaders ¶ added in v1.1.1
WithMaxReaders sets the most connections which read at once, which is DefaultReaders, unless it's set. A database in memory has one connection.
func WithPragma ¶ added in v1.1.1
WithPragma sets a pragma on each connection, such as cache_size, after the defaults: journal_mode WAL, synchronous NORMAL, foreign_keys ON and busy_timeout, which it can change. Its value is a number, a name, or a string in single quotes.
func WithTracer ¶ added in v1.1.1
WithTracer sets the OpenTelemetry tracer, of a span of each query, which is named by the query's name, of Bind.Query, or sqlite.query, or, of a transaction's statements, sqlite.transaction, sqlite.commit or sqlite.rollback, with its SQL, and its arguments, as arg. and their names, and of opening the database, sqlite.open.
type Options ¶ added in v1.1.1
type Options struct {
// The most connections which read at once
Readers int
// The pragmas, which are set on each connection, after the defaults
Pragmas []Pragma
// The functions, which are created on each connection
Functions []Function
// The bind variables of each query
Bind *Bind
// The OpenTelemetry tracer, of a span of each query
Tracer trace.Tracer
// The broadcaster, which the rows which are written are published to,
// once they're committed, and the tables which are, or all of them
Broadcaster Publisher
Tables []string
}
Options are what Open is given, which an implementation, such as package ncruces, reads, with Apply
type PoolConn ¶ added in v1.1.1
type PoolConn interface {
Conn
// Ping the database
Ping(context.Context) error
// Close the database
Close() error
}
PoolConn is a database, opened from a file or in memory, which writes on one connection, and reads on many.
type Pragma ¶ added in v1.1.1
Pragma is a pragma, which is set on each connection, such as cache_size, of -4000.
type Publisher ¶ added in v1.1.1
Publisher is what changes are published to, such as go-server's broadcaster.Broadcaster. Publish doesn't block, and returns the watchers which missed the change.
type Queries ¶ added in v1.1.1
type Queries struct {
// contains filtered or unexported fields
}
Queries represents a collection of named SQL statements parsed from a reader. SQL statements are separated by comment lines in the format: -- <identifier> or -- <identifier>.<identifier>, with any number of dots between them. Each statement is stored with its key.
func NewQueries ¶ added in v1.1.1
NewQueries parses SQL statements from a reader and returns a Queries collection. SQL statements must be separated by comment lines with a key: -- <identifier>.<identifier>, with one or more dots, where an identifier consists of letters, digits and underscores, and doesn't start with a digit, so a comment line of one word, such as "-- change", isn't a key. A key is only a key outside of a string, a quoted identifier, or a block comment, so a line of a string which looks like one, such as "-- note", is part of its string. Other comment lines, such as those which describe a statement, or one which ends a sentence with a period, such as "-- form.", and comments at the end of a line, are removed from the statement, except within strings, quoted identifiers and block comments. Returns an error if duplicate keys are found, a key has no statement, or reading fails.
Example input format:
-- user.select SELECT * FROM users WHERE id = @id; -- user.insert INSERT INTO users (name, email) VALUES (@name, @email);
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
filesearch
module
|
|
|
Package ncruces opens SQLite databases with ncruces/go-sqlite3, which is SQLite compiled to WebAssembly, so it's pure Go, without cgo.
|
Package ncruces opens SQLite databases with ncruces/go-sqlite3, which is SQLite compiled to WebAssembly, so it's pure Go, without cgo. |
|
fts5
Package fts5 adds SQLite's full-text search, of the fts5 virtual table, to each connection, which package ncruces opens, when it's imported:
|
Package fts5 adds SQLite's full-text search, of the fts5 virtual table, to each connection, which package ncruces opens, when it's imported: |
|
rtree
Package rtree adds SQLite's spatial indexes, of the rtree and geopoly virtual tables, to each connection, which package ncruces opens, when it's imported:
|
Package rtree adds SQLite's spatial indexes, of the rtree and geopoly virtual tables, to each connection, which package ncruces opens, when it's imported: |
|
vec1
Package vec1 adds SQLite's vector search, of the vec1 virtual table, to each connection, which package ncruces opens, when it's imported:
|
Package vec1 adds SQLite's vector search, of the vec1 virtual table, to each connection, which package ncruces opens, when it's imported: |
|
Package test is the tests which an implementation, such as package ncruces, passes, so it can be replaced by another, which behaves the same.
|
Package test is the tests which an implementation, such as package ncruces, passes, so it can be replaced by another, which behaves the same. |
|
Package values converts values between Go and SQLite, of SQLite's five types: integers, floats, text, blobs and NULL.
|
Package values converts values between Go and SQLite, of SQLite's five types: integers, floats, text, blobs and NULL. |