Files
sqlite3/README.md

143 lines
7.7 KiB
Markdown
Raw Normal View History

2023-01-12 16:29:57 +00:00
# Go bindings to SQLite using Wazero
2023-01-26 01:11:09 +00:00
[![Go Reference](https://pkg.go.dev/badge/image)](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
[![Go Report](https://goreportcard.com/badge/github.com/ncruces/go-sqlite3)](https://goreportcard.com/report/github.com/ncruces/go-sqlite3)
2023-03-01 13:27:50 +00:00
[![Go Coverage](https://github.com/ncruces/go-sqlite3/wiki/coverage.svg)](https://github.com/ncruces/go-sqlite3/wiki/Test-coverage-report)
2023-01-12 16:29:57 +00:00
2023-12-15 10:46:29 +00:00
Go module `github.com/ncruces/go-sqlite3` is `cgo`-free [SQLite](https://sqlite.org/) wrapper.\
It provides a [`database/sql`](https://pkg.go.dev/database/sql) compatible driver,
as well as direct access to most of the [C SQLite API](https://sqlite.org/cintro.html).
2024-03-25 15:14:24 +00:00
It wraps a [Wasm](https://webassembly.org/) build of SQLite, and uses [wazero](https://wazero.io/) as the runtime.\
2023-12-15 10:46:29 +00:00
Go, wazero and [`x/sys`](https://pkg.go.dev/golang.org/x/sys) are the _only_ runtime dependencies.
### Packages
2023-02-24 14:31:41 +00:00
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3`](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
2023-11-09 16:35:45 +00:00
wraps the [C SQLite API](https://sqlite.org/cintro.html)
2023-09-01 15:54:40 +01:00
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-package)).
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3/driver`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver)
2023-09-01 15:54:40 +01:00
provides a [`database/sql`](https://pkg.go.dev/database/sql) driver
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package)).
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3/embed`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/embed)
2023-09-01 15:54:40 +01:00
embeds a build of SQLite into your application.
2023-11-21 13:40:55 +00:00
- [`github.com/ncruces/go-sqlite3/vfs`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs)
wraps the [C SQLite VFS API](https://sqlite.org/vfs.html) and provides a pure Go implementation.
- [`github.com/ncruces/go-sqlite3/gormlite`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/gormlite)
provides a [GORM](https://gorm.io) driver.
2023-12-15 10:46:29 +00:00
### Extensions
2023-11-21 13:40:55 +00:00
2023-12-14 20:36:07 +00:00
- [`github.com/ncruces/go-sqlite3/ext/array`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/array)
2023-11-21 13:40:55 +00:00
provides the [`array`](https://sqlite.org/carray.html) table-valued function.
2023-12-14 20:36:07 +00:00
- [`github.com/ncruces/go-sqlite3/ext/blobio`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/blobio)
2023-11-21 13:40:55 +00:00
simplifies [incremental BLOB I/O](https://sqlite.org/c3ref/blob_open.html).
2023-11-23 13:03:38 +00:00
- [`github.com/ncruces/go-sqlite3/ext/csv`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/csv)
reads [comma-separated values](https://sqlite.org/csv.html).
2023-12-12 14:06:54 +00:00
- [`github.com/ncruces/go-sqlite3/ext/fileio`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/fileio)
2023-12-14 20:36:07 +00:00
reads, writes and lists files.
2023-12-30 00:47:16 +00:00
- [`github.com/ncruces/go-sqlite3/ext/hash`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/hash)
provides cryptographic hash functions.
2023-11-23 15:32:28 +00:00
- [`github.com/ncruces/go-sqlite3/ext/lines`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/lines)
2023-12-14 20:36:07 +00:00
reads data [line-by-line](https://github.com/asg017/sqlite-lines).
2023-12-06 15:39:26 +00:00
- [`github.com/ncruces/go-sqlite3/ext/pivot`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/pivot)
creates [pivot tables](https://github.com/jakethaw/pivot_vtab).
- [`github.com/ncruces/go-sqlite3/ext/statement`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/statement)
2023-12-14 20:36:07 +00:00
creates [parameterized views](https://github.com/0x09/sqlite-statement-vtab).
2023-11-08 14:41:11 +00:00
- [`github.com/ncruces/go-sqlite3/ext/stats`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/stats)
2023-12-14 20:36:07 +00:00
provides [statistics](https://www.oreilly.com/library/view/sql-in-a/9780596155322/ch04s02.html) functions.
2023-11-08 14:41:11 +00:00
- [`github.com/ncruces/go-sqlite3/ext/unicode`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/unicode)
2023-11-21 13:40:55 +00:00
provides [Unicode aware](https://sqlite.org/src/dir/ext/icu) functions.
2024-02-08 00:31:32 +00:00
- [`github.com/ncruces/go-sqlite3/ext/zorder`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/zorder)
maps multidimensional data to one dimension.
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3/vfs/memdb`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/memdb)
2023-09-01 15:54:40 +01:00
implements an in-memory VFS.
2023-09-15 15:37:57 +01:00
- [`github.com/ncruces/go-sqlite3/vfs/readervfs`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/readervfs)
2023-09-01 15:54:40 +01:00
implements a VFS for immutable databases.
2023-11-21 13:40:55 +00:00
### Advanced features
2023-12-30 00:47:16 +00:00
- [incremental BLOB I/O](https://sqlite.org/c3ref/blob_open.html)
- [nested transactions](https://sqlite.org/lang_savepoint.html)
- [custom functions](https://sqlite.org/c3ref/create_function.html)
- [virtual tables](https://sqlite.org/vtab.html)
- [custom VFSes](https://sqlite.org/vfs.html)
- [online backup](https://sqlite.org/backup.html)
- [JSON support](https://sqlite.org/json1.html)
- [math functions](https://sqlite.org/lang_mathfunc.html)
- [full-text search](https://sqlite.org/fts5.html)
- [geospatial search](https://sqlite.org/geopoly.html)
- [and more…](embed/README.md)
2023-02-24 14:31:41 +00:00
### Caveats
2023-11-09 16:35:45 +00:00
This module replaces the SQLite [OS Interface](https://sqlite.org/vfs.html)
2023-06-01 18:11:37 +01:00
(aka VFS) with a [pure Go](vfs/) implementation.
2023-05-31 18:45:45 +01:00
This has benefits, but also comes with some drawbacks.
2023-03-10 16:25:07 +00:00
2023-03-01 12:16:36 +00:00
#### Write-Ahead Logging
2024-03-25 15:14:24 +00:00
Because Wasm does not support shared memory,
2023-11-09 16:35:45 +00:00
[WAL](https://sqlite.org/wal.html) support is [limited](https://sqlite.org/wal.html#noshm).
2023-02-24 14:31:41 +00:00
2023-06-24 02:18:56 +01:00
To work around this limitation, SQLite is [patched](sqlite3/locking_mode.patch)
to always use `EXCLUSIVE` locking mode for WAL databases.
2023-02-24 14:31:41 +00:00
Because connection pooling is incompatible with `EXCLUSIVE` locking mode,
2023-10-19 11:53:31 +01:00
to use the [`database/sql`](https://pkg.go.dev/database/sql) driver
with WAL mode databases you should disable connection pooling by calling
2023-03-10 15:50:11 +00:00
[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns).
2023-02-24 14:31:41 +00:00
2023-10-17 14:04:23 +01:00
#### File Locking
2023-03-01 12:16:36 +00:00
2023-10-17 14:04:23 +01:00
POSIX advisory locks, which SQLite uses on Unix, are
2023-11-09 16:35:45 +00:00
[broken by design](https://sqlite.org/src/artifact/2e8b12?ln=1073-1161).
2023-03-22 03:15:54 +00:00
2023-03-23 13:28:25 +00:00
On Linux, macOS and illumos, this module uses
[OFD locks](https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html)
2023-03-01 12:16:36 +00:00
to synchronize access to database files.
2023-10-17 14:04:23 +01:00
OFD locks are fully compatible with POSIX advisory locks.
2023-03-01 12:16:36 +00:00
2023-10-16 02:11:20 +01:00
On BSD Unixes, this module uses
2023-03-23 13:28:25 +00:00
[BSD locks](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2).
2023-10-17 14:04:23 +01:00
On BSD Unixes, BSD locks are fully compatible with POSIX advisory locks.
2023-03-01 12:16:36 +00:00
2024-01-24 09:58:05 +00:00
On Windows, this module uses `LockFileEx` and `UnlockFileEx`,
2023-10-19 11:53:31 +01:00
like SQLite.
2023-09-01 15:54:40 +01:00
2023-10-17 14:04:23 +01:00
On all other platforms, file locking is not supported, and you must use
2023-11-09 16:35:45 +00:00
[`nolock=1`](https://sqlite.org/uri.html#urinolock)
2024-01-24 09:58:05 +00:00
(or [`immutable=1`](https://sqlite.org/uri.html#uriimmutable))
2023-10-17 14:04:23 +01:00
to open database files.
2024-02-10 10:03:12 +00:00
You can use [`vfs.SupportsFileLocking`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs#SupportsFileLocking)
to check if your platform supports file locking.
2024-01-24 09:58:05 +00:00
2023-10-19 11:53:31 +01:00
To use the [`database/sql`](https://pkg.go.dev/database/sql) driver
with `nolock=1` you must disable connection pooling by calling
[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns).
2023-09-01 15:54:40 +01:00
2023-12-30 00:47:16 +00:00
### Testing
This project aims for [high test coverage](https://github.com/ncruces/go-sqlite3/wiki/Test-coverage-report).
2024-01-18 15:53:00 +00:00
It also benefits greatly from [SQLite's](https://sqlite.org/testing.html) and
2023-12-30 00:47:16 +00:00
[wazero's](https://tetrate.io/blog/introducing-wazero-from-tetrate/#:~:text=Rock%2Dsolid%20test%20approach) thorough testing.
2023-03-10 16:25:07 +00:00
2023-08-09 15:22:24 +01:00
The pure Go VFS is tested by running SQLite's
2023-03-10 16:25:07 +00:00
[mptest](https://github.com/sqlite/sqlite/blob/master/mptest/mptest.c)
2023-10-17 14:04:23 +01:00
on Linux, macOS, Windows and FreeBSD.
2023-12-30 00:47:16 +00:00
### Performance
Perfomance of the [`database/sql`](https://pkg.go.dev/database/sql) driver is
[competitive](https://github.com/cvilsmeier/go-sqlite-bench) with alternatives.
2024-03-25 15:14:24 +00:00
The Wasm and VFS layers are also tested by running SQLite's
2023-03-18 03:03:11 +00:00
[speedtest1](https://github.com/sqlite/sqlite/blob/master/test/speedtest1.c).
2023-02-24 14:31:41 +00:00
### Alternatives
- [`modernc.org/sqlite`](https://pkg.go.dev/modernc.org/sqlite)
- [`crawshaw.io/sqlite`](https://pkg.go.dev/crawshaw.io/sqlite)
2023-02-26 03:22:08 +00:00
- [`github.com/mattn/go-sqlite3`](https://pkg.go.dev/github.com/mattn/go-sqlite3)
2023-09-01 15:54:40 +01:00
- [`github.com/zombiezen/go-sqlite`](https://pkg.go.dev/github.com/zombiezen/go-sqlite)