Compare commits

..
1 Commits
Author SHA1 Message Date
Nuno Cruces fc3a993c3e Parquet vtab. 2025-01-07 16:33:01 +00:00
342 changed files with 8929 additions and 10876 deletions
+6 -1
View File
@@ -1,6 +1,11 @@
name: VM Actions matrix
description: VM Actions matrix template
inputs:
run:
description: The CI command to run
required: true
runs:
using: composite
steps:
@@ -8,4 +13,4 @@ runs:
with:
usesh: true
copyback: false
run: . ./test.sh
run: ${{inputs.run}}
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
echo 'set -eu' > test.sh
for p in $(go list ./...); do
dir=".${p#github.com/ncruces/go-sqlite3}"
name="$(basename "$p").test"
(cd ${dir}; go test -c ${BUILDFLAGS:-})
[ -f "${dir}/${name}" ] && echo "(cd ${dir}; ./${name} ${TESTFLAGS:-})" >> test.sh
done
if [[ -v VMACTIONS ]]; then
envsubst < .github/actions/vmactions/template.yml > .github/actions/vmactions/action.yml
fi
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ "$OSTYPE" == "linux"* ]]; then
WASI_SDK="https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-linux.tar.gz"
BINARYEN="https://github.com/WebAssembly/binaryen/releases/download/version_121/binaryen-version_121-x86_64-linux.tar.gz"
elif [[ "$OSTYPE" == "darwin"* ]]; then
WASI_SDK="https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-arm64-macos.tar.gz"
BINARYEN="https://github.com/WebAssembly/binaryen/releases/download/version_121/binaryen-version_121-arm64-macos.tar.gz"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
WASI_SDK="https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-windows.tar.gz"
BINARYEN="https://github.com/WebAssembly/binaryen/releases/download/version_121/binaryen-version_121-x86_64-windows.tar.gz"
fi
# Download tools
mkdir -p tools/
[ -d "tools/wasi-sdk" ] || curl -#L "$WASI_SDK" | tar xzC tools &
[ -d "tools/binaryen" ] || curl -#L "$BINARYEN" | tar xzC tools &
wait
[ -d "tools/wasi-sdk" ] || mv "tools/wasi-sdk"* "tools/wasi-sdk"
[ -d "tools/binaryen" ] || mv "tools/binaryen"* "tools/binaryen"
# Download and build SQLite
sqlite3/download.sh
embed/build.sh
embed/bcw2/build.sh
# Download and build sqlite-createtable-parser
util/sql3util/wasm/download.sh
util/sql3util/wasm/build.sh
# Check diffs
git diff --exit-code
+32
View File
@@ -0,0 +1,32 @@
name: Reproducible build
on:
workflow_dispatch:
permissions:
contents: read
id-token: write
attestations: write
jobs:
build:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: ilammy/msvc-dev-cmd@v1
- uses: actions/checkout@v4
- name: Build
shell: bash
run: .github/workflows/repro.sh
- uses: actions/attest-build-provenance@v2
if: matrix.os == 'ubuntu-latest'
with:
subject-path: |
embed/sqlite3.wasm
embed/bcw2/bcw2.wasm
util/sql3util/wasm/sql3parse_table.wasm
+218
View File
@@ -0,0 +1,218 @@
name: Test
on:
push:
branches: [ 'main' ]
paths:
- '**.go'
- '**.mod'
- '**.wasm'
pull_request:
branches: [ 'main' ]
paths:
- '**.go'
- '**.mod'
- '**.wasm'
workflow_dispatch:
jobs:
test:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: stable }
- name: Format
run: gofmt -s -w . && git diff --exit-code
if: matrix.os != 'windows-latest'
- name: Tidy
run: go mod tidy && git diff --exit-code
- name: Download
run: go mod download
- name: Verify
run: go mod verify
- name: Vet
run: go vet ./...
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./... -bench . -benchtime=1x
- name: Test BSD locks
run: go test -v -tags sqlite3_flock ./...
if: matrix.os != 'windows-latest'
- name: Test dot locks
run: go test -v -tags sqlite3_dotlk ./...
if: matrix.os != 'windows-latest'
- name: Test GORM
shell: bash
run: gormlite/test.sh
- name: Test modules
shell: bash
run: go test -v ./embed/bcw2/...
- name: Collect coverage
run: go run github.com/dave/courtney@latest
if: |
github.event_name == 'push' &&
matrix.os == 'ubuntu-latest'
- uses: ncruces/go-coverage-report@v0
with:
coverage-file: coverage.out
chart: true
amend: true
if: |
github.event_name == 'push' &&
matrix.os == 'ubuntu-latest'
test-bsd:
strategy:
matrix:
os:
- name: freebsd
version: '14.2'
flags: '-test.v'
- name: netbsd
version: '10.0'
flags: '-test.v'
- name: freebsd
arch: arm64
version: '14.2'
flags: '-test.v -test.short'
- name: netbsd
arch: arm64
version: '10.0'
flags: '-test.v -test.short'
- name: openbsd
version: '7.6'
flags: '-test.v -test.short'
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Build
env:
GOOS: ${{ matrix.os.name }}
GOARCH: ${{ matrix.os.arch }}
TESTFLAGS: ${{ matrix.os.flags }}
run: .github/workflows/build-test.sh
- name: Test
uses: cross-platform-actions/action@v0.26.0
with:
operating_system: ${{ matrix.os.name }}
architecture: ${{ matrix.os.arch }}
version: ${{ matrix.os.version }}
shell: bash
run: . ./test.sh
sync_files: runner-to-vm
test-vm:
strategy:
matrix:
os:
- name: dragonfly
action: 'vmactions/dragonflybsd-vm@v1'
tflags: '-test.v'
- name: illumos
action: 'vmactions/omnios-vm@v1'
tflags: '-test.v'
- name: solaris
action: 'vmactions/solaris-vm@v1'
bflags: '-tags sqlite3_dotlk'
tflags: '-test.v'
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: Build
env:
GOOS: ${{ matrix.os.name }}
BUILDFLAGS: ${{ matrix.os.bflags }}
TESTFLAGS: ${{ matrix.os.tflags }}
VMACTIONS: ${{ matrix.os.action }}
run: .github/workflows/build-test.sh
- name: Test
uses: ./.github/actions/vmactions
with:
usesh: true
copyback: false
run: . ./test.sh
test-wasip1:
runs-on: ubuntu-latest
needs: test
steps:
- uses: bytecodealliance/actions/wasmtime/setup@v1
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: stable }
- name: Set path
run: echo "$(go env GOROOT)/misc/wasm" >> "$GITHUB_PATH"
- name: Test wasmtime
env:
GOOS: wasip1
GOARCH: wasm
GOWASIRUNTIME: wasmtime
GOWASIRUNTIMEARGS: '--env CI=true'
run: go test -v -short -tags sqlite3_dotlk -skip Example ./...
test-qemu:
runs-on: ubuntu-latest
needs: test
steps:
- uses: docker/setup-qemu-action@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: stable }
- name: Test 386 (32-bit)
run: GOARCH=386 go test -v -short ./...
- name: Test arm64 (compiler)
run: GOARCH=arm64 go test -v -short ./...
- name: Test riscv64 (interpreter)
run: GOARCH=riscv64 go test -v -short ./...
- name: Test ppc64le (interpreter)
run: GOARCH=ppc64le go test -v -short ./...
- name: Test s390x (big-endian)
run: GOARCH=s390x go test -v -short -tags sqlite3_dotlk ./...
test-macintel:
runs-on: macos-13
needs: test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with: { go-version: stable }
- name: Test
run: go test -v ./...
+1 -8
View File
@@ -13,11 +13,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
tools
# Go workspace file
go.work
go.work.sum
# env file
.env
tools
-20
View File
@@ -1,20 +0,0 @@
# Fork of ncruces/go-sqlite3. Allowlist UPSTREAM test fixtures + example crypto
# keys (SHA-256/MD5 of the empty string used in the adiantum/XTS VFS tests, and
# the SQLite C amalgamation). Hanzo additions (hanzovfs/, cmd/) are still scanned.
[extend]
useDefault = true
[allowlist]
description = "Upstream SQLite + crypto-VFS test fixtures (not real secrets)"
paths = [
'''sqlite3/sqlite3\.c''',
'''sqlite3/sqlite3ext\.h''',
'''vfs/adiantum/.*''',
'''vfs/xts/.*''',
'''vfs/tests/.*''',
'''tests/.*''',
]
regexes = [
'''e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855''',
'''d41d8cd98f00b204e9800998ecf8427e''',
]
-35
View File
@@ -1,35 +0,0 @@
# CI/CD for hanzoai/platform (the Hanzo PaaS) — NOT GitHub Actions.
# The platform runs `make ci` as a build step and/or a scheduled (cron) task.
# Works identically locally and anywhere: `make ci`.
SHELL := /usr/bin/env bash
export GOPRIVATE := github.com/hanzoai,github.com/luxfi
export GOFLAGS := -mod=mod
BIN := $(CURDIR)/.bin
export PATH := $(BIN):$(PATH)
GITLEAKS_VERSION ?= 8.21.2
ARCH := $(shell uname -m | sed 's/x86_64/x64/; s/aarch64/arm64/')
.PHONY: ci build test scan vuln secrets sbom tools clean
ci: build test scan ## full pipeline (build -> test -> scan)
build:
go build ./...
test:
go test ./...
scan: vuln secrets sbom
vuln:
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
secrets: $(BIN)/gitleaks
gitleaks detect --source . --redact --no-banner --exit-code 1
sbom: $(BIN)/syft
syft . -o cyclonedx-json=sbom.cyclonedx.json -q
tools: $(BIN)/gitleaks $(BIN)/syft
clean:
rm -rf $(BIN) sbom.cyclonedx.json
$(BIN)/gitleaks:
@mkdir -p $(BIN)
curl -sSfL "https://github.com/gitleaks/gitleaks/releases/download/v$(GITLEAKS_VERSION)/gitleaks_$(GITLEAKS_VERSION)_linux_$(ARCH).tar.gz" | tar -xz -C $(BIN) gitleaks
$(BIN)/syft:
@mkdir -p $(BIN)
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b $(BIN) >/dev/null
-7
View File
@@ -1,7 +0,0 @@
github.com/hanzoai/sqlite3 — canonical Hanzo SQLite.
Fork of github.com/ncruces/go-sqlite3 (MIT, Copyright (c) 2023 Nuno Cruces).
The upstream MIT LICENSE is retained in full. Hanzo additions (the hanzovfs
native encrypted VFS, module path, build pins) are likewise MIT-licensed.
Pure-Go, no cgo. WASM SQLite blob from github.com/ncruces/go-sqlite3-wasm (upstream).
-15
View File
@@ -1,15 +0,0 @@
# CI/CD on hanzoai/platform — no GitHub Actions
This repo is built, run, and scanned by **hanzoai/platform** (the Hanzo PaaS),
not GitHub Actions. `.github/workflows` is intentionally absent.
- **Build / run** — the platform builds via Dockerfile / Nixpacks / Railpack and
runs the result; auto-deploy on push.
- **Actions (CI)** — a platform **Scheduled Task** (and/or build step) runs
`make ci` = `build → test → scan` (govulncheck + gitleaks + CycloneDX SBOM),
on push and on a cron. SOC 2 CC7.x/CC8.x evidence (artifacts to hanzoai/audit).
- The platform build env carries Git credentials for `github.com/hanzoai` +
`github.com/luxfi`, so cross-org private modules resolve natively — no per-repo
token dance (the thing that made GitHub Actions painful).
Run anywhere: `make ci`.
+28 -62
View File
@@ -1,4 +1,4 @@
# Go bindings to SQLite using wasm2go
# Go bindings to SQLite using wazero
[![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)
@@ -8,9 +8,9 @@ Go module `github.com/ncruces/go-sqlite3` is a `cgo`-free [SQLite](https://sqlit
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).
It wraps a [Wasm](https://webassembly.org/) build of SQLite,
and uses [wasm2go](https://github.com/ncruces/wasm2go) to translate it to Go.\
Go and [`x/sys`](https://pkg.go.dev/golang.org/x/sys) are the _only_ direct dependencies.
It wraps a [Wasm](https://webassembly.org/) [build](embed/) of SQLite,
and uses [wazero](https://wazero.io/) as the runtime.\
Go, wazero and [`x/sys`](https://pkg.go.dev/golang.org/x/sys) are the _only_ direct dependencies.
### Getting started
@@ -19,6 +19,7 @@ Using the [`database/sql`](https://pkg.go.dev/database/sql) driver:
import "database/sql"
import _ "github.com/ncruces/go-sqlite3/driver"
import _ "github.com/ncruces/go-sqlite3/embed"
var version string
db, _ := sql.Open("sqlite3", "file:demo.db")
@@ -29,10 +30,12 @@ db.QueryRow(`SELECT sqlite_version()`).Scan(&version)
- [`github.com/ncruces/go-sqlite3`](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
wraps the [C SQLite API](https://sqlite.org/cintro.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-package)).
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-package)).
- [`github.com/ncruces/go-sqlite3/driver`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver)
provides a [`database/sql`](https://pkg.go.dev/database/sql) driver
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package)).
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package)).
- [`github.com/ncruces/go-sqlite3/embed`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/embed)
embeds a build of SQLite into your application.
- [`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)
@@ -41,75 +44,52 @@ db.QueryRow(`SELECT sqlite_version()`).Scan(&version)
### Advanced features
- [incremental BLOB I/O](https://sqlite.org/c3ref/blob_open.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/blobio#example-package))
- [nested transactions](https://sqlite.org/lang_savepoint.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-Savepoint))
- [custom functions](https://sqlite.org/c3ref/create_function.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-Conn.CreateFunction))
- [virtual tables](https://sqlite.org/vtab.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-CreateModule))
- [custom VFSes](https://sqlite.org/vfs.html)
([examples](vfs/README.md#custom-vfses))
- [online backup](https://sqlite.org/backup.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#Conn))
- [JSON support](https://sqlite.org/json1.html)
([example](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package-Json))
- [math functions](https://sqlite.org/lang_mathfunc.html)
- [full-text search](https://sqlite.org/fts5.html)
- [geospatial search](https://sqlite.org/geopoly.html)
- [Unicode support](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/unicode)
- [statistics functions](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/stats)
- [encryption at rest](vfs/adiantum/README.md)
- [many extensions](ext/README.md)
- [custom VFSes](vfs/README.md#custom-vfses)
- [and more…](embed/README.md)
### Caveats
This module replaces the SQLite [OS Interface](https://sqlite.org/vfs.html)
(aka VFS) with a [pure Go](vfs/) implementation,
which has advantages and disadvantages.
Read more about the Go VFS design [here](vfs/README.md).
Because each database connection executes within a Wasm sandboxed environment,
memory usage will be higher than alternatives.
Read more about the Go VFS design [here](vfs/README.md).
### Testing
This project aims for [high test coverage](https://github.com/ncruces/go-sqlite3/wiki/Test-coverage-report).
It also benefits greatly from [SQLite's](https://sqlite.org/testing.html) thorough testing.
It also benefits greatly from [SQLite's](https://sqlite.org/testing.html) and
[wazero's](https://tetrate.io/blog/introducing-wazero-from-tetrate/#:~:text=Rock%2Dsolid%20test%20approach) thorough testing.
Every commit is tested on:
* Linux: amd64, arm64, 386, arm, riscv64, ppc64le, loong64, s390x
* macOS: amd64, arm64
* Windows: amd64, arm64
* BSD:
* FreeBSD: amd64, arm64
* NetBSD: amd64, arm64
* DragonFly BSD: amd64
* OpenBSD: amd64
* illumos: amd64
* Solaris: amd64
Certain operating system and CPU combinations have some limitations.
See the [support matrix](https://github.com/ncruces/go-sqlite3/wiki/Support-matrix) for a complete overview.
Every commit is [tested](https://github.com/ncruces/go-sqlite3/wiki/Support-matrix) on
Linux (amd64/arm64/386/riscv64/ppc64le/s390x), macOS (amd64/arm64),
Windows (amd64), FreeBSD (amd64), OpenBSD (amd64), NetBSD (amd64),
DragonFly BSD (amd64), illumos (amd64), and Solaris (amd64).
The Go VFS is tested by running SQLite's
[mptest](https://github.com/sqlite/sqlite/blob/master/mptest/mptest.c).
### Performance
Performance of the [`database/sql`](https://pkg.go.dev/database/sql) driver is
Perfomance of the [`database/sql`](https://pkg.go.dev/database/sql) driver is
[competitive](https://github.com/cvilsmeier/go-sqlite-bench) with alternatives.
The Wasm and VFS layers are also benchmarked by running SQLite's
The Wasm and VFS layers are also tested by running SQLite's
[speedtest1](https://github.com/sqlite/sqlite/blob/master/test/speedtest1.c).
### Concurrency
This module behaves similarly to SQLite in [multi-thread](https://sqlite.org/threadsafe.html) mode:
it is goroutine-safe, provided that no single database connection, or object derived from it,
is used concurrently by multiple goroutines.
The [`database/sql`](https://pkg.go.dev/database/sql) API is safe to use concurrently,
according to its documentation.
### FAQ, issues, new features
For questions, please see [Discussions](https://github.com/ncruces/go-sqlite3/discussions/categories/q-a).
@@ -118,26 +98,12 @@ Also, post there if you used this driver for something interesting
([_"Show and tell"_](https://github.com/ncruces/go-sqlite3/discussions/categories/show-and-tell)),
have an [idea](https://github.com/ncruces/go-sqlite3/discussions/categories/ideas)…
The [Issue](https://github.com/ncruces/go-sqlite3/issues) tracker is for bugs,
The [Issue](https://github.com/ncruces/go-sqlite3/issues) tracker is for bugs we want fixed,
and features we're working on, planning to work on, or asking for help with.
---
### Alternatives
## Hanzo fork
`github.com/hanzoai/sqlite3` is the **canonical Hanzo SQLite** — a fork of
`ncruces/go-sqlite3` (MIT) adding the **`hanzovfs`** native encrypted VFS for
per-tenant SQLite ⇒ `hanzoai/vfs` ⇒ S3 (HIP-0302 / HIP-0107), no FUSE, no cgo.
```go
import (
"github.com/hanzoai/sqlite3"
_ "github.com/hanzoai/sqlite3/hanzovfs" // registers vfs=hanzo
_ "github.com/hanzoai/sqlite3/embed"
)
db, _ := sqlite3.Open("file:/orgs/acme/app.db?vfs=hanzo")
```
**Deprecates** `modernc.org/sqlite` (no custom-VFS hook) and direct use of
`ncruces/go-sqlite3`. Benchmarked 3.6× faster writes / ~92× faster point-reads
than the FUSE-mounted path; per-page AEAD encryption costs ~0.
- [`modernc.org/sqlite`](https://pkg.go.dev/modernc.org/sqlite)
- [`crawshaw.io/sqlite`](https://pkg.go.dev/crawshaw.io/sqlite)
- [`github.com/mattn/go-sqlite3`](https://pkg.go.dev/github.com/mattn/go-sqlite3)
- [`github.com/zombiezen/go-sqlite`](https://pkg.go.dev/github.com/zombiezen/go-sqlite)
+22 -20
View File
@@ -5,8 +5,8 @@ package sqlite3
// https://sqlite.org/c3ref/backup.html
type Backup struct {
c *Conn
handle ptr_t
otherc ptr_t
handle uint32
otherc uint32
}
// Backup backs up srcDB on the src connection to the "main" database in dstURI.
@@ -61,29 +61,29 @@ func (src *Conn) BackupInit(srcDB, dstURI string) (*Backup, error) {
return src.backupInit(dst, "main", src.handle, srcDB)
}
func (c *Conn) backupInit(dst ptr_t, dstName string, src ptr_t, srcName string) (*Backup, error) {
defer c.arena.Mark()()
dstPtr := c.arena.String(dstName)
srcPtr := c.arena.String(srcName)
func (c *Conn) backupInit(dst uint32, dstName string, src uint32, srcName string) (*Backup, error) {
defer c.arena.mark()()
dstPtr := c.arena.string(dstName)
srcPtr := c.arena.string(srcName)
other := dst
if c.handle == dst {
other = src
}
ptr := ptr_t(c.wrp.Xsqlite3_backup_init(
int32(dst), int32(dstPtr),
int32(src), int32(srcPtr)))
if ptr == 0 {
r := c.call("sqlite3_backup_init",
uint64(dst), uint64(dstPtr),
uint64(src), uint64(srcPtr))
if r == 0 {
defer c.closeDB(other)
rc := res_t(c.wrp.Xsqlite3_errcode(int32(dst)))
return nil, c.errorFor(dst, rc)
r = c.call("sqlite3_errcode", uint64(dst))
return nil, c.sqlite.error(r, dst)
}
return &Backup{
c: c,
otherc: other,
handle: ptr,
handle: uint32(r),
}, nil
}
@@ -97,10 +97,10 @@ func (b *Backup) Close() error {
return nil
}
rc := res_t(b.c.wrp.Xsqlite3_backup_finish(int32(b.handle)))
r := b.c.call("sqlite3_backup_finish", uint64(b.handle))
b.c.closeDB(b.otherc)
b.handle = 0
return b.c.error(rc)
return b.c.error(r)
}
// Step copies up to nPage pages between the source and destination databases.
@@ -108,11 +108,11 @@ func (b *Backup) Close() error {
//
// https://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
func (b *Backup) Step(nPage int) (done bool, err error) {
rc := res_t(b.c.wrp.Xsqlite3_backup_step(int32(b.handle), int32(nPage)))
if rc == _DONE {
r := b.c.call("sqlite3_backup_step", uint64(b.handle), uint64(nPage))
if r == _DONE {
return true, nil
}
return false, b.c.error(rc)
return false, b.c.error(r)
}
// Remaining returns the number of pages still to be backed up
@@ -120,7 +120,8 @@ func (b *Backup) Step(nPage int) (done bool, err error) {
//
// https://sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining
func (b *Backup) Remaining() int {
return int(b.c.wrp.Xsqlite3_backup_remaining(int32(b.handle)))
r := b.c.call("sqlite3_backup_remaining", uint64(b.handle))
return int(int32(r))
}
// PageCount returns the total number of pages in the source database
@@ -128,5 +129,6 @@ func (b *Backup) Remaining() int {
//
// https://sqlite.org/c3ref/backup_finish.html#sqlite3backuppagecount
func (b *Backup) PageCount() int {
return int(b.c.wrp.Xsqlite3_backup_pagecount(int32(b.handle)))
r := b.c.call("sqlite3_backup_pagecount", uint64(b.handle))
return int(int32(r))
}
+44 -49
View File
@@ -3,7 +3,7 @@ package sqlite3
import (
"io"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3/internal/util"
)
// ZeroBlob represents a zero-filled, length n BLOB
@@ -20,8 +20,8 @@ type Blob struct {
c *Conn
bytes int64
offset int64
handle ptr_t
bufptr ptr_t
handle uint32
bufptr uint32
buflen int64
}
@@ -31,32 +31,29 @@ var _ io.ReadWriteSeeker = &Blob{}
//
// https://sqlite.org/c3ref/blob_open.html
func (c *Conn) OpenBlob(db, table, column string, row int64, write bool) (*Blob, error) {
if c.interrupt.Err() != nil {
return nil, INTERRUPT
}
defer c.arena.mark()()
blobPtr := c.arena.new(ptrlen)
dbPtr := c.arena.string(db)
tablePtr := c.arena.string(table)
columnPtr := c.arena.string(column)
defer c.arena.Mark()()
blobPtr := c.arena.New(ptrlen)
dbPtr := c.arena.String(db)
tablePtr := c.arena.String(table)
columnPtr := c.arena.String(column)
var flags int32
var flags uint64
if write {
flags = 1
}
rc := res_t(c.wrp.Xsqlite3_blob_open(int32(c.handle),
int32(dbPtr), int32(tablePtr), int32(columnPtr),
row, flags, int32(blobPtr)))
c.checkInterrupt(c.handle)
r := c.call("sqlite3_blob_open", uint64(c.handle),
uint64(dbPtr), uint64(tablePtr), uint64(columnPtr),
uint64(row), flags, uint64(blobPtr))
if err := c.error(rc); err != nil {
if err := c.error(r); err != nil {
return nil, err
}
blob := Blob{c: c}
blob.handle = ptr_t(c.wrp.Read32(blobPtr))
blob.bytes = int64(c.wrp.Xsqlite3_blob_bytes(int32(blob.handle)))
blob.handle = util.ReadUint32(c.mod, blobPtr)
blob.bytes = int64(c.call("sqlite3_blob_bytes", uint64(blob.handle)))
return &blob, nil
}
@@ -70,10 +67,10 @@ func (b *Blob) Close() error {
return nil
}
rc := res_t(b.c.wrp.Xsqlite3_blob_close(int32(b.handle)))
b.c.wrp.Free(b.bufptr)
r := b.c.call("sqlite3_blob_close", uint64(b.handle))
b.c.free(b.bufptr)
b.handle = 0
return b.c.error(rc)
return b.c.error(r)
}
// Size returns the size of the BLOB in bytes.
@@ -97,13 +94,13 @@ func (b *Blob) Read(p []byte) (n int, err error) {
want = avail
}
if want > b.buflen {
b.bufptr = b.c.wrp.Realloc(b.bufptr, want)
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
rc := res_t(b.c.wrp.Xsqlite3_blob_read(int32(b.handle),
int32(b.bufptr), int32(want), int32(b.offset)))
err = b.c.error(rc)
r := b.c.call("sqlite3_blob_read", uint64(b.handle),
uint64(b.bufptr), uint64(want), uint64(b.offset))
err = b.c.error(r)
if err != nil {
return 0, err
}
@@ -112,7 +109,7 @@ func (b *Blob) Read(p []byte) (n int, err error) {
err = io.EOF
}
copy(p, b.c.wrp.Bytes(b.bufptr, want))
copy(p, util.View(b.c.mod, b.bufptr, uint64(want)))
return int(want), err
}
@@ -130,19 +127,19 @@ func (b *Blob) WriteTo(w io.Writer) (n int64, err error) {
want = avail
}
if want > b.buflen {
b.bufptr = b.c.wrp.Realloc(b.bufptr, want)
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
for want > 0 {
rc := res_t(b.c.wrp.Xsqlite3_blob_read(int32(b.handle),
int32(b.bufptr), int32(want), int32(b.offset)))
err = b.c.error(rc)
r := b.c.call("sqlite3_blob_read", uint64(b.handle),
uint64(b.bufptr), uint64(want), uint64(b.offset))
err = b.c.error(r)
if err != nil {
return n, err
}
mem := b.c.wrp.Bytes(b.bufptr, want)
mem := util.View(b.c.mod, b.bufptr, uint64(want))
m, err := w.Write(mem[:want])
b.offset += int64(m)
n += int64(m)
@@ -168,14 +165,14 @@ func (b *Blob) WriteTo(w io.Writer) (n int64, err error) {
func (b *Blob) Write(p []byte) (n int, err error) {
want := int64(len(p))
if want > b.buflen {
b.bufptr = b.c.wrp.Realloc(b.bufptr, want)
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
b.c.wrp.WriteBytes(b.bufptr, p)
util.WriteBytes(b.c.mod, b.bufptr, p)
rc := res_t(b.c.wrp.Xsqlite3_blob_write(int32(b.handle),
int32(b.bufptr), int32(want), int32(b.offset)))
err = b.c.error(rc)
r := b.c.call("sqlite3_blob_write", uint64(b.handle),
uint64(b.bufptr), uint64(want), uint64(b.offset))
err = b.c.error(r)
if err != nil {
return 0, err
}
@@ -199,17 +196,17 @@ func (b *Blob) ReadFrom(r io.Reader) (n int64, err error) {
want = 1
}
if want > b.buflen {
b.bufptr = b.c.wrp.Realloc(b.bufptr, want)
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
b.buflen = want
}
for {
mem := b.c.wrp.Bytes(b.bufptr, want)
mem := util.View(b.c.mod, b.bufptr, uint64(want))
m, err := r.Read(mem[:want])
if m > 0 {
rc := res_t(b.c.wrp.Xsqlite3_blob_write(int32(b.handle),
int32(b.bufptr), int32(m), int32(b.offset)))
err := b.c.error(rc)
r := b.c.call("sqlite3_blob_write", uint64(b.handle),
uint64(b.bufptr), uint64(m), uint64(b.offset))
err := b.c.error(r)
if err != nil {
return n, err
}
@@ -237,7 +234,7 @@ func (b *Blob) ReadFrom(r io.Reader) (n int64, err error) {
func (b *Blob) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
return 0, errutil.WhenceErr
return 0, util.WhenceErr
case io.SeekStart:
break
case io.SeekCurrent:
@@ -246,7 +243,7 @@ func (b *Blob) Seek(offset int64, whence int) (int64, error) {
offset += b.bytes
}
if offset < 0 {
return 0, errutil.OffsetErr
return 0, util.OffsetErr
}
b.offset = offset
return offset, nil
@@ -256,11 +253,9 @@ func (b *Blob) Seek(offset int64, whence int) (int64, error) {
//
// https://sqlite.org/c3ref/blob_reopen.html
func (b *Blob) Reopen(row int64) error {
if b.c.interrupt.Err() != nil {
return INTERRUPT
}
err := b.c.error(res_t(b.c.wrp.Xsqlite3_blob_reopen(int32(b.handle), row)))
b.bytes = int64(b.c.wrp.Xsqlite3_blob_bytes(int32(b.handle)))
b.c.checkInterrupt(b.c.handle)
err := b.c.error(b.c.call("sqlite3_blob_reopen", uint64(b.handle), uint64(row)))
b.bytes = int64(b.c.call("sqlite3_blob_bytes", uint64(b.handle)))
b.offset = 0
return err
}
-48
View File
@@ -1,48 +0,0 @@
// hanzovfs-bench: native PQ SQLite VFS throughput (in-process, no FUSE).
// go run github.com/hanzoai/sqlite3/cmd/hanzovfs-bench@latest
package main
import (
"fmt"
"os"
"runtime"
"time"
"github.com/hanzoai/sqlite3"
_ "github.com/hanzoai/sqlite3/embed"
"github.com/hanzoai/sqlite3/hanzovfs"
"github.com/luxfi/age"
)
func bench(label, dsn string) {
db, err := sqlite3.Open(dsn)
if err != nil { fmt.Printf("%-24s OPEN ERR %v\n", label, err); return }
defer db.Close()
db.Exec(`PRAGMA journal_mode=MEMORY; PRAGMA synchronous=OFF;`)
db.Exec(`CREATE TABLE t(id INTEGER PRIMARY KEY, email TEXT, body TEXT)`)
const n = 20000
t0 := time.Now()
db.Exec("BEGIN")
st, _, _ := db.Prepare("INSERT INTO t(email,body) VALUES(?,?)")
for i := 0; i < n; i++ {
st.BindText(1, fmt.Sprintf("u%d@hanzo.ai", i))
st.BindText(2, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
st.Step(); st.Reset()
}
st.Close(); db.Exec("COMMIT")
ins := float64(n) / time.Since(t0).Seconds()
t0 = time.Now()
sel, _, _ := db.Prepare("SELECT body FROM t WHERE id=?")
for i := 0; i < 5000; i++ { sel.BindInt(1, 1+(i*7)%n); sel.Step(); sel.Reset() }
sel.Close()
fmt.Printf("%-24s insert=%9.0f rows/s point_sel=%9.0f q/s\n", label, ins, 5000/time.Since(t0).Seconds())
}
func main() {
fmt.Printf("hanzovfs-bench %s/%s (%d CPU)\n", runtime.GOOS, runtime.GOARCH, runtime.NumCPU())
id, rcpt, err := hanzovfs.GenerateIdentity()
if err != nil { panic(err) }
dir, _ := os.MkdirTemp("", "hvfs")
hanzovfs.Register("pq", hanzovfs.Config{Dir: dir, Recipients: []age.Recipient{rcpt}, Identities: []age.Identity{id}})
bench("native PQ VFS (ML-KEM)", "file:/orgs/acme/bench.db?vfs=pq")
}
+123 -137
View File
@@ -1,12 +1,14 @@
package sqlite3
import (
"context"
"fmt"
"strconv"
"sync/atomic"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/vfs"
"github.com/tetratelabs/wazero/api"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/vfs"
)
// Config makes configuration changes to a database connection.
@@ -27,10 +29,10 @@ func (c *Conn) Config(op DBConfig, arg ...bool) (bool, error) {
// The vararg is a pointer to an array containing these arguments:
// an int and an int* pointing to that int.
defer c.arena.Mark()()
argsPtr := c.arena.New(intlen + ptrlen)
defer c.arena.mark()()
argsPtr := c.arena.new(intlen + ptrlen)
var flag int32
var flag int
switch {
case len(arg) == 0:
flag = -1
@@ -38,42 +40,33 @@ func (c *Conn) Config(op DBConfig, arg ...bool) (bool, error) {
flag = 1
}
c.wrp.Write32(argsPtr+0*ptrlen, uint32(flag))
c.wrp.Write32(argsPtr+1*ptrlen, uint32(argsPtr))
util.WriteUint32(c.mod, argsPtr+0*ptrlen, uint32(flag))
util.WriteUint32(c.mod, argsPtr+1*ptrlen, argsPtr)
rc := res_t(c.wrp.Xsqlite3_db_config(int32(c.handle),
int32(op), int32(argsPtr)))
return c.wrp.ReadBool(argsPtr), c.error(rc)
}
var defaultLogger atomic.Pointer[func(code ExtendedErrorCode, msg string)]
// ConfigLog sets up the default error logging callback for new connections.
//
// https://sqlite.org/errlog.html
func ConfigLog(cb func(code ExtendedErrorCode, msg string)) {
defaultLogger.Store(&cb)
r := c.call("sqlite3_db_config", uint64(c.handle),
uint64(op), uint64(argsPtr))
return util.ReadUint32(c.mod, argsPtr) != 0, c.error(r)
}
// ConfigLog sets up the error logging callback for the connection.
//
// https://sqlite.org/errlog.html
func (c *Conn) ConfigLog(cb func(code ExtendedErrorCode, msg string)) error {
var enable int32
var enable uint64
if cb != nil {
enable = 1
}
rc := res_t(c.wrp.Xsqlite3_config_log_go(enable))
if err := c.error(rc); err != nil {
r := c.call("sqlite3_config_log_go", enable)
if err := c.error(r); err != nil {
return err
}
c.log = cb
return nil
}
func (e *env) Xgo_log(_, iCode, zMsg int32) {
if c, ok := e.DB.(*Conn); ok && c.log != nil {
msg := e.ReadString(ptr_t(zMsg), _MAX_LENGTH)
func logCallback(ctx context.Context, mod api.Module, _, iCode, zMsg uint32) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.log != nil {
msg := util.ReadString(mod, zMsg, _MAX_LENGTH)
c.log(xErrorCode(iCode), msg)
}
}
@@ -92,100 +85,96 @@ func (c *Conn) Log(code ExtendedErrorCode, format string, a ...any) {
//
// https://sqlite.org/c3ref/file_control.html
func (c *Conn) FileControl(schema string, op FcntlOpcode, arg ...any) (any, error) {
defer c.arena.Mark()()
ptr := c.arena.New(max(ptrlen, intlen))
defer c.arena.mark()()
ptr := c.arena.new(max(ptrlen, intlen))
var schemaPtr ptr_t
var schemaPtr uint32
if schema != "" {
schemaPtr = c.arena.String(schema)
schemaPtr = c.arena.string(schema)
}
var rc res_t
var ret any
var rc uint64
var res any
switch op {
default:
return nil, MISUSE
case FCNTL_RESET_CACHE, FCNTL_NULL_IO:
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), 0))
case FCNTL_RESET_CACHE:
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), 0)
case FCNTL_PERSIST_WAL, FCNTL_POWERSAFE_OVERWRITE:
var flag int32
var flag int
switch {
case len(arg) == 0:
flag = -1
case arg[0]:
flag = 1
}
c.wrp.Write32(ptr, uint32(flag))
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), int32(ptr)))
ret = c.wrp.ReadBool(ptr)
util.WriteUint32(c.mod, ptr, uint32(flag))
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
res = util.ReadUint32(c.mod, ptr) != 0
case FCNTL_CHUNK_SIZE:
c.wrp.Write32(ptr, uint32(arg[0].(int)))
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), int32(ptr)))
util.WriteUint32(c.mod, ptr, uint32(arg[0].(int)))
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
case FCNTL_RESERVE_BYTES:
bytes := -1
if len(arg) > 0 {
bytes = arg[0].(int)
}
c.wrp.Write32(ptr, uint32(bytes))
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), int32(ptr)))
ret = int(int32(c.wrp.Read32(ptr)))
util.WriteUint32(c.mod, ptr, uint32(bytes))
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
res = int(util.ReadUint32(c.mod, ptr))
case FCNTL_DATA_VERSION:
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), int32(ptr)))
ret = c.wrp.Read32(ptr)
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
res = util.ReadUint32(c.mod, ptr)
case FCNTL_LOCKSTATE:
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), int32(ptr)))
ret = vfs.LockLevel(c.wrp.Read32(ptr))
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
res = vfs.LockLevel(util.ReadUint32(c.mod, ptr))
case FCNTL_VFSNAME, FCNTL_VFS_POINTER:
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(FCNTL_VFS_POINTER), int32(ptr)))
case FCNTL_VFS_POINTER:
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
if rc == _OK {
const zNameOffset = 16
ptr = ptr_t(c.wrp.Read32(ptr))
ptr = ptr_t(c.wrp.Read32(ptr + zNameOffset))
name := c.wrp.ReadString(ptr, _MAX_NAME)
if op == FCNTL_VFS_POINTER {
ret = vfs.Find(name)
} else {
ret = name
}
ptr = util.ReadUint32(c.mod, ptr)
ptr = util.ReadUint32(c.mod, ptr+zNameOffset)
name := util.ReadString(c.mod, ptr, _MAX_NAME)
res = vfs.Find(name)
}
case FCNTL_FILE_POINTER, FCNTL_JOURNAL_POINTER:
rc = res_t(c.wrp.Xsqlite3_file_control(
int32(c.handle), int32(schemaPtr),
int32(op), int32(ptr)))
rc = c.call("sqlite3_file_control",
uint64(c.handle), uint64(schemaPtr),
uint64(op), uint64(ptr))
if rc == _OK {
const fileHandleOffset = 4
ptr = ptr_t(c.wrp.Read32(ptr))
ptr = ptr_t(c.wrp.Read32(ptr + fileHandleOffset))
ret = c.wrp.GetHandle(ptr)
ptr = util.ReadUint32(c.mod, ptr)
ptr = util.ReadUint32(c.mod, ptr+fileHandleOffset)
res = util.GetHandle(c.ctx, ptr)
}
}
if err := c.error(rc); err != nil {
return nil, err
}
return ret, nil
return res, nil
}
// Limit allows the size of various constructs to be
@@ -193,109 +182,107 @@ func (c *Conn) FileControl(schema string, op FcntlOpcode, arg ...any) (any, erro
//
// https://sqlite.org/c3ref/limit.html
func (c *Conn) Limit(id LimitCategory, value int) int {
return int(c.wrp.Xsqlite3_limit(int32(c.handle), int32(id), int32(value)))
r := c.call("sqlite3_limit", uint64(c.handle), uint64(id), uint64(value))
return int(int32(r))
}
// SetAuthorizer registers an authorizer callback with the database connection.
//
// https://sqlite.org/c3ref/set_authorizer.html
func (c *Conn) SetAuthorizer(cb func(action AuthorizerActionCode, name3rd, name4th, schema, inner string) AuthorizerReturnCode) error {
var enable int32
var enable uint64
if cb != nil {
enable = 1
}
rc := res_t(c.wrp.Xsqlite3_set_authorizer_go(int32(c.handle), enable))
if err := c.error(rc); err != nil {
r := c.call("sqlite3_set_authorizer_go", uint64(c.handle), enable)
if err := c.error(r); err != nil {
return err
}
c.authorizer = cb
return nil
}
func (e *env) Xgo_authorizer(pDB, action, zName3rd, zName4th, zSchema, zInner int32) (rc int32) {
if c, ok := e.DB.(*Conn); ok && c.handle == ptr_t(pDB) && c.authorizer != nil {
func authorizerCallback(ctx context.Context, mod api.Module, pDB uint32, action AuthorizerActionCode, zName3rd, zName4th, zSchema, zInner uint32) (rc AuthorizerReturnCode) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.authorizer != nil {
var name3rd, name4th, schema, inner string
if zName3rd != 0 {
name3rd = e.ReadString(ptr_t(zName3rd), _MAX_NAME)
name3rd = util.ReadString(mod, zName3rd, _MAX_NAME)
}
if zName4th != 0 {
name4th = e.ReadString(ptr_t(zName4th), _MAX_NAME)
name4th = util.ReadString(mod, zName4th, _MAX_NAME)
}
if zSchema != 0 {
schema = e.ReadString(ptr_t(zSchema), _MAX_NAME)
schema = util.ReadString(mod, zSchema, _MAX_NAME)
}
if zInner != 0 {
inner = e.ReadString(ptr_t(zInner), _MAX_NAME)
inner = util.ReadString(mod, zInner, _MAX_NAME)
}
return int32(c.authorizer(AuthorizerActionCode(action), name3rd, name4th, schema, inner))
rc = c.authorizer(action, name3rd, name4th, schema, inner)
}
return _OK
return rc
}
// Trace registers a trace callback function against the database connection.
//
// https://sqlite.org/c3ref/trace_v2.html
func (c *Conn) Trace(mask TraceEvent, cb func(evt TraceEvent, arg1, arg2 any) error) error {
rc := res_t(c.wrp.Xsqlite3_trace_go(int32(c.handle), int32(mask)))
if err := c.error(rc); err != nil {
func (c *Conn) Trace(mask TraceEvent, cb func(evt TraceEvent, arg1 any, arg2 any) error) error {
r := c.call("sqlite3_trace_go", uint64(c.handle), uint64(mask))
if err := c.error(r); err != nil {
return err
}
c.trace = cb
return nil
}
func (e *env) Xgo_trace(evt, pDB, pArg1, pArg2 int32) int32 {
if c, ok := e.DB.(*Conn); ok && c.handle == ptr_t(pDB) && c.trace != nil {
func traceCallback(ctx context.Context, mod api.Module, evt TraceEvent, pDB, pArg1, pArg2 uint32) (rc uint32) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.trace != nil {
var arg1, arg2 any
if TraceEvent(evt) == TRACE_CLOSE {
if evt == TRACE_CLOSE {
arg1 = c
} else {
for _, s := range c.stmts {
if ptr_t(pArg1) == s.handle {
if pArg1 == s.handle {
arg1 = s
switch TraceEvent(evt) {
switch evt {
case TRACE_STMT:
arg2 = s.SQL()
case TRACE_PROFILE:
arg2 = int64(e.Read64(ptr_t(pArg2)))
arg2 = int64(util.ReadUint64(mod, pArg2))
}
break
}
}
}
if arg1 != nil {
_ = c.trace(TraceEvent(evt), arg1, arg2)
_, rc = errorCode(c.trace(evt, arg1, arg2), ERROR)
}
}
return _OK
return rc
}
// WALCheckpoint checkpoints a WAL database.
//
// https://sqlite.org/c3ref/wal_checkpoint_v2.html
func (c *Conn) WALCheckpoint(schema string, mode CheckpointMode) (nLog, nCkpt int, err error) {
if c.interrupt.Err() != nil {
return 0, 0, INTERRUPT
}
defer c.arena.Mark()()
nLogPtr := c.arena.New(ptrlen)
nCkptPtr := c.arena.New(ptrlen)
schemaPtr := c.arena.String(schema)
rc := res_t(c.wrp.Xsqlite3_wal_checkpoint_v2(
int32(c.handle), int32(schemaPtr), int32(mode),
int32(nLogPtr), int32(nCkptPtr)))
nLog = int(int32(c.wrp.Read32(nLogPtr)))
nCkpt = int(int32(c.wrp.Read32(nCkptPtr)))
return nLog, nCkpt, c.error(rc)
defer c.arena.mark()()
nLogPtr := c.arena.new(ptrlen)
nCkptPtr := c.arena.new(ptrlen)
schemaPtr := c.arena.string(schema)
r := c.call("sqlite3_wal_checkpoint_v2",
uint64(c.handle), uint64(schemaPtr), uint64(mode),
uint64(nLogPtr), uint64(nCkptPtr))
nLog = int(int32(util.ReadUint32(c.mod, nLogPtr)))
nCkpt = int(int32(util.ReadUint32(c.mod, nCkptPtr)))
return nLog, nCkpt, c.error(r)
}
// WALAutoCheckpoint configures WAL auto-checkpoints.
//
// https://sqlite.org/c3ref/wal_autocheckpoint.html
func (c *Conn) WALAutoCheckpoint(pages int) error {
rc := res_t(c.wrp.Xsqlite3_wal_autocheckpoint(int32(c.handle), int32(pages)))
return c.error(rc)
r := c.call("sqlite3_wal_autocheckpoint", uint64(c.handle), uint64(pages))
return c.error(r)
}
// WALHook registers a callback function to be invoked
@@ -303,54 +290,53 @@ func (c *Conn) WALAutoCheckpoint(pages int) error {
//
// https://sqlite.org/c3ref/wal_hook.html
func (c *Conn) WALHook(cb func(db *Conn, schema string, pages int) error) {
var enable int32
var enable uint64
if cb != nil {
enable = 1
}
c.wrp.Xsqlite3_wal_hook_go(int32(c.handle), enable)
c.call("sqlite3_wal_hook_go", uint64(c.handle), enable)
c.wal = cb
}
func (e *env) Xgo_wal_hook(_, pDB, zSchema, pages int32) int32 {
if c, ok := e.DB.(*Conn); ok && c.handle == ptr_t(pDB) && c.wal != nil {
schema := e.ReadString(ptr_t(zSchema), _MAX_NAME)
func walCallback(ctx context.Context, mod api.Module, _, pDB, zSchema uint32, pages int32) (rc uint32) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.wal != nil {
schema := util.ReadString(mod, zSchema, _MAX_NAME)
err := c.wal(c, schema, int(pages))
_, rc := errorCode(err, ERROR)
return int32(rc)
_, rc = errorCode(err, ERROR)
}
return _OK
return rc
}
// AutoVacuumPages registers a autovacuum compaction amount callback.
//
// https://sqlite.org/c3ref/autovacuum_pages.html
func (c *Conn) AutoVacuumPages(cb func(schema string, dbPages, freePages, bytesPerPage uint) uint) error {
var funcPtr ptr_t
var funcPtr uint32
if cb != nil {
funcPtr = c.wrp.AddHandle(cb)
funcPtr = util.AddHandle(c.ctx, cb)
}
rc := res_t(c.wrp.Xsqlite3_autovacuum_pages_go(int32(c.handle), int32(funcPtr)))
return c.error(rc)
r := c.call("sqlite3_autovacuum_pages_go", uint64(c.handle), uint64(funcPtr))
return c.error(r)
}
func (e *env) Xgo_autovacuum_pages(pApp, zSchema, nDbPage, nFreePage, nBytePerPage int32) int32 {
fn := e.GetHandle(ptr_t(pApp)).(func(schema string, dbPages, freePages, bytesPerPage uint) uint)
schema := e.ReadString(ptr_t(zSchema), _MAX_NAME)
return int32(fn(schema, uint(uint32(nDbPage)), uint(uint32(nFreePage)), uint(uint32(nBytePerPage))))
func autoVacuumCallback(ctx context.Context, mod api.Module, pApp, zSchema, nDbPage, nFreePage, nBytePerPage uint32) uint32 {
fn := util.GetHandle(ctx, pApp).(func(schema string, dbPages, freePages, bytesPerPage uint) uint)
schema := util.ReadString(mod, zSchema, _MAX_NAME)
return uint32(fn(schema, uint(nDbPage), uint(nFreePage), uint(nBytePerPage)))
}
// SoftHeapLimit imposes a soft limit on heap size.
//
// https://sqlite.org/c3ref/hard_heap_limit64.html
func (c *Conn) SoftHeapLimit(n int64) int64 {
return c.wrp.Xsqlite3_soft_heap_limit64(n)
return int64(c.call("sqlite3_soft_heap_limit64", uint64(n)))
}
// HardHeapLimit imposes a hard limit on heap size.
//
// https://sqlite.org/c3ref/hard_heap_limit64.html
func (c *Conn) HardHeapLimit(n int64) int64 {
return c.wrp.Xsqlite3_hard_heap_limit64(n)
return int64(c.call("sqlite3_hard_heap_limit64", uint64(n)))
}
// EnableChecksums enables checksums on a database.
@@ -378,7 +364,7 @@ func (c *Conn) EnableChecksums(schema string) error {
}
if r != 8 {
// Invalid value.
return errutil.ErrorString("sqlite3: reserve bytes must be 8, is: " + strconv.Itoa(r.(int)))
return util.ErrorString("sqlite3: reserve bytes must be 8, is: " + strconv.Itoa(r.(int)))
}
// VACUUM the database.
@@ -392,6 +378,6 @@ func (c *Conn) EnableChecksums(schema string) error {
}
// Checkpoint the WAL.
_, _, err = c.WALCheckpoint(schema, CHECKPOINT_FULL)
_, _, err = c.WALCheckpoint(schema, CHECKPOINT_RESTART)
return err
}
+172 -195
View File
@@ -3,7 +3,6 @@ package sqlite3
import (
"context"
"fmt"
"iter"
"math"
"math/rand"
"net/url"
@@ -11,9 +10,10 @@ import (
"strings"
"time"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/internal/sqlite3_wrap"
"github.com/hanzoai/sqlite3/vfs"
"github.com/tetratelabs/wazero/api"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/vfs"
)
// Conn is a database connection handle.
@@ -21,9 +21,10 @@ import (
//
// https://sqlite.org/c3ref/sqlite3.html
type Conn struct {
wrp *sqlite3_wrap.Wrapper
*sqlite
interrupt context.Context
pending *Stmt
stmts []*Stmt
busy func(context.Context, int) bool
log func(xErrorCode, string)
@@ -34,13 +35,11 @@ type Conn struct {
update func(AuthorizerActionCode, string, string, int64)
commit func() bool
rollback func()
preupdate func(PreUpdateData)
arena arena
busy1st time.Time
busylst time.Time
arena sqlite3_wrap.Arena
handle ptr_t
gosched uint8
handle uint32
}
// Open calls [OpenFlags] with [OPEN_READWRITE], [OPEN_CREATE] and [OPEN_URI].
@@ -49,7 +48,7 @@ func Open(filename string) (*Conn, error) {
}
// OpenContext is like [Open] but includes a context,
// which is used to interrupt the process of opening the connection.
// which is used to interrupt the process of opening the connectiton.
func OpenContext(ctx context.Context, filename string) (*Conn, error) {
return newConn(ctx, filename, OPEN_READWRITE|OPEN_CREATE|OPEN_URI)
}
@@ -69,30 +68,30 @@ func OpenFlags(filename string, flags OpenFlag) (*Conn, error) {
return newConn(context.Background(), filename, flags)
}
func newConn(ctx context.Context, filename string, flags OpenFlag) (ret *Conn, _ error) {
type connKey struct{}
func newConn(ctx context.Context, filename string, flags OpenFlag) (res *Conn, _ error) {
err := ctx.Err()
if err != nil {
return nil, err
}
c := &Conn{interrupt: ctx}
c.wrp, err = createWrapper(ctx)
c.sqlite, err = instantiateSQLite()
if err != nil {
return nil, err
}
defer func() {
if ret == nil {
if res == nil {
c.Close()
c.sqlite.close()
} else {
c.interrupt = context.Background()
}
}()
c.wrp.DB = c
if logger := defaultLogger.Load(); logger != nil {
c.ConfigLog(*logger)
}
c.arena = c.wrp.NewArena()
c.ctx = context.WithValue(c.ctx, connKey{}, c)
c.arena = c.newArena(1024)
c.handle, err = c.openDB(filename, flags)
if err == nil {
err = initExtensions(c)
@@ -103,25 +102,25 @@ func newConn(ctx context.Context, filename string, flags OpenFlag) (ret *Conn, _
return c, nil
}
func (c *Conn) openDB(filename string, flags OpenFlag) (ptr_t, error) {
defer c.arena.Mark()()
connPtr := c.arena.New(ptrlen)
namePtr := c.arena.String(filename)
func (c *Conn) openDB(filename string, flags OpenFlag) (uint32, error) {
defer c.arena.mark()()
connPtr := c.arena.new(ptrlen)
namePtr := c.arena.string(filename)
flags |= OPEN_EXRESCODE
rc := res_t(c.wrp.Xsqlite3_open_v2(int32(namePtr), int32(connPtr), int32(flags), 0))
r := c.call("sqlite3_open_v2", uint64(namePtr), uint64(connPtr), uint64(flags), 0)
handle := ptr_t(c.wrp.Read32(connPtr))
if err := c.errorFor(handle, rc); err != nil {
handle := util.ReadUint32(c.mod, connPtr)
if err := c.sqlite.error(r, handle); err != nil {
c.closeDB(handle)
return 0, err
}
c.wrp.Xsqlite3_progress_handler_go(int32(handle), 1000)
c.call("sqlite3_progress_handler_go", uint64(handle), 100)
if flags|OPEN_URI != 0 && strings.HasPrefix(filename, "file:") {
var pragmas strings.Builder
if u, err := url.Parse(filename); err == nil {
query := u.Query()
if _, after, ok := strings.Cut(filename, "?"); ok {
query, _ := url.ParseQuery(after)
for _, p := range query["_pragma"] {
pragmas.WriteString(`PRAGMA `)
pragmas.WriteString(p)
@@ -129,9 +128,10 @@ func (c *Conn) openDB(filename string, flags OpenFlag) (ptr_t, error) {
}
}
if pragmas.Len() != 0 {
pragmaPtr := c.arena.String(pragmas.String())
rc := res_t(c.wrp.Xsqlite3_exec(int32(handle), int32(pragmaPtr), 0, 0, 0))
if err := c.errorFor(handle, rc, pragmas.String()); err != nil {
c.checkInterrupt(handle)
pragmaPtr := c.arena.string(pragmas.String())
r := c.call("sqlite3_exec", uint64(handle), uint64(pragmaPtr), 0, 0, 0)
if err := c.sqlite.error(r, handle, pragmas.String()); err != nil {
err = fmt.Errorf("sqlite3: invalid _pragma: %w", err)
c.closeDB(handle)
return 0, err
@@ -141,9 +141,9 @@ func (c *Conn) openDB(filename string, flags OpenFlag) (ptr_t, error) {
return handle, nil
}
func (c *Conn) closeDB(handle ptr_t) {
rc := res_t(c.wrp.Xsqlite3_close_v2(int32(handle)))
if err := c.errorFor(handle, rc); err != nil {
func (c *Conn) closeDB(handle uint32) {
r := c.call("sqlite3_close_v2", uint64(handle))
if err := c.sqlite.error(r, handle); err != nil {
panic(err)
}
}
@@ -162,13 +162,16 @@ func (c *Conn) Close() error {
return nil
}
rc := res_t(c.wrp.Xsqlite3_close(int32(c.handle)))
if err := c.error(rc); err != nil {
c.pending.Close()
c.pending = nil
r := c.call("sqlite3_close", uint64(c.handle))
if err := c.error(r); err != nil {
return err
}
c.handle = 0
return c.wrp.Close()
return c.close()
}
// Exec is a convenience function that allows an application to run
@@ -176,17 +179,12 @@ func (c *Conn) Close() error {
//
// https://sqlite.org/c3ref/exec.html
func (c *Conn) Exec(sql string) error {
if c.interrupt.Err() != nil {
return INTERRUPT
}
return c.exec(sql)
}
defer c.arena.mark()()
sqlPtr := c.arena.string(sql)
func (c *Conn) exec(sql string) error {
defer c.arena.Mark()()
textPtr := c.arena.String(sql)
rc := res_t(c.wrp.Xsqlite3_exec(int32(c.handle), int32(textPtr), 0, 0, 0))
return c.error(rc, sql)
c.checkInterrupt(c.handle)
r := c.call("sqlite3_exec", uint64(c.handle), uint64(sqlPtr), 0, 0, 0)
return c.error(r, sql)
}
// Prepare calls [Conn.PrepareFlags] with no flags.
@@ -204,26 +202,24 @@ func (c *Conn) PrepareFlags(sql string, flags PrepareFlag) (stmt *Stmt, tail str
if len(sql) > _MAX_SQL_LENGTH {
return nil, "", TOOBIG
}
if c.interrupt.Err() != nil {
return nil, "", INTERRUPT
}
defer c.arena.Mark()()
stmtPtr := c.arena.New(ptrlen)
tailPtr := c.arena.New(ptrlen)
textPtr := c.arena.String(sql)
defer c.arena.mark()()
stmtPtr := c.arena.new(ptrlen)
tailPtr := c.arena.new(ptrlen)
sqlPtr := c.arena.string(sql)
rc := res_t(c.wrp.Xsqlite3_prepare_v3(int32(c.handle),
int32(textPtr), int32(len(sql)+1), int32(flags),
int32(stmtPtr), int32(tailPtr)))
c.checkInterrupt(c.handle)
r := c.call("sqlite3_prepare_v3", uint64(c.handle),
uint64(sqlPtr), uint64(len(sql)+1), uint64(flags),
uint64(stmtPtr), uint64(tailPtr))
stmt = &Stmt{c: c, sql: sql}
stmt.handle = ptr_t(c.wrp.Read32(stmtPtr))
if sql := sql[ptr_t(c.wrp.Read32(tailPtr))-textPtr:]; sql != "" {
stmt = &Stmt{c: c}
stmt.handle = util.ReadUint32(c.mod, stmtPtr)
if sql := sql[util.ReadUint32(c.mod, tailPtr)-sqlPtr:]; sql != "" {
tail = sql
}
if err := c.error(rc, sql); err != nil {
if err := c.error(r, sql); err != nil {
return nil, "", err
}
if stmt.handle == 0 {
@@ -237,45 +233,47 @@ func (c *Conn) PrepareFlags(sql string, flags PrepareFlag) (stmt *Stmt, tail str
//
// https://sqlite.org/c3ref/db_name.html
func (c *Conn) DBName(n int) string {
ptr := ptr_t(c.wrp.Xsqlite3_db_name(int32(c.handle), int32(n)))
r := c.call("sqlite3_db_name", uint64(c.handle), uint64(n))
ptr := uint32(r)
if ptr == 0 {
return ""
}
return c.wrp.ReadString(ptr, _MAX_NAME)
return util.ReadString(c.mod, ptr, _MAX_NAME)
}
// Filename returns the filename for a database.
//
// https://sqlite.org/c3ref/db_filename.html
func (c *Conn) Filename(schema string) *vfs.Filename {
var ptr ptr_t
var ptr uint32
if schema != "" {
defer c.arena.Mark()()
ptr = c.arena.String(schema)
defer c.arena.mark()()
ptr = c.arena.string(schema)
}
ptr = ptr_t(c.wrp.Xsqlite3_db_filename(int32(c.handle), int32(ptr)))
return vfs.GetFilename(c.wrp, ptr, vfs.OPEN_MAIN_DB)
r := c.call("sqlite3_db_filename", uint64(c.handle), uint64(ptr))
return vfs.GetFilename(c.ctx, c.mod, uint32(r), vfs.OPEN_MAIN_DB)
}
// ReadOnly determines if a database is read-only.
//
// https://sqlite.org/c3ref/db_readonly.html
func (c *Conn) ReadOnly(schema string) (ro, ok bool) {
var ptr ptr_t
func (c *Conn) ReadOnly(schema string) (ro bool, ok bool) {
var ptr uint32
if schema != "" {
defer c.arena.Mark()()
ptr = c.arena.String(schema)
defer c.arena.mark()()
ptr = c.arena.string(schema)
}
b := c.wrp.Xsqlite3_db_readonly(int32(c.handle), int32(ptr))
return b > 0, b < 0
r := c.call("sqlite3_db_readonly", uint64(c.handle), uint64(ptr))
return int32(r) > 0, int32(r) < 0
}
// GetAutocommit tests the connection for auto-commit mode.
//
// https://sqlite.org/c3ref/get_autocommit.html
func (c *Conn) GetAutocommit() bool {
b := c.wrp.Xsqlite3_get_autocommit(int32(c.handle))
return b != 0
r := c.call("sqlite3_get_autocommit", uint64(c.handle))
return r != 0
}
// LastInsertRowID returns the rowid of the most recent successful INSERT
@@ -283,7 +281,8 @@ func (c *Conn) GetAutocommit() bool {
//
// https://sqlite.org/c3ref/last_insert_rowid.html
func (c *Conn) LastInsertRowID() int64 {
return c.wrp.Xsqlite3_last_insert_rowid(int32(c.handle))
r := c.call("sqlite3_last_insert_rowid", uint64(c.handle))
return int64(r)
}
// SetLastInsertRowID allows the application to set the value returned by
@@ -291,7 +290,7 @@ func (c *Conn) LastInsertRowID() int64 {
//
// https://sqlite.org/c3ref/set_last_insert_rowid.html
func (c *Conn) SetLastInsertRowID(id int64) {
c.wrp.Xsqlite3_set_last_insert_rowid(int32(c.handle), id)
c.call("sqlite3_set_last_insert_rowid", uint64(c.handle), uint64(id))
}
// Changes returns the number of rows modified, inserted or deleted
@@ -300,7 +299,8 @@ func (c *Conn) SetLastInsertRowID(id int64) {
//
// https://sqlite.org/c3ref/changes.html
func (c *Conn) Changes() int64 {
return c.wrp.Xsqlite3_changes64(int32(c.handle))
r := c.call("sqlite3_changes64", uint64(c.handle))
return int64(r)
}
// TotalChanges returns the number of rows modified, inserted or deleted
@@ -309,15 +309,16 @@ func (c *Conn) Changes() int64 {
//
// https://sqlite.org/c3ref/total_changes.html
func (c *Conn) TotalChanges() int64 {
return c.wrp.Xsqlite3_total_changes64(int32(c.handle))
r := c.call("sqlite3_total_changes64", uint64(c.handle))
return int64(r)
}
// ReleaseMemory frees memory used by a database connection.
//
// https://sqlite.org/c3ref/db_release_memory.html
func (c *Conn) ReleaseMemory() error {
rc := res_t(c.wrp.Xsqlite3_db_release_memory(int32(c.handle)))
return c.error(rc)
r := c.call("sqlite3_db_release_memory", uint64(c.handle))
return c.error(r)
}
// GetInterrupt gets the context set with [Conn.SetInterrupt].
@@ -340,17 +341,43 @@ func (c *Conn) GetInterrupt() context.Context {
//
// https://sqlite.org/c3ref/interrupt.html
func (c *Conn) SetInterrupt(ctx context.Context) (old context.Context) {
if ctx == nil {
panic("nil Context")
}
old = c.interrupt
c.interrupt = ctx
if ctx == old || ctx.Done() == old.Done() {
return old
}
// A busy SQL statement prevents SQLite from ignoring an interrupt
// that comes before any other statements are started.
if c.pending == nil {
defer c.arena.mark()()
stmtPtr := c.arena.new(ptrlen)
loopPtr := c.arena.string(`WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x FROM c) SELECT x FROM c`)
c.call("sqlite3_prepare_v3", uint64(c.handle), uint64(loopPtr), math.MaxUint64,
uint64(PREPARE_PERSISTENT), uint64(stmtPtr), 0)
c.pending = &Stmt{c: c}
c.pending.handle = util.ReadUint32(c.mod, stmtPtr)
}
if old.Done() != nil && ctx.Err() == nil {
c.pending.Reset()
}
if ctx.Done() != nil {
c.pending.Step()
}
return old
}
func (e *env) Xgo_progress_handler(_ int32) (interrupt int32) {
if c, ok := e.DB.(*Conn); ok {
if c.gosched++; c.gosched%16 == 0 {
func (c *Conn) checkInterrupt(handle uint32) {
if c.interrupt.Err() != nil {
c.call("sqlite3_interrupt", uint64(handle))
}
}
func progressCallback(ctx context.Context, mod api.Module, _ uint32) (interrupt uint32) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok {
if c.interrupt.Done() != nil {
runtime.Gosched()
}
if c.interrupt.Err() != nil {
@@ -365,13 +392,13 @@ func (e *env) Xgo_progress_handler(_ int32) (interrupt int32) {
// https://sqlite.org/c3ref/busy_timeout.html
func (c *Conn) BusyTimeout(timeout time.Duration) error {
ms := min((timeout+time.Millisecond-1)/time.Millisecond, math.MaxInt32)
rc := res_t(c.wrp.Xsqlite3_busy_timeout(int32(c.handle), int32(ms)))
return c.error(rc)
r := c.call("sqlite3_busy_timeout", uint64(c.handle), uint64(ms))
return c.error(r)
}
func (e *env) Xgo_busy_timeout(count, tmout int32) (retry int32) {
func timeoutCallback(ctx context.Context, mod api.Module, count, tmout int32) (retry uint32) {
// https://fractaledmind.github.io/2024/04/15/sqlite-on-rails-the-how-and-why-of-optimal-performance/
if c, ok := e.DB.(*Conn); ok && c.interrupt.Err() == nil {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.interrupt.Err() == nil {
switch {
case count == 0:
c.busy1st = time.Now()
@@ -392,22 +419,25 @@ func (e *env) Xgo_busy_timeout(count, tmout int32) (retry int32) {
//
// https://sqlite.org/c3ref/busy_handler.html
func (c *Conn) BusyHandler(cb func(ctx context.Context, count int) (retry bool)) error {
var enable int32
var enable uint64
if cb != nil {
enable = 1
}
rc := res_t(c.wrp.Xsqlite3_busy_handler_go(int32(c.handle), enable))
if err := c.error(rc); err != nil {
r := c.call("sqlite3_busy_handler_go", uint64(c.handle), enable)
if err := c.error(r); err != nil {
return err
}
c.busy = cb
return nil
}
func (e *env) Xgo_busy_handler(pDB, count int32) (retry int32) {
if c, ok := e.DB.(*Conn); ok && c.handle == ptr_t(pDB) && c.busy != nil {
if interrupt := c.interrupt; interrupt.Err() == nil &&
c.busy(interrupt, int(count)) {
func busyCallback(ctx context.Context, mod api.Module, pDB uint32, count int32) (retry uint32) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.busy != nil {
interrupt := c.interrupt
if interrupt == nil {
interrupt = context.Background()
}
if interrupt.Err() == nil && c.busy(interrupt, int(count)) {
retry = 1
}
}
@@ -417,21 +447,21 @@ func (e *env) Xgo_busy_handler(pDB, count int32) (retry int32) {
// Status retrieves runtime status information about a database connection.
//
// https://sqlite.org/c3ref/db_status.html
func (c *Conn) Status(op DBStatus, reset bool) (current, highwater int64, err error) {
defer c.arena.Mark()()
hiPtr := c.arena.New(8)
curPtr := c.arena.New(8)
func (c *Conn) Status(op DBStatus, reset bool) (current, highwater int, err error) {
defer c.arena.mark()()
hiPtr := c.arena.new(intlen)
curPtr := c.arena.new(intlen)
var i int32
var i uint64
if reset {
i = 1
}
rc := res_t(c.wrp.Xsqlite3_db_status64(int32(c.handle),
int32(op), int32(curPtr), int32(hiPtr), i))
if err = c.error(rc); err == nil {
current = int64(c.wrp.Read64(curPtr))
highwater = int64(c.wrp.Read64(hiPtr))
r := c.call("sqlite3_db_status", uint64(c.handle),
uint64(op), uint64(curPtr), uint64(hiPtr), i)
if err = c.error(r); err == nil {
current = int(util.ReadUint32(c.mod, curPtr))
highwater = int(util.ReadUint32(c.mod, hiPtr))
}
return
}
@@ -440,101 +470,48 @@ func (c *Conn) Status(op DBStatus, reset bool) (current, highwater int64, err er
//
// https://sqlite.org/c3ref/table_column_metadata.html
func (c *Conn) TableColumnMetadata(schema, table, column string) (declType, collSeq string, notNull, primaryKey, autoInc bool, err error) {
defer c.arena.Mark()()
var (
declTypePtr ptr_t
collSeqPtr ptr_t
notNullPtr ptr_t
primaryKeyPtr ptr_t
autoIncPtr ptr_t
columnPtr ptr_t
schemaPtr ptr_t
)
if column != "" {
declTypePtr = c.arena.New(ptrlen)
collSeqPtr = c.arena.New(ptrlen)
notNullPtr = c.arena.New(ptrlen)
primaryKeyPtr = c.arena.New(ptrlen)
autoIncPtr = c.arena.New(ptrlen)
columnPtr = c.arena.String(column)
}
if schema != "" {
schemaPtr = c.arena.String(schema)
}
tablePtr := c.arena.String(table)
defer c.arena.mark()()
rc := res_t(c.wrp.Xsqlite3_table_column_metadata(int32(c.handle),
int32(schemaPtr), int32(tablePtr), int32(columnPtr),
int32(declTypePtr), int32(collSeqPtr),
int32(notNullPtr), int32(primaryKeyPtr), int32(autoIncPtr)))
if err = c.error(rc); err == nil && column != "" {
if ptr := ptr_t(c.wrp.Read32(declTypePtr)); ptr != 0 {
declType = c.wrp.ReadString(ptr, _MAX_NAME)
var schemaPtr, columnPtr uint32
declTypePtr := c.arena.new(ptrlen)
collSeqPtr := c.arena.new(ptrlen)
notNullPtr := c.arena.new(ptrlen)
autoIncPtr := c.arena.new(ptrlen)
primaryKeyPtr := c.arena.new(ptrlen)
if schema != "" {
schemaPtr = c.arena.string(schema)
}
tablePtr := c.arena.string(table)
if column != "" {
columnPtr = c.arena.string(column)
}
r := c.call("sqlite3_table_column_metadata", uint64(c.handle),
uint64(schemaPtr), uint64(tablePtr), uint64(columnPtr),
uint64(declTypePtr), uint64(collSeqPtr),
uint64(notNullPtr), uint64(primaryKeyPtr), uint64(autoIncPtr))
if err = c.error(r); err == nil && column != "" {
if ptr := util.ReadUint32(c.mod, declTypePtr); ptr != 0 {
declType = util.ReadString(c.mod, ptr, _MAX_NAME)
}
if ptr := ptr_t(c.wrp.Read32(collSeqPtr)); ptr != 0 {
collSeq = c.wrp.ReadString(ptr, _MAX_NAME)
if ptr := util.ReadUint32(c.mod, collSeqPtr); ptr != 0 {
collSeq = util.ReadString(c.mod, ptr, _MAX_NAME)
}
notNull = c.wrp.ReadBool(notNullPtr)
autoInc = c.wrp.ReadBool(autoIncPtr)
primaryKey = c.wrp.ReadBool(primaryKeyPtr)
notNull = util.ReadUint32(c.mod, notNullPtr) != 0
autoInc = util.ReadUint32(c.mod, autoIncPtr) != 0
primaryKey = util.ReadUint32(c.mod, primaryKeyPtr) != 0
}
return
}
func (c *Conn) error(rc res_t, sql ...string) error {
return c.errorFor(c.handle, rc, sql...)
func (c *Conn) error(rc uint64, sql ...string) error {
return c.sqlite.error(rc, c.handle, sql...)
}
func (c *Conn) errorFor(handle ptr_t, rc res_t, sql ...string) error {
if rc == _OK {
return nil
}
if ErrorCode(rc) == NOMEM || xErrorCode(rc) == IOERR_NOMEM {
panic(errutil.OOMErr)
}
var msg, query string
if handle != 0 {
if ptr := ptr_t(c.wrp.Xsqlite3_errmsg(int32(handle))); ptr != 0 {
msg = c.wrp.ReadString(ptr, _MAX_LENGTH)
msg = strings.TrimPrefix(msg, "sqlite3: ")
msg = strings.TrimPrefix(msg, sqlite3_wrap.ErrorCodeString(rc)[len("sqlite3: "):])
msg = strings.TrimPrefix(msg, ": ")
if msg == "" || msg == "not an error" {
msg = ""
}
}
if len(sql) != 0 {
if i := c.wrp.Xsqlite3_error_offset(int32(handle)); i != -1 {
query = sql[0][i:]
}
}
}
var sys error
switch ErrorCode(rc) {
case CANTOPEN, IOERR:
sys = c.wrp.SysError
}
if sys != nil || msg != "" || query != "" {
return &Error{code: rc, sys: sys, msg: msg, sql: query}
}
return xErrorCode(rc)
}
// Stmts returns an iterator for the prepared statements
// associated with the database connection.
//
// https://sqlite.org/c3ref/next_stmt.html
func (c *Conn) Stmts() iter.Seq[*Stmt] {
return func(yield func(*Stmt) bool) {
for _, s := range c.stmts {
if !yield(s) {
break
}
func (c *Conn) stmtsIter(yield func(*Stmt) bool) {
for _, s := range c.stmts {
if !yield(s) {
break
}
}
}
+11
View File
@@ -0,0 +1,11 @@
//go:build go1.23
package sqlite3
import "iter"
// Stmts returns an iterator for the prepared statements
// associated with the database connection.
//
// https://sqlite.org/c3ref/next_stmt.html
func (c *Conn) Stmts() iter.Seq[*Stmt] { return c.stmtsIter }
+9
View File
@@ -0,0 +1,9 @@
//go:build !go1.23
package sqlite3
// Stmts returns an iterator for the prepared statements
// associated with the database connection.
//
// https://sqlite.org/c3ref/next_stmt.html
func (c *Conn) Stmts() func(func(*Stmt) bool) { return c.stmtsIter }
+20 -45
View File
@@ -1,27 +1,19 @@
package sqlite3
import (
"strconv"
"github.com/hanzoai/sqlite3/internal/sqlite3_wrap"
)
import "strconv"
const (
_OK = 0 /* Successful result */
_ROW = 100 /* sqlite3_step() has another row ready */
_DONE = 101 /* sqlite3_step() has finished executing */
_MAX_NAME = 1e6 // Self-imposed limit for most NUL terminated strings.
_MAX_LENGTH = 1e9
_MAX_SQL_LENGTH = 1e9
_MAX_NAME = 1e6 // Self-imposed limit for most NUL terminated strings.
_MAX_LENGTH = 1e9
_MAX_SQL_LENGTH = 1e9
_MAX_FUNCTION_ARG = 100
ptrlen = sqlite3_wrap.PtrLen
intlen = sqlite3_wrap.IntLen
)
type (
ptr_t = sqlite3_wrap.Ptr_t
res_t = sqlite3_wrap.Res_t
ptrlen = 4
intlen = 4
)
// ErrorCode is a result code that [Error.Code] might return.
@@ -72,9 +64,6 @@ const (
ERROR_MISSING_COLLSEQ ExtendedErrorCode = xErrorCode(ERROR) | (1 << 8)
ERROR_RETRY ExtendedErrorCode = xErrorCode(ERROR) | (2 << 8)
ERROR_SNAPSHOT ExtendedErrorCode = xErrorCode(ERROR) | (3 << 8)
ERROR_RESERVESIZE ExtendedErrorCode = xErrorCode(ERROR) | (4 << 8)
ERROR_KEY ExtendedErrorCode = xErrorCode(ERROR) | (5 << 8)
ERROR_UNABLE ExtendedErrorCode = xErrorCode(ERROR) | (6 << 8)
IOERR_READ ExtendedErrorCode = xErrorCode(IOERR) | (1 << 8)
IOERR_SHORT_READ ExtendedErrorCode = xErrorCode(IOERR) | (2 << 8)
IOERR_WRITE ExtendedErrorCode = xErrorCode(IOERR) | (3 << 8)
@@ -109,8 +98,6 @@ const (
IOERR_DATA ExtendedErrorCode = xErrorCode(IOERR) | (32 << 8)
IOERR_CORRUPTFS ExtendedErrorCode = xErrorCode(IOERR) | (33 << 8)
IOERR_IN_PAGE ExtendedErrorCode = xErrorCode(IOERR) | (34 << 8)
IOERR_BADKEY ExtendedErrorCode = xErrorCode(IOERR) | (35 << 8)
IOERR_CODEC ExtendedErrorCode = xErrorCode(IOERR) | (36 << 8)
LOCKED_SHAREDCACHE ExtendedErrorCode = xErrorCode(LOCKED) | (1 << 8)
LOCKED_VTAB ExtendedErrorCode = xErrorCode(LOCKED) | (2 << 8)
BUSY_RECOVERY ExtendedErrorCode = xErrorCode(BUSY) | (1 << 8)
@@ -172,15 +159,13 @@ const (
// PrepareFlag is a flag that can be passed to [Conn.PrepareFlags].
//
// https://sqlite.org/c3ref/c_prepare_dont_log.html
// https://sqlite.org/c3ref/c_prepare_normalize.html
type PrepareFlag uint32
const (
PREPARE_PERSISTENT PrepareFlag = 0x01
PREPARE_NORMALIZE PrepareFlag = 0x02
PREPARE_NO_VTAB PrepareFlag = 0x04
PREPARE_DONT_LOG PrepareFlag = 0x10
PREPARE_FROM_DDL PrepareFlag = 0x20
)
// FunctionFlag is a flag that can be passed to
@@ -190,12 +175,12 @@ const (
type FunctionFlag uint32
const (
DETERMINISTIC FunctionFlag = 0x000000800
DIRECTONLY FunctionFlag = 0x000080000
SUBTYPE FunctionFlag = 0x000100000
INNOCUOUS FunctionFlag = 0x000200000
RESULT_SUBTYPE FunctionFlag = 0x001000000
SELFORDER1 FunctionFlag = 0x002000000
DETERMINISTIC FunctionFlag = 0x000000800
DIRECTONLY FunctionFlag = 0x000080000
INNOCUOUS FunctionFlag = 0x000200000
SELFORDER1 FunctionFlag = 0x002000000
// SUBTYPE FunctionFlag = 0x000100000
// RESULT_SUBTYPE FunctionFlag = 0x001000000
)
// StmtStatus name counter values associated with the [Stmt.Status] method.
@@ -234,8 +219,6 @@ const (
DBSTATUS_DEFERRED_FKS DBStatus = 10
DBSTATUS_CACHE_USED_SHARED DBStatus = 11
DBSTATUS_CACHE_SPILL DBStatus = 12
DBSTATUS_TEMPBUF_SPILL DBStatus = 13
// DBSTATUS_MAX DBStatus = 13
)
// DBConfig are the available database connection configuration options.
@@ -264,11 +247,7 @@ const (
DBCONFIG_TRUSTED_SCHEMA DBConfig = 1017
DBCONFIG_STMT_SCANSTATUS DBConfig = 1018
DBCONFIG_REVERSE_SCANORDER DBConfig = 1019
DBCONFIG_ENABLE_ATTACH_CREATE DBConfig = 1020
DBCONFIG_ENABLE_ATTACH_WRITE DBConfig = 1021
DBCONFIG_ENABLE_COMMENTS DBConfig = 1022
DBCONFIG_FP_DIGITS DBConfig = 1023
// DBCONFIG_MAX DBConfig = 1023
// DBCONFIG_MAX DBConfig = 1019
)
// FcntlOpcode are the available opcodes for [Conn.FileControl].
@@ -281,14 +260,12 @@ const (
FCNTL_CHUNK_SIZE FcntlOpcode = 6
FCNTL_FILE_POINTER FcntlOpcode = 7
FCNTL_PERSIST_WAL FcntlOpcode = 10
FCNTL_VFSNAME FcntlOpcode = 12
FCNTL_POWERSAFE_OVERWRITE FcntlOpcode = 13
FCNTL_VFS_POINTER FcntlOpcode = 27
FCNTL_JOURNAL_POINTER FcntlOpcode = 28
FCNTL_DATA_VERSION FcntlOpcode = 35
FCNTL_RESERVE_BYTES FcntlOpcode = 38
FCNTL_RESET_CACHE FcntlOpcode = 42
FCNTL_NULL_IO FcntlOpcode = 43
)
// LimitCategory are the available run-time limit categories.
@@ -309,7 +286,6 @@ const (
LIMIT_VARIABLE_NUMBER LimitCategory = 9
LIMIT_TRIGGER_DEPTH LimitCategory = 10
LIMIT_WORKER_THREADS LimitCategory = 11
LIMIT_PARSER_DEPTH LimitCategory = 12
)
// AuthorizerActionCode are the integer action codes
@@ -371,14 +347,13 @@ const (
// CheckpointMode are all the checkpoint mode values.
//
// https://sqlite.org/c3ref/c_checkpoint_full.html
type CheckpointMode int32
type CheckpointMode uint32
const (
CHECKPOINT_NOOP CheckpointMode = -1 /* Do no work at all */
CHECKPOINT_PASSIVE CheckpointMode = 0 /* Do as much as possible w/o blocking */
CHECKPOINT_FULL CheckpointMode = 1 /* Wait for writers, then checkpoint */
CHECKPOINT_RESTART CheckpointMode = 2 /* Like FULL but wait for readers */
CHECKPOINT_TRUNCATE CheckpointMode = 3 /* Like RESTART but also truncate WAL */
CHECKPOINT_PASSIVE CheckpointMode = 0 /* Do as much as possible w/o blocking */
CHECKPOINT_FULL CheckpointMode = 1 /* Wait for writers, then checkpoint */
CHECKPOINT_RESTART CheckpointMode = 2 /* Like FULL but wait for readers */
CHECKPOINT_TRUNCATE CheckpointMode = 3 /* Like RESTART but also truncate WAL */
)
// TxnState are the allowed return values from [Conn.TxnState].
+64 -58
View File
@@ -1,10 +1,12 @@
package sqlite3
import (
"encoding/json"
"errors"
"math"
"time"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Context is the context in which an SQL function executes.
@@ -13,7 +15,7 @@ import (
// https://sqlite.org/c3ref/context.html
type Context struct {
c *Conn
handle ptr_t
handle uint32
}
// Conn returns the database connection of the
@@ -29,16 +31,16 @@ func (ctx Context) Conn() *Conn {
//
// https://sqlite.org/c3ref/get_auxdata.html
func (ctx Context) SetAuxData(n int, data any) {
ptr := ctx.c.wrp.AddHandle(data)
ctx.c.wrp.Xsqlite3_set_auxdata_go(int32(ctx.handle), int32(n), int32(ptr))
ptr := util.AddHandle(ctx.c.ctx, data)
ctx.c.call("sqlite3_set_auxdata_go", uint64(ctx.handle), uint64(n), uint64(ptr))
}
// GetAuxData returns metadata for argument n of the function.
//
// https://sqlite.org/c3ref/get_auxdata.html
func (ctx Context) GetAuxData(n int) any {
ptr := ptr_t(ctx.c.wrp.Xsqlite3_get_auxdata(int32(ctx.handle), int32(n)))
return ctx.c.wrp.GetHandle(ptr)
ptr := uint32(ctx.c.call("sqlite3_get_auxdata", uint64(ctx.handle), uint64(n)))
return util.GetHandle(ctx.c.ctx, ptr)
}
// ResultBool sets the result of the function to a bool.
@@ -65,61 +67,61 @@ func (ctx Context) ResultInt(value int) {
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultInt64(value int64) {
ctx.c.wrp.Xsqlite3_result_int64(
int32(ctx.handle), value)
ctx.c.call("sqlite3_result_int64",
uint64(ctx.handle), uint64(value))
}
// ResultFloat sets the result of the function to a float64.
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultFloat(value float64) {
ctx.c.wrp.Xsqlite3_result_double(
int32(ctx.handle), value)
ctx.c.call("sqlite3_result_double",
uint64(ctx.handle), math.Float64bits(value))
}
// ResultText sets the result of the function to a string.
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultText(value string) {
ptr := ctx.c.wrp.NewString(value)
ctx.c.wrp.Xsqlite3_result_text_go(
int32(ctx.handle), int32(ptr), int64(len(value)))
ptr := ctx.c.newString(value)
ctx.c.call("sqlite3_result_text_go",
uint64(ctx.handle), uint64(ptr), uint64(len(value)))
}
// ResultRawText sets the text result of the function to a []byte.
// Returning a nil slice is the same as calling [Context.ResultNull].
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultRawText(value []byte) {
ctx.ResultText(string(value)) // does not escape
ptr := ctx.c.newBytes(value)
ctx.c.call("sqlite3_result_text_go",
uint64(ctx.handle), uint64(ptr), uint64(len(value)))
}
// ResultBlob sets the result of the function to a []byte.
// Returning a nil slice is the same as calling [Context.ResultNull].
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultBlob(value []byte) {
if len(value) == 0 {
ctx.ResultZeroBlob(0)
return
}
ptr := ctx.c.wrp.NewBytes(value)
ctx.c.wrp.Xsqlite3_result_blob_go(
int32(ctx.handle), int32(ptr), int64(len(value)))
ptr := ctx.c.newBytes(value)
ctx.c.call("sqlite3_result_blob_go",
uint64(ctx.handle), uint64(ptr), uint64(len(value)))
}
// ResultZeroBlob sets the result of the function to a zero-filled, length n BLOB.
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultZeroBlob(n int64) {
ctx.c.wrp.Xsqlite3_result_zeroblob64(
int32(ctx.handle), n)
ctx.c.call("sqlite3_result_zeroblob64",
uint64(ctx.handle), uint64(n))
}
// ResultNull sets the result of the function to NULL.
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultNull() {
ctx.c.wrp.Xsqlite3_result_null(
int32(ctx.handle))
ctx.c.call("sqlite3_result_null",
uint64(ctx.handle))
}
// ResultTime sets the result of the function to a [time.Time].
@@ -139,19 +141,19 @@ func (ctx Context) ResultTime(value time.Time, format TimeFormat) {
case float64:
ctx.ResultFloat(v)
default:
panic(errutil.AssertErr())
panic(util.AssertErr())
}
}
func (ctx Context) resultRFC3339Nano(value time.Time) {
const maxlen = 48
ptr := ctx.c.wrp.New(maxlen)
buf := ctx.c.wrp.Bytes(ptr, maxlen)
buf = value.AppendFormat(buf[:0], time.RFC3339Nano)
_ = append(buf, 0)
const maxlen = uint64(len(time.RFC3339Nano)) + 5
ctx.c.wrp.Xsqlite3_result_text_go(
int32(ctx.handle), int32(ptr), int64(len(buf)))
ptr := ctx.c.new(maxlen)
buf := util.View(ctx.c.mod, ptr, maxlen)
buf = value.AppendFormat(buf[:0], time.RFC3339Nano)
ctx.c.call("sqlite3_result_text_go",
uint64(ctx.handle), uint64(ptr), uint64(len(buf)))
}
// ResultPointer sets the result of the function to NULL, just like [Context.ResultNull],
@@ -160,9 +162,21 @@ func (ctx Context) resultRFC3339Nano(value time.Time) {
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultPointer(ptr any) {
valPtr := ctx.c.wrp.AddHandle(ptr)
ctx.c.wrp.Xsqlite3_result_pointer_go(
int32(ctx.handle), int32(valPtr))
valPtr := util.AddHandle(ctx.c.ctx, ptr)
ctx.c.call("sqlite3_result_pointer_go",
uint64(ctx.handle), uint64(valPtr))
}
// ResultJSON sets the result of the function to the JSON encoding of value.
//
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultJSON(value any) {
data, err := json.Marshal(value)
if err != nil {
ctx.ResultError(err)
return // notest
}
ctx.ResultRawText(data)
}
// ResultValue sets the result of the function to a copy of [Value].
@@ -173,8 +187,8 @@ func (ctx Context) ResultValue(value Value) {
ctx.ResultError(MISUSE)
return
}
ctx.c.wrp.Xsqlite3_result_value(
int32(ctx.handle), int32(value.handle))
ctx.c.call("sqlite3_result_value",
uint64(ctx.handle), uint64(value.handle))
}
// ResultError sets the result of the function an error.
@@ -182,41 +196,33 @@ func (ctx Context) ResultValue(value Value) {
// https://sqlite.org/c3ref/result_blob.html
func (ctx Context) ResultError(err error) {
if errors.Is(err, NOMEM) {
ctx.c.wrp.Xsqlite3_result_error_nomem(int32(ctx.handle))
ctx.c.call("sqlite3_result_error_nomem", uint64(ctx.handle))
return
}
if errors.Is(err, TOOBIG) {
ctx.c.wrp.Xsqlite3_result_error_toobig(int32(ctx.handle))
ctx.c.call("sqlite3_result_error_toobig", uint64(ctx.handle))
return
}
msg, code := errorCode(err, ERROR)
msg, code := errorCode(err, _OK)
if msg != "" {
defer ctx.c.arena.Mark()()
ptr := ctx.c.arena.String(msg)
ctx.c.wrp.Xsqlite3_result_error(
int32(ctx.handle), int32(ptr), int32(len(msg)))
defer ctx.c.arena.mark()()
ptr := ctx.c.arena.string(msg)
ctx.c.call("sqlite3_result_error",
uint64(ctx.handle), uint64(ptr), uint64(len(msg)))
}
if code != res_t(ERROR) {
ctx.c.wrp.Xsqlite3_result_error_code(
int32(ctx.handle), int32(code))
if code != _OK {
ctx.c.call("sqlite3_result_error_code",
uint64(ctx.handle), uint64(code))
}
}
// ResultSubtype sets the subtype of the result of the function.
//
// https://sqlite.org/c3ref/result_subtype.html
func (ctx Context) ResultSubtype(t uint) {
ctx.c.wrp.Xsqlite3_result_subtype(
int32(ctx.handle), int32(t))
}
// VTabNoChange may return true if a column is being fetched as part
// of an update during which the column value will not change.
//
// https://sqlite.org/c3ref/vtab_nochange.html
func (ctx Context) VTabNoChange() bool {
b := ctx.c.wrp.Xsqlite3_vtab_nochange(int32(ctx.handle))
return b != 0
r := ctx.c.call("sqlite3_vtab_nochange", uint64(ctx.handle))
return r != 0
}
+157 -221
View File
@@ -1,8 +1,10 @@
// Package driver provides a database/sql driver for SQLite.
//
// Importing package driver registers a [database/sql] driver named "sqlite3".
// You may also need to import package embed.
//
// import _ "github.com/hanzoai/sqlite3/driver"
// import _ "github.com/ncruces/go-sqlite3/driver"
// import _ "github.com/ncruces/go-sqlite3/embed"
//
// The data source name for "sqlite3" databases can be a filename or a "file:" [URI].
//
@@ -18,45 +20,22 @@
// - a [serializable] transaction is always "immediate";
// - a [read-only] transaction is always "deferred".
//
// # Datatypes In SQLite
//
// SQLite is dynamically typed.
// Columns can mostly hold any value regardless of their declared type.
// SQLite supports most [driver.Value] types out of the box,
// but bool and [time.Time] require special care.
//
// Booleans can be stored on any column type and scanned back to a *bool.
// However, if scanned to a *any, booleans may either become an
// int64, string or bool, depending on the declared type of the column.
// If you use BOOLEAN for your column type,
// 1 and 0 will always scan as true and false.
//
// # Working with time
//
// Time values can similarly be stored on any column type.
// The time encoding/decoding format can be specified using "_timefmt":
//
// sql.Open("sqlite3", "file:demo.db?_timefmt=sqlite")
//
// Special values are: "auto" (the default), "sqlite", "rfc3339";
// Possible values are: "auto" (the default), "sqlite", "rfc3339";
// - "auto" encodes as RFC 3339 and decodes any [format] supported by SQLite;
// - "sqlite" encodes as SQLite and decodes any [format] supported by SQLite;
// - "rfc3339" encodes and decodes RFC 3339 only.
//
// You can also set "_timefmt" to an arbitrary [sqlite3.TimeFormat] or [time.Layout].
//
// If you encode as RFC 3339 (the default),
// consider using the TIME [collating sequence] to produce time-ordered sequences.
// consider using the TIME [collating sequence] to produce a time-ordered sequence.
//
// If you encode as RFC 3339 (the default),
// time values will scan back to a *time.Time unless your column type is TEXT.
// Otherwise, if scanned to a *any, time values may either become an
// int64, float64 or string, depending on the time format and declared type of the column.
// If you use DATE, TIME, DATETIME, or TIMESTAMP for your column type,
// "_timefmt" will be used to decode values.
//
// To scan values in custom formats, [sqlite3.TimeFormat.Scanner] may be helpful.
// To bind values in custom formats, [sqlite3.TimeFormat.Encode] them before binding.
// To scan values in other formats, [sqlite3.TimeFormat.Scanner] may be helpful.
// To bind values in other formats, [sqlite3.TimeFormat.Encode] them before binding.
//
// When using a custom time struct, you'll have to implement
// [database/sql/driver.Valuer] and [database/sql.Scanner].
@@ -69,7 +48,7 @@
// The Scan method needs to take into account that the value it receives can be of differing types.
// It can already be a [time.Time], if the driver decoded the value according to "_timefmt" rules.
// Or it can be a: string, int64, float64, []byte, or nil,
// depending on the column type and whoever wrote the value.
// depending on the column type and what whoever wrote the value.
// [sqlite3.TimeFormat.Decode] may help.
//
// # Setting PRAGMAs
@@ -107,14 +86,13 @@ import (
"time"
"unsafe"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/internal/util"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
// This variable can be replaced with -ldflags:
//
// go build -ldflags="-X github.com/hanzoai/sqlite3/driver.driverName=sqlite"
// go build -ldflags="-X github.com/ncruces/go-sqlite3/driver.driverName=sqlite"
var driverName = "sqlite3"
func init() {
@@ -174,17 +152,15 @@ func newConnector(name string, init, term func(*sqlite3.Conn) error) (*connector
var txlock, timefmt string
if strings.HasPrefix(name, "file:") {
u, err := url.Parse(name)
if err != nil {
return nil, err
if _, after, ok := strings.Cut(name, "?"); ok {
query, err := url.ParseQuery(after)
if err != nil {
return nil, err
}
txlock = query.Get("_txlock")
timefmt = query.Get("_timefmt")
c.pragmas = query.Has("_pragma")
}
query, err := url.ParseQuery(u.RawQuery)
if err != nil {
return nil, err
}
txlock = query.Get("_txlock")
timefmt = query.Get("_timefmt")
c.pragmas = query.Has("_pragma")
}
switch txlock {
@@ -225,7 +201,7 @@ func (n *connector) Driver() driver.Driver {
return &SQLite{}
}
func (n *connector) Connect(ctx context.Context) (ret driver.Conn, err error) {
func (n *connector) Connect(ctx context.Context) (res driver.Conn, err error) {
c := &conn{
txLock: n.txLock,
tmRead: n.tmRead,
@@ -237,14 +213,13 @@ func (n *connector) Connect(ctx context.Context) (ret driver.Conn, err error) {
return nil, err
}
defer func() {
if ret == nil {
if res == nil {
c.Close()
}
}()
if old := c.Conn.SetInterrupt(ctx); old != ctx {
defer c.Conn.SetInterrupt(old)
}
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
if !n.pragmas {
err = c.Conn.BusyTimeout(time.Minute)
@@ -264,8 +239,10 @@ func (n *connector) Connect(ctx context.Context) (ret driver.Conn, err error) {
return nil, err
}
defer s.Close()
if s.Step() {
c.readOnly = s.ColumnBool(0)
if s.Step() && s.ColumnBool(0) {
c.readOnly = '1'
} else {
c.readOnly = '0'
}
err = s.Close()
if err != nil {
@@ -321,7 +298,7 @@ type conn struct {
txReset string
tmRead sqlite3.TimeFormat
tmWrite sqlite3.TimeFormat
readOnly bool
readOnly byte
}
var (
@@ -344,7 +321,7 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
var txLock string
switch opts.Isolation {
default:
return nil, errutil.IsolationErr
return nil, util.IsolationErr
case driver.IsolationLevel(sql.LevelLinearizable):
txLock = "exclusive"
case driver.IsolationLevel(sql.LevelSerializable):
@@ -357,14 +334,13 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
c.txReset = ``
txBegin := `BEGIN ` + txLock
if opts.ReadOnly && !c.readOnly {
if opts.ReadOnly {
txBegin += ` ; PRAGMA query_only=on`
c.txReset = `; PRAGMA query_only=off`
c.txReset = `; PRAGMA query_only=` + string(c.readOnly)
}
if old := c.Conn.SetInterrupt(ctx); old != ctx {
defer c.Conn.SetInterrupt(old)
}
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
err := c.Conn.Exec(txBegin)
if err != nil {
@@ -382,12 +358,13 @@ func (c *conn) Commit() error {
}
func (c *conn) Rollback() error {
// ROLLBACK even if interrupted.
ctx := context.Background()
if old := c.Conn.SetInterrupt(ctx); old != ctx {
err := c.Conn.Exec(`ROLLBACK` + c.txReset)
if errors.Is(err, sqlite3.INTERRUPT) {
old := c.Conn.SetInterrupt(context.Background())
defer c.Conn.SetInterrupt(old)
err = c.Conn.Exec(`ROLLBACK` + c.txReset)
}
return c.Conn.Exec(`ROLLBACK` + c.txReset)
return err
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
@@ -396,9 +373,8 @@ func (c *conn) Prepare(query string) (driver.Stmt, error) {
}
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
if old := c.Conn.SetInterrupt(ctx); old != ctx {
defer c.Conn.SetInterrupt(old)
}
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
s, tail, err := c.Conn.Prepare(query)
if err != nil {
@@ -406,7 +382,7 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
}
if notWhitespace(tail) {
s.Close()
return nil, errutil.TailErr
return nil, util.TailErr
}
return &stmt{Stmt: s, tmRead: c.tmRead, tmWrite: c.tmWrite, inputs: -2}, nil
}
@@ -423,9 +399,8 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
return resultRowsAffected(0), nil
}
if old := c.Conn.SetInterrupt(ctx); old != ctx {
defer c.Conn.SetInterrupt(old)
}
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
err := c.Conn.Exec(query)
if err != nil {
@@ -488,19 +463,16 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
return nil, err
}
c := s.Stmt.Conn()
if old := c.SetInterrupt(ctx); old != ctx {
defer c.SetInterrupt(old)
}
old := s.Stmt.Conn().SetInterrupt(ctx)
defer s.Stmt.Conn().SetInterrupt(old)
err = errors.Join(
s.Stmt.Exec(),
s.Stmt.ClearBindings())
err = s.Stmt.Exec()
s.Stmt.ClearBindings()
if err != nil {
return nil, err
}
return newResult(c), nil
return newResult(s.Stmt.Conn()), nil
}
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
@@ -545,12 +517,12 @@ func (s *stmt) setupBindings(args []driver.NamedValue) (err error) {
err = s.Stmt.BindTime(id, a, s.tmWrite)
case util.JSON:
err = s.Stmt.BindJSON(id, a.Value)
case util.Pointer:
err = s.Stmt.BindPointer(id, a.Value)
case util.PointerUnwrap:
err = s.Stmt.BindPointer(id, util.UnwrapPointer(a))
case nil:
err = s.Stmt.BindNull(id)
default:
panic(errutil.AssertErr())
panic(util.AssertErr())
}
if err != nil {
return err
@@ -564,7 +536,7 @@ func (s *stmt) CheckNamedValue(arg *driver.NamedValue) error {
switch arg.Value.(type) {
case bool, int, int64, float64, string, []byte,
time.Time, sqlite3.ZeroBlob,
util.JSON, util.Pointer,
util.JSON, util.PointerUnwrap,
nil:
return nil
default:
@@ -603,59 +575,28 @@ func (r resultRowsAffected) RowsAffected() (int64, error) {
return int64(r), nil
}
type scantype byte
const (
_ANY scantype = iota
_INT
_REAL
_TEXT
_BLOB
_NULL
_BOOL
_TIME
_NOT_NULL
)
var (
_ [0]struct{} = [scantype(sqlite3.INTEGER) - _INT]struct{}{}
_ [0]struct{} = [scantype(sqlite3.FLOAT) - _REAL]struct{}{}
_ [0]struct{} = [scantype(sqlite3.TEXT) - _TEXT]struct{}{}
_ [0]struct{} = [scantype(sqlite3.BLOB) - _BLOB]struct{}{}
_ [0]struct{} = [scantype(sqlite3.NULL) - _NULL]struct{}{}
_ [0]struct{} = [_NOT_NULL & (_NOT_NULL - 1)]struct{}{}
)
func scanFromDecl(decl string) scantype {
// These types are only used before we have rows,
// and otherwise as type hints.
// The first few ensure STRICT tables are strictly typed.
// The other two are type hints for booleans and time.
switch decl {
case "INT", "INTEGER":
return _INT
case "REAL":
return _REAL
case "TEXT":
return _TEXT
case "BLOB":
return _BLOB
case "BOOLEAN":
return _BOOL
case "DATE", "TIME", "DATETIME", "TIMESTAMP":
return _TIME
}
return _ANY
}
type rows struct {
ctx context.Context
*stmt
names []string
types []string
nulls []bool
scans []scantype
}
type scantype byte
const (
_ANY scantype = iota
_INT scantype = scantype(sqlite3.INTEGER)
_REAL scantype = scantype(sqlite3.FLOAT)
_TEXT scantype = scantype(sqlite3.TEXT)
_BLOB scantype = scantype(sqlite3.BLOB)
_NULL scantype = scantype(sqlite3.NULL)
_BOOL scantype = iota
_TIME
)
var (
// Ensure these interfaces are implemented:
_ driver.RowsColumnTypeDatabaseTypeName = &rows{}
@@ -663,9 +604,8 @@ var (
)
func (r *rows) Close() error {
return errors.Join(
r.Stmt.Reset(),
r.Stmt.ClearBindings())
r.Stmt.ClearBindings()
return r.Stmt.Reset()
}
func (r *rows) Columns() []string {
@@ -680,69 +620,79 @@ func (r *rows) Columns() []string {
return r.names
}
func (r *rows) scanType(index int) scantype {
if r.scans == nil {
count := len(r.names)
scans := make([]scantype, count)
for i := range scans {
scans[i] = scanFromDecl(strings.ToUpper(r.Stmt.ColumnDeclType(i)))
}
r.scans = scans
}
return r.scans[index] &^ _NOT_NULL
}
func (r *rows) loadColumnMetadata() {
if r.types == nil {
c := r.Stmt.Conn()
count := len(r.names)
if r.nulls == nil {
count := r.Stmt.ColumnCount()
nulls := make([]bool, count)
types := make([]string, count)
scans := make([]scantype, count)
for i := range types {
var declType string
var notNull, autoInc bool
if column := r.Stmt.ColumnOriginName(i); column != "" {
declType, _, notNull, _, autoInc, _ = c.TableColumnMetadata(
for i := range nulls {
if col := r.Stmt.ColumnOriginName(i); col != "" {
types[i], _, nulls[i], _, _, _ = r.Stmt.Conn().TableColumnMetadata(
r.Stmt.ColumnDatabaseName(i),
r.Stmt.ColumnTableName(i),
column)
} else {
declType = r.Stmt.ColumnDeclType(i)
}
if declType != "" {
declType = strings.ToUpper(declType)
scans[i] = scanFromDecl(declType)
types[i] = declType
}
if notNull || autoInc {
scans[i] |= _NOT_NULL
col)
types[i] = strings.ToUpper(types[i])
// These types are only used before we have rows,
// and otherwise as type hints.
// The first few ensure STRICT tables are strictly typed.
// The other two are type hints for booleans and time.
switch types[i] {
case "INT", "INTEGER":
scans[i] = _INT
case "REAL":
scans[i] = _REAL
case "TEXT":
scans[i] = _TEXT
case "BLOB":
scans[i] = _BLOB
case "BOOLEAN":
scans[i] = _BOOL
case "DATE", "TIME", "DATETIME", "TIMESTAMP":
scans[i] = _TIME
}
}
}
r.nulls = nulls
r.types = types
r.scans = scans
}
}
func (r *rows) declType(index int) string {
if r.types == nil {
count := r.Stmt.ColumnCount()
types := make([]string, count)
for i := range types {
types[i] = strings.ToUpper(r.Stmt.ColumnDeclType(i))
}
r.types = types
}
return r.types[index]
}
func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
r.loadColumnMetadata()
decl := r.types[index]
if len := len(decl); len > 0 && decl[len-1] == ')' {
if i := strings.LastIndexByte(decl, '('); i >= 0 {
decl = decl[:i]
decltype := r.types[index]
if len := len(decltype); len > 0 && decltype[len-1] == ')' {
if i := strings.LastIndexByte(decltype, '('); i >= 0 {
decltype = decltype[:i]
}
}
return strings.TrimSpace(decl)
return strings.TrimSpace(decltype)
}
func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) {
r.loadColumnMetadata()
nullable = r.scans[index]&^_NOT_NULL == 0
return nullable, !nullable
if r.nulls[index] {
return false, true
}
return true, false
}
func (r *rows) ColumnTypeScanType(index int) (typ reflect.Type) {
r.loadColumnMetadata()
scan := r.scans[index] &^ _NOT_NULL
scan := r.scans[index]
if r.Stmt.Busy() {
// SQLite is dynamically typed and we now have a row.
@@ -754,7 +704,7 @@ func (r *rows) ColumnTypeScanType(index int) (typ reflect.Type) {
switch {
case scan == _TIME && val != _BLOB && val != _NULL:
t := r.Stmt.ColumnTime(index, r.tmRead)
useValType = t.IsZero()
useValType = t == time.Time{}
case scan == _BOOL && val == _INT:
i := r.Stmt.ColumnInt64(index)
useValType = i != 0 && i != 1
@@ -768,78 +718,64 @@ func (r *rows) ColumnTypeScanType(index int) (typ reflect.Type) {
switch scan {
case _INT:
return reflect.TypeFor[int64]()
return reflect.TypeOf(int64(0))
case _REAL:
return reflect.TypeFor[float64]()
return reflect.TypeOf(float64(0))
case _TEXT:
return reflect.TypeFor[string]()
return reflect.TypeOf("")
case _BLOB:
return reflect.TypeFor[[]byte]()
return reflect.TypeOf([]byte{})
case _BOOL:
return reflect.TypeFor[bool]()
return reflect.TypeOf(false)
case _TIME:
return reflect.TypeFor[time.Time]()
return reflect.TypeOf(time.Time{})
default:
return reflect.TypeFor[any]()
return reflect.TypeOf((*any)(nil)).Elem()
}
}
func (r *rows) NextRow() error {
c := r.Stmt.Conn()
if old := c.SetInterrupt(r.ctx); old != r.ctx {
defer c.SetInterrupt(old)
}
if r.Stmt.Step() {
return nil
}
if err := r.Stmt.Err(); err != nil {
return err
}
return io.EOF
}
func (r *rows) Next(dest []driver.Value) error {
if err := r.NextRow(); err != nil {
return err
old := r.Stmt.Conn().SetInterrupt(r.ctx)
defer r.Stmt.Conn().SetInterrupt(old)
if !r.Stmt.Step() {
if err := r.Stmt.Err(); err != nil {
return err
}
return io.EOF
}
data := unsafe.Slice((*any)(unsafe.SliceData(dest)), len(dest))
if err := r.Stmt.ColumnsRaw(data...); err != nil {
return err
}
err := r.Stmt.Columns(data...)
for i := range dest {
dest[i] = r.convert(i, dest[i])
if t, ok := r.decodeTime(i, dest[i]); ok {
dest[i] = t
}
}
return nil
return err
}
func (r *rows) convert(i int, val driver.Value) driver.Value {
scan := r.scanType(i)
if v, ok := val.([]byte); ok {
if len(v) == cap(v) { // a BLOB
return val
func (r *rows) decodeTime(i int, v any) (_ time.Time, ok bool) {
switch v := v.(type) {
case int64, float64:
// could be a time value
case string:
if r.tmWrite != "" && r.tmWrite != time.RFC3339 && r.tmWrite != time.RFC3339Nano {
break
}
if scan != _TEXT {
t, ok := r.maybeTime(v)
if ok {
return t
}
t, ok := maybeTime(v)
if ok {
return t, true
}
val = string(v)
default:
return
}
switch scan {
case _TIME:
t, err := r.tmRead.Decode(val)
if err == nil {
return t
}
case _BOOL:
switch val {
case int64(0):
return false
case int64(1):
return true
}
switch r.declType(i) {
case "DATE", "TIME", "DATETIME", "TIMESTAMP":
// could be a time value
default:
return
}
return val
t, err := r.tmRead.Decode(v)
return t, err == nil
}
-146
View File
@@ -1,146 +0,0 @@
//go:build go1.27
package driver
import (
"database/sql"
"database/sql/driver"
"math"
"time"
"github.com/hanzoai/sqlite3"
)
func (r *rows) ScanColumn(ctx driver.ScanContext, i int, dest any) error {
typ := r.Stmt.ColumnType(i)
// Fast path.
var src any
switch typ {
case sqlite3.NULL:
// src = nil
case sqlite3.FLOAT:
f := r.stmt.ColumnFloat(i)
if r.scanFloat(f, dest) {
return nil
}
src = f
case sqlite3.INTEGER:
i := r.stmt.ColumnInt64(i)
if r.scanInt(i, dest) {
return nil
}
if r.scanFloat(float64(i), dest) {
return nil
}
src = i
default:
var b []byte
if typ == sqlite3.TEXT {
b = r.stmt.ColumnRawText(i)
} else {
b = r.stmt.ColumnRawBlob(i)
}
switch d := dest.(type) {
case *sql.RawBytes:
*d = b
return nil
case *string:
*d = string(b)
return nil
case *sql.NullString:
d.String = string(b)
d.Valid = true
return nil
case *[]byte:
*d = append((*d)[:0], b...)
return nil
}
src = b
}
// Time handling.
if typ != sqlite3.NULL && typ != sqlite3.BLOB {
var ok bool
switch d := dest.(type) {
case *time.Time:
*d, ok = r.scanTime(src)
case *sql.NullTime:
d.Time, ok = r.scanTime(src)
d.Valid = ok
}
if ok {
return nil
}
}
// Fallback.
return sql.ConvertAssign(ctx, dest, r.convert(i, src))
}
func (r *rows) scanTime(src any) (_ time.Time, _ bool) {
if s, ok := src.([]byte); ok {
if t, ok := r.maybeTime(s); ok {
return t, true
}
src = string(s)
}
t, err := r.tmRead.Decode(src)
return t, err == nil
}
func (r *rows) scanFloat(f float64, dest any) bool {
switch d := dest.(type) {
case *float64:
*d = f
case *float32:
*d = float32(f)
case *sql.NullFloat64:
d.Float64 = f
d.Valid = true
case *sql.Null[float32]:
d.V = float32(f)
d.Valid = true
default:
return false
}
return true
}
func (r *rows) scanInt(i int64, dest any) bool {
switch d := dest.(type) {
case *int64:
*d = i
case *sql.NullInt64:
d.Int64 = i
d.Valid = true
case *uint64:
*d = uint64(i)
return 0 <= i
case *sql.Null[uint64]:
d.V = uint64(i)
d.Valid = true
return 0 <= i
case *int:
*d = int(i)
return math.MinInt <= i && i <= math.MaxInt
case *sql.Null[int]:
d.V = int(i)
d.Valid = true
return math.MinInt <= i && i <= math.MaxInt
case *uint:
*d = uint(i)
return 0 <= i && uint64(i) <= math.MaxUint
case *sql.Null[uint]:
d.V = uint(i)
d.Valid = true
return 0 <= i && uint64(i) <= math.MaxUint
default:
return false
}
return true
}
+60 -189
View File
@@ -8,15 +8,14 @@ import (
"math"
"net/url"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Test_Open_error(t *testing.T) {
@@ -34,40 +33,35 @@ func Test_Open_error(t *testing.T) {
func Test_Open_dir(t *testing.T) {
t.Parallel()
ctx := testcfg.Context(t)
db, err := Open(".")
db, err := sql.Open("sqlite3", ".")
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.Conn(ctx)
_, err = db.Conn(context.TODO())
if err == nil {
t.Fatal("want error")
}
if runtime.GOOS == "js" {
t.Skip("skipping on JS")
}
if !errors.Is(err, sqlite3.CANTOPEN_ISDIR) {
t.Errorf("got %v, want sqlite3.CANTOPEN_ISDIR", err)
if !errors.Is(err, sqlite3.CANTOPEN) {
t.Errorf("got %v, want sqlite3.CANTOPEN", err)
}
}
func Test_Open_pragma(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t, url.Values{
tmp := memdb.TestDB(t, url.Values{
"_pragma": {"busy_timeout(1000)"},
})
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
var timeout int
err = db.QueryRowContext(ctx, `PRAGMA busy_timeout`).Scan(&timeout)
err = db.QueryRow(`PRAGMA busy_timeout`).Scan(&timeout)
if err != nil {
t.Fatal(err)
}
@@ -78,18 +72,17 @@ func Test_Open_pragma(t *testing.T) {
func Test_Open_pragma_invalid(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t, url.Values{
tmp := memdb.TestDB(t, url.Values{
"_pragma": {"busy_timeout 1000"},
})
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.Conn(ctx)
_, err = db.Conn(context.TODO())
if err == nil {
t.Fatal("want error")
}
@@ -107,24 +100,23 @@ func Test_Open_pragma_invalid(t *testing.T) {
func Test_Open_txLock(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t, url.Values{
tmp := memdb.TestDB(t, url.Values{
"_txlock": {"exclusive"},
"_pragma": {"busy_timeout(1000)"},
})
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
tx1, err := db.BeginTx(ctx, nil)
tx1, err := db.Begin()
if err != nil {
t.Fatal(err)
}
_, err = db.BeginTx(ctx, nil)
_, err = db.Begin()
if err == nil {
t.Error("want error")
}
@@ -144,11 +136,11 @@ func Test_Open_txLock(t *testing.T) {
func Test_Open_txLock_invalid(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t, url.Values{
tmp := memdb.TestDB(t, url.Values{
"_txlock": {"xclusive"},
})
_, err := Open(dsn)
_, err := sql.Open("sqlite3", tmp+"_txlock=xclusive")
if err == nil {
t.Fatal("want error")
}
@@ -159,20 +151,22 @@ func Test_Open_txLock_invalid(t *testing.T) {
func Test_BeginTx(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t, url.Values{
tmp := memdb.TestDB(t, url.Values{
"_txlock": {"exclusive"},
"_pragma": {"busy_timeout(0)"},
})
ctx := testcfg.Context(t)
db, err := Open(dsn)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
if err.Error() != string(errutil.IsolationErr) {
if err.Error() != string(util.IsolationErr) {
t.Error("want isolationErr")
}
@@ -205,78 +199,18 @@ func Test_BeginTx(t *testing.T) {
}
}
func Test_nested_context(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := Open(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
outer, err := tx.Query(`SELECT value FROM generate_series(0)`)
if err != nil {
t.Fatal(err)
}
defer outer.Close()
want := func(rows *sql.Rows, want int) {
t.Helper()
var got int
rows.Next()
if err := rows.Scan(&got); err != nil {
t.Fatal(err)
}
if got != want {
t.Errorf("got %d, want %d", got, want)
}
}
want(outer, 0)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
inner, err := tx.QueryContext(ctx, `SELECT value FROM generate_series(0)`)
if err != nil {
t.Fatal(err)
}
defer inner.Close()
want(inner, 0)
cancel()
var terr interface{ Temporary() bool }
if inner.Next() || !errors.Is(inner.Err(), context.Canceled) &&
(!errors.As(inner.Err(), &terr) || !terr.Temporary()) {
t.Fatalf("got %v, want cancellation", inner.Err())
}
want(outer, 1)
}
func Test_Prepare(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
var serr *sqlite3.Error
_, err = db.PrepareContext(ctx, `SELECT`)
_, err = db.Prepare(`SELECT`)
if err == nil {
t.Error("want error")
}
@@ -290,28 +224,30 @@ func Test_Prepare(t *testing.T) {
t.Error("got message:", got)
}
_, err = db.PrepareContext(ctx, `SELECT 1; `)
_, err = db.Prepare(`SELECT 1; `)
if err != nil {
t.Error(err)
}
_, err = db.PrepareContext(ctx, `SELECT 1; SELECT`)
if err.Error() != string(errutil.TailErr) {
_, err = db.Prepare(`SELECT 1; SELECT`)
if err.Error() != string(util.TailErr) {
t.Error("want tailErr")
}
_, err = db.PrepareContext(ctx, `SELECT 1; SELECT 2`)
if err.Error() != string(errutil.TailErr) {
_, err = db.Prepare(`SELECT 1; SELECT 2`)
if err.Error() != string(util.TailErr) {
t.Error("want tailErr")
}
}
func Test_QueryRow_named(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := Open(dsn)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
@@ -330,7 +266,7 @@ func Test_QueryRow_named(t *testing.T) {
defer stmt.Close()
date := time.Now()
row := stmt.QueryRowContext(ctx, true, sql.Named("AAA", math.Pi), nil /*3*/, nil /*4*/, date /*5*/)
row := stmt.QueryRow(true, sql.Named("AAA", math.Pi), nil /*3*/, nil /*4*/, date /*5*/)
var first bool
var fifth time.Time
@@ -359,16 +295,15 @@ func Test_QueryRow_named(t *testing.T) {
func Test_QueryRow_blob_null(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
rows, err := db.QueryContext(ctx, `
rows, err := db.Query(`
SELECT NULL UNION ALL
SELECT x'cafe' UNION ALL
SELECT x'babe' UNION ALL
@@ -397,12 +332,11 @@ func Test_time(t *testing.T) {
for _, fmt := range []string{"auto", "sqlite", "rfc3339", time.ANSIC} {
t.Run(fmt, func(t *testing.T) {
dsn := memdb.TestDB(t, url.Values{
tmp := memdb.TestDB(t, url.Values{
"_timefmt": {fmt},
})
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
@@ -410,18 +344,18 @@ func Test_time(t *testing.T) {
twosday := time.Date(2022, 2, 22, 22, 22, 22, 0, time.UTC)
_, err = db.ExecContext(ctx, `CREATE TABLE test (at DATETIME)`)
_, err = db.Exec(`CREATE TABLE test (at DATETIME)`)
if err != nil {
t.Fatal(err)
}
_, err = db.ExecContext(ctx, `INSERT INTO test VALUES (?)`, twosday)
_, err = db.Exec(`INSERT INTO test VALUES (?)`, twosday)
if err != nil {
t.Fatal(err)
}
var got time.Time
err = db.QueryRowContext(ctx, `SELECT * FROM test`).Scan(&got)
err = db.QueryRow(`SELECT * FROM test`).Scan(&got)
if err != nil {
t.Fatal(err)
}
@@ -435,26 +369,25 @@ func Test_time(t *testing.T) {
func Test_ColumnType_ScanType(t *testing.T) {
var (
INT = reflect.TypeFor[int64]()
REAL = reflect.TypeFor[float64]()
TEXT = reflect.TypeFor[string]()
BLOB = reflect.TypeFor[[]byte]()
BOOL = reflect.TypeFor[bool]()
TIME = reflect.TypeFor[time.Time]()
ANY = reflect.TypeFor[any]()
INT = reflect.TypeOf(int64(0))
REAL = reflect.TypeOf(float64(0))
TEXT = reflect.TypeOf("")
BLOB = reflect.TypeOf([]byte{})
BOOL = reflect.TypeOf(false)
TIME = reflect.TypeOf(time.Time{})
ANY = reflect.TypeOf((*any)(nil)).Elem()
)
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := Open(dsn)
db, err := sql.Open("sqlite3", tmp)
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.ExecContext(ctx, `
_, err = db.Exec(`
CREATE TABLE test (
col_int INTEGER,
col_real REAL,
@@ -479,7 +412,7 @@ func Test_ColumnType_ScanType(t *testing.T) {
t.Fatal(err)
}
rows, err := db.QueryContext(ctx, `SELECT * FROM test`)
rows, err := db.Query(`SELECT * FROM test`)
if err != nil {
t.Fatal(err)
}
@@ -534,65 +467,3 @@ func Test_ColumnType_ScanType(t *testing.T) {
t.Fatal(err)
}
}
func Test_rows_ScanColumn(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
db, err := Open(dsn)
if err != nil {
t.Fatal(err)
}
defer db.Close()
var tm time.Time
err = db.QueryRow(`SELECT NULL`).Scan(&tm)
if err == nil {
t.Error("want error")
}
// Go 1.27
err = db.QueryRow(`SELECT datetime()`).Scan(&tm)
if err != nil && !strings.HasPrefix(err.Error(), "sql: Scan error") {
t.Error(err)
}
if err == nil && tm.IsZero() {
t.Error(tm)
}
var nt sql.NullTime
err = db.QueryRow(`SELECT NULL`).Scan(&nt)
if err != nil {
t.Error(err)
}
// Go 1.27
err = db.QueryRow(`SELECT datetime()`).Scan(&nt)
if err != nil && !strings.HasPrefix(err.Error(), "sql: Scan error") {
t.Error(err)
}
if err == nil && nt.Time.IsZero() {
t.Error(nt.Time)
}
}
func Benchmark_loop(b *testing.B) {
ctx := testcfg.Context(b)
db, err := Open(":memory:")
if err != nil {
b.Fatal(err)
}
defer db.Close()
var version string
err = db.QueryRowContext(ctx, `SELECT sqlite_version();`).Scan(&version)
if err != nil {
b.Fatal(err)
}
for b.Loop() {
_, err := db.ExecContext(b.Context(),
`WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x < 1000000) SELECT x FROM c;`)
if err != nil {
b.Fatal(err)
}
}
}
+9 -4
View File
@@ -1,5 +1,9 @@
//go:build linux || darwin || windows || freebsd || openbsd || netbsd || dragonfly || illumos || sqlite3_flock || sqlite3_dotlk
package driver_test
// Adapted from: https://go.dev/doc/tutorial/database-access
import (
"database/sql"
"database/sql/driver"
@@ -7,9 +11,10 @@ import (
"log"
"time"
"github.com/hanzoai/sqlite3"
_ "github.com/hanzoai/sqlite3/driver"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Example_customTime() {
@@ -22,7 +27,7 @@ func Example_customTime() {
_, err = db.Exec(`
CREATE TABLE data (
id INTEGER PRIMARY KEY,
date_time ANY
date_time TEXT
) STRICT;
`)
if err != nil {
+3 -2
View File
@@ -10,8 +10,9 @@ import (
"log"
"os"
_ "github.com/hanzoai/sqlite3/driver"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
var db *sql.DB
+4 -3
View File
@@ -4,9 +4,10 @@ import (
"fmt"
"log"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Example_json() {
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"database/sql"
"time"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
)
// Savepoint establishes a new transaction savepoint.
+4 -2
View File
@@ -4,8 +4,10 @@ import (
"fmt"
"log"
"github.com/hanzoai/sqlite3/driver"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
func ExampleSavepoint() {
+6 -17
View File
@@ -1,23 +1,12 @@
package driver
import (
"bytes"
"time"
)
import "time"
// Convert a string in [time.RFC3339Nano] format into a [time.Time]
// if that's the format we're using to persist them,
// and if it roundtrips back to the same string.
// if it roundtrips back to the same string.
// This way times can be persisted to, and recovered from, the database,
// but if a string is wanted, [database/sql] will recover the same string.
func (r *rows) maybeTime(text []byte) (_ time.Time, _ bool) {
switch r.tmWrite {
case "", time.RFC3339, time.RFC3339Nano:
break
default:
return
}
// but if a string is needed, [database/sql] will recover the same string.
func maybeTime(text string) (_ time.Time, _ bool) {
// Weed out (some) values that can't possibly be
// [time.RFC3339Nano] timestamps.
if len(text) < len("2006-01-02T15:04:05Z") {
@@ -32,8 +21,8 @@ func (r *rows) maybeTime(text []byte) (_ time.Time, _ bool) {
// Slow path.
var buf [len(time.RFC3339Nano)]byte
date, err := time.Parse(time.RFC3339Nano, string(text))
if err == nil && bytes.Equal(text, date.AppendFormat(buf[:0], time.RFC3339Nano)) {
date, err := time.Parse(time.RFC3339Nano, text)
if err == nil && text == string(date.AppendFormat(buf[:0], time.RFC3339Nano)) {
return date, true
}
return
+2 -4
View File
@@ -21,9 +21,8 @@ func Fuzz_stringOrTime_1(f *testing.F) {
f.Add("2006-01-02T15:04:05.9999999999Z")
f.Add("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
r := rows{stmt: &stmt{}}
f.Fuzz(func(t *testing.T, str string) {
v, ok := r.maybeTime([]byte(str))
v, ok := maybeTime(str)
if ok {
// Make sure times round-trip to the same string:
// https://pkg.go.dev/database/sql#Rows.Scan
@@ -51,9 +50,8 @@ func Fuzz_stringOrTime_2(f *testing.F) {
f.Add(int64(639095955742), int64(222_222_222)) // twosday, year 22222AD
f.Add(int64(-763421161058), int64(222_222_222)) // twosday, year 22222BC
r := rows{stmt: &stmt{}}
checkTime := func(t testing.TB, date time.Time) {
v, ok := r.maybeTime(date.AppendFormat(nil, time.RFC3339Nano))
v, ok := maybeTime(date.Format(time.RFC3339Nano))
if ok {
// Make sure times round-trip to the same time:
if !v.Equal(date) {
+6 -5
View File
@@ -1,11 +1,13 @@
package driver
import (
"context"
"database/sql/driver"
"slices"
"reflect"
"testing"
"github.com/hanzoai/sqlite3/internal/testcfg"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
)
func Test_namedValues(t *testing.T) {
@@ -14,7 +16,7 @@ func Test_namedValues(t *testing.T) {
{Ordinal: 2, Value: false},
}
got := namedValues([]driver.Value{true, false})
if !slices.Equal(got, want) {
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
@@ -54,8 +56,7 @@ func Fuzz_notWhitespace(f *testing.F) {
t.SkipNow()
}
ctx := testcfg.Context(t)
c, err := db.Conn(ctx)
c, err := db.Conn(context.Background())
if err != nil {
t.Fatal(err)
}
+41
View File
@@ -0,0 +1,41 @@
# Embeddable Wasm build of SQLite
This folder includes an embeddable Wasm build of SQLite 3.47.2 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](https://sqlite.org/lang_mathfunc.html)
- [FTS5](https://sqlite.org/fts5.html)
- [JSON](https://sqlite.org/json1.html)
- [R*Tree](https://sqlite.org/rtree.html)
- [GeoPoly](https://sqlite.org/geopoly.html)
- [Spellfix1](https://sqlite.org/spellfix1.html)
- [soundex](https://sqlite.org/lang_corefunc.html#soundex)
- [stat4](https://sqlite.org/compile.html#enable_stat4)
- [base64](https://github.com/sqlite/sqlite/blob/master/ext/misc/base64.c)
- [decimal](https://github.com/sqlite/sqlite/blob/master/ext/misc/decimal.c)
- [ieee754](https://github.com/sqlite/sqlite/blob/master/ext/misc/ieee754.c)
- [regexp](https://github.com/sqlite/sqlite/blob/master/ext/misc/regexp.c)
- [series](https://github.com/sqlite/sqlite/blob/master/ext/misc/series.c)
- [uint](https://github.com/sqlite/sqlite/blob/master/ext/misc/uint.c)
- [time](../sqlite3/time.c)
See the [configuration options](../sqlite3/sqlite_opt.h),
and [patches](../sqlite3) applied.
Built using [`wasi-sdk`](https://github.com/WebAssembly/wasi-sdk),
and [`binaryen`](https://github.com/WebAssembly/binaryen).
The build is easily reproducible, and verifiable, using
[Artifact Attestations](https://github.com/ncruces/go-sqlite3/attestations).
### Customizing the build
You can use your own custom build of SQLite.
Examples of custom builds of SQLite are:
- [`github.com/ncruces/go-sqlite3/embed/bcw2`](https://github.com/ncruces/go-sqlite3/tree/main/embed/bcw2)
built from a branch supporting [`BEGIN CONCURRENT`](https://sqlite.org/src/doc/begin-concurrent/doc/begin_concurrent.md)
and [Wal2](https://sqlite.org/cgi/src/doc/wal2/doc/wal2.md).
- [`github.com/asg017/sqlite-vec-go-bindings/ncruces`](https://github.com/asg017/sqlite-vec-go-bindings)
which includes the [`sqlite-vec`](https://github.com/asg017/sqlite-vec) vector search extension.
+2
View File
@@ -0,0 +1,2 @@
build/
sqlite/
+16
View File
@@ -0,0 +1,16 @@
# Embeddable Wasm build of SQLite
This folder includes an embeddable Wasm build of SQLite, including the experimental
[`BEGIN CONCURRENT`](https://sqlite.org/src/doc/begin-concurrent/doc/begin_concurrent.md) and
[Wal2](https://sqlite.org/cgi/src/doc/wal2/doc/wal2.md) patches.
> [!IMPORTANT]
> This package is experimental.
> It is built from the `bedrock` branch of SQLite,
> since that is _currently_ the most stable, maintained branch to include both features.
> [!CAUTION]
> The Wal2 journaling mode creates databases that other versions of SQLite cannot access.
The build is easily reproducible, and verifiable, using
[Artifact Attestations](https://github.com/ncruces/go-sqlite3/attestations).
BIN
View File
Binary file not shown.
+53
View File
@@ -0,0 +1,53 @@
package bcw2
import (
"path/filepath"
"testing"
"github.com/ncruces/go-sqlite3/driver"
"github.com/ncruces/go-sqlite3/vfs"
)
func Test_bcw2(t *testing.T) {
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}
tmp := filepath.ToSlash(filepath.Join(t.TempDir(), "test.db"))
db, err := driver.Open("file:" + tmp + "?_pragma=journal_mode(wal2)&_txlock=concurrent")
if err != nil {
t.Fatal(err)
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
_, err = tx.Exec(`CREATE TABLE test (col)`)
if err != nil {
t.Fatal(err)
}
_, err = tx.Exec(`DELETE FROM test LIMIT 1`)
if err != nil {
t.Fatal(err)
}
err = tx.Commit()
if err != nil {
t.Fatal(err)
}
var version string
err = db.QueryRow(`SELECT sqlite_version()`).Scan(&version)
if err != nil {
t.Fatal(err)
}
if version != "3.48.0" {
t.Error(version)
}
}
+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
set -euo pipefail
cd -P -- "$(dirname -- "$0")"
ROOT=../../
BINARYEN="$ROOT/tools/binaryen/bin"
WASI_SDK="$ROOT/tools/wasi-sdk/bin"
trap 'rm -rf build/ sqlite/ bcw2.tmp' EXIT
mkdir -p build/ext/
cp "$ROOT"/sqlite3/*.[ch] build/
cp "$ROOT"/sqlite3/*.patch build/
# https://sqlite.org/src/info/ec5d7025cba9f4ac
curl -# https://sqlite.org/src/tarball/sqlite.tar.gz?r=ec5d7025 | tar xz
cd sqlite
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
MSYS_NO_PATHCONV=1 nmake /f makefile.msc sqlite3.c OPTS=-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
else
sh configure --enable-update-limit && make sqlite3.c
fi
cd ~-
mv sqlite/sqlite3.c build/
mv sqlite/sqlite3.h build/
mv sqlite/sqlite3ext.h build/
mv sqlite/ext/misc/anycollseq.c build/ext/
mv sqlite/ext/misc/base64.c build/ext/
mv sqlite/ext/misc/decimal.c build/ext/
mv sqlite/ext/misc/ieee754.c build/ext/
mv sqlite/ext/misc/regexp.c build/ext/
mv sqlite/ext/misc/series.c build/ext/
mv sqlite/ext/misc/spellfix.c build/ext/
mv sqlite/ext/misc/uint.c build/ext/
cd build
cat *.patch | patch --no-backup-if-mismatch
cd ~-
"$WASI_SDK/clang" --target=wasm32-wasi -std=c23 -g0 -O2 \
-Wall -Wextra -Wno-unused-parameter -Wno-unused-function \
-o bcw2.wasm "build/main.c" \
-I"build" \
-mexec-model=reactor \
-msimd128 -mmutable-globals -mmultivalue \
-mbulk-memory -mreference-types \
-mnontrapping-fptoint -msign-ext \
-fno-stack-protector -fno-stack-clash-protection \
-Wl,--stack-first \
-Wl,--import-undefined \
-Wl,--initial-memory=327680 \
-D_HAVE_SQLITE_CONFIG_H \
-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT \
-DSQLITE_CUSTOM_INCLUDE=sqlite_opt.h \
$(awk '{print "-Wl,--export="$0}' ../exports.txt)
"$BINARYEN/wasm-ctor-eval" -g -c _initialize bcw2.wasm -o bcw2.tmp
"$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \
bcw2.tmp -o bcw2.wasm \
--enable-simd --enable-mutable-globals --enable-multivalue \
--enable-bulk-memory --enable-reference-types \
--enable-nontrapping-float-to-int --enable-sign-ext
+13
View File
@@ -0,0 +1,13 @@
module github.com/ncruces/go-sqlite3/embed/bcw2
go 1.21
toolchain go1.23.0
require github.com/ncruces/go-sqlite3 v0.21.3
require (
github.com/ncruces/julianday v1.0.0 // indirect
github.com/tetratelabs/wazero v1.8.2 // indirect
golang.org/x/sys v0.29.0 // indirect
)
+10
View File
@@ -0,0 +1,10 @@
github.com/ncruces/go-sqlite3 v0.21.3 h1:hHkfNQLcbnxPJZhC/RGw9SwP3bfkv/Y0xUHWsr1CdMQ=
github.com/ncruces/go-sqlite3 v0.21.3/go.mod h1:zxMOaSG5kFYVFK4xQa0pdwIszqxqJ0W0BxBgwdrNjuA=
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4=
github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
+23
View File
@@ -0,0 +1,23 @@
// Package bcw2 embeds SQLite into your application.
//
// Importing package bcw2 initializes the [sqlite3.Binary] variable
// with a build of SQLite that includes the [BEGIN CONCURRENT] and [Wal2] patches:
//
// import _ "github.com/ncruces/go-sqlite3/embed/bcw2"
//
// [BEGIN CONCURRENT]: https://sqlite.org/src/doc/begin-concurrent/doc/begin_concurrent.md
// [Wal2]: https://sqlite.org/cgi/src/doc/wal2/doc/wal2.md
package bcw2
import (
_ "embed"
"github.com/ncruces/go-sqlite3"
)
//go:embed bcw2.wasm
var binary []byte
func init() {
sqlite3.Binary = binary
}
Executable
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail
cd -P -- "$(dirname -- "$0")"
ROOT=../
BINARYEN="$ROOT/tools/binaryen/bin"
WASI_SDK="$ROOT/tools/wasi-sdk/bin"
trap 'rm -f sqlite3.tmp' EXIT
"$WASI_SDK/clang" --target=wasm32-wasi -std=c23 -g0 -O2 \
-Wall -Wextra -Wno-unused-parameter -Wno-unused-function \
-o sqlite3.wasm "$ROOT/sqlite3/main.c" \
-I"$ROOT/sqlite3" \
-mexec-model=reactor \
-msimd128 -mmutable-globals -mmultivalue \
-mbulk-memory -mreference-types \
-mnontrapping-fptoint -msign-ext \
-fno-stack-protector -fno-stack-clash-protection \
-Wl,--stack-first \
-Wl,--import-undefined \
-Wl,--initial-memory=327680 \
-D_HAVE_SQLITE_CONFIG_H \
-DSQLITE_CUSTOM_INCLUDE=sqlite_opt.h \
$(awk '{print "-Wl,--export="$0}' exports.txt)
"$BINARYEN/wasm-ctor-eval" -g -c _initialize sqlite3.wasm -o sqlite3.tmp
"$BINARYEN/wasm-opt" -g --strip --strip-producers -c -O3 \
sqlite3.tmp -o sqlite3.wasm \
--enable-simd --enable-mutable-globals --enable-multivalue \
--enable-bulk-memory --enable-reference-types \
--enable-nontrapping-float-to-int --enable-sign-ext
-8
View File
@@ -1,8 +0,0 @@
// Package embed is no longer needed.
//
// Deprecated: importing github.com/hanzoai/sqlite3/embed is unnecessary.
package embed
func init() {
println("If you're reading this, you're unnecessarily importing github.com/hanzoai/sqlite3/embed.")
}
+139
View File
@@ -0,0 +1,139 @@
aligned_alloc
sqlite3_anycollseq_init
sqlite3_autovacuum_pages_go
sqlite3_backup_finish
sqlite3_backup_init
sqlite3_backup_pagecount
sqlite3_backup_remaining
sqlite3_backup_step
sqlite3_bind_blob_go
sqlite3_bind_double
sqlite3_bind_int64
sqlite3_bind_null
sqlite3_bind_parameter_count
sqlite3_bind_parameter_index
sqlite3_bind_parameter_name
sqlite3_bind_pointer_go
sqlite3_bind_text_go
sqlite3_bind_value
sqlite3_bind_zeroblob64
sqlite3_blob_bytes
sqlite3_blob_close
sqlite3_blob_open
sqlite3_blob_read
sqlite3_blob_reopen
sqlite3_blob_write
sqlite3_busy_handler_go
sqlite3_busy_timeout
sqlite3_changes64
sqlite3_clear_bindings
sqlite3_close
sqlite3_close_v2
sqlite3_collation_needed_go
sqlite3_column_blob
sqlite3_column_bytes
sqlite3_column_count
sqlite3_column_database_name
sqlite3_column_decltype
sqlite3_column_double
sqlite3_column_int64
sqlite3_column_name
sqlite3_column_origin_name
sqlite3_column_table_name
sqlite3_column_text
sqlite3_column_type
sqlite3_column_value
sqlite3_columns_go
sqlite3_commit_hook_go
sqlite3_config_log_go
sqlite3_create_aggregate_function_go
sqlite3_create_collation_go
sqlite3_create_function_go
sqlite3_create_module_go
sqlite3_create_window_function_go
sqlite3_data_count
sqlite3_database_file_object
sqlite3_db_cacheflush
sqlite3_db_config
sqlite3_db_filename
sqlite3_db_name
sqlite3_db_readonly
sqlite3_db_release_memory
sqlite3_db_status
sqlite3_declare_vtab
sqlite3_errcode
sqlite3_errmsg
sqlite3_error_offset
sqlite3_errstr
sqlite3_exec
sqlite3_expanded_sql
sqlite3_file_control
sqlite3_filename_database
sqlite3_filename_journal
sqlite3_filename_wal
sqlite3_finalize
sqlite3_free
sqlite3_get_autocommit
sqlite3_get_auxdata
sqlite3_hard_heap_limit64
sqlite3_interrupt
sqlite3_last_insert_rowid
sqlite3_limit
sqlite3_malloc64
sqlite3_open_v2
sqlite3_overload_function
sqlite3_prepare_v3
sqlite3_progress_handler_go
sqlite3_realloc64
sqlite3_reset
sqlite3_result_blob_go
sqlite3_result_double
sqlite3_result_error
sqlite3_result_error_code
sqlite3_result_error_nomem
sqlite3_result_error_toobig
sqlite3_result_int64
sqlite3_result_null
sqlite3_result_pointer_go
sqlite3_result_text_go
sqlite3_result_value
sqlite3_result_zeroblob64
sqlite3_rollback_hook_go
sqlite3_set_authorizer_go
sqlite3_set_auxdata_go
sqlite3_set_last_insert_rowid
sqlite3_soft_heap_limit64
sqlite3_step
sqlite3_stmt_busy
sqlite3_stmt_readonly
sqlite3_stmt_status
sqlite3_table_column_metadata
sqlite3_total_changes64
sqlite3_trace_go
sqlite3_txn_state
sqlite3_update_hook_go
sqlite3_uri_key
sqlite3_value_blob
sqlite3_value_bytes
sqlite3_value_double
sqlite3_value_dup
sqlite3_value_free
sqlite3_value_frombind
sqlite3_value_int64
sqlite3_value_nochange
sqlite3_value_numeric_type
sqlite3_value_pointer_go
sqlite3_value_text
sqlite3_value_type
sqlite3_vtab_collation
sqlite3_vtab_config_go
sqlite3_vtab_distinct
sqlite3_vtab_in
sqlite3_vtab_in_first
sqlite3_vtab_in_next
sqlite3_vtab_nochange
sqlite3_vtab_on_conflict
sqlite3_vtab_rhs_value
sqlite3_wal_autocheckpoint
sqlite3_wal_checkpoint_v2
sqlite3_wal_hook_go
+20
View File
@@ -0,0 +1,20 @@
// Package embed embeds SQLite into your application.
//
// Importing package embed initializes the [sqlite3.Binary] variable
// with an appropriate build of SQLite:
//
// import _ "github.com/ncruces/go-sqlite3/embed"
package embed
import (
_ "embed"
"github.com/ncruces/go-sqlite3"
)
//go:embed sqlite3.wasm
var binary []byte
func init() {
sqlite3.Binary = binary
}
+25
View File
@@ -0,0 +1,25 @@
package embed
import (
"testing"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Test_init(t *testing.T) {
db, err := driver.Open("file:/test.db?vfs=memdb")
if err != nil {
t.Fatal(err)
}
defer db.Close()
var version string
err = db.QueryRow(`SELECT sqlite_version()`).Scan(&version)
if err != nil {
t.Fatal(err)
}
if version != "3.47.2" {
t.Error(version)
}
}
BIN
View File
Binary file not shown.
+30 -40
View File
@@ -2,19 +2,20 @@ package sqlite3
import (
"errors"
"strconv"
"strings"
"github.com/hanzoai/sqlite3/internal/sqlite3_wrap"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Error wraps an SQLite Error Code.
//
// https://sqlite.org/c3ref/errcode.html
type Error struct {
sys error
str string
msg string
sql string
code res_t
code uint64
}
// Code returns the primary error code for this error.
@@ -28,34 +29,28 @@ func (e *Error) Code() ErrorCode {
//
// https://sqlite.org/rescode.html
func (e *Error) ExtendedCode() ExtendedErrorCode {
return xErrorCode(e.code)
return ExtendedErrorCode(e.code)
}
// Error implements the error interface.
func (e *Error) Error() string {
var b strings.Builder
b.WriteString(sqlite3_wrap.ErrorCodeString(e.code))
b.WriteString("sqlite3: ")
if e.str != "" {
b.WriteString(e.str)
} else {
b.WriteString(strconv.Itoa(int(e.code)))
}
if e.msg != "" {
b.WriteString(": ")
b.WriteString(e.msg)
}
if e.sys != nil {
b.WriteString(": ")
b.WriteString(e.sys.Error())
}
return b.String()
}
// Unwrap returns the underlying operating system error
// that caused the I/O error or failure to open a file.
//
// https://sqlite.org/c3ref/system_errno.html
func (e *Error) Unwrap() error {
return e.sys
}
// Is tests whether this error matches a given [ErrorCode] or [ExtendedErrorCode].
//
// It makes it possible to do:
@@ -88,7 +83,7 @@ func (e *Error) As(err any) bool {
// Temporary returns true for [BUSY] errors.
func (e *Error) Temporary() bool {
return e.Code() == BUSY || e.Code() == INTERRUPT
return e.Code() == BUSY
}
// Timeout returns true for [BUSY_TIMEOUT] errors.
@@ -103,31 +98,22 @@ func (e *Error) SQL() string {
// Error implements the error interface.
func (e ErrorCode) Error() string {
return sqlite3_wrap.ErrorCodeString(e)
}
// As converts this error to an [ExtendedErrorCode].
func (e ErrorCode) As(err any) bool {
c, ok := err.(*xErrorCode)
if ok {
*c = xErrorCode(e)
}
return ok
return util.ErrorCodeString(uint32(e))
}
// Temporary returns true for [BUSY] errors.
func (e ErrorCode) Temporary() bool {
return e == BUSY || e == INTERRUPT
return e == BUSY
}
// ExtendedCode returns the extended error code for this error.
func (e ErrorCode) ExtendedCode() ExtendedErrorCode {
return xErrorCode(e)
return ExtendedErrorCode(e)
}
// Error implements the error interface.
func (e ExtendedErrorCode) Error() string {
return sqlite3_wrap.ErrorCodeString(e)
return util.ErrorCodeString(uint32(e))
}
// Is tests whether this error matches a given [ErrorCode].
@@ -147,7 +133,7 @@ func (e ExtendedErrorCode) As(err any) bool {
// Temporary returns true for [BUSY] errors.
func (e ExtendedErrorCode) Temporary() bool {
return ErrorCode(e) == BUSY || ErrorCode(e) == INTERRUPT
return ErrorCode(e) == BUSY
}
// Timeout returns true for [BUSY_TIMEOUT] errors.
@@ -160,23 +146,27 @@ func (e ExtendedErrorCode) Code() ErrorCode {
return ErrorCode(e)
}
func errorCode(err error, def ErrorCode) (msg string, code res_t) {
func errorCode(err error, def ErrorCode) (msg string, code uint32) {
switch code := err.(type) {
case nil:
return "", _OK
case ErrorCode:
return "", res_t(code)
return "", uint32(code)
case xErrorCode:
return "", res_t(code)
return "", uint32(code)
case *Error:
return code.msg, res_t(code.code)
return code.msg, uint32(code.code)
}
var ecode ErrorCode
var xcode xErrorCode
if errors.As(err, &xcode) {
code = res_t(xcode)
} else {
code = res_t(def)
switch {
case errors.As(err, &xcode):
code = uint32(xcode)
case errors.As(err, &ecode):
code = uint32(ecode)
default:
code = uint32(def)
}
return err.Error(), code
}
+36 -37
View File
@@ -6,13 +6,12 @@ import (
"strings"
"testing"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/internal/sqlite3_wrap"
"github.com/ncruces/go-sqlite3/internal/util"
)
func Test_assertErr(t *testing.T) {
err := errutil.AssertErr()
if s := err.Error(); !strings.HasPrefix(s, "sqlite3: assertion failed") || !strings.HasSuffix(s, "error_test.go:14)") {
err := util.AssertErr()
if s := err.Error(); !strings.HasPrefix(s, "sqlite3: assertion failed") || !strings.HasSuffix(s, "error_test.go:13)") {
t.Errorf("got %q", s)
}
}
@@ -44,7 +43,7 @@ func TestError(t *testing.T) {
if !errors.Is(err, xErrorCode(0x8080)) {
t.Errorf("want true")
}
if s := err.Error(); s != "sqlite3: unknown error" {
if s := err.Error(); s != "sqlite3: 32896" {
t.Errorf("got %q", s)
}
if ok := errors.As(err.ExtendedCode(), &ecode); !ok || ecode != ErrorCode(0x80) {
@@ -60,14 +59,14 @@ func TestError_Temporary(t *testing.T) {
tests := []struct {
name string
code res_t
code uint64
want bool
}{
{"ERROR", res_t(ERROR), false},
{"BUSY", res_t(BUSY), true},
{"BUSY_RECOVERY", res_t(BUSY_RECOVERY), true},
{"BUSY_SNAPSHOT", res_t(BUSY_SNAPSHOT), true},
{"BUSY_TIMEOUT", res_t(BUSY_TIMEOUT), true},
{"ERROR", uint64(ERROR), false},
{"BUSY", uint64(BUSY), true},
{"BUSY_RECOVERY", uint64(BUSY_RECOVERY), true},
{"BUSY_SNAPSHOT", uint64(BUSY_SNAPSHOT), true},
{"BUSY_TIMEOUT", uint64(BUSY_TIMEOUT), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -84,7 +83,7 @@ func TestError_Temporary(t *testing.T) {
}
}
{
err := xErrorCode(tt.code)
err := ExtendedErrorCode(tt.code)
if got := err.Temporary(); got != tt.want {
t.Errorf("ExtendedErrorCode.Temporary(%d) = %v, want %v", tt.code, got, tt.want)
}
@@ -98,14 +97,14 @@ func TestError_Timeout(t *testing.T) {
tests := []struct {
name string
code res_t
code uint64
want bool
}{
{"ERROR", res_t(ERROR), false},
{"BUSY", res_t(BUSY), false},
{"BUSY_RECOVERY", res_t(BUSY_RECOVERY), false},
{"BUSY_SNAPSHOT", res_t(BUSY_SNAPSHOT), false},
{"BUSY_TIMEOUT", res_t(BUSY_TIMEOUT), true},
{"ERROR", uint64(ERROR), false},
{"BUSY", uint64(BUSY), false},
{"BUSY_RECOVERY", uint64(BUSY_RECOVERY), false},
{"BUSY_SNAPSHOT", uint64(BUSY_SNAPSHOT), false},
{"BUSY_TIMEOUT", uint64(BUSY_TIMEOUT), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -116,7 +115,7 @@ func TestError_Timeout(t *testing.T) {
}
}
{
err := xErrorCode(tt.code)
err := ExtendedErrorCode(tt.code)
if got := err.Timeout(); got != tt.want {
t.Errorf("Error.Timeout(%d) = %v, want %v", tt.code, got, tt.want)
}
@@ -128,7 +127,7 @@ func TestError_Timeout(t *testing.T) {
func Test_ErrorCode_Error(t *testing.T) {
t.Parallel()
db, err := OpenContext(testContext(t), ":memory:")
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -137,8 +136,8 @@ func Test_ErrorCode_Error(t *testing.T) {
// Test all error codes.
for i := 0; i == int(ErrorCode(i)); i++ {
want := "sqlite3: "
ptr := ptr_t(db.wrp.Xsqlite3_errstr(int32(i)))
want += db.wrp.ReadString(ptr, _MAX_NAME)
r := db.call("sqlite3_errstr", uint64(i))
want += util.ReadString(db.mod, uint32(r), _MAX_NAME)
got := ErrorCode(i).Error()
if got != want {
@@ -150,19 +149,19 @@ func Test_ErrorCode_Error(t *testing.T) {
func Test_ExtendedErrorCode_Error(t *testing.T) {
t.Parallel()
db, err := OpenContext(testContext(t), ":memory:")
db, err := Open(":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
// Test all extended error codes.
for i := 0; i == int(xErrorCode(i)); i++ {
for i := 0; i == int(ExtendedErrorCode(i)); i++ {
want := "sqlite3: "
ptr := ptr_t(db.wrp.Xsqlite3_errstr(int32(i)))
want += db.wrp.ReadString(ptr, _MAX_NAME)
r := db.call("sqlite3_errstr", uint64(i))
want += util.ReadString(db.mod, uint32(r), _MAX_NAME)
got := xErrorCode(i).Error()
got := ExtendedErrorCode(i).Error()
if got != want {
t.Fatalf("got %q, want %q, with %d", got, want, i)
}
@@ -173,17 +172,17 @@ func Test_errorCode(t *testing.T) {
tests := []struct {
arg error
wantMsg string
wantCode res_t
wantCode uint32
}{
{nil, "", _OK},
{ERROR, "", sqlite3_wrap.ERROR},
{IOERR, "", sqlite3_wrap.IOERR},
{IOERR_READ, "", sqlite3_wrap.IOERR_READ},
{&Error{code: sqlite3_wrap.ERROR}, "", sqlite3_wrap.ERROR},
{fmt.Errorf("%w", ERROR), ERROR.Error(), sqlite3_wrap.ERROR},
{fmt.Errorf("%w", IOERR), IOERR.Error(), sqlite3_wrap.IOERR},
{fmt.Errorf("%w", IOERR_READ), IOERR_READ.Error(), sqlite3_wrap.IOERR_READ},
{fmt.Errorf("error"), "error", sqlite3_wrap.ERROR},
{ERROR, "", util.ERROR},
{IOERR, "", util.IOERR},
{IOERR_READ, "", util.IOERR_READ},
{&Error{code: util.ERROR}, "", util.ERROR},
{fmt.Errorf("%w", ERROR), ERROR.Error(), util.ERROR},
{fmt.Errorf("%w", IOERR), IOERR.Error(), util.IOERR},
{fmt.Errorf("%w", IOERR_READ), IOERR_READ.Error(), util.IOERR_READ},
{fmt.Errorf("error"), "error", util.ERROR},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
@@ -191,7 +190,7 @@ func Test_errorCode(t *testing.T) {
if gotMsg != tt.wantMsg {
t.Errorf("errorCode() gotMsg = %q, want %q", gotMsg, tt.wantMsg)
}
if gotCode != tt.wantCode {
if gotCode != uint32(tt.wantCode) {
t.Errorf("errorCode() gotCode = %d, want %d", gotCode, tt.wantCode)
}
})
+2 -1
View File
@@ -4,7 +4,8 @@ import (
"fmt"
"log"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
)
const memory = ":memory:"
-138
View File
@@ -1,138 +0,0 @@
package sqlite3
import (
"bytes"
"encoding/base64"
"sync"
"github.com/hanzoai/sqlite3/internal/errutil"
)
var (
// +checklocks:extRegistryMtx
extRegistry []func(*Conn) error
extRegistryMtx sync.RWMutex
)
// AutoExtension causes the entryPoint function to be invoked
// for each new database connection that is created.
//
// https://sqlite.org/c3ref/auto_extension.html
func AutoExtension(entryPoint func(*Conn) error) {
extRegistryMtx.Lock()
extRegistry = append(extRegistry, entryPoint)
extRegistryMtx.Unlock()
}
func initExtensions(c *Conn) error {
c.base64()
extRegistryMtx.RLock()
defer extRegistryMtx.RUnlock()
for _, f := range extRegistry {
if err := f(c); err != nil {
return err
}
}
return nil
}
func (c *Conn) base64() error {
return c.CreateFunction("base64", 1, DETERMINISTIC, func(ctx Context, arg ...Value) {
switch a := arg[0]; a.Type() {
case NULL:
case BLOB:
data := a.RawBlob()
code := base64.StdEncoding
size := int64(code.EncodedLen(len(data)))
if size > _MAX_LENGTH {
ctx.ResultError(TOOBIG)
return
}
ptr := c.wrp.New(size)
if size > 0 {
code.Encode(c.wrp.Bytes(ptr, size), data)
}
ctx.c.wrp.Xsqlite3_result_text_go(int32(ctx.handle), int32(ptr), size)
case TEXT:
data := a.RawText()
data = bytes.Trim(data, " \t\n\v\f\r")
data = bytes.TrimRight(data, "=")
code := base64.RawStdEncoding
size := int64(code.DecodedLen(len(data)))
if size > _MAX_LENGTH {
ctx.ResultError(TOOBIG)
return
}
ptr := c.wrp.New(size)
if size > 0 {
n, _ := code.Decode(c.wrp.Bytes(ptr, size), data)
size = int64(n)
}
ctx.c.wrp.Xsqlite3_result_blob_go(int32(ctx.handle), int32(ptr), size)
default:
ctx.ResultError(errutil.ErrorString("base64: accepts only blob or text"))
}
})
}
// ExtensionLibrary represents a dynamically linked SQLite extension.
type ExtensionLibrary interface {
Xsqlite3_extension_init(db, _, _ int32) int32
}
// ExtensionInfo returns values needed to load a dynamically linked SQLite extension.
type ExtensionInfo func() (memorySize, memoryAlignment, tableSize, tableAlignment int64)
type extEnv struct {
*env
memoryBase int32
tableBase int32
}
func (e *extEnv) X__memory_base() *int32 { return &e.memoryBase }
func (e *extEnv) X__table_base() *int32 { return &e.tableBase }
// ExtensionInit loads an SQLite extension library.
//
// https://sqlite.org/loadext.html
func ExtensionInit[Env any, Mod ExtensionLibrary](db *Conn, init func(env Env) Mod, info ExtensionInfo) error {
memSize, memAlign, tableSize, tableAlign := info()
var memBase int32
if memSize > 0 {
memBase = db.wrp.Xaligned_alloc(int32(memAlign), int32(memSize))
if memBase == 0 {
panic(errutil.OOMErr)
}
}
var tableBase int
if tableSize > 0 {
// Round up to the alignment.
rnd := int(tableAlign) - 1
tab := db.wrp.X__indirect_function_table()
tableBase = (len(*tab) + rnd) &^ rnd
if add := tableBase + int(tableSize) - len(*tab); add > 0 {
*tab = append(*tab, make([]any, add)...)
}
}
e := &extEnv{
env: &env{db.wrp},
memoryBase: memBase,
tableBase: int32(tableBase),
}
mod := init(any(e).(Env))
if opt, ok := any(mod).(interface{ X__wasm_apply_data_relocs() }); ok {
opt.X__wasm_apply_data_relocs()
}
if opt, ok := any(mod).(interface{ X__wasm_call_ctors() }); ok {
opt.X__wasm_call_ctors()
}
rc := mod.Xsqlite3_extension_init(int32(db.handle), 0, 0)
return db.error(res_t(rc))
}
+3 -24
View File
@@ -17,42 +17,21 @@ you can load into your database connections.
reads [comma-separated values](https://sqlite.org/csv.html).
- [`github.com/ncruces/go-sqlite3/ext/fileio`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/fileio)
reads, writes and lists files.
- [`github.com/ncruces/go-sqlite3/ext/fts5`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/fts5)
provides [full-text search](https://sqlite.org/fts5.html).
- [`github.com/ncruces/go-sqlite3/ext/hash`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/hash)
provides cryptographic hash functions.
- [`github.com/ncruces/go-sqlite3/ext/ipaddr`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/ipaddr)
provides functions to manipulate IPs and CIDRs.
- [`github.com/ncruces/go-sqlite3/ext/lines`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/lines)
reads data [line-by-line](https://github.com/asg017/sqlite-lines).
- [`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/regexp`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/regexp)
provides [regular expression](https://github.com/nalgeon/sqlean/blob/main/docs/regexp.md) functions.
- [`github.com/ncruces/go-sqlite3/ext/rtree`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/rtree)
provides [multi-dimensional](https://sqlite.org/rtree.html) and [geospatial](https://sqlite.org/geopoly.html) indexes.
- [`github.com/ncruces/go-sqlite3/ext/serdes`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/serdes)
(de)serializes databases.
- [`github.com/ncruces/go-sqlite3/ext/spellfix1`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/spellfix1)
searches a vocabulary for [close matches](https://sqlite.org/spellfix1.html).
provides regular expression functions.
- [`github.com/ncruces/go-sqlite3/ext/statement`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/statement)
creates [parameterized views](https://github.com/0x09/sqlite-statement-vtab).
- [`github.com/ncruces/go-sqlite3/ext/stats`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/stats)
provides [statistics](https://oreilly.com/library/view/sql-in-a/9780596155322/ch04s02.html) functions.
provides [statistics](https://www.oreilly.com/library/view/sql-in-a/9780596155322/ch04s02.html) functions.
- [`github.com/ncruces/go-sqlite3/ext/unicode`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/unicode)
provides [Unicode aware](https://sqlite.org/src/dir/ext/icu) functions.
- [`github.com/ncruces/go-sqlite3/ext/uuid`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/uuid)
generates [UUIDs](https://en.wikipedia.org/wiki/Universally_unique_identifier).
- [`github.com/ncruces/go-sqlite3/ext/vec1`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/vec1)
provides approximate nearest-neighbor [vector search](https://sqlite.org/vec1/doc/trunk/doc/vec1.md).
- [`github.com/ncruces/go-sqlite3/ext/zorder`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/zorder)
maps multidimensional data to one dimension.
### Packages
These packages may also be useful to work with SQLite:
- [`github.com/ncruces/decimal`](https://pkg.go.dev/github.com/ncruces/decimal)
decimal arithmetic.
- [`github.com/ncruces/julianday`](https://pkg.go.dev/github.com/ncruces/julianday)
Julian day math.
maps multidimensional data to one dimension.
+12 -30
View File
@@ -7,8 +7,8 @@ import (
"fmt"
"reflect"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/util"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Register registers the array single-argument, table-valued SQL function.
@@ -45,14 +45,12 @@ func (array) Open() (sqlite3.VTabCursor, error) {
}
type cursor struct {
value reflect.Value
slice any
rowID int
length int
array reflect.Value
rowID int
}
func (c *cursor) EOF() bool {
return c.rowID >= c.length
return c.rowID >= c.array.Len()
}
func (c *cursor) Next() error {
@@ -61,8 +59,7 @@ func (c *cursor) Next() error {
}
func (c *cursor) RowID() (int64, error) {
// One-based RowID for consistency with carray and other tables.
return int64(c.rowID) + 1, nil
return int64(c.rowID), nil
}
func (c *cursor) Column(ctx sqlite3.Context, n int) error {
@@ -70,22 +67,7 @@ func (c *cursor) Column(ctx sqlite3.Context, n int) error {
return nil
}
switch arr := c.slice.(type) {
case []int:
ctx.ResultInt(arr[c.rowID])
return nil
case []int64:
ctx.ResultInt64(arr[c.rowID])
return nil
case []float64:
ctx.ResultFloat(arr[c.rowID])
return nil
case []string:
ctx.ResultText(arr[c.rowID])
return nil
}
v := c.value.Index(c.rowID)
v := c.array.Index(c.rowID)
k := v.Kind()
if k == reflect.Interface {
@@ -127,14 +109,14 @@ func (c *cursor) Column(ctx sqlite3.Context, n int) error {
return nil
}
func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) (err error) {
c.slice = arg[0].Pointer()
c.value = reflect.ValueOf(c.slice)
c.value, err = indexable(c.value)
func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
array := reflect.ValueOf(arg[0].Pointer())
array, err := indexable(array)
if err != nil {
return err
}
c.length = c.value.Len()
c.array = array
c.rowID = 0
return nil
}
+11 -18
View File
@@ -1,27 +1,22 @@
package array_test
import (
"errors"
"fmt"
"log"
"math"
"reflect"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/array"
"github.com/hanzoai/sqlite3/ext/rtree"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/array"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Example_driver() {
db, err := driver.Open("file:/test.db?vfs=memdb", func(c *sqlite3.Conn) error {
return errors.Join(
array.Register(c),
rtree.Register(c))
})
db, err := driver.Open("file:/test.db?vfs=memdb", array.Register)
if err != nil {
log.Fatal(err)
}
@@ -57,7 +52,6 @@ func Example_driver() {
func Example() {
sqlite3.AutoExtension(array.Register)
sqlite3.AutoExtension(rtree.Register)
db, err := sqlite3.Open(":memory:")
if err != nil {
@@ -94,16 +88,15 @@ func Example() {
func Test_cursor_Column(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, array.Register)
db, err := driver.Open(tmp, array.Register)
if err != nil {
t.Fatal(err)
}
defer db.Close()
rows, err := db.QueryContext(ctx, `
rows, err := db.Query(`
SELECT rowid, value FROM array(?)`,
sqlite3.Pointer(&[...]any{nil, true, 1, uint(2), math.Pi, "text", []byte{1, 2, 3}}))
if err != nil {
@@ -136,7 +129,7 @@ func Test_cursor_Column(t *testing.T) {
func Test_array_errors(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
+3 -3
View File
@@ -5,8 +5,8 @@ import (
"errors"
"io"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Register registers the SQL functions:
@@ -111,7 +111,7 @@ func writeblob(ctx sqlite3.Context, arg ...sqlite3.Value) {
func openblob(ctx sqlite3.Context, arg ...sqlite3.Value) {
if len(arg) < 6 {
ctx.ResultError(errutil.ErrorString("openblob: wrong number of arguments"))
ctx.ResultError(util.ErrorString("openblob: wrong number of arguments"))
return
}
+28 -31
View File
@@ -1,20 +1,20 @@
package blobio_test
import (
"database/sql"
"io"
"log"
"os"
"slices"
"reflect"
"strings"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/array"
"github.com/hanzoai/sqlite3/ext/blobio"
"github.com/hanzoai/sqlite3/internal/testcfg"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/array"
"github.com/ncruces/go-sqlite3/ext/blobio"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Example() {
@@ -34,8 +34,7 @@ func Example() {
const message = "Hello BLOB!"
// Create the BLOB.
r, err := db.Exec(`INSERT INTO test VALUES (:data)`,
sql.Named("data", sqlite3.ZeroBlob(len(message))))
r, err := db.Exec(`INSERT INTO test VALUES (?)`, sqlite3.ZeroBlob(len(message)))
if err != nil {
log.Fatal(err)
}
@@ -46,19 +45,15 @@ func Example() {
}
// Write the BLOB.
_, err = db.Exec(`SELECT writeblob('main', 'test', 'col', :rowid, :offset, :message)`,
sql.Named("rowid", id),
sql.Named("offset", 0),
sql.Named("message", message))
_, err = db.Exec(`SELECT writeblob('main', 'test', 'col', ?, 0, ?)`,
id, message)
if err != nil {
log.Fatal(err)
}
// Read the BLOB.
_, err = db.Exec(`SELECT readblob('main', 'test', 'col', :rowid, :offset, :writer)`,
sql.Named("rowid", id),
sql.Named("offset", 0),
sql.Named("writer", sqlite3.Pointer(os.Stdout)))
_, err = db.Exec(`SELECT readblob('main', 'test', 'col', ?, 0, ?)`,
id, sqlite3.Pointer(os.Stdout))
if err != nil {
log.Fatal(err)
}
@@ -69,13 +64,13 @@ func Example() {
func TestMain(m *testing.M) {
sqlite3.AutoExtension(blobio.Register)
sqlite3.AutoExtension(array.Register)
os.Exit(m.Run())
m.Run()
}
func Test_readblob(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -143,16 +138,18 @@ func Test_readblob(t *testing.T) {
}
}
if !stmt.Step() {
t.Fatal(stmt.Err())
} else if got := stmt.ColumnText(0); got != tt.want1 {
t.Errorf("got %q", got)
if stmt.Step() {
got := stmt.ColumnText(0)
if got != tt.want1 {
t.Errorf("got %q", got)
}
}
if !stmt.Step() {
t.Fatal(stmt.Err())
} else if got := stmt.ColumnText(0); got != tt.want2 {
t.Errorf("got %q", got)
if stmt.Step() {
got := stmt.ColumnText(0)
if got != tt.want2 {
t.Errorf("got %q", got)
}
}
err = stmt.Err()
@@ -166,7 +163,7 @@ func Test_readblob(t *testing.T) {
func Test_writeblob(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -221,7 +218,7 @@ func Test_writeblob(t *testing.T) {
func Test_openblob(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -281,7 +278,7 @@ func Test_openblob(t *testing.T) {
}
want := []string{"\xca\xfe", "\xba\xbe"}
if !slices.Equal(got, want) {
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
+25 -26
View File
@@ -14,9 +14,8 @@ import (
"github.com/dchest/siphash"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/util/sql3util"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Register registers the bloom_filter virtual table:
@@ -35,8 +34,6 @@ type bloom struct {
hashes int
}
const vtab = `CREATE TABLE x(present, word TEXT HIDDEN NOT NULL PRIMARY KEY) WITHOUT ROWID`
func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom, err error) {
b := bloom{
db: db,
@@ -51,17 +48,19 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
return nil, err
}
if nelem <= 0 {
return nil, errutil.ErrorString("bloom: number of elements in filter must be positive")
return nil, util.ErrorString("bloom: number of elements in filter must be positive")
}
} else {
nelem = 100
}
if len(arg) > 1 {
var ok bool
b.prob, ok = sql3util.ParseFloat(arg[1])
if !ok || b.prob <= 0 || b.prob >= 1 {
return nil, errutil.ErrorString("bloom: probability must be in the range (0,1)")
b.prob, err = strconv.ParseFloat(arg[1], 64)
if err != nil {
return nil, err
}
if b.prob <= 0 || b.prob >= 1 {
return nil, util.ErrorString("bloom: probability must be in the range (0,1)")
}
} else {
b.prob = 0.01
@@ -73,7 +72,7 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
return nil, err
}
if b.hashes <= 0 {
return nil, errutil.ErrorString("bloom: number of hash functions must be positive")
return nil, util.ErrorString("bloom: number of hash functions must be positive")
}
} else {
b.hashes = max(1, numHashes(b.prob))
@@ -81,7 +80,8 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
b.bytes = numBytes(nelem, b.prob)
err = db.DeclareVTab(vtab)
err = db.DeclareVTab(
`CREATE TABLE x(present, word HIDDEN NOT NULL PRIMARY KEY) WITHOUT ROWID`)
if err != nil {
return nil, err
}
@@ -115,15 +115,15 @@ func connect(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom
storage: table + "_storage",
}
err = db.DeclareVTab(vtab)
err = db.DeclareVTab(
`CREATE TABLE x(present, word HIDDEN NOT NULL PRIMARY KEY) WITHOUT ROWID`)
if err != nil {
return nil, err
}
load, _, err := db.PrepareFlags(fmt.Sprintf(
load, _, err := db.Prepare(fmt.Sprintf(
`SELECT m/8, p, k FROM %s.%s WHERE rowid = 1`,
sqlite3.QuoteIdentifier(b.schema), sqlite3.QuoteIdentifier(b.storage)),
sqlite3.PREPARE_DONT_LOG)
sqlite3.QuoteIdentifier(b.schema), sqlite3.QuoteIdentifier(b.storage)))
if err != nil {
return nil, err
}
@@ -166,16 +166,15 @@ func (t *bloom) ShadowTables() {
}
func (t *bloom) Integrity(schema, table string, flags int) error {
load, _, err := t.db.PrepareFlags(fmt.Sprintf(
load, _, err := t.db.Prepare(fmt.Sprintf(
`SELECT typeof(data), length(data), p, n, m, k FROM %s.%s WHERE rowid = 1`,
sqlite3.QuoteIdentifier(t.schema), sqlite3.QuoteIdentifier(t.storage)),
sqlite3.PREPARE_DONT_LOG)
sqlite3.QuoteIdentifier(t.schema), sqlite3.QuoteIdentifier(t.storage)))
if err != nil {
return fmt.Errorf("bloom: %v", err) // can't wrap!
}
defer load.Close()
err = errutil.ErrorString("bloom: invalid parameters")
err = util.ErrorString("bloom: invalid parameters")
if !load.Step() {
return err
}
@@ -217,9 +216,9 @@ func (b *bloom) BestIndex(idx *sqlite3.IndexInfo) error {
func (b *bloom) Update(arg ...sqlite3.Value) (rowid int64, err error) {
if arg[0].Type() != sqlite3.NULL {
if len(arg) == 1 {
return 0, errutil.ErrorString("bloom: elements cannot be deleted")
return 0, util.ErrorString("bloom: elements cannot be deleted")
}
return 0, errutil.ErrorString("bloom: elements cannot be updated")
return 0, util.ErrorString("bloom: elements cannot be updated")
}
if arg[2].NoChange() {
@@ -233,7 +232,7 @@ func (b *bloom) Update(arg ...sqlite3.Value) (rowid int64, err error) {
}
defer f.Close()
for n := range b.hashes {
for n := 0; n < b.hashes; n++ {
hash := calcHash(n, blob)
hash %= uint64(b.bytes * 8)
bitpos := byte(hash % 8)
@@ -269,13 +268,13 @@ func (b *bloom) Open() (sqlite3.VTabCursor, error) {
type cursor struct {
*bloom
arg sqlite3.Value
arg *sqlite3.Value
eof bool
}
func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
c.eof = false
c.arg = arg[0]
c.arg = &arg[0]
blob := arg[0].RawBlob()
f, err := c.db.OpenBlob(c.schema, c.storage, "data", 1, false)
@@ -313,7 +312,7 @@ func (c *cursor) Column(ctx sqlite3.Context, n int) error {
case 0:
ctx.ResultBool(true)
case 1:
ctx.ResultValue(c.arg)
ctx.ResultValue(*c.arg)
}
return nil
}
+8 -7
View File
@@ -6,20 +6,21 @@ import (
"path/filepath"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/ext/bloom"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/bloom"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
)
func TestMain(m *testing.M) {
sqlite3.AutoExtension(bloom.Register)
os.Exit(m.Run())
m.Run()
}
func TestRegister(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -101,7 +102,7 @@ func Test_compatible(t *testing.T) {
t.Fatal(err)
}
db, err := sqlite3.OpenContext(testcfg.Context(t), "file:"+filepath.ToSlash(tmp)+"?nolock=1")
db, err := sqlite3.Open("file:" + filepath.ToSlash(tmp) + "?nolock=1")
if err != nil {
t.Fatal(err)
}
@@ -157,7 +158,7 @@ func Test_compatible(t *testing.T) {
func Test_errors(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
+8 -13
View File
@@ -10,9 +10,9 @@ import (
"fmt"
"math"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/util"
"github.com/hanzoai/sqlite3/util/sql3util"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/util/sql3util"
)
const (
@@ -56,7 +56,7 @@ func Register(db *sqlite3.Conn) error {
done.Add(key)
}
err := db.DeclareVTab(`CREATE TABLE x(id INT,depth INT,root HIDDEN,tablename TEXT HIDDEN,idcolumn TEXT HIDDEN,parentcolumn TEXT HIDDEN)`)
err := db.DeclareVTab(`CREATE TABLE x(id,depth,root HIDDEN,tablename HIDDEN,idcolumn HIDDEN,parentcolumn HIDDEN)`)
if err != nil {
return nil, err
}
@@ -154,7 +154,6 @@ func (c *closure) BestIndex(idx *sqlite3.IndexInfo) error {
return sqlite3.CONSTRAINT
}
idx.IdxFlags = sqlite3.INDEX_SCAN_HEX
idx.EstimatedCost = cost
idx.IdxNum = plan
return nil
@@ -202,7 +201,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
sqlite3.QuoteIdentifier(column),
sqlite3.QuoteIdentifier(parent),
)
stmt, _, err := c.db.PrepareFlags(sql, sqlite3.PREPARE_DONT_LOG)
stmt, _, err := c.db.Prepare(sql)
if err != nil {
return err
}
@@ -211,14 +210,12 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
c.nodes = []node{{root, 0}}
set := util.Set[int64]{}
set.Add(root)
for i := range c.nodes {
for i := 0; i < len(c.nodes); i++ {
curr := c.nodes[i]
if curr.depth >= maxDepth {
continue
}
if err := stmt.BindInt64(1, curr.id); err != nil {
return err
}
stmt.BindInt64(1, curr.id)
for stmt.Step() {
if stmt.ColumnType(0) == sqlite3.INTEGER {
next := stmt.ColumnInt64(0)
@@ -228,9 +225,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
}
}
}
if err := stmt.Reset(); err != nil {
return err
}
stmt.Reset()
}
return nil
}
+7 -7
View File
@@ -4,17 +4,17 @@ import (
_ "embed"
"fmt"
"log"
"os"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/ext/closure"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/closure"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
)
func TestMain(m *testing.M) {
sqlite3.AutoExtension(closure.Register)
os.Exit(m.Run())
m.Run()
}
func Example() {
@@ -88,7 +88,7 @@ func Example() {
func TestRegister(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -156,7 +156,7 @@ func TestRegister(t *testing.T) {
func Test_errors(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"fmt"
"strconv"
"github.com/hanzoai/sqlite3/util/sql3util"
"github.com/ncruces/go-sqlite3/util/sql3util"
)
func uintArg(key, val string) (int, error) {
+1 -1
View File
@@ -3,7 +3,7 @@ package csv
import (
"testing"
"github.com/hanzoai/sqlite3/util/sql3util"
"github.com/ncruces/go-sqlite3/util/sql3util"
)
func Test_uintArg(t *testing.T) {
+29 -27
View File
@@ -15,11 +15,10 @@ import (
"strconv"
"strings"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/internal/util"
"github.com/hanzoai/sqlite3/util/osutil"
"github.com/hanzoai/sqlite3/util/sql3util"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/util/osutil"
"github.com/ncruces/go-sqlite3/util/sql3util"
)
// Register registers the CSV virtual table.
@@ -31,7 +30,7 @@ func Register(db *sqlite3.Conn) error {
// RegisterFS registers the CSV virtual table.
// If a filename is specified, fsys is used to open the file.
func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
declare := func(db *sqlite3.Conn, _, _, _ string, arg ...string) (_ *table, err error) {
declare := func(db *sqlite3.Conn, _, _, _ string, arg ...string) (res *table, err error) {
var (
filename string
data string
@@ -74,7 +73,7 @@ func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
}
if (filename == "") == (data == "") {
return nil, errutil.ErrorString(`csv: must specify either "filename" or "data" but not both`)
return nil, util.ErrorString(`csv: must specify either "filename" or "data" but not both`)
}
t := &table{
@@ -86,8 +85,7 @@ func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
header: header,
}
hadSchema := schema != ""
if !hadSchema {
if schema == "" {
var row []string
if header || columns < 0 {
csv, c, err := t.newReader()
@@ -101,14 +99,17 @@ func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
}
}
schema = getSchema(header, columns, row)
} else {
t.typs, err = getColumnAffinities(schema)
if err != nil {
return nil, err
}
}
err = db.DeclareVTab(schema)
if err == nil {
err = db.VTabConfig(sqlite3.VTAB_DIRECTONLY)
}
if err == nil && hadSchema {
t.typs, err = getColumnAffinities(schema)
}
if err != nil {
return nil, err
}
@@ -122,7 +123,7 @@ type table struct {
fsys fs.FS
name string
data string
typs []sql3util.Affinity
typs []affinity
comma rune
comment rune
header bool
@@ -213,10 +214,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
return err
}
if c.table.header {
err = c.Next() // skip header
if err != nil {
return err
}
c.Next() // skip header
}
c.rowID = 0
return c.Next()
@@ -241,27 +239,31 @@ func (c *cursor) RowID() (int64, error) {
func (c *cursor) Column(ctx sqlite3.Context, col int) error {
if col < len(c.row) {
typ := sql3util.TEXT
typ := text
if col < len(c.table.typs) {
typ = c.table.typs[col]
}
txt := c.row[col]
if txt == "" && typ != sql3util.TEXT {
if txt == "" && typ != text {
return nil
}
switch typ {
case sql3util.NUMERIC, sql3util.INTEGER:
if i, err := strconv.ParseInt(txt, 10, 64); err == nil {
ctx.ResultInt64(i)
return nil
case numeric, integer:
if strings.TrimLeft(txt, "+-0123456789") == "" {
if i, err := strconv.ParseInt(txt, 10, 64); err == nil {
ctx.ResultInt64(i)
return nil
}
}
fallthrough
case sql3util.REAL:
if f, ok := sql3util.ParseFloat(txt); ok {
ctx.ResultFloat(f)
return nil
case real:
if strings.TrimLeft(txt, "+-.0123456789Ee") == "" {
if f, err := strconv.ParseFloat(txt, 64); err == nil {
ctx.ResultFloat(f)
return nil
}
}
fallthrough
default:
+20 -21
View File
@@ -3,12 +3,12 @@ package csv_test
import (
"fmt"
"log"
"os"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/ext/csv"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/csv"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
)
func Example() {
@@ -56,13 +56,13 @@ func Example() {
func TestMain(m *testing.M) {
sqlite3.AutoExtension(csv.Register)
os.Exit(m.Run())
m.Run()
}
func TestRegister(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -124,7 +124,7 @@ Robert "Griesemer" "gri"`
func TestAffinity(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -146,28 +146,27 @@ func TestAffinity(t *testing.T) {
}
defer stmt.Close()
if !stmt.Step() {
t.Fatal(stmt.Err())
} else if got := stmt.ColumnText(0); got != "1" {
t.Errorf("got %q want 1", got)
if stmt.Step() {
if got := stmt.ColumnText(0); got != "1" {
t.Errorf("got %q want 1", got)
}
}
if !stmt.Step() {
t.Fatal(stmt.Err())
} else if got := stmt.ColumnText(0); got != "0.1" {
t.Errorf("got %q want 0.1", got)
if stmt.Step() {
if got := stmt.ColumnText(0); got != "0.1" {
t.Errorf("got %q want 0.1", got)
}
}
if !stmt.Step() {
t.Fatal(stmt.Err())
} else if got := stmt.ColumnText(0); got != "e" {
t.Errorf("got %q want e", got)
if stmt.Step() {
if got := stmt.ColumnText(0); got != "e" {
t.Errorf("got %q want e", got)
}
}
}
func TestRegister_errors(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
+14 -14
View File
@@ -4,36 +4,36 @@ import (
"strconv"
"strings"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
)
func getSchema(header bool, columns int, row []string) string {
var sep string
var buf strings.Builder
buf.WriteString("CREATE TABLE x(")
var str strings.Builder
str.WriteString("CREATE TABLE x(")
if 0 <= columns && columns < len(row) {
row = row[:columns]
}
for i, f := range row {
buf.WriteString(sep)
str.WriteString(sep)
if header && f != "" {
buf.WriteString(sqlite3.QuoteIdentifier(f))
str.WriteString(sqlite3.QuoteIdentifier(f))
} else {
buf.WriteString("c")
buf.WriteString(strconv.Itoa(i + 1))
str.WriteString("c")
str.WriteString(strconv.Itoa(i + 1))
}
buf.WriteString(" TEXT")
str.WriteString(" TEXT")
sep = ","
}
for i := len(row); i < columns; i++ {
buf.WriteString(sep)
buf.WriteString("c")
buf.WriteString(strconv.Itoa(i + 1))
buf.WriteString(" TEXT")
str.WriteString(sep)
str.WriteString("c")
str.WriteString(strconv.Itoa(i + 1))
str.WriteString(" TEXT")
sep = ","
}
buf.WriteByte(')')
str.WriteByte(')')
return buf.String()
return str.String()
}
+39 -4
View File
@@ -1,17 +1,52 @@
package csv
import "github.com/hanzoai/sqlite3/util/sql3util"
import (
"strings"
func getColumnAffinities(schema string) ([]sql3util.Affinity, error) {
"github.com/ncruces/go-sqlite3/util/sql3util"
)
type affinity byte
const (
blob affinity = 0
text affinity = 1
numeric affinity = 2
integer affinity = 3
real affinity = 4
)
func getColumnAffinities(schema string) ([]affinity, error) {
tab, err := sql3util.ParseTable(schema)
if err != nil {
return nil, err
}
columns := tab.Columns
types := make([]sql3util.Affinity, len(columns))
types := make([]affinity, len(columns))
for i, col := range columns {
types[i] = sql3util.GetAffinity(col.Type)
types[i] = getAffinity(col.Type)
}
return types, nil
}
func getAffinity(declType string) affinity {
// https://sqlite.org/datatype3.html#determination_of_column_affinity
if declType == "" {
return blob
}
name := strings.ToUpper(declType)
if strings.Contains(name, "INT") {
return integer
}
if strings.Contains(name, "CHAR") || strings.Contains(name, "CLOB") || strings.Contains(name, "TEXT") {
return text
}
if strings.Contains(name, "BLOB") {
return blob
}
if strings.Contains(name, "REAL") || strings.Contains(name, "FLOA") || strings.Contains(name, "DOUB") {
return real
}
return numeric
}
+32
View File
@@ -0,0 +1,32 @@
package csv
import "testing"
func Test_getAffinity(t *testing.T) {
tests := []struct {
decl string
want affinity
}{
{"", blob},
{"INTEGER", integer},
{"TINYINT", integer},
{"TEXT", text},
{"CHAR", text},
{"CLOB", text},
{"BLOB", blob},
{"REAL", real},
{"FLOAT", real},
{"DOUBLE", real},
{"NUMERIC", numeric},
{"DECIMAL", numeric},
{"BOOLEAN", numeric},
{"DATETIME", numeric},
}
for _, tt := range tests {
t.Run(tt.decl, func(t *testing.T) {
if got := getAffinity(tt.decl); got != tt.want {
t.Errorf("getAffinity() = %v, want %v", got, tt.want)
}
})
}
}
+70
View File
@@ -0,0 +1,70 @@
//go:build !go1.23
package fileio
import (
"fmt"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Adapted from: https://research.swtch.com/coro
const errCoroCanceled = util.ErrorString("coroutine canceled")
func coroNew[In, Out any](f func(In, func(Out) In) Out) (resume func(In) (Out, bool), cancel func()) {
type msg[T any] struct {
panic any
val T
}
cin := make(chan msg[In])
cout := make(chan msg[Out])
running := true
resume = func(in In) (out Out, ok bool) {
if !running {
return
}
cin <- msg[In]{val: in}
m := <-cout
if m.panic != nil {
panic(m.panic)
}
return m.val, running
}
cancel = func() {
if !running {
return
}
e := fmt.Errorf("%w", errCoroCanceled)
cin <- msg[In]{panic: e}
m := <-cout
if m.panic != nil && m.panic != e {
panic(m.panic)
}
}
yield := func(out Out) In {
cout <- msg[Out]{val: out}
m := <-cin
if m.panic != nil {
panic(m.panic)
}
return m.val
}
go func() {
defer func() {
if running {
running = false
cout <- msg[Out]{panic: recover()}
}
}()
var out Out
m := <-cin
if m.panic == nil {
out = f(m.val, yield)
}
running = false
cout <- msg[Out]{val: out}
}()
return resume, cancel
}
+4 -4
View File
@@ -9,7 +9,7 @@ import (
"io/fs"
"os"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
)
// Register registers SQL functions readfile, writefile, lsmode,
@@ -18,7 +18,7 @@ func Register(db *sqlite3.Conn) error {
return RegisterFS(db, nil)
}
// RegisterFS registers SQL functions readfile, lsmode,
// Register registers SQL functions readfile, lsmode,
// and the table-valued function fsdir;
// fsys will be used to read files and list directories.
func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
@@ -30,7 +30,7 @@ func RegisterFS(db *sqlite3.Conn, fsys fs.FS) error {
db.CreateFunction("readfile", 1, sqlite3.DIRECTONLY, readfile(fsys)),
db.CreateFunction("lsmode", 1, sqlite3.DETERMINISTIC, lsmode),
sqlite3.CreateModule(db, "fsdir", nil, func(db *sqlite3.Conn, _, _, _ string, _ ...string) (fsdir, error) {
err := db.DeclareVTab(`CREATE TABLE x(name TEXT,mode INT,mtime TIMESTAMP,data BLOB,level INT,path HIDDEN,dir HIDDEN)`)
err := db.DeclareVTab(`CREATE TABLE x(name,mode,mtime TIMESTAMP,data,path HIDDEN,dir HIDDEN)`)
if err == nil {
err = db.VTabConfig(sqlite3.VTAB_DIRECTONLY)
}
@@ -42,7 +42,7 @@ func lsmode(ctx sqlite3.Context, arg ...sqlite3.Value) {
ctx.ResultText(fs.FileMode(arg[0].Int()).String())
}
func readfile(fsys fs.FS) sqlite3.ScalarFunction {
func readfile(fsys fs.FS) func(ctx sqlite3.Context, arg ...sqlite3.Value) {
return func(ctx sqlite3.Context, arg ...sqlite3.Value) {
var err error
var data []byte
+12 -13
View File
@@ -7,19 +7,19 @@ import (
"os"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/fileio"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/fileio"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Test_lsmode(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, fileio.Register)
db, err := driver.Open(tmp, fileio.Register)
if err != nil {
t.Fatal(err)
}
@@ -36,7 +36,7 @@ func Test_lsmode(t *testing.T) {
}
var mode string
err = db.QueryRowContext(ctx, `SELECT lsmode(?)`, s.Mode()).Scan(&mode)
err = db.QueryRow(`SELECT lsmode(?)`, s.Mode()).Scan(&mode)
if err != nil {
t.Fatal(err)
}
@@ -53,10 +53,9 @@ func Test_readfile(t *testing.T) {
for _, fsys := range []fs.FS{nil, os.DirFS(".")} {
t.Run("", func(t *testing.T) {
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, func(c *sqlite3.Conn) error {
db, err := driver.Open(tmp, func(c *sqlite3.Conn) error {
fileio.RegisterFS(c, fsys)
return nil
})
@@ -65,7 +64,7 @@ func Test_readfile(t *testing.T) {
}
defer db.Close()
rows, err := db.QueryContext(ctx, `SELECT readfile('fileio_test.go')`)
rows, err := db.Query(`SELECT readfile('fileio_test.go')`)
if err != nil {
t.Fatal(err)
}
+33 -109
View File
@@ -2,33 +2,27 @@ package fileio
import (
"io/fs"
"iter"
"os"
"path"
"path/filepath"
"strings"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
)
const (
_COL_NAME = iota
_COL_MODE
_COL_MTIME
_COL_DATA
_COL_LEVEL
_COL_ROOT
_COL_BASE
_COL_NAME = 0
_COL_MODE = 1
_COL_TIME = 2
_COL_DATA = 3
_COL_ROOT = 4
_COL_BASE = 5
)
type fsdir struct{ fsys fs.FS }
func (d fsdir) BestIndex(idx *sqlite3.IndexInfo) error {
var levelOp sqlite3.IndexConstraintOp
var root bool
level := -1
base := -1
var root, base bool
for i, cst := range idx.Constraint {
switch cst.Column {
case _COL_ROOT:
@@ -44,44 +38,20 @@ func (d fsdir) BestIndex(idx *sqlite3.IndexInfo) error {
if !cst.Usable || cst.Op != sqlite3.INDEX_CONSTRAINT_EQ {
return sqlite3.CONSTRAINT
}
base = i
case _COL_LEVEL:
if !cst.Usable {
break
}
switch cst.Op {
case sqlite3.INDEX_CONSTRAINT_EQ, sqlite3.INDEX_CONSTRAINT_LE, sqlite3.INDEX_CONSTRAINT_LT:
levelOp = cst.Op
level = i
idx.ConstraintUsage[i] = sqlite3.IndexConstraintUsage{
Omit: true,
ArgvIndex: 2,
}
base = true
}
}
if !root {
return sqlite3.CONSTRAINT
}
args := 2
idx.IdxNum = 0
idx.EstimatedCost = 1e9
if base >= 0 {
idx.IdxNum |= 1
idx.EstimatedCost /= 1e4
idx.ConstraintUsage[base] = sqlite3.IndexConstraintUsage{
Omit: true,
ArgvIndex: args,
}
args++
}
if level >= 0 {
idx.EstimatedCost /= 1e4
idx.IdxNum |= (args - 1) << 1
if levelOp == sqlite3.INDEX_CONSTRAINT_LT {
idx.IdxNum |= 1 << 3
}
idx.ConstraintUsage[level] = sqlite3.IndexConstraintUsage{
Omit: levelOp != sqlite3.INDEX_CONSTRAINT_EQ,
ArgvIndex: args,
}
if base {
idx.EstimatedCost = 10
} else {
idx.EstimatedCost = 100
}
return nil
}
@@ -92,25 +62,23 @@ func (d fsdir) Open() (sqlite3.VTabCursor, error) {
type cursor struct {
fsdir
base string
next func() (entry, bool)
stop func()
curr entry
eof bool
rowID int64
maxLevel int
base string
resume resume
cancel func()
curr entry
eof bool
rowID int64
}
type entry struct {
fs.DirEntry
err error
path string
level int
err error
path string
}
func (c *cursor) Close() error {
if c.stop != nil {
c.stop()
if c.cancel != nil {
c.cancel()
}
return nil
}
@@ -121,54 +89,26 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
}
root := arg[0].Text()
if i := idxNum & 1; i > 0 {
base := arg[i].Text()
if len(arg) > 1 {
base := arg[1].Text()
if c.fsys != nil {
root = path.Join(base, root)
base = path.Clean(base) + "/"
} else {
root = filepath.Join(base, root)
base = filepath.Clean(base) + string(filepath.Separator)
}
root = base + root
c.base = base
}
if i := idxNum >> 1; i > 0 {
c.maxLevel = arg[i&3].Int() - i>>2
}
c.next, c.stop = iter.Pull(func(yield func(entry) bool) {
var stack []string
walkDir := func(p string, d fs.DirEntry, err error) error {
level := len(stack)
for level > 1 && !strings.HasPrefix(c.dir(p), stack[level-1]) {
level--
}
stack = stack[:level]
level++
if !yield(entry{d, err, p, level}) {
return fs.SkipAll
}
if d != nil && d.IsDir() {
if 0 < c.maxLevel && c.maxLevel <= level {
return fs.SkipDir
}
stack = append(stack, p)
}
return nil
}
if c.fsys != nil {
fs.WalkDir(c.fsys, root, walkDir)
} else {
filepath.WalkDir(root, walkDir)
}
})
c.resume, c.cancel = pull(c, root)
c.eof = false
c.rowID = 0
return c.Next()
}
func (c *cursor) Next() error {
curr, ok := c.next()
curr, ok := next(c)
c.curr = curr
c.eof = !ok
c.rowID++
@@ -196,7 +136,7 @@ func (c *cursor) Column(ctx sqlite3.Context, n int) error {
}
ctx.ResultInt64(int64(i.Mode()))
case _COL_MTIME:
case _COL_TIME:
i, err := c.curr.Info()
if err != nil {
return err
@@ -225,22 +165,6 @@ func (c *cursor) Column(ctx sqlite3.Context, n int) error {
}
ctx.ResultText(t)
}
case _COL_LEVEL:
ctx.ResultInt(c.curr.level)
}
return nil
}
func (c *cursor) dir(p string) string {
var dir string
if c.fsys != nil {
dir, _ = path.Split(p)
} else {
dir, _ = filepath.Split(p)
}
if dir == "" {
dir = "."
}
return dir
}
+29
View File
@@ -0,0 +1,29 @@
//go:build !go1.23
package fileio
import (
"io/fs"
"path/filepath"
)
type resume = func(struct{}) (entry, bool)
func next(c *cursor) (entry, bool) {
return c.resume(struct{}{})
}
func pull(c *cursor, root string) (resume, func()) {
return coroNew(func(_ struct{}, yield func(entry) struct{}) entry {
walkDir := func(path string, d fs.DirEntry, err error) error {
yield(entry{d, err, path})
return nil
}
if c.fsys != nil {
fs.WalkDir(c.fsys, root, walkDir)
} else {
filepath.WalkDir(root, walkDir)
}
return entry{}
})
}
+31
View File
@@ -0,0 +1,31 @@
//go:build go1.23
package fileio
import (
"io/fs"
"iter"
"path/filepath"
)
type resume = func() (entry, bool)
func next(c *cursor) (entry, bool) {
return c.resume()
}
func pull(c *cursor, root string) (resume, func()) {
return iter.Pull(func(yield func(entry) bool) {
walkDir := func(path string, d fs.DirEntry, err error) error {
if yield(entry{d, err, path}) {
return nil
}
return fs.SkipAll
}
if c.fsys != nil {
fs.WalkDir(c.fsys, root, walkDir)
} else {
filepath.WalkDir(root, walkDir)
}
})
}
+11 -15
View File
@@ -8,11 +8,12 @@ import (
"testing"
"time"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/fileio"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/fileio"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Test_fsdir(t *testing.T) {
@@ -20,10 +21,9 @@ func Test_fsdir(t *testing.T) {
for _, fsys := range []fs.FS{nil, os.DirFS(".")} {
t.Run("", func(t *testing.T) {
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, func(c *sqlite3.Conn) error {
db, err := driver.Open(tmp, func(c *sqlite3.Conn) error {
fileio.RegisterFS(c, fsys)
return nil
})
@@ -32,7 +32,7 @@ func Test_fsdir(t *testing.T) {
}
defer db.Close()
rows, err := db.QueryContext(ctx, `SELECT * FROM fsdir('.') WHERE level <= 2`)
rows, err := db.Query(`SELECT * FROM fsdir('.', '.')`)
if err != nil {
t.Fatal(err)
}
@@ -42,8 +42,7 @@ func Test_fsdir(t *testing.T) {
var mode fs.FileMode
var mtime time.Time
var data sql.RawBytes
var level int
err := rows.Scan(&name, &mode, sqlite3.TimeFormatUnixFrac.Scanner(&mtime), &data, &level)
err := rows.Scan(&name, &mode, sqlite3.TimeFormatUnixFrac.Scanner(&mtime), &data)
if err != nil {
t.Fatal(err)
}
@@ -58,9 +57,6 @@ func Test_fsdir(t *testing.T) {
t.Errorf("got: %s", data[:min(64, len(data))])
}
}
if level > 2 {
t.Errorf("got: %v", level)
}
}
})
}
@@ -69,7 +65,7 @@ func Test_fsdir(t *testing.T) {
func Test_fsdir_errors(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
+4 -4
View File
@@ -8,14 +8,14 @@ import (
"path/filepath"
"time"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/hanzoai/sqlite3/util/fsutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/util/fsutil"
)
func writefile(ctx sqlite3.Context, arg ...sqlite3.Value) {
if len(arg) < 2 || len(arg) > 4 {
ctx.ResultError(errutil.ErrorString("writefile: wrong number of arguments"))
ctx.ResultError(util.ErrorString("writefile: wrong number of arguments"))
return
}
+14 -17
View File
@@ -1,5 +1,3 @@
//go:build !js
package fileio
import (
@@ -9,17 +7,17 @@ import (
"testing"
"time"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Test_writefile(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, Register)
db, err := driver.Open(tmp, Register)
if err != nil {
t.Fatal(err)
}
@@ -32,22 +30,22 @@ func Test_writefile(t *testing.T) {
sock := filepath.Join(dir, "sock")
twosday := time.Date(2022, 2, 22, 22, 22, 22, 0, time.UTC)
_, err = db.ExecContext(ctx, `SELECT writefile(?, 'Hello world!')`, file)
_, err = db.Exec(`SELECT writefile(?, 'Hello world!')`, file)
if err != nil {
t.Fatal(err)
}
_, err = db.ExecContext(ctx, `SELECT writefile(?, ?, ?)`, link, "test.txt", fs.ModeSymlink)
_, err = db.Exec(`SELECT writefile(?, ?, ?)`, link, "test.txt", fs.ModeSymlink)
if err != nil {
t.Fatal(err)
}
_, err = db.ExecContext(ctx, `SELECT writefile(?, ?, ?, ?)`, dir, nil, 0040700, twosday.Unix())
_, err = db.Exec(`SELECT writefile(?, ?, ?, ?)`, dir, nil, 0040700, twosday.Unix())
if err != nil {
t.Fatal(err)
}
rows, err := db.QueryContext(ctx, `SELECT * FROM fsdir('.', ?)`, dir)
rows, err := db.Query(`SELECT * FROM fsdir('.', ?)`, dir)
if err != nil {
t.Fatal(err)
}
@@ -57,8 +55,7 @@ func Test_writefile(t *testing.T) {
var mode fs.FileMode
var mtime time.Time
var data sql.NullString
var level int
err := rows.Scan(&name, &mode, &mtime, &data, &level)
err := rows.Scan(&name, &mode, &mtime, &data)
if err != nil {
t.Fatal(err)
}
@@ -73,19 +70,19 @@ func Test_writefile(t *testing.T) {
}
}
_, err = db.ExecContext(ctx, `SELECT writefile(?, 'Hello world!')`, nest)
_, err = db.Exec(`SELECT writefile(?, 'Hello world!')`, nest)
if err != nil {
t.Fatal(err)
}
_, err = db.ExecContext(ctx, `SELECT writefile(?, ?, ?)`, sock, nil, fs.ModeSocket)
_, err = db.Exec(`SELECT writefile(?, ?, ?)`, sock, nil, fs.ModeSocket)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}
_, err = db.ExecContext(ctx, `SELECT writefile()`)
_, err = db.Exec(`SELECT writefile()`)
if err == nil {
t.Fatal("want error")
} else {
-14
View File
@@ -1,14 +0,0 @@
// Package fts5 provides the fts5 extension.
//
// https://sqlite.org/fts5.html
package fts5
import (
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3-wasm/v3/fts5"
)
// Register registers the fts5 extension.
func Register(db *sqlite3.Conn) error {
return sqlite3.ExtensionInit(db, fts5.New, fts5.DylinkInfo)
}
-37
View File
@@ -1,37 +0,0 @@
package fts5_test
import (
"fmt"
"log"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/fts5"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
)
func Example() {
db, err := driver.Open("file:/test.db?vfs=memdb", fts5.Register)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`
CREATE VIRTUAL TABLE docs USING fts5(title, body);
INSERT INTO docs(title, body) VALUES
('Go Programming', 'An intensive guide to Go routines.'),
('SQLite Tutorial', 'Learn how to use virtual tables efficiently.');
`)
if err != nil {
log.Fatal(err)
}
var title string
err = db.QueryRow("SELECT title FROM docs WHERE docs MATCH 'Go AND routines'").Scan(&title)
if err != nil {
log.Fatal(err)
}
fmt.Println(title)
// Output: Go Programming
}
+3 -3
View File
@@ -3,8 +3,8 @@ package hash
import (
"crypto"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
func blake2sFunc(ctx sqlite3.Context, arg ...sqlite3.Value) {
@@ -25,6 +25,6 @@ func blake2bFunc(ctx sqlite3.Context, arg ...sqlite3.Value) {
case 512:
hashFunc(ctx, arg[0], crypto.BLAKE2b_512)
default:
ctx.ResultError(errutil.ErrorString("blake2b: size must be 256, 384, 512"))
ctx.ResultError(util.ErrorString("blake2b: size must be 256, 384, 512"))
}
}
+3 -3
View File
@@ -23,15 +23,15 @@ import (
"crypto"
"errors"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Register registers cryptographic hash functions for a database connection.
func Register(db *sqlite3.Conn) error {
const flags = sqlite3.DETERMINISTIC | sqlite3.INNOCUOUS
var errs errutil.ErrorJoiner
var errs util.ErrorJoiner
if crypto.MD4.Available() {
errs.Join(
db.CreateFunction("md4", 1, flags, md4Func))
+12 -12
View File
@@ -4,7 +4,6 @@ import (
_ "crypto/md5"
_ "crypto/sha1"
_ "crypto/sha256"
_ "crypto/sha3"
_ "crypto/sha512"
"testing"
@@ -12,15 +11,17 @@ import (
_ "golang.org/x/crypto/blake2s"
_ "golang.org/x/crypto/md4"
_ "golang.org/x/crypto/ripemd160"
_ "golang.org/x/crypto/sha3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func TestRegister(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
tests := []struct {
name string
@@ -54,8 +55,7 @@ func TestRegister(t *testing.T) {
{"blake2b('', 256)", "0E5751C026E543B2E8AB2EB06099DAA1D1E5DF47778F7787FAAB45CDF12FE3A8"},
}
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, Register)
db, err := driver.Open(tmp, Register)
if err != nil {
t.Fatal(err)
}
@@ -65,7 +65,7 @@ func TestRegister(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
var hash string
err = db.QueryRowContext(ctx, `SELECT hex(`+tt.name+`)`).Scan(&hash)
err = db.QueryRow(`SELECT hex(` + tt.name + `)`).Scan(&hash)
if err != nil {
t.Fatal(err)
}
@@ -76,22 +76,22 @@ func TestRegister(t *testing.T) {
})
}
_, err = db.ExecContext(ctx, `SELECT sha256('', 255)`)
_, err = db.Exec(`SELECT sha256('', 255)`)
if err == nil {
t.Error("want error")
}
_, err = db.ExecContext(ctx, `SELECT sha512('', 255)`)
_, err = db.Exec(`SELECT sha512('', 255)`)
if err == nil {
t.Error("want error")
}
_, err = db.ExecContext(ctx, `SELECT sha3('', 255)`)
_, err = db.Exec(`SELECT sha3('', 255)`)
if err == nil {
t.Error("want error")
}
_, err = db.ExecContext(ctx, `SELECT blake2b('', 255)`)
_, err = db.Exec(`SELECT blake2b('', 255)`)
if err == nil {
t.Error("want error")
}
+4 -4
View File
@@ -3,8 +3,8 @@ package hash
import (
"crypto"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
func sha224Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
@@ -27,7 +27,7 @@ func sha256Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
case 256:
hashFunc(ctx, arg[0], crypto.SHA256)
default:
ctx.ResultError(errutil.ErrorString("sha256: size must be 224, 256"))
ctx.ResultError(util.ErrorString("sha256: size must be 224, 256"))
}
}
@@ -47,7 +47,7 @@ func sha512Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
case 512:
hashFunc(ctx, arg[0], crypto.SHA512)
default:
ctx.ResultError(errutil.ErrorString("sha512: size must be 224, 256, 384, 512"))
ctx.ResultError(util.ErrorString("sha512: size must be 224, 256, 384, 512"))
}
}
+3 -3
View File
@@ -3,8 +3,8 @@ package hash
import (
"crypto"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
func sha3Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
@@ -23,6 +23,6 @@ func sha3Func(ctx sqlite3.Context, arg ...sqlite3.Value) {
case 512:
hashFunc(ctx, arg[0], crypto.SHA3_512)
default:
ctx.ResultError(errutil.ErrorString("sha3: size must be 224, 256, 384, 512"))
ctx.ResultError(util.ErrorString("sha3: size must be 224, 256, 384, 512"))
}
}
-113
View File
@@ -1,113 +0,0 @@
// Package ipaddr provides functions to manipulate IPs and CIDRs.
//
// It provides the following functions:
// - ipcontains(prefix, ip)
// - ipoverlaps(prefix1, prefix2)
// - ipfamily(ip/prefix)
// - iphost(ip/prefix)
// - ipmasklen(prefix)
// - ipnetwork(prefix)
package ipaddr
import (
"errors"
"net/netip"
"github.com/hanzoai/sqlite3"
)
// Register IP/CIDR functions for a database connection.
func Register(db *sqlite3.Conn) error {
const flags = sqlite3.DETERMINISTIC | sqlite3.INNOCUOUS
return errors.Join(
db.CreateFunction("ipcontains", 2, flags, contains),
db.CreateFunction("ipoverlaps", 2, flags, overlaps),
db.CreateFunction("ipfamily", 1, flags, family),
db.CreateFunction("iphost", 1, flags, host),
db.CreateFunction("ipmasklen", 1, flags, masklen),
db.CreateFunction("ipnetwork", 1, flags, network))
}
func contains(ctx sqlite3.Context, arg ...sqlite3.Value) {
prefix, err := netip.ParsePrefix(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
addr, err := netip.ParseAddr(arg[1].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
ctx.ResultBool(prefix.Contains(addr))
}
func overlaps(ctx sqlite3.Context, arg ...sqlite3.Value) {
prefix1, err := netip.ParsePrefix(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
prefix2, err := netip.ParsePrefix(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
ctx.ResultBool(prefix1.Overlaps(prefix2))
}
func family(ctx sqlite3.Context, arg ...sqlite3.Value) {
addr, err := addr(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
switch {
case addr.Is4():
ctx.ResultInt(4)
case addr.Is6():
ctx.ResultInt(6)
}
}
func host(ctx sqlite3.Context, arg ...sqlite3.Value) {
addr, err := addr(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
buf, _ := addr.MarshalText()
ctx.ResultRawText(buf)
}
func masklen(ctx sqlite3.Context, arg ...sqlite3.Value) {
prefix, err := netip.ParsePrefix(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
ctx.ResultInt(prefix.Bits())
}
func network(ctx sqlite3.Context, arg ...sqlite3.Value) {
prefix, err := netip.ParsePrefix(arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
buf, _ := prefix.Masked().MarshalText()
ctx.ResultRawText(buf)
}
func addr(text string) (netip.Addr, error) {
addr, err := netip.ParseAddr(text)
if err != nil {
if prefix, err := netip.ParsePrefix(text); err == nil {
return prefix.Addr(), nil
}
if addrpt, err := netip.ParseAddrPort(text); err == nil {
return addrpt.Addr(), nil
}
}
return addr, err
}
-88
View File
@@ -1,88 +0,0 @@
package ipaddr_test
import (
"testing"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/ipaddr"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
)
func TestRegister(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, ipaddr.Register)
if err != nil {
t.Fatal(err)
}
defer db.Close()
var got string
err = db.QueryRowContext(ctx, `SELECT ipfamily('::1')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "6" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT ipfamily('[::1]:80')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "6" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT ipfamily('192.168.1.5/24')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "4" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT iphost('192.168.1.5/24')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "192.168.1.5" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT ipmasklen('192.168.1.5/24')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "24" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT ipnetwork('192.168.1.5/24')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "192.168.1.0/24" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT ipcontains('192.168.1.0/24', '192.168.1.5')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "1" {
t.Fatalf("got %s", got)
}
err = db.QueryRowContext(ctx, `SELECT ipoverlaps('192.168.1.0/24', '192.168.1.5/32')`).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != "1" {
t.Fatalf("got %s", got)
}
}
+2 -2
View File
@@ -18,8 +18,8 @@ import (
"io"
"io/fs"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/util/osutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/util/osutil"
)
// Register registers the lines and lines_read table-valued functions.
+19 -22
View File
@@ -10,11 +10,12 @@ import (
"strings"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/lines"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/lines"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func Example() {
@@ -66,10 +67,9 @@ func Example() {
func Test_lines(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, lines.Register)
db, err := driver.Open(tmp, lines.Register)
if err != nil {
log.Fatal(err)
}
@@ -77,7 +77,7 @@ func Test_lines(t *testing.T) {
const data = "line 1\nline 2\r\nline 3\n"
rows, err := db.QueryContext(ctx, `SELECT rowid, line FROM lines(?)`, data)
rows, err := db.Query(`SELECT rowid, line FROM lines(?)`, data)
if err != nil {
t.Fatal(err)
}
@@ -98,23 +98,22 @@ func Test_lines(t *testing.T) {
func Test_lines_error(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, lines.Register)
db, err := driver.Open(tmp, lines.Register)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.ExecContext(ctx, `SELECT rowid, line FROM lines(?)`, nil)
_, err = db.Exec(`SELECT rowid, line FROM lines(?)`, nil)
if err == nil {
t.Fatal("want error")
} else {
t.Log(err)
}
_, err = db.ExecContext(ctx, `SELECT rowid, line FROM lines_read(?)`, "xpto")
_, err = db.Exec(`SELECT rowid, line FROM lines_read(?)`, "xpto")
if err == nil {
t.Fatal("want error")
} else {
@@ -124,10 +123,9 @@ func Test_lines_error(t *testing.T) {
func Test_lines_read(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, lines.Register)
db, err := driver.Open(tmp, lines.Register)
if err != nil {
log.Fatal(err)
}
@@ -135,7 +133,7 @@ func Test_lines_read(t *testing.T) {
const data = "line 1\nline 2\r\nline 3\n"
rows, err := db.QueryContext(ctx, `SELECT rowid, line FROM lines_read(?)`,
rows, err := db.Query(`SELECT rowid, line FROM lines_read(?)`,
sqlite3.Pointer(strings.NewReader(data)))
if err != nil {
t.Fatal(err)
@@ -157,16 +155,15 @@ func Test_lines_read(t *testing.T) {
func Test_lines_test(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, lines.Register)
db, err := driver.Open(tmp, lines.Register)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.QueryContext(ctx, `SELECT rowid, line FROM lines_read(?, '}')`, "lines_test.go")
rows, err := db.Query(`SELECT rowid, line FROM lines_read(?, '}')`, "lines_test.go")
if errors.Is(err, os.ErrNotExist) {
t.Skip(err)
}
+23
View File
@@ -0,0 +1,23 @@
module github.com/ncruces/go-sqlite3/ext/parquet
go 1.22
toolchain go1.23.0
require (
github.com/ncruces/go-sqlite3 v0.21.0
github.com/parquet-go/parquet-go v0.24.0
)
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/ncruces/julianday v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/tetratelabs/wazero v1.8.2 // indirect
golang.org/x/sys v0.28.0 // indirect
)
+32
View File
@@ -0,0 +1,32 @@
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/ncruces/go-sqlite3 v0.21.0 h1:EwKFoy1hHEopN4sFZarmi+McXdbCcbTuLixhEayXVbQ=
github.com/ncruces/go-sqlite3 v0.21.0/go.mod h1:zxMOaSG5kFYVFK4xQa0pdwIszqxqJ0W0BxBgwdrNjuA=
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/parquet-go/parquet-go v0.24.0 h1:VrsifmLPDnas8zpoHmYiWDZ1YHzLmc7NmNwPGkI2JM4=
github.com/parquet-go/parquet-go v0.24.0/go.mod h1:OqBBRGBl7+llplCvDMql8dEKaDqjaFA/VAPw+OJiNiw=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4=
github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
+62
View File
@@ -0,0 +1,62 @@
package parquet
import (
"os"
"strings"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
"github.com/ncruces/go-sqlite3/util/osutil"
"github.com/ncruces/go-sqlite3/util/sql3util"
"github.com/parquet-go/parquet-go"
)
func Register(db *sqlite3.Conn) error {
declare := func(db *sqlite3.Conn, _, _, _ string, arg ...string) (_ *table, err error) {
if len(arg) == 0 {
return nil, util.ErrorString(`parquet: must specify a filename`)
}
file, err := osutil.OpenFile(sql3util.Unquote(arg[0]), os.O_RDONLY, 0)
if err != nil {
return nil, err
}
reader := parquet.NewReader(file)
column := make(map[int]string)
var schema strings.Builder
schema.WriteString("CREATE TABLE x(")
for i, field := range reader.Schema().Fields() {
if i > 0 {
schema.WriteByte(',')
}
schema.WriteString(sqlite3.QuoteIdentifier(field.Name()))
schema.WriteByte(' ')
switch field.Type().Kind() {
case parquet.Boolean:
schema.WriteString("BOOLEAN")
case parquet.Int32, parquet.Int64, parquet.Int96:
schema.WriteString("INTEGER")
case parquet.Float, parquet.Double:
schema.WriteString("REAL")
case parquet.ByteArray, parquet.FixedLenByteArray:
schema.WriteString("TEXT")
}
// Save the column name
column[i] = field.Name()
}
schema.WriteString(");")
err = db.DeclareVTab(schema.String())
if err != nil {
return nil, err
}
return &table{}, nil
}
return sqlite3.CreateModule(db, "parquet", declare, declare)
}
type table struct {
}
+1 -1
View File
@@ -3,7 +3,7 @@ package pivot
import (
"testing"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
)
func Test_operator(t *testing.T) {
+15 -32
View File
@@ -8,8 +8,8 @@ import (
"fmt"
"strings"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)
// Register registers the pivot virtual table.
@@ -25,14 +25,14 @@ type table struct {
cols []*sqlite3.Value
}
func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err error) {
func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (res *table, err error) {
if len(arg) != 3 {
return nil, fmt.Errorf("pivot: wrong number of arguments")
}
t := &table{db: db}
defer func() {
if ret == nil {
if res == nil {
t.Close()
}
}()
@@ -43,14 +43,11 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err e
// Row key query.
t.scan = "SELECT * FROM\n" + arg[0]
stmt, tail, err := db.PrepareFlags(t.scan, sqlite3.PREPARE_FROM_DDL)
stmt, _, err := db.Prepare(t.scan)
if err != nil {
return nil, err
}
defer stmt.Close()
if tail != "" {
return nil, errutil.TailErr
}
t.keys = make([]string, stmt.ColumnCount())
for i := range t.keys {
@@ -58,47 +55,36 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err e
t.keys[i] = name
create.WriteString(sep)
create.WriteString(name)
create.WriteString(" ")
create.WriteString(stmt.ColumnDeclType(i))
sep = ","
}
stmt.Close()
// Column definition query.
stmt, tail, err = db.PrepareFlags("SELECT * FROM\n"+arg[1], sqlite3.PREPARE_FROM_DDL)
stmt, _, err = db.Prepare("SELECT * FROM\n" + arg[1])
if err != nil {
return nil, err
}
if tail != "" {
return nil, errutil.TailErr
}
if stmt.ColumnCount() != 2 {
return nil, errutil.ErrorString("pivot: column definition query expects 2 result columns")
return nil, util.ErrorString("pivot: column definition query expects 2 result columns")
}
for stmt.Step() {
name := sqlite3.QuoteIdentifier(stmt.ColumnText(1))
t.cols = append(t.cols, stmt.ColumnValue(0).Dup())
create.WriteString(sep)
create.WriteString(",")
create.WriteString(name)
create.WriteString(" ")
create.WriteString(stmt.ColumnDeclType(1))
sep = ","
}
stmt.Close()
// Pivot cell query.
t.cell = "SELECT * FROM\n" + arg[2]
stmt, tail, err = db.PrepareFlags(t.cell, sqlite3.PREPARE_FROM_DDL)
stmt, _, err = db.Prepare(t.cell)
if err != nil {
return nil, err
}
if tail != "" {
return nil, errutil.TailErr
}
if stmt.ColumnCount() != 1 {
return nil, errutil.ErrorString("pivot: cell query expects 1 result columns")
return nil, util.ErrorString("pivot: cell query expects 1 result columns")
}
if stmt.BindCount() != len(t.keys)+1 {
return nil, fmt.Errorf("pivot: cell query expects %d bound parameters", len(t.keys)+1)
@@ -113,11 +99,10 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (ret *table, err e
}
func (t *table) Close() error {
var errs []error
for _, c := range t.cols {
errs = append(errs, c.Close())
c.Close()
}
return errors.Join(errs...)
return nil
}
func (t *table) BestIndex(idx *sqlite3.IndexInfo) error {
@@ -196,9 +181,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
return err
}
const prepflags = sqlite3.PREPARE_DONT_LOG | sqlite3.PREPARE_FROM_DDL
c.scan, _, err = c.table.db.PrepareFlags(idxStr, prepflags)
c.scan, _, err = c.table.db.Prepare(idxStr)
if err != nil {
return err
}
@@ -210,7 +193,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
}
if c.cell == nil {
c.cell, _, err = c.table.db.PrepareFlags(c.table.cell, prepflags)
c.cell, _, err = c.table.db.Prepare(c.table.cell)
if err != nil {
return err
}
@@ -223,7 +206,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
func (c *cursor) Next() error {
if c.scan.Step() {
count := c.scan.ColumnCount()
for i := range count {
for i := 0; i < count; i++ {
err := c.cell.BindValue(i+1, c.scan.ColumnValue(i))
if err != nil {
return err
+11 -11
View File
@@ -3,13 +3,13 @@ package pivot_test
import (
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/ext/pivot"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/ext/pivot"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
)
// https://antonz.org/sqlite-pivot-table/
@@ -85,13 +85,13 @@ func Example() {
func TestMain(m *testing.M) {
sqlite3.AutoExtension(pivot.Register)
os.Exit(m.Run())
m.Run()
}
func TestRegister(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
@@ -140,10 +140,10 @@ func TestRegister(t *testing.T) {
}
defer stmt.Close()
if !stmt.Step() {
t.Fatal(stmt.Err())
} else if got := stmt.ColumnInt(0); got != 3 {
t.Errorf("got %d, want 3", got)
if stmt.Step() {
if got := stmt.ColumnInt(0); got != 3 {
t.Errorf("got %d, want 3", got)
}
}
err = db.Exec(`ALTER TABLE v_x RENAME TO v_y`)
@@ -155,7 +155,7 @@ func TestRegister(t *testing.T) {
func TestRegister_errors(t *testing.T) {
t.Parallel()
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
db, err := sqlite3.Open(":memory:")
if err != nil {
t.Fatal(err)
}
+26 -74
View File
@@ -16,11 +16,9 @@ package regexp
import (
"errors"
"regexp"
"regexp/syntax"
"strings"
"unicode/utf8"
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3"
)
// Register registers Unicode aware functions for a database connection.
@@ -52,83 +50,34 @@ func Register(db *sqlite3.Conn) error {
// SELECT column WHERE column GLOB :glob_prefix AND column REGEXP :regexp
//
// [LIKE optimization]: https://sqlite.org/optoverview.html#the_like_optimization
func GlobPrefix(expr string) string {
re, err := syntax.Parse(expr, syntax.Perl)
if err != nil {
return "" // no match possible
}
prog, err := syntax.Compile(re.Simplify())
if err != nil {
return "" // notest
}
i := &prog.Inst[prog.Start]
var empty syntax.EmptyOp
loop1:
for {
switch i.Op {
case syntax.InstFail:
return "" // notest
case syntax.InstCapture, syntax.InstNop:
// skip
case syntax.InstEmptyWidth:
empty |= syntax.EmptyOp(i.Arg)
default:
break loop1
func GlobPrefix(re *regexp.Regexp) string {
prefix, complete := re.LiteralPrefix()
i := strings.IndexAny(prefix, "*?[")
if i < 0 {
if complete {
return prefix
}
i = &prog.Inst[i.Out]
i = len(prefix)
}
if empty&syntax.EmptyBeginText == 0 {
return "*" // not anchored
}
var glob strings.Builder
loop2:
for {
switch i.Op {
case syntax.InstFail:
return "" // notest
case syntax.InstCapture, syntax.InstEmptyWidth, syntax.InstNop:
// skip
case syntax.InstRune, syntax.InstRune1:
if len(i.Rune) != 1 || syntax.Flags(i.Arg)&syntax.FoldCase != 0 {
break loop2
}
switch r := i.Rune[0]; r {
case '*', '?', '[', utf8.RuneError:
break loop2
default:
glob.WriteRune(r)
}
default:
break loop2
}
i = &prog.Inst[i.Out]
}
glob.WriteByte('*')
return glob.String()
return prefix[:i] + "*"
}
func load(ctx sqlite3.Context, arg []sqlite3.Value, i int) (*regexp.Regexp, error) {
func load(ctx sqlite3.Context, i int, expr string) (*regexp.Regexp, error) {
re, ok := ctx.GetAuxData(i).(*regexp.Regexp)
if !ok {
re, ok = arg[i].Pointer().(*regexp.Regexp)
if !ok {
r, err := regexp.Compile(arg[i].Text())
if err != nil {
return nil, err
}
re = r
r, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
ctx.SetAuxData(i, re)
re = r
ctx.SetAuxData(0, r)
}
return re, nil
}
func regex(ctx sqlite3.Context, arg ...sqlite3.Value) {
re, err := load(ctx, arg, 0)
_ = arg[1] // bounds check
re, err := load(ctx, 0, arg[0].Text())
if err != nil {
ctx.ResultError(err)
return // notest
@@ -138,17 +87,18 @@ func regex(ctx sqlite3.Context, arg ...sqlite3.Value) {
}
func regexLike(ctx sqlite3.Context, arg ...sqlite3.Value) {
re, err := load(ctx, arg, 1)
re, err := load(ctx, 1, arg[1].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
text := arg[0].RawText()
ctx.ResultBool(re.Match(text))
}
func regexCount(ctx sqlite3.Context, arg ...sqlite3.Value) {
re, err := load(ctx, arg, 1)
re, err := load(ctx, 1, arg[1].Text())
if err != nil {
ctx.ResultError(err)
return // notest
@@ -163,7 +113,7 @@ func regexCount(ctx sqlite3.Context, arg ...sqlite3.Value) {
}
func regexSubstr(ctx sqlite3.Context, arg ...sqlite3.Value) {
re, err := load(ctx, arg, 1)
re, err := load(ctx, 1, arg[1].Text())
if err != nil {
ctx.ResultError(err)
return // notest
@@ -188,7 +138,7 @@ func regexSubstr(ctx sqlite3.Context, arg ...sqlite3.Value) {
}
func regexInstr(ctx sqlite3.Context, arg ...sqlite3.Value) {
re, err := load(ctx, arg, 1)
re, err := load(ctx, 1, arg[1].Text())
if err != nil {
ctx.ResultError(err)
return // notest
@@ -216,14 +166,16 @@ func regexInstr(ctx sqlite3.Context, arg ...sqlite3.Value) {
}
func regexReplace(ctx sqlite3.Context, arg ...sqlite3.Value) {
re, err := load(ctx, arg, 1)
_ = arg[2] // bounds check
re, err := load(ctx, 1, arg[1].Text())
if err != nil {
ctx.ResultError(err)
return // notest
}
repl := arg[2].RawText()
text := arg[0].RawText()
repl := arg[2].RawText()
var pos, n int
if len(arg) > 3 {
pos = arg[3].Int()
+20 -81
View File
@@ -3,21 +3,19 @@ package regexp
import (
"database/sql"
"regexp"
"strings"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs/memdb"
"github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs/memdb"
)
func TestRegister(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, Register)
db, err := driver.Open(tmp, Register)
if err != nil {
t.Fatal(err)
}
@@ -38,7 +36,7 @@ func TestRegister(t *testing.T) {
{`regexp_instr('Hello', '.', 6)`, ""},
{`regexp_substr('Hello', 'el.')`, "ell"},
{`regexp_replace('Hello', 'llo', 'll')`, "Hell"},
// https://postgresql.org/docs/current/functions-matching.html
// https://www.postgresql.org/docs/current/functions-matching.html
{`regexp_count('ABCABCAXYaxy', 'A.')`, "3"},
{`regexp_count('ABCABCAXYaxy', '(?i)A.', 1)`, "4"},
{`regexp_instr('number of your street, town zip, FR', '[^,]+', 1, 2)`, "23"},
@@ -68,7 +66,7 @@ func TestRegister(t *testing.T) {
for _, tt := range tests {
var got sql.NullString
err := db.QueryRowContext(ctx, `SELECT `+tt.test).Scan(&got)
err := db.QueryRow(`SELECT ` + tt.test).Scan(&got)
if err != nil {
t.Fatal(err)
}
@@ -80,10 +78,9 @@ func TestRegister(t *testing.T) {
func TestRegister_errors(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
tmp := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, Register)
db, err := driver.Open(tmp, Register)
if err != nil {
t.Fatal(err)
}
@@ -99,89 +96,31 @@ func TestRegister_errors(t *testing.T) {
}
for _, tt := range tests {
err := db.QueryRowContext(ctx, `SELECT `+tt, `\`).Scan(nil)
err := db.QueryRow(`SELECT `+tt, `\`).Scan(nil)
if err == nil {
t.Fatal("want error")
}
}
}
func TestRegister_pointer(t *testing.T) {
t.Parallel()
dsn := memdb.TestDB(t)
ctx := testcfg.Context(t)
db, err := driver.Open(dsn, Register)
if err != nil {
t.Fatal(err)
}
defer db.Close()
var got int
err = db.QueryRowContext(ctx, `SELECT regexp_count('ABCABCAXYaxy', ?, 1)`,
sqlite3.Pointer(regexp.MustCompile(`(?i)A.`))).Scan(&got)
if err != nil {
t.Fatal(err)
}
if got != 4 {
t.Errorf("got %d, want %d", got, 4)
}
}
func TestGlobPrefix(t *testing.T) {
tests := []struct {
re string
want string
}{
{`[`, ""},
{``, "*"},
{`^`, "*"},
{`a`, "*"},
{`ab`, "*"},
{`^a`, "a*"},
{`^a*`, "*"},
{`^a+`, "a*"},
{`^ab*`, "a*"},
{`^ab+`, "ab*"},
{`^a\?b`, "a*"},
{`^[a-z]`, "*"},
{``, ""},
{`a`, "a"},
{`a*`, "*"},
{`a+`, "a*"},
{`ab*`, "a*"},
{`ab+`, "ab*"},
{`a\?b`, "a*"},
}
for _, tt := range tests {
t.Run(tt.re, func(t *testing.T) {
if got := GlobPrefix(tt.re); got != tt.want {
t.Errorf("GlobPrefix(%v) = %v, want %v", tt.re, got, tt.want)
if got := GlobPrefix(regexp.MustCompile(tt.re)); got != tt.want {
t.Errorf("GlobPrefix() = %v, want %v", got, tt.want)
}
})
}
}
func FuzzGlobPrefix(f *testing.F) {
f.Add(``, ``)
f.Add(`[`, ``)
f.Add(`^`, ``)
f.Add(`a`, `a`)
f.Add(`ab`, `b`)
f.Add(`^a`, `a`)
f.Add(`^a*`, `ab`)
f.Add(`^a+`, `ab`)
f.Add(`^ab*`, `ab`)
f.Add(`^ab+`, `ab`)
f.Add(`^a\?b`, `ab`)
f.Add(`^[a-z]`, `ab`)
f.Fuzz(func(t *testing.T, lit, str string) {
re, err := regexp.Compile(lit)
if err != nil {
t.SkipNow()
}
if re.MatchString(str) {
prefix, ok := strings.CutSuffix(GlobPrefix(lit), "*")
if !ok {
t.Fatalf("missing * after %q for %q with %q", prefix, lit, str)
}
if !strings.HasPrefix(str, prefix) {
t.Fatalf("missing prefix %q for %q with %q", prefix, lit, str)
}
}
})
}
-15
View File
@@ -1,15 +0,0 @@
// Package rtree provides the rtree and geopoly virtual tables.
//
// https://sqlite.org/rtree.html
// https://sqlite.org/geopoly.html
package rtree
import (
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3-wasm/v3/rtree"
)
// Register registers the rtree and geopoly virtual tables.
func Register(db *sqlite3.Conn) error {
return sqlite3.ExtensionInit(db, rtree.New, rtree.DylinkInfo)
}
-63
View File
@@ -1,63 +0,0 @@
package rtree_test
import (
"fmt"
"log"
"github.com/hanzoai/sqlite3/driver"
"github.com/hanzoai/sqlite3/ext/rtree"
_ "github.com/hanzoai/sqlite3/vfs/memdb"
)
func Example_rtree() {
db, err := driver.Open("file:/test.db?vfs=memdb", rtree.Register)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`
CREATE VIRTUAL TABLE locations USING rtree(id, minX, maxX, minY, maxY);
INSERT INTO locations VALUES (1, -10.0, -5.0, 30.0, 35.0);
INSERT INTO locations VALUES (2, 100.0, 105.0, 0.0, 5.0);
`)
if err != nil {
log.Fatal(err)
}
var id int
err = db.QueryRow(`SELECT id FROM locations WHERE
minX >= -12.0 AND maxX <= -4.0 AND
minY >= 28.0 AND maxY <= 36.0`).Scan(&id)
if err != nil {
log.Fatal(err)
}
fmt.Println(id)
// Output: 1
}
func Example_geopoly() {
db, err := driver.Open("file:/test.db?vfs=memdb", rtree.Register)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`
CREATE VIRTUAL TABLE areas USING geopoly(name);
INSERT INTO areas(name, _shape) VALUES ('Triangle Area', '[[0,0],[2,0],[1,2],[0,0]]');
`)
if err != nil {
log.Fatal(err)
}
var name string
err = db.QueryRow("SELECT name FROM areas WHERE geopoly_contains_point(_shape, 1.0, 1.0)").Scan(&name)
if err != nil {
log.Fatal(err)
}
fmt.Println(name)
// Output: Triangle Area
}
-72
View File
@@ -1,72 +0,0 @@
// Package serdes provides functions to (de)serialize databases.
package serdes
import (
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/util/vfsutil"
"github.com/hanzoai/sqlite3/vfs"
)
const vfsName = "github.com/hanzoai/sqlite3/ext/serdes.sliceVFS"
func init() {
vfs.Register(vfsName, sliceVFS{})
}
var fileToOpen = make(chan *[]byte, 1)
// Serialize backs up a database into a byte slice.
//
// https://sqlite.org/c3ref/serialize.html
func Serialize(db *sqlite3.Conn, schema string) ([]byte, error) {
var file []byte
fileToOpen <- &file
err := db.Backup(schema, "file:serdes.db?nolock=1&vfs="+vfsName)
return file, err
}
// Deserialize restores a database from a byte slice,
// DESTROYING any contents previously stored in schema.
//
// To non-destructively open a database from a byte slice,
// consider alternatives like the ["reader"] or ["memdb"] VFSes.
//
// This differs from the similarly named SQLite API
// in that it DOES NOT disconnect from schema
// to reopen as an in-memory database.
//
// https://sqlite.org/c3ref/deserialize.html
//
// ["memdb"]: https://pkg.go.dev/github.com/hanzoai/sqlite3/vfs/memdb
// ["reader"]: https://pkg.go.dev/github.com/hanzoai/sqlite3/vfs/readervfs
func Deserialize(db *sqlite3.Conn, schema string, data []byte) error {
fileToOpen <- &data
return db.Restore(schema, "file:serdes.db?immutable=1&vfs="+vfsName)
}
type sliceVFS struct{}
func (sliceVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, error) {
if flags&vfs.OPEN_MAIN_DB == 0 || name != "serdes.db" {
return nil, flags, sqlite3.CANTOPEN
}
select {
case file := <-fileToOpen:
return (*vfsutil.SliceFile)(file), flags | vfs.OPEN_MEMORY, nil
default:
return nil, flags, sqlite3.MISUSE
}
}
func (sliceVFS) Delete(name string, dirSync bool) error {
// notest // no journals to delete
return sqlite3.IOERR_DELETE
}
func (sliceVFS) Access(name string, flag vfs.AccessFlag) (bool, error) {
return name == "serdes.db", nil
}
func (sliceVFS) FullPathname(name string) (string, error) {
return name, nil
}
-120
View File
@@ -1,120 +0,0 @@
package serdes_test
import (
_ "embed"
"errors"
"io"
"net/http"
"testing"
"github.com/hanzoai/sqlite3"
"github.com/hanzoai/sqlite3/ext/serdes"
"github.com/hanzoai/sqlite3/internal/testcfg"
"github.com/hanzoai/sqlite3/vfs"
)
//go:embed testdata/wal.db
var walDB []byte
func Test_wal(t *testing.T) {
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
db, err := sqlite3.OpenContext(testcfg.Context(t), "testdata/wal.db")
if err != nil {
t.Fatal(err)
}
defer db.Close()
data, err := serdes.Serialize(db, "main")
if err != nil {
t.Fatal(err)
}
compareDBs(t, data, walDB)
err = serdes.Deserialize(db, "temp", walDB)
if err != nil {
t.Fatal(err)
}
}
func Test_northwind(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
input, err := httpGet()
if err != nil {
t.Fatal(err)
}
db, err := sqlite3.OpenContext(testcfg.Context(t), ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
err = serdes.Deserialize(db, "temp", input)
if err != nil {
t.Fatal(err)
}
output, err := serdes.Serialize(db, "temp")
if err != nil {
t.Fatal(err)
}
compareDBs(t, input, output)
}
func compareDBs(t *testing.T, a, b []byte) {
if len(a) != len(b) {
t.Fatal("lengths are different")
}
for i := range a {
// These may be different.
switch {
case 24 <= i && i < 28:
// File change counter.
continue
case 40 <= i && i < 44:
// Schema cookie.
continue
case 92 <= i && i < 100:
// SQLite version that wrote the file.
continue
}
if a[i] != b[i] {
t.Errorf("difference at %d: %d %d", i, a[i], b[i])
}
}
}
func httpGet() ([]byte, error) {
res, err := http.Get("https://github.com/jpwhite3/northwind-SQLite3/raw/refs/heads/main/dist/northwind.db")
if err != nil {
return nil, err
}
defer res.Body.Close()
return io.ReadAll(res.Body)
}
func TestOpen_errors(t *testing.T) {
_, err := sqlite3.OpenContext(testcfg.Context(t), "file:test.db?vfs=github.com/hanzoai/sqlite3/ext/serdes.sliceVFS")
if err == nil {
t.Error("want error")
}
if !errors.Is(err, sqlite3.CANTOPEN) {
t.Errorf("got %v, want sqlite3.CANTOPEN", err)
}
_, err = sqlite3.OpenContext(testcfg.Context(t), "file:serdes.db?vfs=github.com/hanzoai/sqlite3/ext/serdes.sliceVFS")
if err == nil {
t.Error("want error")
}
if !errors.Is(err, sqlite3.MISUSE) {
t.Errorf("got %v, want sqlite3.MISUSE", err)
}
}
BIN
View File
Binary file not shown.
-14
View File
@@ -1,14 +0,0 @@
// Package spellfix1 provides the spellfix1 virtual table.
//
// https://sqlite.org/spellfix1.html
package spellfix1
import (
"github.com/hanzoai/sqlite3"
"github.com/ncruces/go-sqlite3-wasm/v3/spellfix"
)
// Register registers the spellfix1 virtual table.
func Register(db *sqlite3.Conn) error {
return sqlite3.ExtensionInit(db, spellfix.New, spellfix.DylinkInfo)
}

Some files were not shown because too many files have changed in this diff Show More