Skip to content

Commit f8c7ee1

Browse files
committed
sqlite: let ConnInitFunc use ExecScript
Got about 1 minute into using this before I figured out how to improve it. :) Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
1 parent a0d07b1 commit f8c7ee1

File tree

2 files changed

+6
-15
lines changed

2 files changed

+6
-15
lines changed

sqlite.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
1414
// tracing, use the Connector function:
1515
//
1616
// connInitFunc := func(ctx context.Context, conn driver.ConnPrepareContext) error {
17-
// called++
18-
// stmt, err := conn.PrepareContext(ctx, "PRAGMA journal_mode=WAL;")
19-
// if err != nil {
20-
// return err
21-
// }
22-
// _, err = stmt.(driver.StmtExecContext).ExecContext(ctx, nil)
23-
// return err
17+
// return sqlite.ExecScript(conn.(sqlite.SQLConn), "PRAGMA journal_mode=WAL;")
2418
// }
2519
// db, err = sql.OpenDB(sqlite.Connector(sqliteURI, connInitFunc, nil))
2620
//
@@ -112,7 +106,7 @@ type TraceFunc func(prepCtx context.Context, query string, duration time.Duratio
112106

113107
// ConnInitFunc is a function called by the driver on new connections.
114108
//
115-
// The conn can be used to execute queries.
109+
// The conn can be used to execute queries, and implements SQLConn.
116110
// Any error return closes the conn and passes the error to database/sql.
117111
type ConnInitFunc func(ctx context.Context, conn driver.ConnPrepareContext) error
118112

@@ -279,6 +273,9 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
279273
return &connTx{c: c}, nil
280274
}
281275

276+
// Raw is so ConnInitFunc can cast to SQLConn.
277+
func (c *conn) Raw(fn func(interface{}) error) error { return fn(c) }
278+
282279
type connTx struct {
283280
c *conn
284281
}

sqlite_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,7 @@ func TestConnInit(t *testing.T) {
617617
uri := "file:" + t.TempDir() + "/test.db"
618618
connInitFunc := func(ctx context.Context, conn driver.ConnPrepareContext) error {
619619
called++
620-
stmt, err := conn.PrepareContext(ctx, "PRAGMA journal_mode=WAL;")
621-
if err != nil {
622-
return err
623-
}
624-
_, err = stmt.(driver.StmtExecContext).ExecContext(ctx, nil)
625-
stmt.Close()
626-
return err
620+
return ExecScript(conn.(SQLConn), "PRAGMA journal_mode=WAL;")
627621
}
628622
db := sql.OpenDB(Connector(uri, connInitFunc, nil))
629623
conn, err := db.Conn(context.Background())

0 commit comments

Comments
 (0)