From 6fdf9a58d50a97ca06cb665390f9deb957a357e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20St=C3=BCrmer?= Date: Mon, 12 Aug 2024 11:55:45 +0200 Subject: [PATCH] add support for sqlite3_status64 --- sqlite3.go | 35 +++++++++++++++++++++++++++++++++++ sqlite3_test.go | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/sqlite3.go b/sqlite3.go index ed2a9e2a..d47cd5e6 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -959,6 +959,41 @@ func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.Name } } +type SqliteStatus64Option int + +const ( + SQLITE_STATUS_MEMORY_USED SqliteStatus64Option = 0 + SQLITE_STATUS_PAGECACHE_USED SqliteStatus64Option = 1 + SQLITE_STATUS_PAGECACHE_OVERFLOW SqliteStatus64Option = 2 + SQLITE_STATUS_MALLOC_SIZE SqliteStatus64Option = 5 + SQLITE_STATUS_PARSER_STACK SqliteStatus64Option = 6 + SQLITE_STATUS_PAGECACHE_SIZE SqliteStatus64Option = 7 + SQLITE_STATUS_MALLOC_COUNT SqliteStatus64Option = 9 +) + +// GetStatus64 retrieves runtime status information about the performance of SQLite, +// and optionally resets various high-water marks. +func GetStatus64(op SqliteStatus64Option, resetFlag bool) (current, highWater int64, err error) { + var curr C.sqlite_int64 + var hiwtr C.sqlite_int64 + + reset := C.int(0) + if resetFlag { + reset = C.int(1) + } + + rv := C.sqlite3_status64(C.int(op), &curr, &hiwtr, reset) + if rv != C.SQLITE_OK { + errStr := C.GoString(C.sqlite3_errstr(rv)) + return 0, 0, Error{ + Code: ErrNo(rv), + err: errStr, + } + } + + return int64(curr), int64(hiwtr), nil +} + // Begin transaction. func (c *SQLiteConn) Begin() (driver.Tx, error) { return c.begin(context.Background()) diff --git a/sqlite3_test.go b/sqlite3_test.go index 63c939d3..97d222ea 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1997,6 +1997,12 @@ func TestNamedParam(t *testing.T) { } } +func TestStatus64(t *testing.T) { + if _, _, err := GetStatus64(SQLITE_STATUS_MEMORY_USED, false); err != nil { + t.Fatal("Failed to get status64:", err) + } +} + var customFunctionOnce sync.Once func BenchmarkCustomFunctions(b *testing.B) {