Skip to content

Commit

Permalink
destroy config when closing database
Browse files Browse the repository at this point in the history
  • Loading branch information
levakin committed Jan 19, 2024
1 parent e440fc5 commit f122ad8
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions duckdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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)

Check failure on line 118 in duckdb.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, 1.18)

cannot use _cgo0 (variable of type _Ctype_duckdb_config) as type *_Ctype_duckdb_config in argument to _Cfunc_duckdb_destroy_config

Check failure on line 118 in duckdb.go

View workflow job for this annotation

GitHub Actions / Test (macos-12, 1.18)

cannot use _cgo0 (variable of type _Ctype_duckdb_config) as type *_Ctype_duckdb_config in argument to _Cfunc_duckdb_destroy_config
c.config = nil

return nil
}

Expand All @@ -115,6 +126,7 @@ func extractConnectionString(dataSourceName string) string {
if queryIndex < 0 {
queryIndex = len(dataSourceName)
}

return dataSourceName[0:queryIndex]
}

Expand All @@ -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
}
}
Expand Down

0 comments on commit f122ad8

Please sign in to comment.