From f122ad8803def7ceaf9c1a90f904f5db0ff4eefd Mon Sep 17 00:00:00 2001 From: Anton Levakin Date: Fri, 19 Jan 2024 17:13:42 +0100 Subject: [PATCH] destroy config when closing database DuckDB destroys config when closing database. e.g. here https://github.com/duckdb/duckdb/blob/b58f184e1e87df5fc2d574996a6b63409df96f47/src/common/adbc/adbc.cpp#L218-L228 --- duckdb.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/duckdb.go b/duckdb.go index af92cab5..96bfb6e8 100644 --- a/duckdb.go +++ b/duckdb.go @@ -52,6 +52,8 @@ func NewConnector(dsn string, connInitFn func(execer driver.ExecerContext) error connectionString := C.CString(extractConnectionString(dsn)) defer C.free(unsafe.Pointer(connectionString)) + connector := &Connector{db: &db, connInitFn: connInitFn} + // Check for config options. if len(parsedDSN.RawQuery) == 0 { var errMsg *C.char @@ -66,10 +68,14 @@ func NewConnector(dsn string, connInitFn func(execer driver.ExecerContext) error return nil, err } + connector.config = config + var errMsg *C.char defer C.duckdb_free(unsafe.Pointer(errMsg)) if state := C.duckdb_open_ext(connectionString, &db, config, &errMsg); state == C.DuckDBError { + C.duckdb_destroy_config(&config) + return nil, fmt.Errorf("%w: %s", errOpen, C.GoString(errMsg)) } } @@ -79,6 +85,7 @@ func NewConnector(dsn string, connInitFn func(execer driver.ExecerContext) error type Connector struct { db *C.duckdb_database + config C.duckdb_config connInitFn func(execer driver.ExecerContext) error } @@ -107,6 +114,10 @@ func (c *Connector) Connect(context.Context) (driver.Conn, error) { func (c *Connector) Close() error { C.duckdb_close(c.db) c.db = nil + + C.duckdb_destroy_config(c.config) + c.config = nil + return nil } @@ -115,6 +126,7 @@ func extractConnectionString(dataSourceName string) string { if queryIndex < 0 { queryIndex = len(dataSourceName) } + return dataSourceName[0:queryIndex] } @@ -127,6 +139,8 @@ func prepareConfig(options map[string][]string) (C.duckdb_config, error) { for k, v := range options { if len(v) > 0 { if err := setConfig(config, k, v[0]); err != nil { + C.duckdb_destroy_config(&config) + return nil, err } }