@@ -68,31 +68,10 @@ type DB struct {
6868 declTypes map [string ]string
6969}
7070
71- // cStmt is a wrapper around an sqlite3 *sqlite3_stmt. Except rather than
72- // storing it as a pointer, it's stored as uintptr to avoid allocations due to
73- // poor interactions between cgo's pointer checker and Go's escape analysis.
74- //
75- // The ptr method returns the value as a pointer, for call sites that haven't
76- // yet been optimized or don't need the optimization. This lets us migrate
77- // incrementally.
78- //
79- // See http://go/corp/9919.
80- type cStmt struct {
81- v C.handle_sqlite3_stmt
82- }
83-
84- // cStmtFromPtr returns a cStmt from a C pointer.
85- func cStmtFromPtr (p * C.sqlite3_stmt ) cStmt {
86- return cStmt {v : C .handle_sqlite3_stmt (uintptr (unsafe .Pointer (p )))}
87- }
88-
89- func (h cStmt ) int () C.handle_sqlite3_stmt { return h .v }
90- func (h cStmt ) ptr () * C.sqlite3_stmt { return (* C .sqlite3_stmt )(unsafe .Pointer (uintptr (h .v ))) }
91-
9271// Stmt implements sqliteh.Stmt.
9372type Stmt struct {
9473 db * DB
95- stmt cStmt
74+ stmt * C. sqlite3_stmt
9675 start C.struct_timespec
9776
9877 // used as scratch space when calling into cgo
@@ -202,7 +181,7 @@ func (db *DB) Prepare(query string, prepFlags sqliteh.PrepareFlags) (stmt sqlite
202181 return nil , "" , err
203182 }
204183 remainingQuery = query [len (query )- int (C .strlen (csqlTail )):]
205- return & Stmt {db : db , stmt : cStmtFromPtr ( cstmt ) }, remainingQuery , nil
184+ return & Stmt {db : db , stmt : cstmt }, remainingQuery , nil
206185}
207186
208187func (db * DB ) DisableFunction (name string , numArgs int ) error {
@@ -212,45 +191,45 @@ func (db *DB) DisableFunction(name string, numArgs int) error {
212191}
213192
214193func (stmt * Stmt ) DBHandle () sqliteh.DB {
215- cdb := C .sqlite3_db_handle (stmt .stmt . ptr () )
194+ cdb := C .sqlite3_db_handle (stmt .stmt )
216195 if cdb != nil {
217196 return & DB {db : cdb }
218197 }
219198 return nil
220199}
221200
222201func (stmt * Stmt ) SQL () string {
223- return C .GoString (C .sqlite3_sql (stmt .stmt . ptr () ))
202+ return C .GoString (C .sqlite3_sql (stmt .stmt ))
224203}
225204
226205func (stmt * Stmt ) ExpandedSQL () string {
227206 // sqlite3_expanded_sql returns a string obtained by sqlite3_malloc, which
228207 // must be freed after use.
229- cstr := C .sqlite3_expanded_sql (stmt .stmt . ptr () )
208+ cstr := C .sqlite3_expanded_sql (stmt .stmt )
230209 defer C .sqlite3_free (unsafe .Pointer (cstr ))
231210 return C .GoString (cstr )
232211}
233212
234213func (stmt * Stmt ) Reset () error {
235- return errCode (C .sqlite3_reset (stmt .stmt . ptr () ))
214+ return errCode (C .sqlite3_reset (stmt .stmt ))
236215}
237216
238217func (stmt * Stmt ) Finalize () error {
239- return errCode (C .sqlite3_finalize (stmt .stmt . ptr () ))
218+ return errCode (C .sqlite3_finalize (stmt .stmt ))
240219}
241220
242221func (stmt * Stmt ) ClearBindings () error {
243- return errCode (C .sqlite3_clear_bindings (stmt .stmt . ptr () ))
222+ return errCode (C .sqlite3_clear_bindings (stmt .stmt ))
244223}
245224
246225func (stmt * Stmt ) ResetAndClear () (time.Duration , error ) {
247226 if stmt .start != (C.struct_timespec {}) {
248227 stmt .duration = 0
249- err := errCode (C .reset_and_clear (stmt .stmt . int () , & stmt .start , & stmt .duration ))
228+ err := errCode (C .reset_and_clear (stmt .stmt , & stmt .start , & stmt .duration ))
250229 return time .Duration (stmt .duration ), err
251230 }
252- if sp := stmt .stmt . int (); sp != 0 {
253- return 0 , errCode (C .reset_and_clear (stmt .stmt . int () , nil , nil ))
231+ if stmt .stmt != nil {
232+ return 0 , errCode (C .reset_and_clear (stmt .stmt , nil , nil ))
254233 }
255234 // The statement was never initialized. This can happen if, for example, the
256235 // parser found only comments (so the statement was not empty, but did not
@@ -263,19 +242,19 @@ func (stmt *Stmt) StartTimer() {
263242}
264243
265244func (stmt * Stmt ) ColumnDatabaseName (col int ) string {
266- return C .GoString ((* C .char )(unsafe .Pointer (C .sqlite3_column_database_name (stmt .stmt . ptr () , C .int (col )))))
245+ return C .GoString ((* C .char )(unsafe .Pointer (C .sqlite3_column_database_name (stmt .stmt , C .int (col )))))
267246}
268247
269248func (stmt * Stmt ) ColumnTableName (col int ) string {
270- return C .GoString ((* C .char )(unsafe .Pointer (C .sqlite3_column_table_name (stmt .stmt . ptr () , C .int (col )))))
249+ return C .GoString ((* C .char )(unsafe .Pointer (C .sqlite3_column_table_name (stmt .stmt , C .int (col )))))
271250}
272251
273252func (stmt * Stmt ) Step (colType []sqliteh.ColumnType ) (row bool , err error ) {
274253 var ptr * C.char
275254 if len (colType ) > 0 {
276255 ptr = (* C .char )(unsafe .Pointer (& colType [0 ]))
277256 }
278- res := C .ts_sqlite3_step (stmt .stmt . int () , ptr , C .int (len (colType )))
257+ res := C .ts_sqlite3_step (stmt .stmt , ptr , C .int (len (colType )))
279258 switch res {
280259 case C .SQLITE_ROW :
281260 return true , nil
@@ -288,7 +267,7 @@ func (stmt *Stmt) Step(colType []sqliteh.ColumnType) (row bool, err error) {
288267
289268func (stmt * Stmt ) StepResult () (row bool , lastInsertRowID , changes int64 , d time.Duration , err error ) {
290269 stmt .rowid , stmt .changes , stmt .duration = 0 , 0 , 0
291- res := C .step_result (stmt .stmt . int () , & stmt .rowid , & stmt .changes , & stmt .duration )
270+ res := C .step_result (stmt .stmt , & stmt .rowid , & stmt .changes , & stmt .duration )
292271 lastInsertRowID = int64 (stmt .rowid )
293272 changes = int64 (stmt .changes )
294273 d = time .Duration (stmt .duration )
@@ -304,51 +283,51 @@ func (stmt *Stmt) StepResult() (row bool, lastInsertRowID, changes int64, d time
304283}
305284
306285func (stmt * Stmt ) BindDouble (col int , val float64 ) error {
307- return errCode (C .ts_sqlite3_bind_double (stmt .stmt . int () , C .int (col ), C .double (val )))
286+ return errCode (C .sqlite3_bind_double (stmt .stmt , C .int (col ), C .double (val )))
308287}
309288
310289func (stmt * Stmt ) BindInt64 (col int , val int64 ) error {
311- return errCode (C .ts_sqlite3_bind_int64 (stmt .stmt . int () , C .int (col ), C .sqlite3_int64 (val )))
290+ return errCode (C .sqlite3_bind_int64 (stmt .stmt , C .int (col ), C .sqlite3_int64 (val )))
312291}
313292
314293func (stmt * Stmt ) BindNull (col int ) error {
315- return errCode (C .ts_sqlite3_bind_null (stmt .stmt . int () , C .int (col )))
294+ return errCode (C .sqlite3_bind_null (stmt .stmt , C .int (col )))
316295}
317296
318297func (stmt * Stmt ) BindText64 (col int , val string ) error {
319298 if len (val ) == 0 {
320- return errCode (C .bind_text64_empty (stmt .stmt . int () , C .int (col )))
299+ return errCode (C .bind_text64_empty (stmt .stmt , C .int (col )))
321300 }
322301 v := C .CString (val ) // freed by sqlite
323- return errCode (C .bind_text64 (stmt .stmt . int () , C .int (col ), v , C .sqlite3_uint64 (len (val ))))
302+ return errCode (C .bind_text64 (stmt .stmt , C .int (col ), v , C .sqlite3_uint64 (len (val ))))
324303}
325304
326305func (stmt * Stmt ) BindZeroBlob64 (col int , n uint64 ) error {
327- return errCode (C .sqlite3_bind_zeroblob64 (stmt .stmt . ptr () , C .int (col ), C .sqlite3_uint64 (n )))
306+ return errCode (C .sqlite3_bind_zeroblob64 (stmt .stmt , C .int (col ), C .sqlite3_uint64 (n )))
328307}
329308
330309func (stmt * Stmt ) BindBlob64 (col int , val []byte ) error {
331310 var str * C.char
332311 if len (val ) > 0 {
333312 str = (* C .char )(unsafe .Pointer (& val [0 ]))
334313 }
335- return errCode (C .bind_blob64 (stmt .stmt . int () , C .int (col ), str , C .sqlite3_uint64 (len (val ))))
314+ return errCode (C .bind_blob64 (stmt .stmt , C .int (col ), str , C .sqlite3_uint64 (len (val ))))
336315}
337316
338317func (stmt * Stmt ) BindParameterCount () int {
339- return int (C .sqlite3_bind_parameter_count (stmt .stmt . ptr () ))
318+ return int (C .sqlite3_bind_parameter_count (stmt .stmt ))
340319}
341320
342321func (stmt * Stmt ) BindParameterName (col int ) string {
343- cstr := C .sqlite3_bind_parameter_name (stmt .stmt . ptr () , C .int (col ))
322+ cstr := C .sqlite3_bind_parameter_name (stmt .stmt , C .int (col ))
344323 if cstr == nil {
345324 return ""
346325 }
347326 return C .GoString (cstr )
348327}
349328
350329func (stmt * Stmt ) BindParameterIndex (name string ) int {
351- return int (C .bind_parameter_index (stmt .stmt . int () , name ))
330+ return int (C .bind_parameter_index (stmt .stmt , name ))
352331}
353332
354333func (stmt * Stmt ) BindParameterIndexSearch (name string ) int {
@@ -363,45 +342,45 @@ func (stmt *Stmt) BindParameterIndexSearch(name string) int {
363342}
364343
365344func (stmt * Stmt ) ColumnCount () int {
366- return int (C .sqlite3_column_count (stmt .stmt . ptr () ))
345+ return int (C .sqlite3_column_count (stmt .stmt ))
367346}
368347
369348func (stmt * Stmt ) ColumnName (col int ) string {
370- return C .GoString (C .sqlite3_column_name (stmt .stmt . ptr () , C .int (col )))
349+ return C .GoString (C .sqlite3_column_name (stmt .stmt , C .int (col )))
371350}
372351
373352func (stmt * Stmt ) ColumnText (col int ) string {
374- str := (* C .char )(unsafe .Pointer (C .ts_sqlite3_column_text (stmt .stmt . int () , C .int (col ))))
375- n := C .ts_sqlite3_column_bytes (stmt .stmt . int () , C .int (col ))
353+ str := (* C .char )(unsafe .Pointer (C .ts_sqlite3_column_text (stmt .stmt , C .int (col ))))
354+ n := C .ts_sqlite3_column_bytes (stmt .stmt , C .int (col ))
376355 if str == nil || n == 0 {
377356 return ""
378357 }
379358 return C .GoStringN (str , n )
380359}
381360
382361func (stmt * Stmt ) ColumnBlob (col int ) []byte {
383- res := C .ts_sqlite3_column_blob (stmt .stmt . int () , C .int (col ))
362+ res := C .ts_sqlite3_column_blob (stmt .stmt , C .int (col ))
384363 if res == nil {
385364 return nil
386365 }
387- n := int (C .ts_sqlite3_column_bytes (stmt .stmt . int () , C .int (col )))
366+ n := int (C .ts_sqlite3_column_bytes (stmt .stmt , C .int (col )))
388367 return unsafe .Slice ((* byte )(unsafe .Pointer (res )), n )
389368}
390369
391370func (stmt * Stmt ) ColumnDouble (col int ) float64 {
392- return float64 (C .ts_sqlite3_column_double (stmt .stmt . int () , C .int (col )))
371+ return float64 (C .ts_sqlite3_column_double (stmt .stmt , C .int (col )))
393372}
394373
395374func (stmt * Stmt ) ColumnInt64 (col int ) int64 {
396- return int64 (C .ts_sqlite3_column_int64 (stmt .stmt . int () , C .int (col )))
375+ return int64 (C .ts_sqlite3_column_int64 (stmt .stmt , C .int (col )))
397376}
398377
399378func (stmt * Stmt ) ColumnType (col int ) sqliteh.ColumnType {
400- return sqliteh .ColumnType (C .ts_sqlite3_column_type (stmt .stmt . int () , C .int (col )))
379+ return sqliteh .ColumnType (C .ts_sqlite3_column_type (stmt .stmt , C .int (col )))
401380}
402381
403382func (stmt * Stmt ) ColumnDeclType (col int ) string {
404- cstr := C .sqlite3_column_decltype (stmt .stmt . ptr () , C .int (col ))
383+ cstr := C .sqlite3_column_decltype (stmt .stmt , C .int (col ))
405384 if cstr == nil {
406385 return ""
407386 }
0 commit comments