diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2022-01-28 08:37:05 -0700 |
---|---|---|
committer | mattn <mattn.jp@gmail.com> | 2022-01-29 01:58:27 +0900 |
commit | ae2a61f847e10e6dd771ecd4e1c55e0421cdc7f9 (patch) | |
tree | 7afef61544d9d1b6a4e08e13a847fee8201cbe1f /sqlite3_test.go | |
parent | Add example using driverName (diff) | |
download | golite-ae2a61f847e10e6dd771ecd4e1c55e0421cdc7f9.tar.gz golite-ae2a61f847e10e6dd771ecd4e1c55e0421cdc7f9.tar.xz |
Add sqlite3_file_control() support
This commit adds the SQLiteConn.FileControlInt() method which calls the
underlying sqlite3_file_control() function with an int argument. This can
be used for low-level operations on SQLite databases such as persisting
the WAL file after database close.
Diffstat (limited to 'sqlite3_test.go')
-rw-r--r-- | sqlite3_test.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/sqlite3_test.go b/sqlite3_test.go index c5deb3e..c86aba4 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -3,6 +3,7 @@ // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. +//go:build cgo // +build cgo package sqlite3 @@ -1722,6 +1723,43 @@ func TestAuthorizer(t *testing.T) { } } +func TestSetFileControlInt(t *testing.T) { + t.Run("PERSIST_WAL", func(t *testing.T) { + tempFilename := TempFilename(t) + defer os.Remove(tempFilename) + + sql.Register("sqlite3_FCNTL_PERSIST_WAL", &SQLiteDriver{ + ConnectHook: func(conn *SQLiteConn) error { + if err := conn.SetFileControlInt("", SQLITE_FCNTL_PERSIST_WAL, 1); err != nil { + return fmt.Errorf("Unexpected error from SetFileControlInt(): %w", err) + } + return nil + }, + }) + + db, err := sql.Open("sqlite3_FCNTL_PERSIST_WAL", tempFilename) + if err != nil { + t.Fatal("Failed to open database:", err) + } + defer db.Close() + + // Set to WAL mode & write a page. + if _, err := db.Exec(`PRAGMA journal_mode = wal`); err != nil { + t.Fatal("Failed to set journal mode:", err) + } else if _, err := db.Exec(`CREATE TABLE t (x)`); err != nil { + t.Fatal("Failed to create table:", err) + } + if err := db.Close(); err != nil { + t.Fatal("Failed to close database", err) + } + + // Ensure WAL file persists after close. + if _, err := os.Stat(tempFilename + "-wal"); err != nil { + t.Fatal("Expected WAL file to be persisted after close", err) + } + }) +} + func TestNonColumnString(t *testing.T) { db, err := sql.Open("sqlite3", ":memory:") if err != nil { |