Skip to content

Commit 5071c16

Browse files
committed
Add driver.Connector implementation for SQLite (#1001)
1 parent 7658c06 commit 5071c16

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

sqlite3.go

100644100755
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,3 +2282,28 @@ func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error {
22822282
}
22832283
return nil
22842284
}
2285+
2286+
// SQLiteConnector implements driver.Connector for custom connection handling.
2287+
type SQLiteConnector struct {
2288+
DSN string
2289+
DriverInstance *SQLiteDriver
2290+
}
2291+
2292+
// Connect implements driver.Connector.
2293+
func (c *SQLiteConnector) Connect(ctx context.Context) (driver.Conn, error) {
2294+
// Context is ignored for now, as SQLiteDriver.Open does not use it.
2295+
return c.DriverInstance.Open(c.DSN)
2296+
}
2297+
2298+
// Driver returns the underlying driver.
2299+
func (c *SQLiteConnector) Driver() driver.Driver {
2300+
return c.DriverInstance
2301+
}
2302+
2303+
// NewConnector returns a new SQLiteConnector.
2304+
func NewConnector(dsn string) *SQLiteConnector {
2305+
return &SQLiteConnector{
2306+
DSN: dsn,
2307+
DriverInstance: &SQLiteDriver{},
2308+
}
2309+
}

sqlite3_test.go

100644100755
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package sqlite3
1010

1111
import (
1212
"bytes"
13+
"context"
1314
"database/sql"
1415
"database/sql/driver"
1516
"errors"
@@ -2586,3 +2587,14 @@ func benchmarkQueryParallel(b *testing.B) {
25862587
}
25872588
})
25882589
}
2590+
2591+
func TestSQLiteConnector(t *testing.T) {
2592+
connector := NewConnector(":memory:")
2593+
db := sql.OpenDB(connector)
2594+
defer db.Close()
2595+
2596+
ctx := context.Background()
2597+
if err := db.PingContext(ctx); err != nil {
2598+
t.Fatalf("PingContext failed: %v", err)
2599+
}
2600+
}

0 commit comments

Comments
 (0)