@@ -55,8 +55,8 @@ func init() {
5555//
5656// The init function is called by the driver on new connections.
5757// The conn can be used to execute queries, register functions, etc.
58- // Any error return closes the conn and passes the error to database/sql.
59- func Open (dataSourceName string , init func (ctx context. Context , conn * sqlite3.Conn ) error ) (* sql.DB , error ) {
58+ // Any error return closes the conn and passes the error to [ database/sql] .
59+ func Open (dataSourceName string , init func (* sqlite3.Conn ) error ) (* sql.DB , error ) {
6060 c , err := newConnector (dataSourceName , init )
6161 if err != nil {
6262 return nil , err
@@ -78,7 +78,7 @@ func (sqlite) OpenConnector(name string) (driver.Connector, error) {
7878 return newConnector (name , nil )
7979}
8080
81- func newConnector (name string , init func (ctx context. Context , conn * sqlite3.Conn ) error ) (* connector , error ) {
81+ func newConnector (name string , init func (* sqlite3.Conn ) error ) (* connector , error ) {
8282 c := connector {name : name , init : init }
8383 if strings .HasPrefix (name , "file:" ) {
8484 if _ , after , ok := strings .Cut (name , "?" ); ok {
@@ -94,7 +94,7 @@ func newConnector(name string, init func(ctx context.Context, conn *sqlite3.Conn
9494}
9595
9696type connector struct {
97- init func (ctx context. Context , conn * sqlite3.Conn ) error
97+ init func (* sqlite3.Conn ) error
9898 name string
9999 txlock string
100100 pragmas bool
@@ -132,27 +132,24 @@ func (n *connector) Connect(ctx context.Context) (_ driver.Conn, err error) {
132132 if err != nil {
133133 return nil , err
134134 }
135- c .reusable = true
136- } else {
137- s , _ , err := c .Conn .Prepare (`
138- SELECT * FROM
139- PRAGMA_locking_mode,
140- PRAGMA_query_only;
141- ` )
135+ }
136+ if n .init != nil {
137+ err = n .init (c .Conn )
142138 if err != nil {
143139 return nil , err
144140 }
145- if s .Step () {
146- c .reusable = s .ColumnText (0 ) == "normal"
147- c .readOnly = s .ColumnRawText (1 )[0 ] // 0 or 1
148- }
149- err = s .Close ()
141+ }
142+ if n .pragmas || n .init != nil {
143+ s , _ , err := c .Conn .Prepare (`PRAGMA query_only` )
150144 if err != nil {
151145 return nil , err
152146 }
153- }
154- if n .init != nil {
155- err = n .init (ctx , c .Conn )
147+ if s .Step () && s .ColumnBool (0 ) {
148+ c .readOnly = '1'
149+ } else {
150+ c .readOnly = '0'
151+ }
152+ err = s .Close ()
156153 if err != nil {
157154 return nil , err
158155 }
@@ -165,7 +162,6 @@ type conn struct {
165162 txBegin string
166163 txCommit string
167164 txRollback string
168- reusable bool
169165 readOnly byte
170166}
171167
@@ -174,18 +170,13 @@ var (
174170 _ driver.ConnPrepareContext = & conn {}
175171 _ driver.ExecerContext = & conn {}
176172 _ driver.ConnBeginTx = & conn {}
177- _ driver.Validator = & conn {}
178173 _ sqlite3.DriverConn = & conn {}
179174)
180175
181176func (c * conn ) Raw () * sqlite3.Conn {
182177 return c .Conn
183178}
184179
185- func (c * conn ) IsValid () bool {
186- return c .reusable
187- }
188-
189180func (c * conn ) Begin () (driver.Tx , error ) {
190181 return c .BeginTx (context .Background (), driver.TxOptions {})
191182}
@@ -199,10 +190,10 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
199190 txBegin = `
200191 BEGIN deferred;
201192 PRAGMA query_only=on`
202- c .txCommit = `
193+ c .txRollback = `
203194 ROLLBACK;
204195 PRAGMA query_only=` + string (c .readOnly )
205- c .txRollback = c .txCommit
196+ c .txCommit = c .txRollback
206197 }
207198
208199 switch opts .Isolation {
0 commit comments