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 23, 2024
1 parent 76bcc7e commit 024a64e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions duckdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func NewConnector(dsn string, connInitFn func(execer driver.ExecerContext) error
if err != nil {
return nil, err
}
defer C.duckdb_destroy_config(&config)

connectionString := C.CString(extractConnectionString(dsn))
defer C.free(unsafe.Pointer(connectionString))
Expand All @@ -62,14 +61,21 @@ func NewConnector(dsn string, connInitFn func(execer driver.ExecerContext) error
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))
}

return &Connector{db: &db, connInitFn: connInitFn}, nil
return &Connector{
db: &db,
connInitFn: connInitFn,
config: config,
}, nil
}

type Connector struct {
db *C.duckdb_database
config C.duckdb_config
connInitFn func(execer driver.ExecerContext) error
}

Expand Down Expand Up @@ -98,6 +104,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
}

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

return dataSourceName[0:queryIndex]
}

Expand All @@ -122,6 +133,8 @@ func prepareConfig(parsedDSN *url.URL) (C.duckdb_config, error) {
for k, v := range parsedDSN.Query() {
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 024a64e

Please sign in to comment.