github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/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 6 //go:generate go fmt ./... 7 8 package sqlite // import "modernc.org/sqlite" 9 10 import ( 11 "context" 12 "database/sql" 13 "database/sql/driver" 14 "fmt" 15 "io" 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 const ( 51 driverName = "sqlite" 52 ptrSize = unsafe.Sizeof(uintptr(0)) 53 sqliteLockedSharedcache = sqlite3.SQLITE_LOCKED | (1 << 8) 54 ) 55 56 // Error represents sqlite library error code. 57 type Error struct { 58 msg string 59 code int 60 } 61 62 // Error implements error. 63 func (e *Error) Error() string { return e.msg } 64 65 // Code returns the sqlite result code for this error. 66 func (e *Error) Code() int { return e.code } 67 68 var ( 69 // ErrorCodeString maps Error.Code() to its string representation. 70 ErrorCodeString = map[int]string{ 71 sqlite3.SQLITE_ABORT: "Callback routine requested an abort (SQLITE_ABORT)", 72 sqlite3.SQLITE_AUTH: "Authorization denied (SQLITE_AUTH)", 73 sqlite3.SQLITE_BUSY: "The database file is locked (SQLITE_BUSY)", 74 sqlite3.SQLITE_CANTOPEN: "Unable to open the database file (SQLITE_CANTOPEN)", 75 sqlite3.SQLITE_CONSTRAINT: "Abort due to constraint violation (SQLITE_CONSTRAINT)", 76 sqlite3.SQLITE_CORRUPT: "The database disk image is malformed (SQLITE_CORRUPT)", 77 sqlite3.SQLITE_DONE: "sqlite3_step() has finished executing (SQLITE_DONE)", 78 sqlite3.SQLITE_EMPTY: "Internal use only (SQLITE_EMPTY)", 79 sqlite3.SQLITE_ERROR: "Generic error (SQLITE_ERROR)", 80 sqlite3.SQLITE_FORMAT: "Not used (SQLITE_FORMAT)", 81 sqlite3.SQLITE_FULL: "Insertion failed because database is full (SQLITE_FULL)", 82 sqlite3.SQLITE_INTERNAL: "Internal logic error in SQLite (SQLITE_INTERNAL)", 83 sqlite3.SQLITE_INTERRUPT: "Operation terminated by sqlite3_interrupt()(SQLITE_INTERRUPT)", 84 sqlite3.SQLITE_IOERR | (1 << 8): "(SQLITE_IOERR_READ)", 85 sqlite3.SQLITE_IOERR | (10 << 8): "(SQLITE_IOERR_DELETE)", 86 sqlite3.SQLITE_IOERR | (11 << 8): "(SQLITE_IOERR_BLOCKED)", 87 sqlite3.SQLITE_IOERR | (12 << 8): "(SQLITE_IOERR_NOMEM)", 88 sqlite3.SQLITE_IOERR | (13 << 8): "(SQLITE_IOERR_ACCESS)", 89 sqlite3.SQLITE_IOERR | (14 << 8): "(SQLITE_IOERR_CHECKRESERVEDLOCK)", 90 sqlite3.SQLITE_IOERR | (15 << 8): "(SQLITE_IOERR_LOCK)", 91 sqlite3.SQLITE_IOERR | (16 << 8): "(SQLITE_IOERR_CLOSE)", 92 sqlite3.SQLITE_IOERR | (17 << 8): "(SQLITE_IOERR_DIR_CLOSE)", 93 sqlite3.SQLITE_IOERR | (2 << 8): "(SQLITE_IOERR_SHORT_READ)", 94 sqlite3.SQLITE_IOERR | (3 << 8): "(SQLITE_IOERR_WRITE)", 95 sqlite3.SQLITE_IOERR | (4 << 8): "(SQLITE_IOERR_FSYNC)", 96 sqlite3.SQLITE_IOERR | (5 << 8): "(SQLITE_IOERR_DIR_FSYNC)", 97 sqlite3.SQLITE_IOERR | (6 << 8): "(SQLITE_IOERR_TRUNCATE)", 98 sqlite3.SQLITE_IOERR | (7 << 8): "(SQLITE_IOERR_FSTAT)", 99 sqlite3.SQLITE_IOERR | (8 << 8): "(SQLITE_IOERR_UNLOCK)", 100 sqlite3.SQLITE_IOERR | (9 << 8): "(SQLITE_IOERR_RDLOCK)", 101 sqlite3.SQLITE_IOERR: "Some kind of disk I/O error occurred (SQLITE_IOERR)", 102 sqlite3.SQLITE_LOCKED | (1 << 8): "(SQLITE_LOCKED_SHAREDCACHE)", 103 sqlite3.SQLITE_LOCKED: "A table in the database is locked (SQLITE_LOCKED)", 104 sqlite3.SQLITE_MISMATCH: "Data type mismatch (SQLITE_MISMATCH)", 105 sqlite3.SQLITE_MISUSE: "Library used incorrectly (SQLITE_MISUSE)", 106 sqlite3.SQLITE_NOLFS: "Uses OS features not supported on host (SQLITE_NOLFS)", 107 sqlite3.SQLITE_NOMEM: "A malloc() failed (SQLITE_NOMEM)", 108 sqlite3.SQLITE_NOTADB: "File opened that is not a database file (SQLITE_NOTADB)", 109 sqlite3.SQLITE_NOTFOUND: "Unknown opcode in sqlite3_file_control() (SQLITE_NOTFOUND)", 110 sqlite3.SQLITE_NOTICE: "Notifications from sqlite3_log() (SQLITE_NOTICE)", 111 sqlite3.SQLITE_PERM: "Access permission denied (SQLITE_PERM)", 112 sqlite3.SQLITE_PROTOCOL: "Database lock protocol error (SQLITE_PROTOCOL)", 113 sqlite3.SQLITE_RANGE: "2nd parameter to sqlite3_bind out of range (SQLITE_RANGE)", 114 sqlite3.SQLITE_READONLY: "Attempt to write a readonly database (SQLITE_READONLY)", 115 sqlite3.SQLITE_ROW: "sqlite3_step() has another row ready (SQLITE_ROW)", 116 sqlite3.SQLITE_SCHEMA: "The database schema changed (SQLITE_SCHEMA)", 117 sqlite3.SQLITE_TOOBIG: "String or BLOB exceeds size limit (SQLITE_TOOBIG)", 118 sqlite3.SQLITE_WARNING: "Warnings from sqlite3_log() (SQLITE_WARNING)", 119 } 120 ) 121 122 func init() { 123 sql.Register(driverName, newDriver()) 124 } 125 126 type result struct { 127 lastInsertID int64 128 rowsAffected int 129 } 130 131 func newResult(c *conn) (_ *result, err error) { 132 r := &result{} 133 if r.rowsAffected, err = c.changes(); err != nil { 134 return nil, err 135 } 136 137 if r.lastInsertID, err = c.lastInsertRowID(); err != nil { 138 return nil, err 139 } 140 141 return r, nil 142 } 143 144 // LastInsertId returns the database's auto-generated ID after, for example, an 145 // INSERT into a table with primary key. 146 func (r *result) LastInsertId() (int64, error) { 147 if r == nil { 148 return 0, nil 149 } 150 151 return r.lastInsertID, nil 152 } 153 154 // RowsAffected returns the number of rows affected by the query. 155 func (r *result) RowsAffected() (int64, error) { 156 if r == nil { 157 return 0, nil 158 } 159 160 return int64(r.rowsAffected), nil 161 } 162 163 type rows struct { 164 allocs []uintptr 165 c *conn 166 columns []string 167 pstmt uintptr 168 169 doStep bool 170 empty bool 171 } 172 173 func newRows(c *conn, pstmt uintptr, allocs []uintptr, empty bool) (r *rows, err error) { 174 r = &rows{c: c, pstmt: pstmt, allocs: allocs, empty: empty} 175 176 defer func() { 177 if err != nil { 178 r.Close() 179 r = nil 180 } 181 }() 182 183 n, err := c.columnCount(pstmt) 184 if err != nil { 185 return nil, err 186 } 187 188 r.columns = make([]string, n) 189 for i := range r.columns { 190 if r.columns[i], err = r.c.columnName(pstmt, i); err != nil { 191 return nil, err 192 } 193 } 194 195 return r, nil 196 } 197 198 // Close closes the rows iterator. 199 func (r *rows) Close() (err error) { 200 for _, v := range r.allocs { 201 r.c.free(v) 202 } 203 r.allocs = nil 204 return r.c.finalize(r.pstmt) 205 } 206 207 // Columns returns the names of the columns. The number of columns of the 208 // result is inferred from the length of the slice. If a particular column name 209 // isn't known, an empty string should be returned for that entry. 210 func (r *rows) Columns() (c []string) { 211 return r.columns 212 } 213 214 // Next is called to populate the next row of data into the provided slice. The 215 // provided slice will be the same size as the Columns() are wide. 216 // 217 // Next should return io.EOF when there are no more rows. 218 func (r *rows) Next(dest []driver.Value) (err error) { 219 if r.empty { 220 return io.EOF 221 } 222 223 rc := sqlite3.SQLITE_ROW 224 if r.doStep { 225 if rc, err = r.c.step(r.pstmt); err != nil { 226 return err 227 } 228 } 229 230 r.doStep = true 231 switch rc { 232 case sqlite3.SQLITE_ROW: 233 if g, e := len(dest), len(r.columns); g != e { 234 return fmt.Errorf("sqlite: Next: have %v destination values, expected %v", g, e) 235 } 236 237 for i := range dest { 238 ct, err := r.c.columnType(r.pstmt, i) 239 if err != nil { 240 return err 241 } 242 243 switch ct { 244 case sqlite3.SQLITE_INTEGER: 245 v, err := r.c.columnInt64(r.pstmt, i) 246 if err != nil { 247 return err 248 } 249 250 dest[i] = v 251 case sqlite3.SQLITE_FLOAT: 252 v, err := r.c.columnDouble(r.pstmt, i) 253 if err != nil { 254 return err 255 } 256 257 dest[i] = v 258 case sqlite3.SQLITE_TEXT: 259 v, err := r.c.columnText(r.pstmt, i) 260 if err != nil { 261 return err 262 } 263 264 switch r.ColumnTypeDatabaseTypeName(i) { 265 case "DATE", "DATETIME", "TIMESTAMP": 266 dest[i], _ = r.c.parseTime(v) 267 default: 268 dest[i] = v 269 } 270 case sqlite3.SQLITE_BLOB: 271 v, err := r.c.columnBlob(r.pstmt, i) 272 if err != nil { 273 return err 274 } 275 276 dest[i] = v 277 case sqlite3.SQLITE_NULL: 278 dest[i] = nil 279 default: 280 return fmt.Errorf("internal error: rc %d", rc) 281 } 282 } 283 return nil 284 case sqlite3.SQLITE_DONE: 285 return io.EOF 286 default: 287 return r.c.errstr(int32(rc)) 288 } 289 } 290 291 // Inspired by mattn/go-sqlite3: https://github.com/mattn/go-sqlite3/blob/ab91e934/sqlite3.go#L210-L226 292 // 293 // These time.Parse formats handle formats 1 through 7 listed at https://www.sqlite.org/lang_datefunc.html. 294 var parseTimeFormats = []string{ 295 "2006-01-02 15:04:05.999999999-07:00", 296 "2006-01-02T15:04:05.999999999-07:00", 297 "2006-01-02 15:04:05.999999999", 298 "2006-01-02T15:04:05.999999999", 299 "2006-01-02 15:04", 300 "2006-01-02T15:04", 301 "2006-01-02", 302 } 303 304 // Attempt to parse s as a time. Return (s, false) if s is not 305 // recognized as a valid time encoding. 306 func (c *conn) parseTime(s string) (interface{}, bool) { 307 if v, ok := c.parseTimeString(s, strings.Index(s, "m=")); ok { 308 return v, true 309 } 310 311 ts := strings.TrimSuffix(s, "Z") 312 313 for _, f := range parseTimeFormats { 314 t, err := time.Parse(f, ts) 315 if err == nil { 316 return t, true 317 } 318 } 319 320 return s, false 321 } 322 323 // Attempt to parse s as a time string produced by t.String(). If x > 0 it's 324 // the index of substring "m=" within s. Return (s, false) if s is 325 // not recognized as a valid time encoding. 326 func (c *conn) parseTimeString(s0 string, x int) (interface{}, bool) { 327 s := s0 328 if x > 0 { 329 s = s[:x] // "2006-01-02 15:04:05.999999999 -0700 MST m=+9999" -> "2006-01-02 15:04:05.999999999 -0700 MST " 330 } 331 s = strings.TrimSpace(s) 332 if t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", s); err == nil { 333 return t, true 334 } 335 336 return s0, false 337 } 338 339 // RowsColumnTypeDatabaseTypeName may be implemented by Rows. It should return 340 // the database system type name without the length. Type names should be 341 // uppercase. Examples of returned types: "VARCHAR", "NVARCHAR", "VARCHAR2", 342 // "CHAR", "TEXT", "DECIMAL", "SMALLINT", "INT", "BIGINT", "BOOL", "[]BIGINT", 343 // "JSONB", "XML", "TIMESTAMP". 344 func (r *rows) ColumnTypeDatabaseTypeName(index int) string { 345 return strings.ToUpper(r.c.columnDeclType(r.pstmt, index)) 346 } 347 348 // RowsColumnTypeLength may be implemented by Rows. It should return the length 349 // of the column type if the column is a variable length type. If the column is 350 // not a variable length type ok should return false. If length is not limited 351 // other than system limits, it should return math.MaxInt64. The following are 352 // examples of returned values for various types: 353 // 354 // TEXT (math.MaxInt64, true) 355 // varchar(10) (10, true) 356 // nvarchar(10) (10, true) 357 // decimal (0, false) 358 // int (0, false) 359 // bytea(30) (30, true) 360 func (r *rows) ColumnTypeLength(index int) (length int64, ok bool) { 361 t, err := r.c.columnType(r.pstmt, index) 362 if err != nil { 363 return 0, false 364 } 365 366 switch t { 367 case sqlite3.SQLITE_INTEGER: 368 return 0, false 369 case sqlite3.SQLITE_FLOAT: 370 return 0, false 371 case sqlite3.SQLITE_TEXT: 372 return math.MaxInt64, true 373 case sqlite3.SQLITE_BLOB: 374 return math.MaxInt64, true 375 case sqlite3.SQLITE_NULL: 376 return 0, false 377 default: 378 return 0, false 379 } 380 } 381 382 // RowsColumnTypeNullable may be implemented by Rows. The nullable value should 383 // be true if it is known the column may be null, or false if the column is 384 // known to be not nullable. If the column nullability is unknown, ok should be 385 // false. 386 func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) { 387 return true, true 388 } 389 390 // RowsColumnTypePrecisionScale may be implemented by Rows. It should return 391 // the precision and scale for decimal types. If not applicable, ok should be 392 // false. The following are examples of returned values for various types: 393 // 394 // decimal(38, 4) (38, 4, true) 395 // int (0, 0, false) 396 // decimal (math.MaxInt64, math.MaxInt64, true) 397 func (r *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { 398 return 0, 0, false 399 } 400 401 // RowsColumnTypeScanType may be implemented by Rows. It should return the 402 // value type that can be used to scan types into. For example, the database 403 // column type "bigint" this should return "reflect.TypeOf(int64(0))". 404 func (r *rows) ColumnTypeScanType(index int) reflect.Type { 405 t, err := r.c.columnType(r.pstmt, index) 406 if err != nil { 407 return reflect.TypeOf("") 408 } 409 410 switch t { 411 case sqlite3.SQLITE_INTEGER: 412 switch strings.ToLower(r.c.columnDeclType(r.pstmt, index)) { 413 case "boolean": 414 return reflect.TypeOf(false) 415 case "date", "datetime", "time", "timestamp": 416 return reflect.TypeOf(time.Time{}) 417 default: 418 return reflect.TypeOf(int64(0)) 419 } 420 case sqlite3.SQLITE_FLOAT: 421 return reflect.TypeOf(float64(0)) 422 case sqlite3.SQLITE_TEXT: 423 return reflect.TypeOf("") 424 case sqlite3.SQLITE_BLOB: 425 return reflect.SliceOf(reflect.TypeOf([]byte{})) 426 case sqlite3.SQLITE_NULL: 427 return reflect.TypeOf(nil) 428 default: 429 return reflect.TypeOf("") 430 } 431 } 432 433 type stmt struct { 434 c *conn 435 psql uintptr 436 } 437 438 func newStmt(c *conn, sql string) (*stmt, error) { 439 p, err := libc.CString(sql) 440 if err != nil { 441 return nil, err 442 } 443 stm := stmt{c: c, psql: p} 444 445 return &stm, nil 446 } 447 448 // Close closes the statement. 449 // 450 // As of Go 1.1, a Stmt will not be closed if it's in use by any queries. 451 func (s *stmt) Close() (err error) { 452 s.c.free(s.psql) 453 s.psql = 0 454 return nil 455 } 456 457 // Exec executes a query that doesn't return rows, such as an INSERT or UPDATE. 458 // 459 // 460 // Deprecated: Drivers should implement StmtExecContext instead (or 461 // additionally). 462 func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { //TODO StmtExecContext 463 return s.exec(context.Background(), toNamedValues(args)) 464 } 465 466 // toNamedValues converts []driver.Value to []driver.NamedValue 467 func toNamedValues(vals []driver.Value) (r []driver.NamedValue) { 468 r = make([]driver.NamedValue, len(vals)) 469 for i, val := range vals { 470 r[i] = driver.NamedValue{Value: val, Ordinal: i + 1} 471 } 472 return r 473 } 474 475 func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) { 476 var pstmt uintptr 477 var done int32 478 if ctx != nil && ctx.Done() != nil { 479 donech := make(chan struct{}) 480 481 go func() { 482 select { 483 case <-ctx.Done(): 484 atomic.AddInt32(&done, 1) 485 s.c.interrupt(s.c.db) 486 case <-donech: 487 } 488 }() 489 490 defer func() { 491 close(donech) 492 }() 493 } 494 495 for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; { 496 if pstmt, err = s.c.prepareV2(&psql); err != nil { 497 return nil, err 498 } 499 500 if pstmt == 0 { 501 continue 502 } 503 err = func() (err error) { 504 n, err := s.c.bindParameterCount(pstmt) 505 if err != nil { 506 return err 507 } 508 509 if n != 0 { 510 allocs, err := s.c.bind(pstmt, n, args) 511 if err != nil { 512 return err 513 } 514 515 if len(allocs) != 0 { 516 defer func() { 517 for _, v := range allocs { 518 s.c.free(v) 519 } 520 }() 521 } 522 } 523 524 rc, err := s.c.step(pstmt) 525 if err != nil { 526 return err 527 } 528 529 switch rc & 0xff { 530 case sqlite3.SQLITE_DONE, sqlite3.SQLITE_ROW: 531 // nop 532 default: 533 return s.c.errstr(int32(rc)) 534 } 535 536 return nil 537 }() 538 539 if e := s.c.finalize(pstmt); e != nil && err == nil { 540 err = e 541 } 542 543 if err != nil { 544 return nil, err 545 } 546 } 547 return newResult(s.c) 548 } 549 550 // NumInput returns the number of placeholder parameters. 551 // 552 // If NumInput returns >= 0, the sql package will sanity check argument counts 553 // from callers and return errors to the caller before the statement's Exec or 554 // Query methods are called. 555 // 556 // NumInput may also return -1, if the driver doesn't know its number of 557 // placeholders. In that case, the sql package will not sanity check Exec or 558 // Query argument counts. 559 func (s *stmt) NumInput() (n int) { 560 return -1 561 } 562 563 // Query executes a query that may return rows, such as a 564 // SELECT. 565 // 566 // Deprecated: Drivers should implement StmtQueryContext instead (or 567 // additionally). 568 func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { //TODO StmtQueryContext 569 return s.query(context.Background(), toNamedValues(args)) 570 } 571 572 func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Rows, err error) { 573 var pstmt uintptr 574 var done int32 575 if ctx != nil && ctx.Done() != nil { 576 donech := make(chan struct{}) 577 578 go func() { 579 select { 580 case <-ctx.Done(): 581 atomic.AddInt32(&done, 1) 582 s.c.interrupt(s.c.db) 583 case <-donech: 584 } 585 }() 586 587 defer func() { 588 close(donech) 589 }() 590 } 591 592 var allocs []uintptr 593 for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; { 594 if pstmt, err = s.c.prepareV2(&psql); err != nil { 595 return nil, err 596 } 597 598 if pstmt == 0 { 599 continue 600 } 601 602 err = func() (err error) { 603 n, err := s.c.bindParameterCount(pstmt) 604 if err != nil { 605 return err 606 } 607 608 if n != 0 { 609 if allocs, err = s.c.bind(pstmt, n, args); err != nil { 610 return err 611 } 612 } 613 614 rc, err := s.c.step(pstmt) 615 if err != nil { 616 return err 617 } 618 619 switch rc & 0xff { 620 case sqlite3.SQLITE_ROW: 621 if r != nil { 622 r.Close() 623 } 624 if r, err = newRows(s.c, pstmt, allocs, false); err != nil { 625 return err 626 } 627 628 pstmt = 0 629 return nil 630 case sqlite3.SQLITE_DONE: 631 if r == nil { 632 if r, err = newRows(s.c, pstmt, allocs, true); err != nil { 633 return err 634 } 635 pstmt = 0 636 return nil 637 } 638 639 // nop 640 default: 641 return s.c.errstr(int32(rc)) 642 } 643 644 if *(*byte)(unsafe.Pointer(psql)) == 0 { 645 if r != nil { 646 r.Close() 647 } 648 if r, err = newRows(s.c, pstmt, allocs, true); err != nil { 649 return err 650 } 651 652 pstmt = 0 653 } 654 return nil 655 }() 656 if e := s.c.finalize(pstmt); e != nil && err == nil { 657 err = e 658 } 659 660 if err != nil { 661 return nil, err 662 } 663 } 664 return r, err 665 } 666 667 type tx struct { 668 c *conn 669 } 670 671 func newTx(c *conn) (*tx, error) { 672 r := &tx{c: c} 673 if err := r.exec(context.Background(), "begin"); err != nil { 674 return nil, err 675 } 676 677 return r, nil 678 } 679 680 // Commit implements driver.Tx. 681 func (t *tx) Commit() (err error) { 682 return t.exec(context.Background(), "commit") 683 } 684 685 // Rollback implements driver.Tx. 686 func (t *tx) Rollback() (err error) { 687 return t.exec(context.Background(), "rollback") 688 } 689 690 func (t *tx) exec(ctx context.Context, sql string) (err error) { 691 psql, err := libc.CString(sql) 692 if err != nil { 693 return err 694 } 695 696 defer t.c.free(psql) 697 //TODO use t.conn.ExecContext() instead 698 699 if ctx != nil && ctx.Done() != nil { 700 donech := make(chan struct{}) 701 702 go func() { 703 select { 704 case <-ctx.Done(): 705 t.c.interrupt(t.c.db) 706 case <-donech: 707 } 708 }() 709 710 defer func() { 711 close(donech) 712 }() 713 } 714 715 if rc := sqlite3.Xsqlite3_exec(t.c.tls, t.c.db, psql, 0, 0, 0); rc != sqlite3.SQLITE_OK { 716 return t.c.errstr(rc) 717 } 718 719 return nil 720 } 721 722 type conn struct { 723 db uintptr // *sqlite3.Xsqlite3 724 tls *libc.TLS 725 726 // Context handling can cause conn.Close and conn.interrupt to be invoked 727 // concurrently. 728 sync.Mutex 729 } 730 731 func newConn(dsn string) (*conn, error) { 732 var query string 733 734 // Parse the query parameters from the dsn and them from the dsn if not prefixed by file: 735 // https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1046 736 // https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1383 737 pos := strings.IndexRune(dsn, '?') 738 if pos >= 1 { 739 query = dsn[pos+1:] 740 if !strings.HasPrefix(dsn, "file:") { 741 dsn = dsn[:pos] 742 } 743 } 744 745 c := &conn{tls: libc.NewTLS()} 746 db, err := c.openV2( 747 dsn, 748 sqlite3.SQLITE_OPEN_READWRITE|sqlite3.SQLITE_OPEN_CREATE| 749 sqlite3.SQLITE_OPEN_FULLMUTEX| 750 sqlite3.SQLITE_OPEN_URI, 751 ) 752 if err != nil { 753 return nil, err 754 } 755 756 c.db = db 757 if err = c.extendedResultCodes(true); err != nil { 758 c.Close() 759 return nil, err 760 } 761 762 if err = applyPragmas(c, query); err != nil { 763 c.Close() 764 return nil, err 765 } 766 767 return c, nil 768 } 769 770 func applyPragmas(c *conn, query string) error { 771 q, err := url.ParseQuery(query) 772 if err != nil { 773 return err 774 } 775 for _, v := range q["_pragma"] { 776 cmd := "pragma " + v 777 _, err := c.exec(context.Background(), cmd, nil) 778 if err != nil { 779 return err 780 } 781 } 782 return nil 783 } 784 785 // const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); 786 func (c *conn) columnBlob(pstmt uintptr, iCol int) (v []byte, err error) { 787 p := sqlite3.Xsqlite3_column_blob(c.tls, pstmt, int32(iCol)) 788 len, err := c.columnBytes(pstmt, iCol) 789 if err != nil { 790 return nil, err 791 } 792 793 if p == 0 || len == 0 { 794 return nil, nil 795 } 796 797 v = make([]byte, len) 798 copy(v, (*libc.RawMem)(unsafe.Pointer(p))[:len:len]) 799 return v, nil 800 } 801 802 // int sqlite3_column_bytes(sqlite3_stmt*, int iCol); 803 func (c *conn) columnBytes(pstmt uintptr, iCol int) (_ int, err error) { 804 v := sqlite3.Xsqlite3_column_bytes(c.tls, pstmt, int32(iCol)) 805 return int(v), nil 806 } 807 808 // const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); 809 func (c *conn) columnText(pstmt uintptr, iCol int) (v string, err error) { 810 p := sqlite3.Xsqlite3_column_text(c.tls, pstmt, int32(iCol)) 811 len, err := c.columnBytes(pstmt, iCol) 812 if err != nil { 813 return "", err 814 } 815 816 if p == 0 || len == 0 { 817 return "", nil 818 } 819 820 b := make([]byte, len) 821 copy(b, (*libc.RawMem)(unsafe.Pointer(p))[:len:len]) 822 return string(b), nil 823 } 824 825 // double sqlite3_column_double(sqlite3_stmt*, int iCol); 826 func (c *conn) columnDouble(pstmt uintptr, iCol int) (v float64, err error) { 827 v = sqlite3.Xsqlite3_column_double(c.tls, pstmt, int32(iCol)) 828 return v, nil 829 } 830 831 // sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); 832 func (c *conn) columnInt64(pstmt uintptr, iCol int) (v int64, err error) { 833 v = sqlite3.Xsqlite3_column_int64(c.tls, pstmt, int32(iCol)) 834 return v, nil 835 } 836 837 // int sqlite3_column_type(sqlite3_stmt*, int iCol); 838 func (c *conn) columnType(pstmt uintptr, iCol int) (_ int, err error) { 839 v := sqlite3.Xsqlite3_column_type(c.tls, pstmt, int32(iCol)) 840 return int(v), nil 841 } 842 843 // const char *sqlite3_column_decltype(sqlite3_stmt*,int); 844 func (c *conn) columnDeclType(pstmt uintptr, iCol int) string { 845 return libc.GoString(sqlite3.Xsqlite3_column_decltype(c.tls, pstmt, int32(iCol))) 846 } 847 848 // const char *sqlite3_column_name(sqlite3_stmt*, int N); 849 func (c *conn) columnName(pstmt uintptr, n int) (string, error) { 850 p := sqlite3.Xsqlite3_column_name(c.tls, pstmt, int32(n)) 851 return libc.GoString(p), nil 852 } 853 854 // int sqlite3_column_count(sqlite3_stmt *pStmt); 855 func (c *conn) columnCount(pstmt uintptr) (_ int, err error) { 856 v := sqlite3.Xsqlite3_column_count(c.tls, pstmt) 857 return int(v), nil 858 } 859 860 // sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); 861 func (c *conn) lastInsertRowID() (v int64, _ error) { 862 return sqlite3.Xsqlite3_last_insert_rowid(c.tls, c.db), nil 863 } 864 865 // int sqlite3_changes(sqlite3*); 866 func (c *conn) changes() (int, error) { 867 v := sqlite3.Xsqlite3_changes(c.tls, c.db) 868 return int(v), nil 869 } 870 871 // int sqlite3_step(sqlite3_stmt*); 872 func (c *conn) step(pstmt uintptr) (int, error) { 873 for { 874 switch rc := sqlite3.Xsqlite3_step(c.tls, pstmt); rc { 875 case sqliteLockedSharedcache, sqlite3.SQLITE_BUSY: 876 if err := c.retry(pstmt); err != nil { 877 return sqlite3.SQLITE_LOCKED, err 878 } 879 default: 880 return int(rc), nil 881 } 882 } 883 } 884 885 func (c *conn) retry(pstmt uintptr) error { 886 mu := mutexAlloc(c.tls) 887 (*mutex)(unsafe.Pointer(mu)).Lock() 888 rc := sqlite3.Xsqlite3_unlock_notify( 889 c.tls, 890 c.db, 891 *(*uintptr)(unsafe.Pointer(&struct { 892 f func(*libc.TLS, uintptr, int32) 893 }{unlockNotify})), 894 mu, 895 ) 896 if rc == sqlite3.SQLITE_LOCKED { // Deadlock, see https://www.sqlite.org/c3ref/unlock_notify.html 897 (*mutex)(unsafe.Pointer(mu)).Unlock() 898 mutexFree(c.tls, mu) 899 return c.errstr(rc) 900 } 901 902 (*mutex)(unsafe.Pointer(mu)).Lock() 903 (*mutex)(unsafe.Pointer(mu)).Unlock() 904 mutexFree(c.tls, mu) 905 if pstmt != 0 { 906 sqlite3.Xsqlite3_reset(c.tls, pstmt) 907 } 908 return nil 909 } 910 911 func unlockNotify(t *libc.TLS, ppArg uintptr, nArg int32) { 912 for i := int32(0); i < nArg; i++ { 913 mu := *(*uintptr)(unsafe.Pointer(ppArg)) 914 (*mutex)(unsafe.Pointer(mu)).Unlock() 915 ppArg += ptrSize 916 } 917 } 918 919 func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []uintptr, err error) { 920 defer func() { 921 if err == nil { 922 return 923 } 924 925 for _, v := range allocs { 926 c.free(v) 927 } 928 allocs = nil 929 }() 930 931 for i := 1; i <= n; i++ { 932 name, err := c.bindParameterName(pstmt, i) 933 if err != nil { 934 return allocs, err 935 } 936 937 var found bool 938 var v driver.NamedValue 939 for _, v = range args { 940 if name != "" { 941 // For ?NNN and $NNN params, match if NNN == v.Ordinal. 942 // 943 // Supporting this for $NNN is a special case that makes eg 944 // `select $1, $2, $3 ...` work without needing to use 945 // sql.Named. 946 if (name[0] == '?' || name[0] == '$') && name[1:] == strconv.Itoa(v.Ordinal) { 947 found = true 948 break 949 } 950 951 // sqlite supports '$', '@' and ':' prefixes for string 952 // identifiers and '?' for numeric, so we cannot 953 // combine different prefixes with the same name 954 // because `database/sql` requires variable names 955 // to start with a letter 956 if name[1:] == v.Name[:] { 957 found = true 958 break 959 } 960 } else { 961 if v.Ordinal == i { 962 found = true 963 break 964 } 965 } 966 } 967 968 if !found { 969 if name != "" { 970 return allocs, fmt.Errorf("missing named argument %q", name[1:]) 971 } 972 973 return allocs, fmt.Errorf("missing argument with index %d", i) 974 } 975 976 var p uintptr 977 switch x := v.Value.(type) { 978 case int64: 979 if err := c.bindInt64(pstmt, i, x); err != nil { 980 return allocs, err 981 } 982 case float64: 983 if err := c.bindDouble(pstmt, i, x); err != nil { 984 return allocs, err 985 } 986 case bool: 987 v := 0 988 if x { 989 v = 1 990 } 991 if err := c.bindInt(pstmt, i, v); err != nil { 992 return allocs, err 993 } 994 case []byte: 995 if p, err = c.bindBlob(pstmt, i, x); err != nil { 996 return allocs, err 997 } 998 case string: 999 if p, err = c.bindText(pstmt, i, x); err != nil { 1000 return allocs, err 1001 } 1002 case time.Time: 1003 if p, err = c.bindText(pstmt, i, x.String()); err != nil { 1004 return allocs, err 1005 } 1006 case nil: 1007 if p, err = c.bindNull(pstmt, i); err != nil { 1008 return allocs, err 1009 } 1010 default: 1011 return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x) 1012 } 1013 if p != 0 { 1014 allocs = append(allocs, p) 1015 } 1016 } 1017 return allocs, nil 1018 } 1019 1020 // int sqlite3_bind_null(sqlite3_stmt*, int); 1021 func (c *conn) bindNull(pstmt uintptr, idx1 int) (uintptr, error) { 1022 if rc := sqlite3.Xsqlite3_bind_null(c.tls, pstmt, int32(idx1)); rc != sqlite3.SQLITE_OK { 1023 return 0, c.errstr(rc) 1024 } 1025 1026 return 0, nil 1027 } 1028 1029 // int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); 1030 func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) { 1031 p, err := libc.CString(value) 1032 if err != nil { 1033 return 0, err 1034 } 1035 1036 if rc := sqlite3.Xsqlite3_bind_text(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK { 1037 c.free(p) 1038 return 0, c.errstr(rc) 1039 } 1040 1041 return p, nil 1042 } 1043 1044 // int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); 1045 func (c *conn) bindBlob(pstmt uintptr, idx1 int, value []byte) (uintptr, error) { 1046 p, err := c.malloc(len(value)) 1047 if err != nil { 1048 return 0, err 1049 } 1050 1051 if len(value) != 0 { 1052 copy((*libc.RawMem)(unsafe.Pointer(p))[:len(value):len(value)], value) 1053 } 1054 if rc := sqlite3.Xsqlite3_bind_blob(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK { 1055 c.free(p) 1056 return 0, c.errstr(rc) 1057 } 1058 1059 return p, nil 1060 } 1061 1062 // int sqlite3_bind_int(sqlite3_stmt*, int, int); 1063 func (c *conn) bindInt(pstmt uintptr, idx1, value int) (err error) { 1064 if rc := sqlite3.Xsqlite3_bind_int(c.tls, pstmt, int32(idx1), int32(value)); rc != sqlite3.SQLITE_OK { 1065 return c.errstr(rc) 1066 } 1067 1068 return nil 1069 } 1070 1071 // int sqlite3_bind_double(sqlite3_stmt*, int, double); 1072 func (c *conn) bindDouble(pstmt uintptr, idx1 int, value float64) (err error) { 1073 if rc := sqlite3.Xsqlite3_bind_double(c.tls, pstmt, int32(idx1), value); rc != 0 { 1074 return c.errstr(rc) 1075 } 1076 1077 return nil 1078 } 1079 1080 // int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); 1081 func (c *conn) bindInt64(pstmt uintptr, idx1 int, value int64) (err error) { 1082 if rc := sqlite3.Xsqlite3_bind_int64(c.tls, pstmt, int32(idx1), value); rc != sqlite3.SQLITE_OK { 1083 return c.errstr(rc) 1084 } 1085 1086 return nil 1087 } 1088 1089 // const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); 1090 func (c *conn) bindParameterName(pstmt uintptr, i int) (string, error) { 1091 p := sqlite3.Xsqlite3_bind_parameter_name(c.tls, pstmt, int32(i)) 1092 return libc.GoString(p), nil 1093 } 1094 1095 // int sqlite3_bind_parameter_count(sqlite3_stmt*); 1096 func (c *conn) bindParameterCount(pstmt uintptr) (_ int, err error) { 1097 r := sqlite3.Xsqlite3_bind_parameter_count(c.tls, pstmt) 1098 return int(r), nil 1099 } 1100 1101 // int sqlite3_finalize(sqlite3_stmt *pStmt); 1102 func (c *conn) finalize(pstmt uintptr) error { 1103 if rc := sqlite3.Xsqlite3_finalize(c.tls, pstmt); rc != sqlite3.SQLITE_OK { 1104 return c.errstr(rc) 1105 } 1106 1107 return nil 1108 } 1109 1110 // int sqlite3_prepare_v2( 1111 // sqlite3 *db, /* Database handle */ 1112 // const char *zSql, /* SQL statement, UTF-8 encoded */ 1113 // int nByte, /* Maximum length of zSql in bytes. */ 1114 // sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 1115 // const char **pzTail /* OUT: Pointer to unused portion of zSql */ 1116 // ); 1117 func (c *conn) prepareV2(zSQL *uintptr) (pstmt uintptr, err error) { 1118 var ppstmt, pptail uintptr 1119 1120 defer func() { 1121 c.free(ppstmt) 1122 c.free(pptail) 1123 }() 1124 1125 if ppstmt, err = c.malloc(int(ptrSize)); err != nil { 1126 return 0, err 1127 } 1128 1129 if pptail, err = c.malloc(int(ptrSize)); err != nil { 1130 return 0, err 1131 } 1132 1133 for { 1134 switch rc := sqlite3.Xsqlite3_prepare_v2(c.tls, c.db, *zSQL, -1, ppstmt, pptail); rc { 1135 case sqlite3.SQLITE_OK: 1136 *zSQL = *(*uintptr)(unsafe.Pointer(pptail)) 1137 return *(*uintptr)(unsafe.Pointer(ppstmt)), nil 1138 case sqliteLockedSharedcache, sqlite3.SQLITE_BUSY: 1139 if err := c.retry(0); err != nil { 1140 return 0, err 1141 } 1142 default: 1143 return 0, c.errstr(rc) 1144 } 1145 } 1146 } 1147 1148 // void sqlite3_interrupt(sqlite3*); 1149 func (c *conn) interrupt(pdb uintptr) (err error) { 1150 c.Lock() // Defend against race with .Close invoked by context handling. 1151 1152 defer c.Unlock() 1153 1154 if c.tls != nil { 1155 sqlite3.Xsqlite3_interrupt(c.tls, pdb) 1156 } 1157 return nil 1158 } 1159 1160 // int sqlite3_extended_result_codes(sqlite3*, int onoff); 1161 func (c *conn) extendedResultCodes(on bool) error { 1162 if rc := sqlite3.Xsqlite3_extended_result_codes(c.tls, c.db, libc.Bool32(on)); rc != sqlite3.SQLITE_OK { 1163 return c.errstr(rc) 1164 } 1165 1166 return nil 1167 } 1168 1169 // int sqlite3_open_v2( 1170 // const char *filename, /* Database filename (UTF-8) */ 1171 // sqlite3 **ppDb, /* OUT: SQLite db handle */ 1172 // int flags, /* Flags */ 1173 // const char *zVfs /* Name of VFS module to use */ 1174 // ); 1175 func (c *conn) openV2(name string, flags int32) (uintptr, error) { 1176 var p, s uintptr 1177 1178 defer func() { 1179 if p != 0 { 1180 c.free(p) 1181 } 1182 if s != 0 { 1183 c.free(s) 1184 } 1185 }() 1186 1187 p, err := c.malloc(int(ptrSize)) 1188 if err != nil { 1189 return 0, err 1190 } 1191 1192 if s, err = libc.CString(name); err != nil { 1193 return 0, err 1194 } 1195 1196 if rc := sqlite3.Xsqlite3_open_v2(c.tls, s, p, flags, 0); rc != sqlite3.SQLITE_OK { 1197 return 0, c.errstr(rc) 1198 } 1199 1200 return *(*uintptr)(unsafe.Pointer(p)), nil 1201 } 1202 1203 func (c *conn) malloc(n int) (uintptr, error) { 1204 if p := libc.Xmalloc(c.tls, types.Size_t(n)); p != 0 || n == 0 { 1205 return p, nil 1206 } 1207 1208 return 0, fmt.Errorf("sqlite: cannot allocate %d bytes of memory", n) 1209 } 1210 1211 func (c *conn) free(p uintptr) { 1212 if p != 0 { 1213 libc.Xfree(c.tls, p) 1214 } 1215 } 1216 1217 // const char *sqlite3_errstr(int); 1218 func (c *conn) errstr(rc int32) error { 1219 p := sqlite3.Xsqlite3_errstr(c.tls, rc) 1220 str := libc.GoString(p) 1221 p = sqlite3.Xsqlite3_errmsg(c.tls, c.db) 1222 switch msg := libc.GoString(p); { 1223 case msg == str: 1224 return &Error{msg: fmt.Sprintf("%s (%v)", str, rc), code: int(rc)} 1225 default: 1226 return &Error{msg: fmt.Sprintf("%s: %s (%v)", str, msg, rc), code: int(rc)} 1227 } 1228 } 1229 1230 // Begin starts a transaction. 1231 // 1232 // Deprecated: Drivers should implement ConnBeginTx instead (or additionally). 1233 func (c *conn) Begin() (driver.Tx, error) { 1234 return c.begin(context.Background(), driver.TxOptions{}) 1235 } 1236 1237 func (c *conn) begin(ctx context.Context, opts driver.TxOptions) (t driver.Tx, err error) { 1238 return newTx(c) 1239 } 1240 1241 // Close invalidates and potentially stops any current prepared statements and 1242 // transactions, marking this connection as no longer in use. 1243 // 1244 // Because the sql package maintains a free pool of connections and only calls 1245 // Close when there's a surplus of idle connections, it shouldn't be necessary 1246 // for drivers to do their own connection caching. 1247 func (c *conn) Close() error { 1248 c.Lock() // Defend against race with .interrupt invoked by context handling. 1249 1250 defer c.Unlock() 1251 1252 if c.db != 0 { 1253 if err := c.closeV2(c.db); err != nil { 1254 return err 1255 } 1256 1257 c.db = 0 1258 } 1259 if c.tls != nil { 1260 c.tls.Close() 1261 c.tls = nil 1262 } 1263 return nil 1264 } 1265 1266 // int sqlite3_close_v2(sqlite3*); 1267 func (c *conn) closeV2(db uintptr) error { 1268 if rc := sqlite3.Xsqlite3_close_v2(c.tls, db); rc != sqlite3.SQLITE_OK { 1269 return c.errstr(rc) 1270 } 1271 1272 return nil 1273 } 1274 1275 // Execer is an optional interface that may be implemented by a Conn. 1276 // 1277 // If a Conn does not implement Execer, the sql package's DB.Exec will first 1278 // prepare a query, execute the statement, and then close the statement. 1279 // 1280 // Exec may return ErrSkip. 1281 // 1282 // Deprecated: Drivers should implement ExecerContext instead. 1283 func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) { 1284 return c.exec(context.Background(), query, toNamedValues(args)) 1285 } 1286 1287 func (c *conn) exec(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) { 1288 s, err := c.prepare(ctx, query) 1289 if err != nil { 1290 return nil, err 1291 } 1292 1293 defer func() { 1294 if err2 := s.Close(); err2 != nil && err == nil { 1295 err = err2 1296 } 1297 }() 1298 1299 return s.(*stmt).exec(ctx, args) 1300 } 1301 1302 // Prepare returns a prepared statement, bound to this connection. 1303 func (c *conn) Prepare(query string) (driver.Stmt, error) { 1304 return c.prepare(context.Background(), query) 1305 } 1306 1307 func (c *conn) prepare(ctx context.Context, query string) (s driver.Stmt, err error) { 1308 //TODO use ctx 1309 return newStmt(c, query) 1310 } 1311 1312 // Queryer is an optional interface that may be implemented by a Conn. 1313 // 1314 // If a Conn does not implement Queryer, the sql package's DB.Query will first 1315 // prepare a query, execute the statement, and then close the statement. 1316 // 1317 // Query may return ErrSkip. 1318 // 1319 // Deprecated: Drivers should implement QueryerContext instead. 1320 func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) { 1321 return c.query(context.Background(), query, toNamedValues(args)) 1322 } 1323 1324 func (c *conn) query(ctx context.Context, query string, args []driver.NamedValue) (r driver.Rows, err error) { 1325 s, err := c.prepare(ctx, query) 1326 if err != nil { 1327 return nil, err 1328 } 1329 1330 defer func() { 1331 if err2 := s.Close(); err2 != nil && err == nil { 1332 err = err2 1333 } 1334 }() 1335 1336 return s.(*stmt).query(ctx, args) 1337 } 1338 1339 // Driver implements database/sql/driver.Driver. 1340 type Driver struct{} 1341 1342 func newDriver() *Driver { return &Driver{} } 1343 1344 // Open returns a new connection to the database. The name is a string in a 1345 // driver-specific format. 1346 // 1347 // Open may return a cached connection (one previously closed), but doing so is 1348 // unnecessary; the sql package maintains a pool of idle connections for 1349 // efficient re-use. 1350 // 1351 // The returned connection is only used by one goroutine at a time. 1352 func (d *Driver) Open(name string) (driver.Conn, error) { 1353 return newConn(name) 1354 }