Skip to content
Merged
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
6 changes: 5 additions & 1 deletion cgosqlite/cgosqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ func (stmt *Stmt) SQL() string {
}

func (stmt *Stmt) ExpandedSQL() string {
return C.GoString(C.sqlite3_expanded_sql(stmt.stmt.ptr()))
// sqlite3_expanded_sql returns a string obtained by sqlite3_malloc, which
// must be freed after use.
cstr := C.sqlite3_expanded_sql(stmt.stmt.ptr())
defer C.sqlite3_free(unsafe.Pointer(cstr))
return C.GoString(cstr)
}

func (stmt *Stmt) Reset() error {
Expand Down
29 changes: 29 additions & 0 deletions sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,3 +1539,32 @@ func TestConnLogger_read_tx(t *testing.T) {
}
}
}

func TestExpandedSQL(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess it's too tedious to make this look for leaks, eh?

(probably not visible to https://pkg.go.dev/runtime#ReadMemStats ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, just too fiddly for me on a Sunday morning. I would like to try it tomorrow though.

ctx := context.Background()
connector := Connector("file:"+t.TempDir()+"/test.db", nil, nil)
sqlConn, err := connector.Connect(ctx)
if err != nil {
t.Fatalf("Connect: %v", err)
}
conn := sqlConn.(*conn)

sqlStmt, err := conn.PrepareContext(ctx, "SELECT ? + ?")
if err != nil {
t.Fatalf("PrepareContext: %v", err)
}
stmt, ok := sqlStmt.(*stmt)
if !ok {
t.Fatalf("not a *stmt: %#v", stmt)
}
if err := stmt.bindAll([]driver.NamedValue{
{Ordinal: 1, Value: 6},
{Ordinal: 2, Value: 7},
}); err != nil {
t.Errorf("bindAll: %v", err)
}

if got, want := stmt.stmt.ExpandedSQL(), "SELECT 6 + 7"; got != want {
t.Errorf("wrong sql: got %q, want %q", got, want)
}
}
Loading