Documentation
¶
Overview ¶
Package sqlite links the pure-Go SQLite engine (glebarez/go-sqlite, on modernc's) for the ORM: blank-import it to Connect to "sqlite".
import _ "github.com/vuka-lang/orm/sqlite"
db, err := orm.Connect("sqlite", "file:shop.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)")
Kept out of the orm package itself so a Postgres or MySQL program doesn't ship the transpiled-C SQLite engine (~5MB of binary) it never opens. Don't import it beside another package registering the "sqlite" database/sql driver (modernc.org/sqlite).
It also gives SQLite the functions Postgres's pgvector and pg_trgm have, computed in Go, unindexed: l2_distance, cosine_distance and inner_product of vectors written as text ([1,2,3]), and similarity, the trigram similarity of two texts. The ORM's vector and trigram searches use them, so they run on SQLite in development and tests.
Func registers a Go function of your own the same way, for an orm.Func to call.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Func ¶
Func registers fn, a Go function, as the SQL function name on every SQLite connection opened after it — so register in an init, or before orm.Connect: connections already open don't get it. It is deterministic unless Volatile.
fn's parameters are its SQL arguments, its last one variadic or not: string, []byte, bool, the integer and float kinds, time.Time (read from SQLite's text), any (what SQLite has: int64, float64, string, []byte or nil), or a pointer to one of them, nil for NULL. A NULL passed to a parameter that isn't a pointer or any makes the call NULL without running fn, as SQL's own functions are. It returns one value of those kinds (a driver.Valuer too, a nil pointer for NULL), and an error after it if it can fail.
func init() {
sqlite.MustFunc("editdist", func(a, b string) int { … })
}
var EditDist = orm.OrderedFunc[int]("editdist",
orm.Template("sqlite", "editdist({0}, {1})"), orm.Template("postgres", "levenshtein({0}, {1})"))
Users.Filter(EditDist(orm.F("name"), "jon").Le(1)).Order(EditDist(orm.F("name"), "jon").Asc())
Postgres and MySQL run functions written in SQL: create them with the migration package's CreateFunction, and give the orm function a Template per dialect when the names differ.