Skip to content

Commit

Permalink
Add ability to a int64 file control
Browse files Browse the repository at this point in the history
  • Loading branch information
jonstacks committed Nov 22, 2024
1 parent 82bc911 commit e2bae49
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
28 changes: 28 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,34 @@ func (c *SQLiteConn) SetFileControlInt(dbName string, op int, arg int) error {
return nil
}

// SetFileControlInt invokes the xFileControl method on a given database. The
// dbName is the name of the database. It will default to "main" if left blank.
// The op is one of the opcodes prefixed by "SQLITE_FCNTL_". The arg argument
// and return code are both opcode-specific. Please see the SQLite documentation.
//
// This method is not thread-safe as the returned error code can be changed by
// another call if invoked concurrently.
//
// Prefer this method over SetFileControlInt when the argument to the underlying
// SQLite function is an int64.
//
// See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
func (c *SQLiteConn) SetFileControlInt64(dbName string, op int, arg int64) error {
if dbName == "" {
dbName = "main"
}

cDBName := C.CString(dbName)
defer C.free(unsafe.Pointer(cDBName))

cArg := C.sqlite3_int64(arg)
rv := C.sqlite3_file_control(c.db, cDBName, C.int(op), unsafe.Pointer(&cArg))
if rv != C.SQLITE_OK {
return c.lastError()
}
return nil
}

// Close the statement.
func (s *SQLiteStmt) Close() error {
s.mu.Lock()
Expand Down
29 changes: 29 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,35 @@ func TestSetFileControlInt(t *testing.T) {
})
}

func TestSetFileControlInt64(t *testing.T) {
const GiB = 1024 * 1024 * 1024

t.Run("", func(t *testing.T) {

sql.Register("sqlite3_FCNTL_SIZE_LIMIT", &SQLiteDriver{
ConnectHook: func(conn *SQLiteConn) error {
if err := conn.SetFileControlInt64("", SQLITE_FCNTL_SIZE_LIMIT, 4*GiB); err != nil {
return fmt.Errorf("Unexpected error from SetFileControlInt64(): %w", err)
}
return nil
},
})

db, err := sql.Open("sqlite3", "file:/dbname?vfs=memdb")
if err != nil {
t.Fatal("Failed to open database:", err)
}
if err != nil {
t.Fatal("Failed to open", err)
}
err = db.Ping()
if err != nil {
t.Fatal("Failed to ping", err)
}
db.Close()
})
}

func TestNonColumnString(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
Expand Down

0 comments on commit e2bae49

Please sign in to comment.