{"modulePath":"github.com/mutablelogic/go-sqlite","version":"v1.1.4","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqlite\n\nPackage 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.\n\n## Constants\n\n```go\nconst (\n\t// MaxChanges is the most rows of a table, of a transaction, which are\n\t// published as changes of each, rather than one change of the table\n\tMaxChanges = 1000\n\n\t// DefaultReaders is the most connections which read at once\n\tDefaultReaders = 4\n\n\t// DefaultBusyTimeout is how long, in milliseconds, a connection waits\n\t// for another which is writing\n\tDefaultBusyTimeout = \"10000\"\n\n\t// Memory is the path of a database in memory, which is one connection's,\n\t// so it's both written and read on one\n\tMemory = \":memory:\"\n)\n```\n\n```go\nconst (\n\t// TraceSpanNameArg is the bind var which names a query's span\n\tTraceSpanNameArg = \"otelspan\"\n)\n```\n\n## Functions\n\n```go\nfunc HTTPError(err error) error\n```\nHTTPError returns the error as an HTTP error, of its status.\n\n```go\nfunc NewDatabaseError(err error, kinds ...Err) error\n```\nNewDatabaseError returns an implementation's error, such as SQLite's, which is ErrDatabase, and the kinds of error it is, such as ErrConflict and ErrUniqueViolation\n\n```go\nfunc SetVersion(ctx context.Context, conn Conn, version int64) error\n```\nSetVersion sets the version of the database's schema, such as within the transaction which migrates it.\n\n```go\nfunc Statement(sql string) string\n```\nStatement returns the SQL of one statement, without its comments, or the ; which ends it, so it's within another, such as a subquery, or is followed by more, such as a limit.\n\n```go\nfunc Version(ctx context.Context, conn Conn) (int64, error)\n```\nVersion returns the version of the database's schema, which is 0 until it's set, so a schema can be created, and then migrated, as it changes.\n\n## Types\n\n```go\ntype Bind struct {\n\tsync.RWMutex\n\t// contains filtered or unexported fields\n}\n```\nBind 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.\n\n```go\nfunc NewBind(pairs ...any) *Bind\n```\nNewBind creates a new Bind object with the given name/value pairs. Returns nil if the number of arguments is not even.\n\n```go\nfunc (bind *Bind) Append(key string, value any) bool\n```\nAppend appends a bind var to a list. Returns false if the key is not a list, or the value is not a list.\n\n```go\nfunc (bind *Bind) Copy(pairs ...any) *Bind\n```\nCopy creates a copy of the bind object with additional name/value pairs.\n\n```go\nfunc (bind *Bind) Del(key string)\n```\nDel deletes a bind var.\n\n```go\nfunc (bind *Bind) Expand(query string) (string, map[string]any)\n```\nExpand returns the query with its ${key} substitutions replaced, and the vars, of one copy, which its @key arguments are bound to\n\n```go\nfunc (bind *Bind) Get(key string) any\n```\nGet returns a bind var by key.\n\n```go\nfunc (bind *Bind) Has(key string) bool\n```\nHas returns true if there is a bind var with the given key.\n\n```go\nfunc (bind *Bind) Join(key, sep string) string\n```\nJoin 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.\n\n```go\nfunc (bind *Bind) MarshalJSON() ([]byte, error)\n```\n\n```go\nfunc (bind *Bind) Query(name string) string\n```\nQuery 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.\n\n```go\nfunc (bind *Bind) Replace(query string) string\n```\nReplace returns a query string with ${subtitution} replaced by the values:\n\n  - ${key} =\u003e value\n  - ${'key'} =\u003e 'value'\n  - ${\"key\"} =\u003e \"value\"\n  - ${1} =\u003e $1\n\nAnything 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.\n\n```go\nfunc (bind *Bind) Set(key string, value any) string\n```\nSet sets a bind var and returns the parameter name.\n\n```go\nfunc (bind *Bind) String() string\n```\n\n```go\nfunc (bind *Bind) WithQueries(queries ...*Queries) *Bind\n```\nWithQueries returns a copy of the bind object, with the queries of each set, by their keys, which Query returns\n\n```go\ntype Conn interface {\n\t// Return a new connection with bound parameters\n\tWith(...any) Conn\n\n\t// Return a new connection with bound queries\n\tWithQueries(...*Queries) Conn\n\n\t// Perform a transaction within a function, on the connection which\n\t// writes, which is begun immediately, so it isn't upgraded to write\n\t// later, when another transaction may be writing\n\tTx(context.Context, func(Conn) error) error\n\n\t// Perform a bulk operation within a function, in a transaction, whose\n\t// statements are prepared once\n\tBulk(context.Context, func(Conn) error) error\n\n\t// Execute a query\n\tExec(context.Context, string) error\n\n\t// Perform an insert\n\tInsert(context.Context, Reader, Writer) error\n\n\t// Perform an update\n\tUpdate(context.Context, Reader, Selector, Writer) error\n\n\t// Perform a delete\n\tDelete(context.Context, Reader, Selector) error\n\n\t// Perform a get, on a connection which reads, unless in a transaction\n\tGet(context.Context, Reader, Selector) error\n\n\t// Perform a list, on a connection which reads, unless in a transaction.\n\t// If the reader is a ListReader, then the count of items is also\n\t// calculated\n\tList(context.Context, Reader, Selector) error\n}\n```\nConn is a connection, with bound parameters and queries.\n\n```go\ntype DatabaseError struct {\n\t// contains filtered or unexported fields\n}\n```\nDatabaseError is an implementation's error, which is ErrDatabase, and the kinds of error it is, such as ErrConflict and ErrUniqueViolation, with errors.Is\n\n```go\nfunc (e *DatabaseError) Error() string\n```\nError returns the error's message\n\n```go\nfunc (e *DatabaseError) Is(target error) bool\n```\nIs returns true when the error is of the kind, or the implementation's error is\n\n```go\nfunc (e *DatabaseError) Unwrap() error\n```\nUnwrap returns the implementation's error\n\n```go\ntype Err int\n```\nErr is a kind of error, which SQLite's errors are mapped to\n\n```go\nconst (\n\tErrSuccess Err = iota\n\tErrNotFound\n\tErrNotImplemented\n\tErrBadParameter\n\tErrNotAvailable\n\tErrConflict\n\tErrDatabase\n\tErrUniqueViolation\n\tErrForeignKeyViolation\n\tErrNotNullViolation\n\tErrCheckViolation\n\tErrInternalServerError\n)\n```\n\n```go\nfunc (e Err) Error() string\n```\nError returns the string representation of the error.\n\n```go\nfunc (e Err) With(a ...any) error\n```\nWith returns the error with additional context appended.\n\n```go\nfunc (e Err) Withf(format string, a ...any) error\n```\nWithf returns the error with formatted context appended. As with fmt.Errorf, the format may use %w to wrap errors, so that they can still be matched with errors.Is and errors.As.\n\n```go\ntype Fn struct {\n\t// contains filtered or unexported fields\n}\n```\nFn 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\n\n```go\nfunc Func0[T any](fn func() (T, error)) Fn\n```\nFunc0 returns a function, of no arguments, and its result, which is set as values.Set sets it, such as a \\[]string, which is JSON\n\n```go\nfunc Func1[A, T any](fn func(A) (T, error)) Fn\n```\nFunc1 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\n\n```go\nfunc Func2[A, B, T any](fn func(A, B) (T, error)) Fn\n```\nFunc2 returns a function, of two arguments, and its result\n\n```go\nfunc Func3[A, B, C, T any](fn func(A, B, C) (T, error)) Fn\n```\nFunc3 returns a function, of three arguments, and its result\n\n```go\nfunc FuncN[A, T any](fn func(...A) (T, error)) Fn\n```\nFuncN returns a function, of any number of arguments, of one type, such as any, which is an int64, a float64, a string, a \\[]byte or nil, and its result\n\n```go\ntype Function struct {\n\tName          string\n\tArgs          int  // The number of arguments, or -1 for any\n\tDeterministic bool // The same arguments always return the same result\n\n\t// Fn is called with the arguments, which it scans, and returns its\n\t// result, which is set as values.Set sets it, or an error\n\tFn func(args []values.Value) (any, error)\n}\n```\nFunction is a function, which SQL calls by its name, which an implementation creates on each connection\n\n```go\ntype ListReader interface {\n\tReader\n\n\t// Scan count into the result\n\tScanCount(Row) error\n}\n```\nListReader scans database rows and counts total results.\n\n```go\ntype OffsetLimit struct {\n\tOffset uint64  `json:\"offset,omitempty\" help:\"Offset within the result set\" jsonschema:\"Offset for pagination\" example:\"0\"`\n\tLimit  *uint64 `json:\"limit,omitempty\" help:\"Number of items to return\" jsonschema:\"Limit for pagination\" example:\"100\"`\n}\n```\n\n```go\nfunc (r *OffsetLimit) Bind(bind *Bind, max uint64)\n```\nBind sets the offset and limit SQL fragment on the bind object.\n\n```go\nfunc (r *OffsetLimit) Clamp(total uint64)\n```\nClamp restricts the limit to the number of rows available after offset.\n\n```go\nfunc (r *OffsetLimit) Query(params url.Values)\n```\nQuery sets offset and limit query parameters on the provided map.\n\n```go\ntype Op uint\n```\nOp represents a database operation type.\n\n```go\nconst (\n\tNone Op = iota\n\tGet\n\tInsert\n\tUpdate\n\tDelete\n\tList\n)\n```\nOperations\n\n```go\nfunc (o Op) String() string\n```\n\n```go\ntype Opt func(*Options) error\n```\nOpt is an option of a database, which Open is given\n\n```go\nfunc WithBind(k string, v any) Opt\n```\nWithBind sets a bind variable for each query.\n\n```go\nfunc WithBroadcaster(b Publisher, tables ...string) Opt\n```\nWithBroadcaster 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.\n\nPublish is called after the commit, before the writer is released, so it mustn't block, or query the database.\n\n```go\nfunc WithFunction(name string, deterministic bool, fn Fn) Opt\n```\nWithFunction creates a function on each connection, which SQL calls by its name, of Func0, Func1, Func2, Func3 or FuncN, such as:\n\n\tsqlite.WithFunction(\"double\", true, sqlite.Func1(func(n int64) (int64, error) {\n\t\treturn n * 2, nil\n\t}))\n\nA deterministic function always returns the same result for the same arguments, so it can be used in indexes.\n\n```go\nfunc WithMaxReaders(n int) Opt\n```\nWithMaxReaders sets the most connections which read at once, which is DefaultReaders, unless it's set. A database in memory has one connection.\n\n```go\nfunc WithPragma(name, value string) Opt\n```\nWithPragma 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.\n\n```go\nfunc WithSchemaName(name string) Opt\n```\nWithSchemaName sets the schema of the changes of the main database, which are published to the broadcaster, with WithBroadcaster, rather than main, such as webhook, so the changes of databases whose tables have the same names, which are published to one broadcaster, are told apart. The changes of attached databases are of their names.\n\n```go\nfunc WithTracer(t trace.Tracer) Opt\n```\nWithTracer 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.\n\n```go\ntype Options struct {\n\t// The most connections which read at once\n\tReaders int\n\n\t// The pragmas, which are set on each connection, after the defaults\n\tPragmas []Pragma\n\n\t// The functions, which are created on each connection\n\tFunctions []Function\n\n\t// The bind variables of each query\n\tBind *Bind\n\n\t// The OpenTelemetry tracer, of a span of each query\n\tTracer trace.Tracer\n\n\t// The broadcaster, which the rows which are written are published to,\n\t// once they're committed, and the tables which are, or all of them\n\tBroadcaster Publisher\n\tTables      []string\n\n\t// The schema of the changes of the main database, which are published,\n\t// rather than main\n\tSchema string\n}\n```\nOptions are what Open is given, which an implementation, such as package ncruces, reads, with Apply\n\n```go\nfunc Apply(opts ...Opt) (*Options, error)\n```\nApply returns the options, of their defaults, as they're set\n\n```go\ntype PoolConn interface {\n\tConn\n\n\t// Ping the database\n\tPing(context.Context) error\n\n\t// Close the database\n\tClose() error\n}\n```\nPoolConn is a database, opened from a file or in memory, which writes on one connection, and reads on many.\n\n```go\ntype Pragma struct {\n\tName  string\n\tValue string\n}\n```\nPragma is a pragma, which is set on each connection, such as cache\\_size, of -4000.\n\n```go\ntype Publisher interface {\n\tPublish(schema.Change) int\n}\n```\nPublisher 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.\n\n```go\ntype Queries struct {\n\t// contains filtered or unexported fields\n}\n```\nQueries represents a collection of named SQL statements parsed from a reader. SQL statements are separated by comment lines in the format: -- \\\u003cidentifier\u003e or -- \\\u003cidentifier\u003e.\\\u003cidentifier\u003e, with any number of dots between them. Each statement is stored with its key.\n\n```go\nfunc NewQueries(r io.Reader) (*Queries, error)\n```\nNewQueries parses SQL statements from a reader and returns a Queries collection. SQL statements must be separated by comment lines with a key: -- \\\u003cidentifier\u003e.\\\u003cidentifier\u003e, 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.\n\nExample input format:\n\n\t-- user.select\n\tSELECT * FROM users WHERE id = @id;\n\n\t-- user.insert\n\tINSERT INTO users (name, email) VALUES (@name, @email);\n\n```go\nfunc (s *Queries) Keys() []string\n```\nKeys returns a slice of all SQL statement identifiers in the order they were parsed.\n\n```go\nfunc (s *Queries) Query(key string) string\n```\nQuery retrieves the SQL statement associated with the given key. Returns an empty string if the key does not exist.\n\n```go\ntype Reader interface {\n\t// Scan row into a result\n\tScan(Row) error\n}\n```\nReader scans a database row into an object.\n\n```go\ntype Row interface {\n\tScan(...any) error\n}\n```\nRow is a row of a query's results, which is scanned into values.\n\n```go\ntype Selector interface {\n\t// Set bind parameters for getting, updating or deleting\n\tSelect(*Bind, Op) (string, error)\n}\n```\nSelector binds parameters for get, update, or delete operations.\n\n```go\ntype Writer interface {\n\t// Set bind parameters for an insert\n\tInsert(*Bind) (string, error)\n\n\t// Set bind parameters for an update\n\tUpdate(*Bind) error\n}\n```\nWriter binds object fields for insert or update operations.\n\n","path":"github.com/mutablelogic/go-sqlite","name":"sqlite","synopsis":"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.","isRedistributable":true}
