github.com/glebarez/go-sqlite@v1.21.1/sqlite.go (about) 1 // Copyright 2017 The Sqlite Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:generate go run generator.go -full-path-comments 6 7 package sqlite // import "modernc.org/sqlite" 8 9 import ( 10 "context" 11 "database/sql" 12 "database/sql/driver" 13 "fmt" 14 "io" 15 "log" 16 "math" 17 "net/url" 18 "reflect" 19 "strconv" 20 "strings" 21 "sync" 22 "sync/atomic" 23 "time" 24 "unsafe" 25 26 "modernc.org/libc" 27 "modernc.org/libc/sys/types" 28 sqlite3 "modernc.org/sqlite/lib" 29 ) 30 31 var ( 32 _ driver.Conn = (*conn)(nil) 33 _ driver.Driver = (*Driver)(nil) 34 //lint:ignore SA1019 TODO implement ExecerContext 35 _ driver.Execer = (*conn)(nil) 36 //lint:ignore SA1019 TODO implement QueryerContext 37 _ driver.Queryer = (*conn)(nil) 38 _ driver.Result = (*result)(nil) 39 _ driver.Rows = (*rows)(nil) 40 _ driver.RowsColumnTypeDatabaseTypeName = (*rows)(nil) 41 _ driver.RowsColumnTypeLength = (*rows)(nil) 42 _ driver.RowsColumnTypeNullable = (*rows)(nil) 43 _ driver.RowsColumnTypePrecisionScale = (*rows)(nil) 44 _ driver.RowsColumnTypeScanType = (*rows)(nil) 45 _ driver.Stmt = (*stmt)(nil) 46 _ driver.Tx = (*tx)(nil) 47 _ error = (*Error)(nil) 48 ) 49 50 // LogSqlStatements - if true when Open() is called, all SQL statements will be logged to standard output 51 var LogSqlStatements bool 52 53 const ( 54 driverName = "sqlite" 55 ptrSize = unsafe.Sizeof(uintptr(0)) 56 sqliteLockedSharedcache = sqlite3.SQLITE_LOCKED | (1 << 8) 57 ) 58 59 // Error represents sqlite library error code. 60 type Error struct { 61 msg string 62 code int 63 } 64 65 // Error implements error. 66 func (e *Error) Error() string { return e.msg } 67 68 // Code returns the sqlite result code for this error. 69 func (e *Error) Code() int { return e.code } 70 71 var ( 72 // ErrorCodeString maps Error.Code() to its string representation. 73 ErrorCodeString = map[int]string{ 74 sqlite3.SQLITE_ABORT: "Callback routine requested an abort (SQLITE_ABORT)", 75 sqlite3.SQLITE_AUTH: "Authorization denied (SQLITE_AUTH)", 76 sqlite3.SQLITE_BUSY: "The database file is locked (SQLITE_BUSY)", 77 sqlite3.SQLITE_CANTOPEN: "Unable to open the database file (SQLITE_CANTOPEN)", 78 sqlite3.SQLITE_CONSTRAINT: "Abort due to constraint violation (SQLITE_CONSTRAINT)", 79 sqlite3.SQLITE_CORRUPT: "The database disk image is malformed (SQLITE_CORRUPT)", 80 sqlite3.SQLITE_DONE: "sqlite3_step() has finished executing (SQLITE_DONE)", 81 sqlite3.SQLITE_EMPTY: "Internal use only (SQLITE_EMPTY)", 82 sqlite3.SQLITE_ERROR: "Generic error (SQLITE_ERROR)", 83 sqlite3.SQLITE_FORMAT: "Not used (SQLITE_FORMAT)", 84 sqlite3.SQLITE_FULL: "Insertion failed because database is full (SQLITE_FULL)", 85 sqlite3.SQLITE_INTERNAL: "Internal logic error in SQLite (SQLITE_INTERNAL)", 86 sqlite3.SQLITE_INTERRUPT: "Operation terminated by sqlite3_interrupt()(SQLITE_INTERRUPT)", 87 sqlite3.SQLITE_IOERR | (1 << 8): "(SQLITE_IOERR_READ)", 88 sqlite3.SQLITE_IOERR | (10 << 8): "(SQLITE_IOERR_DELETE)", 89 sqlite3.SQLITE_IOERR | (11 << 8): "(SQLITE_IOERR_BLOCKED)", 90 sqlite3.SQLITE_IOERR | (12 << 8): "(SQLITE_IOERR_NOMEM)", 91 sqlite3.SQLITE_IOERR | (13 << 8): "(SQLITE_IOERR_ACCESS)", 92 sqlite3.SQLITE_IOERR | (14 << 8): "(SQLITE_IOERR_CHECKRESERVEDLOCK)", 93 sqlite3.SQLITE_IOERR | (15 << 8): "(SQLITE_IOERR_LOCK)", 94 sqlite3.SQLITE_IOERR | (16 << 8): "(SQLITE_IOERR_CLOSE)", 95 sqlite3.SQLITE_IOERR | (17 << 8): "(SQLITE_IOERR_DIR_CLOSE)", 96 sqlite3.SQLITE_IOERR | (2 << 8): "(SQLITE_IOERR_SHORT_READ)", 97 sqlite3.SQLITE_IOERR | (3 << 8): "(SQLITE_IOERR_WRITE)", 98 sqlite3.SQLITE_IOERR | (4 << 8): "(SQLITE_IOERR_FSYNC)", 99 sqlite3.SQLITE_IOERR | (5 << 8): "(SQLITE_IOERR_DIR_FSYNC)", 100 sqlite3.SQLITE_IOERR | (6 << 8): "(SQLITE_IOERR_TRUNCATE)", 101 sqlite3.SQLITE_IOERR | (7 << 8): "(SQLITE_IOERR_FSTAT)", 102 sqlite3.SQLITE_IOERR | (8 << 8): "(SQLITE_IOERR_UNLOCK)", 103 sqlite3.SQLITE_IOERR | (9 << 8): "(SQLITE_IOERR_RDLOCK)", 104 sqlite3.SQLITE_IOERR: "Some kind of disk I/O error occurred (SQLITE_IOERR)", 105 sqlite3.SQLITE_LOCKED | (1 << 8): "(SQLITE_LOCKED_SHAREDCACHE)", 106 sqlite3.SQLITE_LOCKED: "A table in the database is locked (SQLITE_LOCKED)", 107 sqlite3.SQLITE_MISMATCH: "Data type mismatch (SQLITE_MISMATCH)", 108 sqlite3.SQLITE_MISUSE: "Library used incorrectly (SQLITE_MISUSE)", 109 sqlite3.SQLITE_NOLFS: "Uses OS features not supported on host (SQLITE_NOLFS)", 110 sqlite3.SQLITE_NOMEM: "A malloc() failed (SQLITE_NOMEM)", 111 sqlite3.SQLITE_NOTADB: "File opened that is not a database file (SQLITE_NOTADB)", 112 sqlite3.SQLITE_NOTFOUND: "Unknown opcode in sqlite3_file_control() (SQLITE_NOTFOUND)", 113 sqlite3.SQLITE_NOTICE: "Notifications from sqlite3_log() (SQLITE_NOTICE)", 114 sqlite3.SQLITE_PERM: "Access permission denied (SQLITE_PERM)", 115 sqlite3.SQLITE_PROTOCOL: "Database lock protocol error (SQLITE_PROTOCOL)", 116 sqlite3.SQLITE_RANGE: "2nd parameter to sqlite3_bind out of range (SQLITE_RANGE)", 117 sqlite3.SQLITE_READONLY: "Attempt to write a readonly database (SQLITE_READONLY)", 118 sqlite3.SQLITE_ROW: "sqlite3_step() has another row ready (SQLITE_ROW)", 119 sqlite3.SQLITE_SCHEMA: "The database schema changed (SQLITE_SCHEMA)", 120 sqlite3.SQLITE_TOOBIG: "String or BLOB exceeds size limit (SQLITE_TOOBIG)", 121 sqlite3.SQLITE_WARNING: "Warnings from sqlite3_log() (SQLITE_WARNING)", 122 } 123 ) 124 125 func init() { 126 sql.Register(driverName, newDriver()) 127 } 128 129 type result struct { 130 lastInsertID int64 131 rowsAffected int 132 } 133 134 func newResult(c *conn) (_ *result, err error) { 135 r := &result{} 136 if r.rowsAffected, err = c.changes(); err != nil { 137 return nil, err 138 } 139 140 if r.lastInsertID, err = c.lastInsertRowID(); err != nil { 141 return nil, err 142 } 143 144 return r, nil 145 } 146 147 // LastInsertId returns the database's auto-generated ID after, for example, an 148 // INSERT into a table with primary key. 149 func (r *result) LastInsertId() (int64, error) { 150 if r == nil { 151 return 0, nil 152 } 153 154 return r.lastInsertID, nil 155 } 156 157 // RowsAffected returns the number of rows affected by the query. 158 func (r *result) RowsAffected() (int64, error) { 159 if r == nil { 160 return 0, nil 161 } 162 163 return int64(r.rowsAffected), nil 164 } 165 166 type rows struct { 167 allocs []uintptr // allocations made for this prepared statement (to be freed) 168 c *conn // connection 169 columns []string // column names 170 pstmt uintptr // correspodning prepared statement 171 } 172 173 func newRows(c *conn, pstmt uintptr, allocs []uintptr) (r *rows, err error) { 174 r = &rows{c: c, pstmt: pstmt, allocs: allocs} 175 176 // deferred close if anything goes wrong 177 defer func() { 178 if err != nil { 179 r.Close() 180 r = nil 181 } 182 }() 183 184 // get columns count 185 n, err := c.columnCount(pstmt) 186 if err != nil { 187 return nil, err 188 } 189 190 // get column names 191 r.columns = make([]string, n) 192 for i := range r.columns { 193 if r.columns[i], err = r.c.columnName(pstmt, i); err != nil { 194 return nil, err 195 } 196 } 197 198 return r, nil 199 } 200 201 // Close closes the rows iterator. 202 func (r *rows) Close() (err error) { 203 // free all allocations made for this rows 204 for _, v := range r.allocs { 205 r.c.free(v) 206 } 207 r.allocs = nil 208 209 // finalize prepared statement 210 return r.c.finalize(r.pstmt) 211 } 212 213 // Columns returns the names of the columns. The number of columns of the 214 // result is inferred from the length of the slice. If a particular column name 215 // isn't known, an empty string should be returned for that entry. 216 func (r *rows) Columns() (c []string) { 217 return r.columns 218 } 219 220 // Next is called to populate the next row of data into the provided slice. The 221 // provided slice will be the same size as the Columns() are wide. 222 // 223 // Next should return io.EOF when there are no more rows. 224 func (r *rows) Next(dest []driver.Value) error { 225 // yet another step 226 rc, err := r.c.step(r.pstmt) 227 if err != nil { 228 return err 229 } 230 231 // analyze error code 232 switch rc { 233 case sqlite3.SQLITE_ROW: 234 if g, e := len(dest), len(r.columns); g != e { 235 return fmt.Errorf("sqlite: Next: have %v destination values, expected %v", g, e) 236 } 237 238 for i := range dest { 239 ct, err := r.c.columnType(r.pstmt, i) 240 if err != nil { 241 return err 242 } 243 244 switch ct { 245 case sqlite3.SQLITE_INTEGER: 246 v, err := r.c.columnInt64(r.pstmt, i) 247 if err != nil { 248 return err 249 } 250 251 dest[i] = v 252 case sqlite3.SQLITE_FLOAT: 253 v, err := r.c.columnDouble(r.pstmt, i) 254 if err != nil { 255 return err 256 } 257 258 dest[i] = v 259 case sqlite3.SQLITE_TEXT: 260 v, err := r.c.columnText(r.pstmt, i) 261 if err != nil { 262 return err 263 } 264 265 switch r.ColumnTypeDatabaseTypeName(i) { 266 case "DATE", "DATETIME", "TIMESTAMP": 267 dest[i], _ = r.c.parseTime(v) 268 default: 269 dest[i] = v 270 } 271 case sqlite3.SQLITE_BLOB: 272 v, err := r.c.columnBlob(r.pstmt, i) 273 if err != nil { 274 return err 275 } 276 277 dest[i] = v 278 case sqlite3.SQLITE_NULL: 279 dest[i] = nil 280 default: 281 return fmt.Errorf("internal error: rc %d", rc) 282 } 283 } 284 return nil 285 case sqlite3.SQLITE_DONE: 286 return io.EOF 287 default: 288 return r.c.errstr(int32(rc)) 289 } 290 } 291 292 // Inspired by mattn/go-sqlite3: https://github.com/mattn/go-sqlite3/blob/ab91e934/sqlite3.go#L210-L226 293 // 294 // These time.Parse formats handle formats 1 through 7 listed at https://www.sqlite.org/lang_datefunc.html. 295 var parseTimeFormats = []string{ 296 "2006-01-02 15:04:05.999999999-07:00", 297 "2006-01-02T15:04:05.999999999-07:00", 298 "2006-01-02 15:04:05.999999999", 299 "2006-01-02T15:04:05.999999999", 300 "2006-01-02 15:04:05", 301 "2006-01-02T15:04:05", 302 "2006-01-02 15:04", 303 "2006-01-02T15:04", 304 "2006-01-02", 305 } 306 307 // Attempt to parse s as a time. Return (s, false) if s is not 308 // recognized as a valid time encoding. 309 func (c *conn) parseTime(s string) (interface{}, bool) { 310 if v, ok := c.parseTimeString(s, strings.Index(s, "m=")); ok { 311 return v, true 312 } 313 314 ts := strings.TrimSuffix(s, "Z") 315 316 for _, f := range parseTimeFormats { 317 t, err := time.ParseInLocation(f, ts, time.UTC) 318 if err == nil { 319 return t, true 320 } 321 } 322 323 return s, false 324 } 325 326 // Attempt to parse s as a time string produced by t.String(). If x > 0 it's 327 // the index of substring "m=" within s. Return (s, false) if s is 328 // not recognized as a valid time encoding. 329 func (c *conn) parseTimeString(s0 string, x int) (interface{}, bool) { 330 s := s0 331 if x > 0 { 332 s = s[:x] // "2006-01-02 15:04:05.999999999 -0700 MST m=+9999" -> "2006-01-02 15:04:05.999999999 -0700 MST " 333 } 334 s = strings.TrimSpace(s) 335 if t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", s); err == nil { 336 return t, true 337 } 338 339 return s0, false 340 } 341 342 // writeTimeFormats are the names and formats supported 343 // by the `_time_format` DSN query param. 344 var writeTimeFormats = map[string]string{ 345 "sqlite": parseTimeFormats[0], 346 } 347 348 func (c *conn) formatTime(t time.Time) string { 349 // default format is the first element from parseTimeFormats slice 350 // this is inspired by https://github.com/mattn/go-sqlite3/blob/85436841b33e86c07dce0fa2e88c31a97c96a22f/sqlite3.go#L1893 351 if c.writeTimeFormat == "" { 352 return t.Format(parseTimeFormats[0]) 353 } 354 return t.Format(c.writeTimeFormat) 355 } 356 357 // RowsColumnTypeDatabaseTypeName may be implemented by Rows. It should return 358 // the database system type name without the length. Type names should be 359 // uppercase. Examples of returned types: "VARCHAR", "NVARCHAR", "VARCHAR2", 360 // "CHAR", "TEXT", "DECIMAL", "SMALLINT", "INT", "BIGINT", "BOOL", "[]BIGINT", 361 // "JSONB", "XML", "TIMESTAMP". 362 func (r *rows) ColumnTypeDatabaseTypeName(index int) string { 363 return strings.ToUpper(r.c.columnDeclType(r.pstmt, index)) 364 } 365 366 // RowsColumnTypeLength may be implemented by Rows. It should return the length 367 // of the column type if the column is a variable length type. If the column is 368 // not a variable length type ok should return false. If length is not limited 369 // other than system limits, it should return math.MaxInt64. The following are 370 // examples of returned values for various types: 371 // 372 // TEXT (math.MaxInt64, true) 373 // varchar(10) (10, true) 374 // nvarchar(10) (10, true) 375 // decimal (0, false) 376 // int (0, false) 377 // bytea(30) (30, true) 378 func (r *rows) ColumnTypeLength(index int) (length int64, ok bool) { 379 t, err := r.c.columnType(r.pstmt, index) 380 if err != nil { 381 return 0, false 382 } 383 384 switch t { 385 case sqlite3.SQLITE_INTEGER: 386 return 0, false 387 case sqlite3.SQLITE_FLOAT: 388 return 0, false 389 case sqlite3.SQLITE_TEXT: 390 return math.MaxInt64, true 391 case sqlite3.SQLITE_BLOB: 392 return math.MaxInt64, true 393 case sqlite3.SQLITE_NULL: 394 return 0, false 395 default: 396 return 0, false 397 } 398 } 399 400 // RowsColumnTypeNullable may be implemented by Rows. The nullable value should 401 // be true if it is known the column may be null, or false if the column is 402 // known to be not nullable. If the column nullability is unknown, ok should be 403 // false. 404 func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) { 405 return true, true 406 } 407 408 // RowsColumnTypePrecisionScale may be implemented by Rows. It should return 409 // the precision and scale for decimal types. If not applicable, ok should be 410 // false. The following are examples of returned values for various types: 411 // 412 // decimal(38, 4) (38, 4, true) 413 // int (0, 0, false) 414 // decimal (math.MaxInt64, math.MaxInt64, true) 415 func (r *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { 416 return 0, 0, false 417 } 418 419 // RowsColumnTypeScanType may be implemented by Rows. It should return the 420 // value type that can be used to scan types into. For example, the database 421 // column type "bigint" this should return "reflect.TypeOf(int64(0))". 422 func (r *rows) ColumnTypeScanType(index int) reflect.Type { 423 t, err := r.c.columnType(r.pstmt, index) 424 if err != nil { 425 return reflect.TypeOf("") 426 } 427 428 declType := strings.ToLower(r.c.columnDeclType(r.pstmt, index)) 429 430 switch t { 431 case sqlite3.SQLITE_INTEGER: 432 if declType == "boolean" { 433 // SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true). 434 return reflect.TypeOf(false) 435 } else { 436 return reflect.TypeOf(int64(0)) 437 } 438 case sqlite3.SQLITE_FLOAT: 439 return reflect.TypeOf(float64(0)) 440 case sqlite3.SQLITE_TEXT: 441 // SQLite does not have a storage class set aside for storing dates and/or times. 442 // Instead, the built-in Date And Time Functions of SQLite are capable of storing 443 // dates and times as TEXT, REAL, or INTEGER values 444 switch declType { 445 case "date", "datetime", "time", "timestamp": 446 return reflect.TypeOf(time.Time{}) 447 default: 448 return reflect.TypeOf("") 449 } 450 case sqlite3.SQLITE_BLOB: 451 return reflect.SliceOf(reflect.TypeOf([]byte{})) 452 case sqlite3.SQLITE_NULL: 453 return reflect.TypeOf(nil) 454 default: 455 return reflect.TypeOf("") 456 } 457 } 458 459 type stmt struct { 460 c *conn 461 psql uintptr 462 } 463 464 func newStmt(c *conn, sql string) (*stmt, error) { 465 p, err := libc.CString(sql) 466 if err != nil { 467 return nil, err 468 } 469 stm := stmt{c: c, psql: p} 470 471 return &stm, nil 472 } 473 474 // Close closes the statement. 475 // 476 // As of Go 1.1, a Stmt will not be closed if it's in use by any queries. 477 func (s *stmt) Close() (err error) { 478 s.c.free(s.psql) 479 s.psql = 0 480 return nil 481 } 482 483 // Exec executes a query that doesn't return rows, such as an INSERT or UPDATE. 484 // 485 // Deprecated: Drivers should implement StmtExecContext instead (or 486 // additionally). 487 func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { //TODO StmtExecContext 488 return s.exec(context.Background(), toNamedValues(args)) 489 } 490 491 // toNamedValues converts []driver.Value to []driver.NamedValue 492 func toNamedValues(vals []driver.Value) (r []driver.NamedValue) { 493 r = make([]driver.NamedValue, len(vals)) 494 for i, val := range vals { 495 r[i] = driver.NamedValue{Value: val, Ordinal: i + 1} 496 } 497 return r 498 } 499 500 func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) { 501 var pstmt uintptr 502 var done int32 503 if ctx != nil && ctx.Done() != nil { 504 defer interruptOnDone(ctx, s.c, &done)() 505 } 506 507 for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; { 508 if pstmt, err = s.c.prepareV2(&psql); err != nil { 509 return nil, err 510 } 511 512 if pstmt == 0 { 513 continue 514 } 515 err = func() (err error) { 516 n, err := s.c.bindParameterCount(pstmt) 517 if err != nil { 518 return err 519 } 520 521 if n != 0 { 522 allocs, err := s.c.bind(pstmt, n, args) 523 if err != nil { 524 return err 525 } 526 527 if len(allocs) != 0 { 528 defer func() { 529 for _, v := range allocs { 530 s.c.free(v) 531 } 532 }() 533 } 534 } 535 536 rc, err := s.c.step(pstmt) 537 if err != nil { 538 return err 539 } 540 541 switch rc & 0xff { 542 case sqlite3.SQLITE_DONE, sqlite3.SQLITE_ROW: 543 // nop 544 default: 545 return s.c.errstr(int32(rc)) 546 } 547 548 return nil 549 }() 550 551 if e := s.c.finalize(pstmt); e != nil && err == nil { 552 err = e 553 } 554 555 if err != nil { 556 return nil, err 557 } 558 } 559 return newResult(s.c) 560 } 561 562 // NumInput returns the number of placeholder parameters. 563 // 564 // If NumInput returns >= 0, the sql package will sanity check argument counts 565 // from callers and return errors to the caller before the statement's Exec or 566 // Query methods are called. 567 // 568 // NumInput may also return -1, if the driver doesn't know its number of 569 // placeholders. In that case, the sql package will not sanity check Exec or 570 // Query argument counts. 571 func (s *stmt) NumInput() (n int) { 572 return -1 573 } 574 575 // Query executes a query that may return rows, such as a 576 // SELECT. 577 // 578 // Deprecated: Drivers should implement StmtQueryContext instead (or 579 // additionally). 580 func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { //TODO StmtQueryContext 581 return s.query(context.Background(), toNamedValues(args)) 582 } 583 584 func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Rows, err error) { 585 var pstmt uintptr // C-pointer to prepared statement 586 var done int32 // done indicator (atomic usage) 587 588 // context honoring 589 if ctx != nil && ctx.Done() != nil { 590 defer interruptOnDone(ctx, s.c, &done)() 591 } 592 593 // generally, query may contain multiple SQL statements 594 // here we execute every but the last statement 595 // we then create rows instance for deferred execution of the last statement 596 597 // loop on all but last statements 598 for pzTail := s.psql; ; { 599 // honor the context 600 if atomic.LoadInt32(&done) != 0 { 601 return nil, ctx.Err() 602 } 603 604 // prepare yet another portion of SQL string 605 if pstmt, err = s.c.prepareV2(&pzTail); err != nil { 606 return nil, err 607 } 608 609 // *pzTail is left pointing to what remains uncompiled 610 // so we can check if it was the last statement (if pzTail is NULL) 611 if *(*byte)(unsafe.Pointer(pzTail)) == 0 { 612 // it is the last statement, leave the prepared statement for the rows{} instance 613 break 614 } 615 616 // If the input text contains no SQL (if the input is an empty string or a comment) then *ppStmt is set to NULL 617 if pstmt == 0 { 618 // we can safely skip it 619 continue 620 } 621 622 // This routine can be used to find the number of SQL parameters in a prepared statement 623 nParams, err := s.c.bindParameterCount(pstmt) 624 if err != nil { 625 return nil, err 626 } 627 628 if nParams > 0 { 629 // bind the required portion of args 630 if allocs, err := s.c.bind(pstmt, nParams, args); err != nil { 631 return nil, err 632 } else { 633 // defer free allocated data 634 defer func() { 635 for _, v := range allocs { 636 s.c.free(v) 637 } 638 }() 639 } 640 641 // shift the args to what has left after binding 642 args = args[nParams:] 643 for i := range args { 644 args[i].Ordinal = i + 1 645 } 646 } 647 648 // execute the statement 649 rc, err := s.c.step(pstmt) 650 if err != nil { 651 return nil, err 652 } 653 654 // inspect return code 655 switch rc & 0xff { 656 case sqlite3.SQLITE_ROW, sqlite3.SQLITE_DONE: 657 // we actually don't care if it is ROW or DONE 658 // we ain't going to read the results anyway 659 default: 660 // other RCs considered error 661 return nil, s.c.errstr(int32(rc)) 662 } 663 664 // The application must finalize every prepared statement in order to avoid resource leaks. 665 if err := s.c.finalize(pstmt); err != nil { 666 return nil, err 667 } 668 } 669 670 // OK, at this point, we've executed all but last statements in SQL query 671 // let's create rows{} object with prepared statement of the last statement in SQL query 672 673 // This routine can be used to find the number of SQL parameters in a prepared statement 674 nParams, err := s.c.bindParameterCount(pstmt) 675 if err != nil { 676 return nil, err 677 } 678 679 // bind the required portion of args 680 var allocs []uintptr 681 allocs, err = s.c.bind(pstmt, nParams, args) 682 if err != nil { 683 return nil, err 684 } 685 686 // create rows 687 r, err = newRows(s.c, pstmt, allocs) 688 if err != nil { 689 return nil, err 690 } 691 return r, nil 692 } 693 694 type tx struct { 695 c *conn 696 } 697 698 func newTx(c *conn, opts driver.TxOptions) (*tx, error) { 699 r := &tx{c: c} 700 701 sql := "begin" 702 if !opts.ReadOnly && c.beginMode != "" { 703 sql = "begin " + c.beginMode 704 } 705 706 if err := r.exec(context.Background(), sql); err != nil { 707 return nil, err 708 } 709 710 return r, nil 711 } 712 713 // Commit implements driver.Tx. 714 func (t *tx) Commit() (err error) { 715 return t.exec(context.Background(), "commit") 716 } 717 718 // Rollback implements driver.Tx. 719 func (t *tx) Rollback() (err error) { 720 return t.exec(context.Background(), "rollback") 721 } 722 723 func (t *tx) exec(ctx context.Context, sql string) (err error) { 724 psql, err := libc.CString(sql) 725 if err != nil { 726 return err 727 } 728 729 defer t.c.free(psql) 730 //TODO use t.conn.ExecContext() instead 731 732 if ctx != nil && ctx.Done() != nil { 733 defer interruptOnDone(ctx, t.c, nil)() 734 } 735 736 if rc := sqlite3.Xsqlite3_exec(t.c.tls, t.c.db, psql, 0, 0, 0); rc != sqlite3.SQLITE_OK { 737 return t.c.errstr(rc) 738 } 739 740 return nil 741 } 742 743 // interruptOnDone sets up a goroutine to interrupt the provided db when the 744 // context is canceled, and returns a function the caller must defer so it 745 // doesn't interrupt after the caller finishes. 746 func interruptOnDone( 747 ctx context.Context, 748 c *conn, 749 done *int32, 750 ) func() { 751 if done == nil { 752 var d int32 753 done = &d 754 } 755 756 donech := make(chan struct{}) 757 758 go func() { 759 select { 760 case <-ctx.Done(): 761 // don't call interrupt if we were already done: it indicates that this 762 // call to exec is no longer running and we would be interrupting 763 // nothing, or even possibly an unrelated later call to exec. 764 if atomic.AddInt32(done, 1) == 1 { 765 c.interrupt(c.db) 766 } 767 case <-donech: 768 } 769 }() 770 771 // the caller is expected to defer this function 772 return func() { 773 // set the done flag so that a context cancellation right after the caller 774 // returns doesn't trigger a call to interrupt for some other statement. 775 atomic.AddInt32(done, 1) 776 close(donech) 777 } 778 } 779 780 type conn struct { 781 db uintptr // *sqlite3.Xsqlite3 782 tls *libc.TLS 783 784 // Context handling can cause conn.Close and conn.interrupt to be invoked 785 // concurrently. 786 sync.Mutex 787 788 writeTimeFormat string 789 beginMode string 790 } 791 792 func newConn(dsn string) (*conn, error) { 793 var query, vfsName string 794 795 // Parse the query parameters from the dsn and them from the dsn if not prefixed by file: 796 // https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1046 797 // https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1383 798 pos := strings.IndexRune(dsn, '?') 799 if pos >= 1 { 800 query = dsn[pos+1:] 801 var err error 802 vfsName, err = getVFSName(query) 803 if err != nil { 804 return nil, err 805 } 806 807 if !strings.HasPrefix(dsn, "file:") { 808 dsn = dsn[:pos] 809 } 810 } 811 812 c := &conn{tls: libc.NewTLS()} 813 db, err := c.openV2( 814 dsn, 815 vfsName, 816 sqlite3.SQLITE_OPEN_READWRITE|sqlite3.SQLITE_OPEN_CREATE| 817 sqlite3.SQLITE_OPEN_FULLMUTEX| 818 sqlite3.SQLITE_OPEN_URI, 819 ) 820 if err != nil { 821 return nil, err 822 } 823 824 // register statement logger 825 if LogSqlStatements { 826 if sqlite3.Xsqlite3_trace_v2(c.tls, db, sqlite3.SQLITE_TRACE_STMT, *(*uintptr)(unsafe.Pointer(&struct { 827 f func(*libc.TLS, uint32, uintptr, uintptr, uintptr) int32 828 }{stmtLog})), 0) != 0 { 829 log.Fatal("failed to register tracing handler") 830 } 831 } 832 833 c.db = db 834 if err = c.extendedResultCodes(true); err != nil { 835 c.Close() 836 return nil, err 837 } 838 839 if err = applyQueryParams(c, query); err != nil { 840 c.Close() 841 return nil, err 842 } 843 844 return c, nil 845 } 846 847 func stmtLog(tls *libc.TLS, type1 uint32, cd uintptr, pd uintptr, xd uintptr) int32 { /* tclsqlite.c:661:12: */ 848 if type1 == uint32(sqlite3.SQLITE_TRACE_STMT) { 849 // get SQL string 850 stmtEx := libc.GoString(sqlite3.Xsqlite3_expanded_sql(tls, pd)) 851 log.Println(strings.Trim(stmtEx, "\r\n\t ")) 852 } 853 return sqlite3.SQLITE_OK 854 } 855 856 func getVFSName(query string) (r string, err error) { 857 q, err := url.ParseQuery(query) 858 if err != nil { 859 return "", err 860 } 861 862 for _, v := range q["vfs"] { 863 if r != "" && r != v { 864 return "", fmt.Errorf("conflicting vfs query parameters: %v", q["vfs"]) 865 } 866 867 r = v 868 } 869 870 return r, nil 871 } 872 873 func applyQueryParams(c *conn, query string) error { 874 q, err := url.ParseQuery(query) 875 if err != nil { 876 return err 877 } 878 879 // set default BUSY_TIMEOUT, just like mattn/go-sqlite3 does. 880 _, err = c.exec(context.Background(), `pragma BUSY_TIMEOUT(5000)`, nil) 881 if err != nil { 882 return err 883 } 884 885 for _, v := range q["_pragma"] { 886 cmd := "pragma " + v 887 _, err := c.exec(context.Background(), cmd, nil) 888 if err != nil { 889 return err 890 } 891 } 892 893 if v := q.Get("_time_format"); v != "" { 894 f, ok := writeTimeFormats[v] 895 if !ok { 896 return fmt.Errorf("unknown _time_format %q", v) 897 } 898 c.writeTimeFormat = f 899 return nil 900 } 901 902 if v := q.Get("_txlock"); v != "" { 903 lower := strings.ToLower(v) 904 if lower != "deferred" && lower != "immediate" && lower != "exclusive" { 905 return fmt.Errorf("unknown _txlock %q", v) 906 } 907 c.beginMode = v 908 } 909 910 return nil 911 } 912 913 // const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); 914 func (c *conn) columnBlob(pstmt uintptr, iCol int) (v []byte, err error) { 915 p := sqlite3.Xsqlite3_column_blob(c.tls, pstmt, int32(iCol)) 916 len, err := c.columnBytes(pstmt, iCol) 917 if err != nil { 918 return nil, err 919 } 920 921 if p == 0 || len == 0 { 922 return nil, nil 923 } 924 925 v = make([]byte, len) 926 copy(v, (*libc.RawMem)(unsafe.Pointer(p))[:len:len]) 927 return v, nil 928 } 929 930 // int sqlite3_column_bytes(sqlite3_stmt*, int iCol); 931 func (c *conn) columnBytes(pstmt uintptr, iCol int) (_ int, err error) { 932 v := sqlite3.Xsqlite3_column_bytes(c.tls, pstmt, int32(iCol)) 933 return int(v), nil 934 } 935 936 // const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); 937 func (c *conn) columnText(pstmt uintptr, iCol int) (v string, err error) { 938 p := sqlite3.Xsqlite3_column_text(c.tls, pstmt, int32(iCol)) 939 len, err := c.columnBytes(pstmt, iCol) 940 if err != nil { 941 return "", err 942 } 943 944 if p == 0 || len == 0 { 945 return "", nil 946 } 947 948 b := make([]byte, len) 949 copy(b, (*libc.RawMem)(unsafe.Pointer(p))[:len:len]) 950 return string(b), nil 951 } 952 953 // double sqlite3_column_double(sqlite3_stmt*, int iCol); 954 func (c *conn) columnDouble(pstmt uintptr, iCol int) (v float64, err error) { 955 v = sqlite3.Xsqlite3_column_double(c.tls, pstmt, int32(iCol)) 956 return v, nil 957 } 958 959 // sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); 960 func (c *conn) columnInt64(pstmt uintptr, iCol int) (v int64, err error) { 961 v = sqlite3.Xsqlite3_column_int64(c.tls, pstmt, int32(iCol)) 962 return v, nil 963 } 964 965 // int sqlite3_column_type(sqlite3_stmt*, int iCol); 966 func (c *conn) columnType(pstmt uintptr, iCol int) (_ int, err error) { 967 v := sqlite3.Xsqlite3_column_type(c.tls, pstmt, int32(iCol)) 968 return int(v), nil 969 } 970 971 // const char *sqlite3_column_decltype(sqlite3_stmt*,int); 972 func (c *conn) columnDeclType(pstmt uintptr, iCol int) string { 973 return libc.GoString(sqlite3.Xsqlite3_column_decltype(c.tls, pstmt, int32(iCol))) 974 } 975 976 // const char *sqlite3_column_name(sqlite3_stmt*, int N); 977 func (c *conn) columnName(pstmt uintptr, n int) (string, error) { 978 p := sqlite3.Xsqlite3_column_name(c.tls, pstmt, int32(n)) 979 return libc.GoString(p), nil 980 } 981 982 // int sqlite3_column_count(sqlite3_stmt *pStmt); 983 func (c *conn) columnCount(pstmt uintptr) (_ int, err error) { 984 v := sqlite3.Xsqlite3_column_count(c.tls, pstmt) 985 return int(v), nil 986 } 987 988 // sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); 989 func (c *conn) lastInsertRowID() (v int64, _ error) { 990 return sqlite3.Xsqlite3_last_insert_rowid(c.tls, c.db), nil 991 } 992 993 // int sqlite3_changes(sqlite3*); 994 func (c *conn) changes() (int, error) { 995 v := sqlite3.Xsqlite3_changes(c.tls, c.db) 996 return int(v), nil 997 } 998 999 // int sqlite3_step(sqlite3_stmt*); 1000 func (c *conn) step(pstmt uintptr) (int, error) { 1001 for { 1002 switch rc := sqlite3.Xsqlite3_step(c.tls, pstmt); rc { 1003 case sqliteLockedSharedcache: 1004 if err := c.retry(pstmt); err != nil { 1005 return sqlite3.SQLITE_LOCKED, err 1006 } 1007 case 1008 sqlite3.SQLITE_DONE, 1009 sqlite3.SQLITE_ROW: 1010 1011 return int(rc), nil 1012 default: 1013 return int(rc), c.errstr(rc) 1014 } 1015 } 1016 } 1017 1018 func (c *conn) retry(pstmt uintptr) error { 1019 mu := mutexAlloc(c.tls) 1020 (*mutex)(unsafe.Pointer(mu)).Lock() 1021 rc := sqlite3.Xsqlite3_unlock_notify( 1022 c.tls, 1023 c.db, 1024 *(*uintptr)(unsafe.Pointer(&struct { 1025 f func(*libc.TLS, uintptr, int32) 1026 }{unlockNotify})), 1027 mu, 1028 ) 1029 if rc == sqlite3.SQLITE_LOCKED { // Deadlock, see https://www.sqlite.org/c3ref/unlock_notify.html 1030 (*mutex)(unsafe.Pointer(mu)).Unlock() 1031 mutexFree(c.tls, mu) 1032 return c.errstr(rc) 1033 } 1034 1035 (*mutex)(unsafe.Pointer(mu)).Lock() 1036 (*mutex)(unsafe.Pointer(mu)).Unlock() 1037 mutexFree(c.tls, mu) 1038 if pstmt != 0 { 1039 sqlite3.Xsqlite3_reset(c.tls, pstmt) 1040 } 1041 return nil 1042 } 1043 1044 func unlockNotify(t *libc.TLS, ppArg uintptr, nArg int32) { 1045 for i := int32(0); i < nArg; i++ { 1046 mu := *(*uintptr)(unsafe.Pointer(ppArg)) 1047 (*mutex)(unsafe.Pointer(mu)).Unlock() 1048 ppArg += ptrSize 1049 } 1050 } 1051 1052 func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []uintptr, err error) { 1053 defer func() { 1054 if err == nil { 1055 return 1056 } 1057 1058 for _, v := range allocs { 1059 c.free(v) 1060 } 1061 allocs = nil 1062 }() 1063 1064 for i := 1; i <= n; i++ { 1065 name, err := c.bindParameterName(pstmt, i) 1066 if err != nil { 1067 return allocs, err 1068 } 1069 1070 var found bool 1071 var v driver.NamedValue 1072 for _, v = range args { 1073 if name != "" { 1074 // For ?NNN and $NNN params, match if NNN == v.Ordinal. 1075 // 1076 // Supporting this for $NNN is a special case that makes eg 1077 // `select $1, $2, $3 ...` work without needing to use 1078 // sql.Named. 1079 if (name[0] == '?' || name[0] == '$') && name[1:] == strconv.Itoa(v.Ordinal) { 1080 found = true 1081 break 1082 } 1083 1084 // sqlite supports '$', '@' and ':' prefixes for string 1085 // identifiers and '?' for numeric, so we cannot 1086 // combine different prefixes with the same name 1087 // because `database/sql` requires variable names 1088 // to start with a letter 1089 if name[1:] == v.Name[:] { 1090 found = true 1091 break 1092 } 1093 } else { 1094 if v.Ordinal == i { 1095 found = true 1096 break 1097 } 1098 } 1099 } 1100 1101 if !found { 1102 if name != "" { 1103 return allocs, fmt.Errorf("missing named argument %q", name[1:]) 1104 } 1105 1106 return allocs, fmt.Errorf("missing argument with index %d", i) 1107 } 1108 1109 var p uintptr 1110 switch x := v.Value.(type) { 1111 case int64: 1112 if err := c.bindInt64(pstmt, i, x); err != nil { 1113 return allocs, err 1114 } 1115 case float64: 1116 if err := c.bindDouble(pstmt, i, x); err != nil { 1117 return allocs, err 1118 } 1119 case bool: 1120 v := 0 1121 if x { 1122 v = 1 1123 } 1124 if err := c.bindInt(pstmt, i, v); err != nil { 1125 return allocs, err 1126 } 1127 case []byte: 1128 if p, err = c.bindBlob(pstmt, i, x); err != nil { 1129 return allocs, err 1130 } 1131 case string: 1132 if p, err = c.bindText(pstmt, i, x); err != nil { 1133 return allocs, err 1134 } 1135 case time.Time: 1136 if p, err = c.bindText(pstmt, i, c.formatTime(x)); err != nil { 1137 return allocs, err 1138 } 1139 case nil: 1140 if p, err = c.bindNull(pstmt, i); err != nil { 1141 return allocs, err 1142 } 1143 default: 1144 return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x) 1145 } 1146 if p != 0 { 1147 allocs = append(allocs, p) 1148 } 1149 } 1150 return allocs, nil 1151 } 1152 1153 // int sqlite3_bind_null(sqlite3_stmt*, int); 1154 func (c *conn) bindNull(pstmt uintptr, idx1 int) (uintptr, error) { 1155 if rc := sqlite3.Xsqlite3_bind_null(c.tls, pstmt, int32(idx1)); rc != sqlite3.SQLITE_OK { 1156 return 0, c.errstr(rc) 1157 } 1158 1159 return 0, nil 1160 } 1161 1162 // int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); 1163 func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) { 1164 p, err := libc.CString(value) 1165 if err != nil { 1166 return 0, err 1167 } 1168 1169 if rc := sqlite3.Xsqlite3_bind_text(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK { 1170 c.free(p) 1171 return 0, c.errstr(rc) 1172 } 1173 1174 return p, nil 1175 } 1176 1177 // int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); 1178 func (c *conn) bindBlob(pstmt uintptr, idx1 int, value []byte) (uintptr, error) { 1179 if value != nil && len(value) == 0 { 1180 if rc := sqlite3.Xsqlite3_bind_zeroblob(c.tls, pstmt, int32(idx1), 0); rc != sqlite3.SQLITE_OK { 1181 return 0, c.errstr(rc) 1182 } 1183 return 0, nil 1184 } 1185 1186 p, err := c.malloc(len(value)) 1187 if err != nil { 1188 return 0, err 1189 } 1190 if len(value) != 0 { 1191 copy((*libc.RawMem)(unsafe.Pointer(p))[:len(value):len(value)], value) 1192 } 1193 if rc := sqlite3.Xsqlite3_bind_blob(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK { 1194 c.free(p) 1195 return 0, c.errstr(rc) 1196 } 1197 1198 return p, nil 1199 } 1200 1201 // int sqlite3_bind_int(sqlite3_stmt*, int, int); 1202 func (c *conn) bindInt(pstmt uintptr, idx1, value int) (err error) { 1203 if rc := sqlite3.Xsqlite3_bind_int(c.tls, pstmt, int32(idx1), int32(value)); rc != sqlite3.SQLITE_OK { 1204 return c.errstr(rc) 1205 } 1206 1207 return nil 1208 } 1209 1210 // int sqlite3_bind_double(sqlite3_stmt*, int, double); 1211 func (c *conn) bindDouble(pstmt uintptr, idx1 int, value float64) (err error) { 1212 if rc := sqlite3.Xsqlite3_bind_double(c.tls, pstmt, int32(idx1), value); rc != 0 { 1213 return c.errstr(rc) 1214 } 1215 1216 return nil 1217 } 1218 1219 // int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); 1220 func (c *conn) bindInt64(pstmt uintptr, idx1 int, value int64) (err error) { 1221 if rc := sqlite3.Xsqlite3_bind_int64(c.tls, pstmt, int32(idx1), value); rc != sqlite3.SQLITE_OK { 1222 return c.errstr(rc) 1223 } 1224 1225 return nil 1226 } 1227 1228 // const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); 1229 func (c *conn) bindParameterName(pstmt uintptr, i int) (string, error) { 1230 p := sqlite3.Xsqlite3_bind_parameter_name(c.tls, pstmt, int32(i)) 1231 return libc.GoString(p), nil 1232 } 1233 1234 // int sqlite3_bind_parameter_count(sqlite3_stmt*); 1235 func (c *conn) bindParameterCount(pstmt uintptr) (_ int, err error) { 1236 r := sqlite3.Xsqlite3_bind_parameter_count(c.tls, pstmt) 1237 return int(r), nil 1238 } 1239 1240 // int sqlite3_finalize(sqlite3_stmt *pStmt); 1241 func (c *conn) finalize(pstmt uintptr) error { 1242 if rc := sqlite3.Xsqlite3_finalize(c.tls, pstmt); rc != sqlite3.SQLITE_OK { 1243 return c.errstr(rc) 1244 } 1245 1246 return nil 1247 } 1248 1249 // int sqlite3_prepare_v2( 1250 // 1251 // sqlite3 *db, /* Database handle */ 1252 // const char *zSql, /* SQL statement, UTF-8 encoded */ 1253 // int nByte, /* Maximum length of zSql in bytes. */ 1254 // sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 1255 // const char **pzTail /* OUT: Pointer to unused portion of zSql */ 1256 // 1257 // ); 1258 func (c *conn) prepareV2(zSQL *uintptr) (pstmt uintptr, err error) { 1259 var ppstmt, pptail uintptr 1260 1261 defer func() { 1262 c.free(ppstmt) 1263 c.free(pptail) 1264 }() 1265 1266 if ppstmt, err = c.malloc(int(ptrSize)); err != nil { 1267 return 0, err 1268 } 1269 1270 if pptail, err = c.malloc(int(ptrSize)); err != nil { 1271 return 0, err 1272 } 1273 1274 for { 1275 switch rc := sqlite3.Xsqlite3_prepare_v2(c.tls, c.db, *zSQL, -1, ppstmt, pptail); rc { 1276 case sqlite3.SQLITE_OK: 1277 *zSQL = *(*uintptr)(unsafe.Pointer(pptail)) 1278 return *(*uintptr)(unsafe.Pointer(ppstmt)), nil 1279 case sqliteLockedSharedcache: 1280 if err := c.retry(0); err != nil { 1281 return 0, err 1282 } 1283 default: 1284 return 0, c.errstr(rc) 1285 } 1286 } 1287 } 1288 1289 // void sqlite3_interrupt(sqlite3*); 1290 func (c *conn) interrupt(pdb uintptr) (err error) { 1291 c.Lock() // Defend against race with .Close invoked by context handling. 1292 1293 defer c.Unlock() 1294 1295 if c.tls != nil { 1296 sqlite3.Xsqlite3_interrupt(c.tls, pdb) 1297 } 1298 return nil 1299 } 1300 1301 // int sqlite3_extended_result_codes(sqlite3*, int onoff); 1302 func (c *conn) extendedResultCodes(on bool) error { 1303 if rc := sqlite3.Xsqlite3_extended_result_codes(c.tls, c.db, libc.Bool32(on)); rc != sqlite3.SQLITE_OK { 1304 return c.errstr(rc) 1305 } 1306 1307 return nil 1308 } 1309 1310 // int sqlite3_open_v2( 1311 // 1312 // const char *filename, /* Database filename (UTF-8) */ 1313 // sqlite3 **ppDb, /* OUT: SQLite db handle */ 1314 // int flags, /* Flags */ 1315 // const char *zVfs /* Name of VFS module to use */ 1316 // 1317 // ); 1318 func (c *conn) openV2(name, vfsName string, flags int32) (uintptr, error) { 1319 var p, s, vfs uintptr 1320 1321 defer func() { 1322 if p != 0 { 1323 c.free(p) 1324 } 1325 if s != 0 { 1326 c.free(s) 1327 } 1328 if vfs != 0 { 1329 c.free(vfs) 1330 } 1331 }() 1332 1333 p, err := c.malloc(int(ptrSize)) 1334 if err != nil { 1335 return 0, err 1336 } 1337 1338 if s, err = libc.CString(name); err != nil { 1339 return 0, err 1340 } 1341 1342 if vfsName != "" { 1343 if vfs, err = libc.CString(vfsName); err != nil { 1344 return 0, err 1345 } 1346 } 1347 1348 if rc := sqlite3.Xsqlite3_open_v2(c.tls, s, p, flags, vfs); rc != sqlite3.SQLITE_OK { 1349 return 0, c.errstr(rc) 1350 } 1351 1352 return *(*uintptr)(unsafe.Pointer(p)), nil 1353 } 1354 1355 func (c *conn) malloc(n int) (uintptr, error) { 1356 if p := libc.Xmalloc(c.tls, types.Size_t(n)); p != 0 || n == 0 { 1357 return p, nil 1358 } 1359 1360 return 0, fmt.Errorf("sqlite: cannot allocate %d bytes of memory", n) 1361 } 1362 1363 func (c *conn) free(p uintptr) { 1364 if p != 0 { 1365 libc.Xfree(c.tls, p) 1366 } 1367 } 1368 1369 // const char *sqlite3_errstr(int); 1370 func (c *conn) errstr(rc int32) error { 1371 p := sqlite3.Xsqlite3_errstr(c.tls, rc) 1372 str := libc.GoString(p) 1373 p = sqlite3.Xsqlite3_errmsg(c.tls, c.db) 1374 var s string 1375 if rc == sqlite3.SQLITE_BUSY { 1376 s = " (SQLITE_BUSY)" 1377 } 1378 switch msg := libc.GoString(p); { 1379 case msg == str: 1380 return &Error{msg: fmt.Sprintf("%s (%v)%s", str, rc, s), code: int(rc)} 1381 default: 1382 return &Error{msg: fmt.Sprintf("%s: %s (%v)%s", str, msg, rc, s), code: int(rc)} 1383 } 1384 } 1385 1386 // Begin starts a transaction. 1387 // 1388 // Deprecated: Drivers should implement ConnBeginTx instead (or additionally). 1389 func (c *conn) Begin() (driver.Tx, error) { 1390 return c.begin(context.Background(), driver.TxOptions{}) 1391 } 1392 1393 func (c *conn) begin(ctx context.Context, opts driver.TxOptions) (t driver.Tx, err error) { 1394 return newTx(c, opts) 1395 } 1396 1397 // Close invalidates and potentially stops any current prepared statements and 1398 // transactions, marking this connection as no longer in use. 1399 // 1400 // Because the sql package maintains a free pool of connections and only calls 1401 // Close when there's a surplus of idle connections, it shouldn't be necessary 1402 // for drivers to do their own connection caching. 1403 func (c *conn) Close() error { 1404 c.Lock() // Defend against race with .interrupt invoked by context handling. 1405 1406 defer c.Unlock() 1407 1408 if c.db != 0 { 1409 if err := c.closeV2(c.db); err != nil { 1410 return err 1411 } 1412 1413 c.db = 0 1414 } 1415 1416 if c.tls != nil { 1417 c.tls.Close() 1418 c.tls = nil 1419 } 1420 return nil 1421 } 1422 1423 // int sqlite3_close_v2(sqlite3*); 1424 func (c *conn) closeV2(db uintptr) error { 1425 if rc := sqlite3.Xsqlite3_close_v2(c.tls, db); rc != sqlite3.SQLITE_OK { 1426 return c.errstr(rc) 1427 } 1428 1429 return nil 1430 } 1431 1432 type userDefinedFunction struct { 1433 zFuncName uintptr 1434 nArg int32 1435 eTextRep int32 1436 xFunc func(*libc.TLS, uintptr, int32, uintptr) 1437 1438 freeOnce sync.Once 1439 } 1440 1441 func (c *conn) createFunctionInternal(fun *userDefinedFunction) error { 1442 if rc := sqlite3.Xsqlite3_create_function( 1443 c.tls, 1444 c.db, 1445 fun.zFuncName, 1446 fun.nArg, 1447 fun.eTextRep, 1448 0, 1449 *(*uintptr)(unsafe.Pointer(&fun.xFunc)), 1450 0, 1451 0, 1452 ); rc != sqlite3.SQLITE_OK { 1453 return c.errstr(rc) 1454 } 1455 return nil 1456 } 1457 1458 // Execer is an optional interface that may be implemented by a Conn. 1459 // 1460 // If a Conn does not implement Execer, the sql package's DB.Exec will first 1461 // prepare a query, execute the statement, and then close the statement. 1462 // 1463 // Exec may return ErrSkip. 1464 // 1465 // Deprecated: Drivers should implement ExecerContext instead. 1466 func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) { 1467 return c.exec(context.Background(), query, toNamedValues(args)) 1468 } 1469 1470 func (c *conn) exec(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) { 1471 s, err := c.prepare(ctx, query) 1472 if err != nil { 1473 return nil, err 1474 } 1475 1476 defer func() { 1477 if err2 := s.Close(); err2 != nil && err == nil { 1478 err = err2 1479 } 1480 }() 1481 1482 return s.(*stmt).exec(ctx, args) 1483 } 1484 1485 // Prepare returns a prepared statement, bound to this connection. 1486 func (c *conn) Prepare(query string) (driver.Stmt, error) { 1487 return c.prepare(context.Background(), query) 1488 } 1489 1490 func (c *conn) prepare(ctx context.Context, query string) (s driver.Stmt, err error) { 1491 //TODO use ctx 1492 return newStmt(c, query) 1493 } 1494 1495 // Queryer is an optional interface that may be implemented by a Conn. 1496 // 1497 // If a Conn does not implement Queryer, the sql package's DB.Query will first 1498 // prepare a query, execute the statement, and then close the statement. 1499 // 1500 // Query may return ErrSkip. 1501 // 1502 // Deprecated: Drivers should implement QueryerContext instead. 1503 func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) { 1504 return c.query(context.Background(), query, toNamedValues(args)) 1505 } 1506 1507 func (c *conn) query(ctx context.Context, query string, args []driver.NamedValue) (r driver.Rows, err error) { 1508 s, err := c.prepare(ctx, query) 1509 if err != nil { 1510 return nil, err 1511 } 1512 1513 defer func() { 1514 if err2 := s.Close(); err2 != nil && err == nil { 1515 err = err2 1516 } 1517 }() 1518 1519 return s.(*stmt).query(ctx, args) 1520 } 1521 1522 // Driver implements database/sql/driver.Driver. 1523 type Driver struct { 1524 // user defined functions that are added to every new connection on Open 1525 udfs map[string]*userDefinedFunction 1526 } 1527 1528 var d = &Driver{udfs: make(map[string]*userDefinedFunction)} 1529 1530 func newDriver() *Driver { return d } 1531 1532 // Open returns a new connection to the database. The name is a string in a 1533 // driver-specific format. 1534 // 1535 // Open may return a cached connection (one previously closed), but doing so is 1536 // unnecessary; the sql package maintains a pool of idle connections for 1537 // efficient re-use. 1538 // 1539 // The returned connection is only used by one goroutine at a time. 1540 // 1541 // If name contains a '?', what follows is treated as a query string. This 1542 // driver supports the following query parameters: 1543 // 1544 // _pragma: Each value will be run as a "PRAGMA ..." statement (with the PRAGMA 1545 // keyword added for you). May be specified more than once. Example: 1546 // "_pragma=foreign_keys(1)" will enable foreign key enforcement. More 1547 // information on supported PRAGMAs is available from the SQLite documentation: 1548 // https://www.sqlite.org/pragma.html 1549 // 1550 // _time_format: The name of a format to use when writing time values to the 1551 // database. Currently the only supported value is "sqlite", which corresponds 1552 // to format 7 from https://www.sqlite.org/lang_datefunc.html#time_values, 1553 // including the timezone specifier. If this parameter is not specified, then 1554 // the default String() format will be used. 1555 // 1556 // _txlock: The locking behavior to use when beginning a transaction. May be 1557 // "deferred", "immediate", or "exclusive" (case insensitive). The default is to 1558 // not specify one, which SQLite maps to "deferred". More information is 1559 // available at 1560 // https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions 1561 func (d *Driver) Open(name string) (driver.Conn, error) { 1562 c, err := newConn(name) 1563 if err != nil { 1564 return nil, err 1565 } 1566 1567 for _, udf := range d.udfs { 1568 if err = c.createFunctionInternal(udf); err != nil { 1569 c.Close() 1570 return nil, err 1571 } 1572 } 1573 1574 if LogSqlStatements { 1575 log.Println("new connection") 1576 } 1577 1578 return c, nil 1579 } 1580 1581 // FunctionContext represents the context user defined functions execute in. 1582 // Fields and/or methods of this type may get addedd in the future. 1583 type FunctionContext struct{} 1584 1585 const sqliteValPtrSize = unsafe.Sizeof(&sqlite3.Sqlite3_value{}) 1586 1587 // RegisterScalarFunction registers a scalar function named zFuncName with nArg 1588 // arguments. Passing -1 for nArg indicates the function is variadic. 1589 // 1590 // The new function will be available to all new connections opened after 1591 // executing RegisterScalarFunction. 1592 func RegisterScalarFunction( 1593 zFuncName string, 1594 nArg int32, 1595 xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error), 1596 ) error { 1597 return registerScalarFunction(zFuncName, nArg, sqlite3.SQLITE_UTF8, xFunc) 1598 } 1599 1600 // MustRegisterScalarFunction is like RegisterScalarFunction but panics on 1601 // error. 1602 func MustRegisterScalarFunction( 1603 zFuncName string, 1604 nArg int32, 1605 xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error), 1606 ) { 1607 if err := RegisterScalarFunction(zFuncName, nArg, xFunc); err != nil { 1608 panic(err) 1609 } 1610 } 1611 1612 // MustRegisterDeterministicScalarFunction is like 1613 // RegisterDeterministicScalarFunction but panics on error. 1614 func MustRegisterDeterministicScalarFunction( 1615 zFuncName string, 1616 nArg int32, 1617 xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error), 1618 ) { 1619 if err := RegisterDeterministicScalarFunction(zFuncName, nArg, xFunc); err != nil { 1620 panic(err) 1621 } 1622 } 1623 1624 // RegisterDeterministicScalarFunction registers a deterministic scalar 1625 // function named zFuncName with nArg arguments. Passing -1 for nArg indicates 1626 // the function is variadic. A deterministic function means that the function 1627 // always gives the same output when the input parameters are the same. 1628 // 1629 // The new function will be available to all new connections opened after 1630 // executing RegisterDeterministicScalarFunction. 1631 func RegisterDeterministicScalarFunction( 1632 zFuncName string, 1633 nArg int32, 1634 xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error), 1635 ) error { 1636 return registerScalarFunction(zFuncName, nArg, sqlite3.SQLITE_UTF8|sqlite3.SQLITE_DETERMINISTIC, xFunc) 1637 } 1638 1639 func registerScalarFunction( 1640 zFuncName string, 1641 nArg int32, 1642 eTextRep int32, 1643 xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error), 1644 ) error { 1645 1646 if _, ok := d.udfs[zFuncName]; ok { 1647 return fmt.Errorf("a function named %q is already registered", zFuncName) 1648 } 1649 1650 // dont free, functions registered on the driver live as long as the program 1651 name, err := libc.CString(zFuncName) 1652 if err != nil { 1653 return err 1654 } 1655 1656 udf := &userDefinedFunction{ 1657 zFuncName: name, 1658 nArg: nArg, 1659 eTextRep: eTextRep, 1660 xFunc: func(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) { 1661 setErrorResult := func(res error) { 1662 errmsg, cerr := libc.CString(res.Error()) 1663 if cerr != nil { 1664 panic(cerr) 1665 } 1666 defer libc.Xfree(tls, errmsg) 1667 sqlite3.Xsqlite3_result_error(tls, ctx, errmsg, -1) 1668 sqlite3.Xsqlite3_result_error_code(tls, ctx, sqlite3.SQLITE_ERROR) 1669 } 1670 1671 args := make([]driver.Value, argc) 1672 for i := int32(0); i < argc; i++ { 1673 valPtr := *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*sqliteValPtrSize)) 1674 1675 switch valType := sqlite3.Xsqlite3_value_type(tls, valPtr); valType { 1676 case sqlite3.SQLITE_TEXT: 1677 args[i] = libc.GoString(sqlite3.Xsqlite3_value_text(tls, valPtr)) 1678 case sqlite3.SQLITE_INTEGER: 1679 args[i] = sqlite3.Xsqlite3_value_int64(tls, valPtr) 1680 case sqlite3.SQLITE_FLOAT: 1681 args[i] = sqlite3.Xsqlite3_value_double(tls, valPtr) 1682 case sqlite3.SQLITE_NULL: 1683 args[i] = nil 1684 case sqlite3.SQLITE_BLOB: 1685 size := sqlite3.Xsqlite3_value_bytes(tls, valPtr) 1686 blobPtr := sqlite3.Xsqlite3_value_blob(tls, valPtr) 1687 v := make([]byte, size) 1688 copy(v, (*libc.RawMem)(unsafe.Pointer(blobPtr))[:size:size]) 1689 args[i] = v 1690 default: 1691 panic(fmt.Sprintf("unexpected argument type %q passed by sqlite", valType)) 1692 } 1693 } 1694 1695 res, err := xFunc(&FunctionContext{}, args) 1696 if err != nil { 1697 setErrorResult(err) 1698 return 1699 } 1700 1701 switch resTyped := res.(type) { 1702 case nil: 1703 sqlite3.Xsqlite3_result_null(tls, ctx) 1704 case int64: 1705 sqlite3.Xsqlite3_result_int64(tls, ctx, resTyped) 1706 case float64: 1707 sqlite3.Xsqlite3_result_double(tls, ctx, resTyped) 1708 case bool: 1709 sqlite3.Xsqlite3_result_int(tls, ctx, libc.Bool32(resTyped)) 1710 case time.Time: 1711 sqlite3.Xsqlite3_result_int64(tls, ctx, resTyped.Unix()) 1712 case string: 1713 size := int32(len(resTyped)) 1714 cstr, err := libc.CString(resTyped) 1715 if err != nil { 1716 panic(err) 1717 } 1718 defer libc.Xfree(tls, cstr) 1719 sqlite3.Xsqlite3_result_text(tls, ctx, cstr, size, sqlite3.SQLITE_TRANSIENT) 1720 case []byte: 1721 size := int32(len(resTyped)) 1722 if size == 0 { 1723 sqlite3.Xsqlite3_result_zeroblob(tls, ctx, 0) 1724 return 1725 } 1726 p := libc.Xmalloc(tls, types.Size_t(size)) 1727 if p == 0 { 1728 panic(fmt.Sprintf("unable to allocate space for blob: %d", size)) 1729 } 1730 defer libc.Xfree(tls, p) 1731 copy((*libc.RawMem)(unsafe.Pointer(p))[:size:size], resTyped) 1732 1733 sqlite3.Xsqlite3_result_blob(tls, ctx, p, size, sqlite3.SQLITE_TRANSIENT) 1734 default: 1735 setErrorResult(fmt.Errorf("function did not return a valid driver.Value: %T", resTyped)) 1736 return 1737 } 1738 }, 1739 } 1740 d.udfs[zFuncName] = udf 1741 1742 return nil 1743 }