diff --git a/README.md b/README.md index 2e9476f..b408f3f 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,10 @@ For non-WAL databases, `NORMAL` locking mode can be activated with [`PRAGMA locking_mode=NORMAL`](https://www.sqlite.org/pragma.html#pragma_locking_mode). Because connection pooling is incompatible with `EXCLUSIVE` locking mode, -the `database/sql` driver defaults to `NORMAL` locking mode, -and WAL databases are not supported. +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)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns). #### Open File Description Locks diff --git a/conn.go b/conn.go index 60b1ec6..2bb2d42 100644 --- a/conn.go +++ b/conn.go @@ -3,6 +3,7 @@ package sqlite3 import ( "context" "database/sql/driver" + "errors" "fmt" "net/url" "runtime" @@ -91,8 +92,11 @@ func (c *Conn) openDB(filename string, flags OpenFlag) (uint32, error) { pragmaPtr := c.arena.string(pragmas.String()) r := c.call(c.api.exec, uint64(handle), uint64(pragmaPtr), 0, 0, 0) if err := c.module.error(r[0], handle, pragmas.String()); err != nil { + if errors.Is(err, ERROR) { + err = fmt.Errorf("sqlite3: invalid _pragma: %w", err) + } c.closeDB(handle) - return 0, fmt.Errorf("sqlite3: invalid _pragma: %w", err) + return 0, err } } return handle, nil diff --git a/driver/driver.go b/driver/driver.go index 0638056..06dc4df 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -96,6 +96,7 @@ var ( // Ensure these interfaces are implemented: _ driver.ExecerContext = conn{} _ driver.ConnBeginTx = conn{} + _ driver.Validator = conn{} _ sqlite3.DriverConn = conn{} ) @@ -103,6 +104,11 @@ func (c conn) Close() error { return c.conn.Close() } +func (c conn) IsValid() (valid bool) { + r, err := c.conn.Pragma("locking_mode") + return err == nil && len(r) == 1 && r[0] == "normal" +} + func (c conn) Begin() (driver.Tx, error) { return c.BeginTx(context.Background(), driver.TxOptions{}) } diff --git a/embed/README.md b/embed/README.md new file mode 100644 index 0000000..3acd5df --- /dev/null +++ b/embed/README.md @@ -0,0 +1,15 @@ +# Embeddable WASM build of SQLite + +This folder includes an embeddable WASM build of SQLite 3.41.0 for use with +[`github.com/ncruces/go-sqlite3`](https://pkg.go.dev/github.com/ncruces/go-sqlite3). + +The following optional features are compiled in: +- math functions +- FTS3/4/5 +- JSON +- R*Tree +- GeoPoly + +See the [configuration options](../sqlite3/sqlite_cfg.h). + +Built using [`zig`](https://ziglang.org/) version 0.10.1. \ No newline at end of file diff --git a/embed/init.go b/embed/init.go index 3ad54f8..da527ab 100644 --- a/embed/init.go +++ b/embed/init.go @@ -4,12 +4,6 @@ // with an appropriate build of SQLite: // // import _ "github.com/ncruces/go-sqlite3/embed" -// -// The following optional features are compiled in: -// math functions, JSON, FTS3/4/5, R*Tree, GeoPoly. -// -// You can obtain this build of SQLite from: -// https://github.com/ncruces/go-sqlite3/tree/main/embed package embed import (