1// Package sqlite3 wraps the C SQLite API.
11 "github.com/tetratelabs/wazero"
12 "github.com/tetratelabs/wazero/api"
14 "github.com/ncruces/go-sqlite3/internal/util"
15 "github.com/ncruces/go-sqlite3/vfs"
18// Configure SQLite Wasm.
20// Importing package embed initializes [Binary]
21// with an appropriate build of SQLite:
23// import _ "github.com/ncruces/go-sqlite3/embed"
25 Binary []byte // Wasm binary to load.
26 Path string // Path to load the binary from.
28 RuntimeConfig wazero.RuntimeConfig
31// Initialize decodes and compiles the SQLite Wasm binary.
32// This is called implicitly when the first connection is openned,
33// but is potentially slow, so you may want to call it at a more convenient time.
34func Initialize() error {
35 instance.once.Do(compileSQLite)
40 runtime wazero.Runtime
41 compiled wazero.CompiledModule
47 ctx := context.Background()
50 cfg = wazero.NewRuntimeConfig()
51 if bits.UintSize < 64 {
52 cfg = cfg.WithMemoryLimitPages(512) // 32MB
54 cfg = cfg.WithMemoryLimitPages(4096) // 256MB
56 cfg = cfg.WithCoreFeatures(api.CoreFeaturesV2)
59 instance.runtime = wazero.NewRuntimeWithConfig(ctx, cfg)
61 env := instance.runtime.NewHostModuleBuilder("env")
62 env = vfs.ExportHostFunctions(env)
63 env = exportCallbacks(env)
64 _, instance.err = env.Instantiate(ctx)
65 if instance.err != nil {
70 if bin == nil && Path != "" {
71 bin, instance.err = os.ReadFile(Path)
72 if instance.err != nil {
77 instance.err = util.NoBinaryErr
81 instance.compiled, instance.err = instance.runtime.CompileModule(ctx, bin)
95func instantiateSQLite() (sqlt *sqlite, err error) {
96 if err := Initialize(); err != nil {
101 sqlt.ctx = util.NewContext(context.Background())
103 sqlt.mod, err = instance.runtime.InstantiateModule(sqlt.ctx,
104 instance.compiled, wazero.NewModuleConfig().WithName(""))
108 if sqlt.getfn("sqlite3_progress_handler_go") == nil {
109 return nil, util.BadBinaryErr
114func (sqlt *sqlite) close() error {
115 return sqlt.mod.Close(sqlt.ctx)
118func (sqlt *sqlite) error(rc res_t, handle ptr_t, sql ...string) error {
123 if ErrorCode(rc) == NOMEM || xErrorCode(rc) == IOERR_NOMEM {
128 var msg, query string
129 if ptr := ptr_t(sqlt.call("sqlite3_errmsg", stk_t(handle))); ptr != 0 {
130 msg = util.ReadString(sqlt.mod, ptr, _MAX_LENGTH)
132 case msg == "not an error":
134 case msg == util.ErrorCodeString(uint32(rc))[len("sqlite3: "):]:
140 if i := int32(sqlt.call("sqlite3_error_offset", stk_t(handle))); i != -1 {
145 if msg != "" || query != "" {
146 return &Error{code: rc, msg: msg, sql: query}
149 return xErrorCode(rc)
152func (sqlt *sqlite) getfn(name string) api.Function {
154 p := unsafe.StringData(name)
155 for i := range c.id {
158 c.mask &^= uint32(1) << i
162 return sqlt.mod.ExportedFunction(name)
165func (sqlt *sqlite) putfn(name string, fn api.Function) {
167 p := unsafe.StringData(name)
168 i := bits.TrailingZeros32(^c.mask)
172 c.mask |= uint32(1) << i
180func (sqlt *sqlite) call(name string, params ...stk_t) stk_t {
181 copy(sqlt.stack[:], params)
182 fn := sqlt.getfn(name)
183 err := fn.CallWithStack(sqlt.ctx, sqlt.stack[:])
188 return stk_t(sqlt.stack[0])
191func (sqlt *sqlite) free(ptr ptr_t) {
195 sqlt.call("sqlite3_free", stk_t(ptr))
198func (sqlt *sqlite) new(size int64) ptr_t {
199 ptr := ptr_t(sqlt.call("sqlite3_malloc64", stk_t(size)))
200 if ptr == 0 && size != 0 {
206func (sqlt *sqlite) realloc(ptr ptr_t, size int64) ptr_t {
207 ptr = ptr_t(sqlt.call("sqlite3_realloc64", stk_t(ptr), stk_t(size)))
208 if ptr == 0 && size != 0 {
214func (sqlt *sqlite) newBytes(b []byte) ptr_t {
218 ptr := sqlt.new(int64(len(b)))
219 util.WriteBytes(sqlt.mod, ptr, b)
223func (sqlt *sqlite) newString(s string) ptr_t {
224 ptr := sqlt.new(int64(len(s)) + 1)
225 util.WriteString(sqlt.mod, ptr, s)
229const arenaSize = 4096
231func (sqlt *sqlite) newArena() arena {
234 base: sqlt.new(arenaSize),
245func (a *arena) free() {
249 for _, ptr := range a.ptrs {
256func (a *arena) mark() (reset func()) {
260 rest := a.ptrs[ptrs:]
261 for _, ptr := range a.ptrs[:ptrs] {
269func (a *arena) new(size int64) ptr_t {
270 // Align the next address, to 4 or 8 bytes.
272 a.next = (a.next + 3) &^ 3
274 a.next = (a.next + 7) &^ 7
276 if size <= arenaSize-int64(a.next) {
277 ptr := a.base + ptr_t(a.next)
278 a.next += int32(size)
281 ptr := a.sqlt.new(size)
282 a.ptrs = append(a.ptrs, ptr)
286func (a *arena) bytes(b []byte) ptr_t {
290 ptr := a.new(int64(len(b)))
291 util.WriteBytes(a.sqlt.mod, ptr, b)
295func (a *arena) string(s string) ptr_t {
296 ptr := a.new(int64(len(s)) + 1)
297 util.WriteString(a.sqlt.mod, ptr, s)
301func exportCallbacks(env wazero.HostModuleBuilder) wazero.HostModuleBuilder {
302 util.ExportFuncII(env, "go_progress_handler", progressCallback)
303 util.ExportFuncIII(env, "go_busy_timeout", timeoutCallback)
304 util.ExportFuncIII(env, "go_busy_handler", busyCallback)
305 util.ExportFuncII(env, "go_commit_hook", commitCallback)
306 util.ExportFuncVI(env, "go_rollback_hook", rollbackCallback)
307 util.ExportFuncVIIIIJ(env, "go_update_hook", updateCallback)
308 util.ExportFuncIIIII(env, "go_wal_hook", walCallback)
309 util.ExportFuncIIIII(env, "go_trace", traceCallback)
310 util.ExportFuncIIIIII(env, "go_autovacuum_pages", autoVacuumCallback)
311 util.ExportFuncIIIIIII(env, "go_authorizer", authorizerCallback)
312 util.ExportFuncVIII(env, "go_log", logCallback)
313 util.ExportFuncVI(env, "go_destroy", destroyCallback)
314 util.ExportFuncVIIII(env, "go_func", funcCallback)
315 util.ExportFuncVIIIII(env, "go_step", stepCallback)
316 util.ExportFuncVIIII(env, "go_value", valueCallback)
317 util.ExportFuncVIIII(env, "go_inverse", inverseCallback)
318 util.ExportFuncVIIII(env, "go_collation_needed", collationCallback)
319 util.ExportFuncIIIIII(env, "go_compare", compareCallback)
320 util.ExportFuncIIIIII(env, "go_vtab_create", vtabModuleCallback(xCreate))
321 util.ExportFuncIIIIII(env, "go_vtab_connect", vtabModuleCallback(xConnect))
322 util.ExportFuncII(env, "go_vtab_disconnect", vtabDisconnectCallback)
323 util.ExportFuncII(env, "go_vtab_destroy", vtabDestroyCallback)
324 util.ExportFuncIII(env, "go_vtab_best_index", vtabBestIndexCallback)
325 util.ExportFuncIIIII(env, "go_vtab_update", vtabUpdateCallback)
326 util.ExportFuncIII(env, "go_vtab_rename", vtabRenameCallback)
327 util.ExportFuncIIIII(env, "go_vtab_find_function", vtabFindFuncCallback)
328 util.ExportFuncII(env, "go_vtab_begin", vtabBeginCallback)
329 util.ExportFuncII(env, "go_vtab_sync", vtabSyncCallback)
330 util.ExportFuncII(env, "go_vtab_commit", vtabCommitCallback)
331 util.ExportFuncII(env, "go_vtab_rollback", vtabRollbackCallback)
332 util.ExportFuncIII(env, "go_vtab_savepoint", vtabSavepointCallback)
333 util.ExportFuncIII(env, "go_vtab_release", vtabReleaseCallback)
334 util.ExportFuncIII(env, "go_vtab_rollback_to", vtabRollbackToCallback)
335 util.ExportFuncIIIIII(env, "go_vtab_integrity", vtabIntegrityCallback)
336 util.ExportFuncIII(env, "go_cur_open", cursorOpenCallback)
337 util.ExportFuncII(env, "go_cur_close", cursorCloseCallback)
338 util.ExportFuncIIIIII(env, "go_cur_filter", cursorFilterCallback)
339 util.ExportFuncII(env, "go_cur_next", cursorNextCallback)
340 util.ExportFuncII(env, "go_cur_eof", cursorEOFCallback)
341 util.ExportFuncIIII(env, "go_cur_column", cursorColumnCallback)
342 util.ExportFuncIII(env, "go_cur_rowid", cursorRowIDCallback)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list