4.2 KiB
Go bindings to SQLite using Wazero
Go module github.com/ncruces/go-sqlite3 wraps a WASM build of SQLite,
and uses wazero to provide cgo-free SQLite bindings.
- Package
github.com/ncruces/go-sqlite3wraps the C SQLite API (example usage). - Package
github.com/ncruces/go-sqlite3/driverprovides adatabase/sqldriver (example usage). - Package
github.com/ncruces/go-sqlite3/embedembeds a build of SQLite into your application. - Package
github.com/ncruces/go-sqlite3/vfswraps the C SQLite VFS API and provides a pure Go implementation. - Package
github.com/ncruces/go-sqlite3/gormliteprovides a GORM driver.
Caveats
This module replaces the SQLite OS Interface (aka VFS) with a pure Go implementation. This has benefits, but also comes with some drawbacks.
Write-Ahead Logging
Because WASM does not support shared memory, WAL support is limited.
To work around this limitation, SQLite is compiled with
SQLITE_DEFAULT_LOCKING_MODE=1,
making EXCLUSIVE the default locking mode.
For non-WAL databases, NORMAL locking mode can be activated with
PRAGMA locking_mode=NORMAL.
Because connection pooling is incompatible with EXCLUSIVE locking mode,
the database/sql driver defaults to NORMAL locking mode.
To open WAL databases, or use EXCLUSIVE locking mode,
disable connection pooling by calling
db.SetMaxOpenConns(1).
POSIX Advisory Locks
POSIX advisory locks, which SQLite uses, are broken by design.
On Linux, macOS and illumos, this module uses OFD locks to synchronize access to database files. OFD locks are fully compatible with process-associated POSIX advisory locks.
On BSD Unixes, this module uses BSD locks. BSD locks may not be compatible with process-associated POSIX advisory locks.
Testing
The pure Go VFS is tested by running an unmodified build of SQLite's mptest on Linux, macOS and Windows. Performance is tested by running speedtest1.
Roadmap
- advanced SQLite features
- nested transactions
- incremental BLOB I/O
- online backup
- session extension
- custom VFSes
- custom VFS API
- in-memory VFS
- read-only VFS, wrapping an
io.ReaderAt - cloud-based VFS, based on Cloud Backed SQLite
- custom SQL functions