Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: go-acc . -- -race -v -tags "libsqlite3"

- name: 'Tags: full'
run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_vacuum_incr sqlite_vtable"
run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_dbpage sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_vacuum_incr sqlite_vtable"

- name: 'Tags: vacuum'
run: go-acc . -- -race -v -tags "sqlite_vacuum_full"
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
- name: 'Tags: full'
run: |
echo 'skip this test'
echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_vacuum_incr sqlite_vtable"
echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_dbpage sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_vacuum_incr sqlite_vtable"
shell: msys2 {0}

- name: 'Tags: vacuum'
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ go build -tags "icu json1 fts5 secure_delete"
| Tracing / Debug | sqlite_trace | Activate trace functions |
| User Authentication | sqlite_userauth | SQLite User Authentication see [User Authentication](#user-authentication) for more information. |
| Virtual Tables | sqlite_vtable | SQLite Virtual Tables see [SQLite Official VTABLE Documentation](https://www.sqlite.org/vtab.html) for more information, and a [full example here](https://github.com/mattn/go-sqlite3/tree/master/_example/vtable) |
| SQLITE_DBPAGE Virtual Table | sqlite_dbpage | SQLITE_DBPAGE Virtual Table see [SQLite Official SQLITE_DBPAGE Documentation](https://www.sqlite.org/dbpage.html) for more information. |
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling SQLITE_ENABLE_DBPAGE_VTAB allows reading and (on newer SQLite versions) writing/deleting raw pages via SQL, which is unsafe when executing untrusted SQL. Consider adding a short warning in this README entry about the security implications and that it should only be enabled for trusted use-cases.

Copilot uses AI. Check for mistakes.

# Compilation

Expand Down
15 changes: 15 additions & 0 deletions sqlite3_opt_dbpage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

//go:build sqlite_dbpage
// +build sqlite_dbpage

package sqlite3

/*
#cgo CFLAGS: -DSQLITE_ENABLE_DBPAGE_VTAB
#cgo LDFLAGS: -lm
*/
import "C"
89 changes: 89 additions & 0 deletions sqlite3_opt_dbpage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

//go:build sqlite_dbpage && cgo
// +build sqlite_dbpage,cgo

package sqlite3

import (
"database/sql"
"os"
"testing"
)

func TestDbpage(t *testing.T) {
sourceFilename := TempFilename(t)
defer os.Remove(sourceFilename)

destFilename := TempFilename(t)
defer os.Remove(destFilename)

db, err := sql.Open("sqlite3", sourceFilename)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()

if _, err = db.Exec("PRAGMA journal_mode=WAL"); err != nil {
t.Fatal("Failed to Exec PRAGMA journal_mode:", err)
} else if _, err := db.Exec("CREATE TABLE foo(data TEXT)"); err != nil {
t.Fatal("Failed to create table:", err)
} else if _, err := db.Exec("INSERT INTO foo(data) VALUES(?)", "hello sqlite_dbpage"); err != nil {
t.Fatal("Failed to create table:", err)
}

rows, err := db.Query("SELECT data FROM sqlite_dbpage ORDER BY pgno")
if err != nil && err.Error() == "no such table: sqlite_dbpage" {
t.Skip("sqlite_dbpage not supported")
} else if err != nil {
t.Fatal("Unable to query sqlite_dbpage table:", err)
}
defer rows.Close()

destFile, err := os.OpenFile(destFilename, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
t.Fatal("Unable to open file for writing:", err)
}
defer destFile.Close()

for rows.Next() {
var page []byte
if err := rows.Scan(&page); err != nil {
t.Fatal("Unable to scan results:", err)
}

if _, err := destFile.Write(page); err != nil {
t.Fatal("Unable to write page to file:", err)
}
Comment on lines +52 to +60
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appending each page to the output file assumes pages are returned contiguously starting at page 1. To reliably reconstruct a database, capture pgno and write each page at offset (pgno-1)*pageSize (and truncate the file first), rather than using a plain sequential Write.

Copilot uses AI. Check for mistakes.
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop over rows.Next() should check rows.Err() after iteration to catch any I/O/scan errors that occur during row streaming.

Suggested change
}
}
if err := rows.Err(); err != nil {
t.Fatal("Error iterating over rows:", err)
}

Copilot uses AI. Check for mistakes.
if err := rows.Close(); err != nil {
t.Fatal("Unable to close rows:", err)
} else if err := db.Close(); err != nil {
t.Fatal("Unable to close database:", err)
} else if err := destFile.Close(); err != nil {
t.Fatal("Unable to close file:", err)
}
Comment on lines +62 to +68
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test mixes defer Close() with explicit Close() calls later. That results in double-closes (especially for the file), and the deferred closes ignore any error. Prefer a single close strategy (typically defers that check errors in a deferred func) to avoid masking issues and reduce confusion.

Copilot uses AI. Check for mistakes.

db, err = sql.Open("sqlite3", destFilename)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()

var result string
if err = db.QueryRow("PRAGMA integrity_check").Scan(&result); err != nil {
t.Fatal("Failed to query PRAGMA integrity_check:", err)
} else if result != "ok" {
t.Fatal("Copied database integrity check failed:", result)
}

var hello string
if err = db.QueryRow("SELECT data FROM foo").Scan(&hello); err != nil {
t.Fatal("Failed to query data:", err)
} else if hello != "hello sqlite_dbpage" {
t.Fatal("Unable to find expected data:", hello)
}
}