sqlite.go 29.2 KB
Newer Older
cznic's avatar
cznic committed
1 2 3 4
// Copyright 2017 The Sqlite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

cznic's avatar
cznic committed
5 6 7
//go:generate go run generator.go
//go:generate gofmt -l -s -w .

cznic's avatar
cznic committed
8
package sqlite // import "modernc.org/sqlite"
cznic's avatar
cznic committed
9 10

import (
cznic's avatar
cznic committed
11
	"context"
cznic's avatar
cznic committed
12 13 14 15
	"database/sql"
	"database/sql/driver"
	"fmt"
	"io"
cznic's avatar
cznic committed
16 17 18
	"os"
	"runtime"
	"strings"
cznic's avatar
cznic committed
19 20 21
	"time"
	"unsafe"

cznic's avatar
cznic committed
22 23
	"modernc.org/crt/v3"
	"modernc.org/sqlite/lib"
cznic's avatar
cznic committed
24 25 26 27
)

var (
	_ driver.Conn    = (*conn)(nil)
Aliaksand Mianzhynski's avatar
Aliaksand Mianzhynski committed
28
	_ driver.Driver  = (*Driver)(nil)
cznic's avatar
cznic committed
29 30 31 32 33 34
	_ driver.Execer  = (*conn)(nil)
	_ driver.Queryer = (*conn)(nil)
	_ driver.Result  = (*result)(nil)
	_ driver.Rows    = (*rows)(nil)
	_ driver.Stmt    = (*stmt)(nil)
	_ driver.Tx      = (*tx)(nil)
cznic's avatar
cznic committed
35
	_ error          = (*Error)(nil)
cznic's avatar
cznic committed
36 37 38
)

const (
cznic's avatar
cznic committed
39
	driverName              = "sqlite"
cznic's avatar
cznic committed
40 41
	ptrSize                 = unsafe.Sizeof(uintptr(0))
	sqliteLockedSharedcache = sqlite3.SQLITE_LOCKED | (1 << 8)
cznic's avatar
cznic committed
42 43
)

cznic's avatar
cznic committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
func origin(skip int) string {
	pc, fn, fl, _ := runtime.Caller(skip)
	f := runtime.FuncForPC(pc)
	var fns string
	if f != nil {
		fns = f.Name()
		if x := strings.LastIndex(fns, "."); x > 0 {
			fns = fns[x+1:]
		}
	}
	return fmt.Sprintf("%s:%d:%s", fn, fl, fns)
}

func todo(s string, args ...interface{}) string { //TODO-
	switch {
	case s == "":
		s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
	default:
		s = fmt.Sprintf(s, args...)
	}
	r := fmt.Sprintf("%s: TODOTODO %s", origin(2), s) //TODOOK
	fmt.Fprintf(os.Stdout, "%s\n", r)
	os.Stdout.Sync()
	return r
}

func trc(s string, args ...interface{}) string { //TODO-
	switch {
	case s == "":
		s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
	default:
		s = fmt.Sprintf(s, args...)
	}
	r := fmt.Sprintf("\n%s: TRC %s", origin(2), s)
	fmt.Fprintf(os.Stdout, "%s\n", r)
	os.Stdout.Sync()
	return r
}

cznic's avatar
cznic committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
// Error represents sqlite library error code.
type Error struct {
	msg  string
	code int
}

// Error implements error.
func (e *Error) Error() string { return e.msg }

// Code returns the sqlite result code for this error.
func (e *Error) Code() int { return e.code }

var (
	// ErrorCodeString maps Error.Code() to its string representation.
	ErrorCodeString = map[int]string{
cznic's avatar
cznic committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		sqlite3.SQLITE_ABORT:             "Callback routine requested an abort (SQLITE_ABORT)",
		sqlite3.SQLITE_AUTH:              "Authorization denied (SQLITE_AUTH)",
		sqlite3.SQLITE_BUSY:              "The database file is locked (SQLITE_BUSY)",
		sqlite3.SQLITE_CANTOPEN:          "Unable to open the database file (SQLITE_CANTOPEN)",
		sqlite3.SQLITE_CONSTRAINT:        "Abort due to constraint violation (SQLITE_CONSTRAINT)",
		sqlite3.SQLITE_CORRUPT:           "The database disk image is malformed (SQLITE_CORRUPT)",
		sqlite3.SQLITE_DONE:              "sqlite3_step() has finished executing (SQLITE_DONE)",
		sqlite3.SQLITE_EMPTY:             "Internal use only (SQLITE_EMPTY)",
		sqlite3.SQLITE_ERROR:             "Generic error (SQLITE_ERROR)",
		sqlite3.SQLITE_FORMAT:            "Not used (SQLITE_FORMAT)",
		sqlite3.SQLITE_FULL:              "Insertion failed because database is full (SQLITE_FULL)",
		sqlite3.SQLITE_INTERNAL:          "Internal logic error in SQLite (SQLITE_INTERNAL)",
		sqlite3.SQLITE_INTERRUPT:         "Operation terminated by sqlite3_interrupt()(SQLITE_INTERRUPT)",
		sqlite3.SQLITE_IOERR | (1 << 8):  "(SQLITE_IOERR_READ)",
		sqlite3.SQLITE_IOERR | (10 << 8): "(SQLITE_IOERR_DELETE)",
		sqlite3.SQLITE_IOERR | (11 << 8): "(SQLITE_IOERR_BLOCKED)",
		sqlite3.SQLITE_IOERR | (12 << 8): "(SQLITE_IOERR_NOMEM)",
		sqlite3.SQLITE_IOERR | (13 << 8): "(SQLITE_IOERR_ACCESS)",
		sqlite3.SQLITE_IOERR | (14 << 8): "(SQLITE_IOERR_CHECKRESERVEDLOCK)",
		sqlite3.SQLITE_IOERR | (15 << 8): "(SQLITE_IOERR_LOCK)",
		sqlite3.SQLITE_IOERR | (16 << 8): "(SQLITE_IOERR_CLOSE)",
		sqlite3.SQLITE_IOERR | (17 << 8): "(SQLITE_IOERR_DIR_CLOSE)",
		sqlite3.SQLITE_IOERR | (2 << 8):  "(SQLITE_IOERR_SHORT_READ)",
		sqlite3.SQLITE_IOERR | (3 << 8):  "(SQLITE_IOERR_WRITE)",
		sqlite3.SQLITE_IOERR | (4 << 8):  "(SQLITE_IOERR_FSYNC)",
		sqlite3.SQLITE_IOERR | (5 << 8):  "(SQLITE_IOERR_DIR_FSYNC)",
		sqlite3.SQLITE_IOERR | (6 << 8):  "(SQLITE_IOERR_TRUNCATE)",
		sqlite3.SQLITE_IOERR | (7 << 8):  "(SQLITE_IOERR_FSTAT)",
		sqlite3.SQLITE_IOERR | (8 << 8):  "(SQLITE_IOERR_UNLOCK)",
		sqlite3.SQLITE_IOERR | (9 << 8):  "(SQLITE_IOERR_RDLOCK)",
		sqlite3.SQLITE_IOERR:             "Some kind of disk I/O error occurred (SQLITE_IOERR)",
		sqlite3.SQLITE_LOCKED | (1 << 8): "(SQLITE_LOCKED_SHAREDCACHE)",
		sqlite3.SQLITE_LOCKED:            "A table in the database is locked (SQLITE_LOCKED)",
		sqlite3.SQLITE_MISMATCH:          "Data type mismatch (SQLITE_MISMATCH)",
		sqlite3.SQLITE_MISUSE:            "Library used incorrectly (SQLITE_MISUSE)",
		sqlite3.SQLITE_NOLFS:             "Uses OS features not supported on host (SQLITE_NOLFS)",
		sqlite3.SQLITE_NOMEM:             "A malloc() failed (SQLITE_NOMEM)",
		sqlite3.SQLITE_NOTADB:            "File opened that is not a database file (SQLITE_NOTADB)",
		sqlite3.SQLITE_NOTFOUND:          "Unknown opcode in sqlite3_file_control() (SQLITE_NOTFOUND)",
		sqlite3.SQLITE_NOTICE:            "Notifications from sqlite3_log() (SQLITE_NOTICE)",
		sqlite3.SQLITE_PERM:              "Access permission denied (SQLITE_PERM)",
		sqlite3.SQLITE_PROTOCOL:          "Database lock protocol error (SQLITE_PROTOCOL)",
		sqlite3.SQLITE_RANGE:             "2nd parameter to sqlite3_bind out of range (SQLITE_RANGE)",
		sqlite3.SQLITE_READONLY:          "Attempt to write a readonly database (SQLITE_READONLY)",
		sqlite3.SQLITE_ROW:               "sqlite3_step() has another row ready (SQLITE_ROW)",
		sqlite3.SQLITE_SCHEMA:            "The database schema changed (SQLITE_SCHEMA)",
		sqlite3.SQLITE_TOOBIG:            "String or BLOB exceeds size limit (SQLITE_TOOBIG)",
		sqlite3.SQLITE_WARNING:           "Warnings from sqlite3_log() (SQLITE_WARNING)",
cznic's avatar
cznic committed
146
	}
cznic's avatar
cznic committed
147 148 149
)

func init() {
150
	tls := crt.NewTLS()
cznic's avatar
cznic committed
151
	if sqlite3.Xsqlite3_threadsafe(tls) == 0 {
152 153 154
		panic(fmt.Errorf("sqlite: thread safety configuration error"))
	}

cznic's avatar
cznic committed
155
	varArgs := crt.Xmalloc(tls, crt.Size_t(ptrSize))
cznic's avatar
cznic committed
156 157 158 159 160
	if varArgs == 0 {
		panic(fmt.Errorf("cannot allocate memory"))
	}

	// int sqlite3_config(int, ...);
cznic's avatar
cznic committed
161
	if rc := sqlite3.Xsqlite3_config(tls, sqlite3.SQLITE_CONFIG_MUTEX, crt.VaList(varArgs, uintptr(unsafe.Pointer(&mutexMethods)))); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
162
		p := sqlite3.Xsqlite3_errstr(tls, rc)
cznic's avatar
cznic committed
163 164 165 166 167
		str := crt.GoString(p)
		panic(fmt.Errorf("sqlite: failed to configure mutex methods: %v", str))
	}

	crt.Xfree(tls, varArgs)
cznic's avatar
cznic committed
168
	tls.Close()
cznic's avatar
cznic committed
169
	sql.Register(driverName, newDriver())
cznic's avatar
cznic committed
170 171 172
}

type result struct {
173
	lastInsertID int64
cznic's avatar
cznic committed
174 175 176
	rowsAffected int
}

cznic's avatar
cznic committed
177 178 179
func newResult(c *conn) (_ *result, err error) {
	r := &result{}
	if r.rowsAffected, err = c.changes(); err != nil {
cznic's avatar
cznic committed
180 181 182
		return nil, err
	}

cznic's avatar
cznic committed
183
	if r.lastInsertID, err = c.lastInsertRowID(); err != nil {
cznic's avatar
cznic committed
184 185 186 187 188 189 190 191 192 193 194 195 196
		return nil, err
	}

	return r, nil
}

// LastInsertId returns the database's auto-generated ID after, for example, an
// INSERT into a table with primary key.
func (r *result) LastInsertId() (int64, error) {
	if r == nil {
		return 0, nil
	}

197
	return r.lastInsertID, nil
cznic's avatar
cznic committed
198 199 200 201 202 203 204 205 206 207 208 209
}

// RowsAffected returns the number of rows affected by the query.
func (r *result) RowsAffected() (int64, error) {
	if r == nil {
		return 0, nil
	}

	return int64(r.rowsAffected), nil
}

type rows struct {
cznic's avatar
cznic committed
210
	allocs  []uintptr
cznic's avatar
cznic committed
211
	c       *conn
cznic's avatar
cznic committed
212
	columns []string
cznic's avatar
cznic committed
213
	pstmt   uintptr
cznic's avatar
cznic committed
214 215

	doStep bool
cznic's avatar
cznic committed
216
	empty  bool
cznic's avatar
cznic committed
217 218
}

cznic's avatar
cznic committed
219
func newRows(c *conn, pstmt uintptr, allocs []uintptr, empty bool) (r *rows, err error) {
cznic's avatar
cznic committed
220 221 222 223 224 225
	defer func() {
		if err != nil {
			c.finalize(pstmt)
			r = nil
		}
	}()
cznic's avatar
cznic committed
226

cznic's avatar
cznic committed
227 228
	r = &rows{c: c, pstmt: pstmt, allocs: allocs}
	n, err := c.columnCount(pstmt)
cznic's avatar
cznic committed
229 230 231 232 233 234
	if err != nil {
		return nil, err
	}

	r.columns = make([]string, n)
	for i := range r.columns {
cznic's avatar
cznic committed
235
		if r.columns[i], err = r.c.columnName(pstmt, i); err != nil {
cznic's avatar
cznic committed
236 237 238 239 240 241 242
			return nil, err
		}
	}

	return r, nil
}

cznic's avatar
cznic committed
243 244 245 246 247 248 249 250 251
// Close closes the rows iterator.
func (r *rows) Close() (err error) {
	for _, v := range r.allocs {
		r.c.free(v)
	}
	r.allocs = nil
	return r.c.finalize(r.pstmt)
}

cznic's avatar
cznic committed
252 253 254 255 256 257 258 259 260 261 262 263
// Columns returns the names of the columns. The number of columns of the
// result is inferred from the length of the slice. If a particular column name
// isn't known, an empty string should be returned for that entry.
func (r *rows) Columns() (c []string) {
	return r.columns
}

// Next is called to populate the next row of data into the provided slice. The
// provided slice will be the same size as the Columns() are wide.
//
// Next should return io.EOF when there are no more rows.
func (r *rows) Next(dest []driver.Value) (err error) {
cznic's avatar
cznic committed
264 265 266 267
	if r.empty {
		return io.EOF
	}

cznic's avatar
cznic committed
268
	rc := sqlite3.SQLITE_ROW
269
	if r.doStep {
cznic's avatar
cznic committed
270
		if rc, err = r.c.step(r.pstmt); err != nil {
cznic's avatar
cznic committed
271 272 273 274
			return err
		}
	}

275
	r.doStep = true
cznic's avatar
cznic committed
276
	switch rc {
cznic's avatar
cznic committed
277
	case sqlite3.SQLITE_ROW:
cznic's avatar
cznic committed
278
		if g, e := len(dest), len(r.columns); g != e {
cznic's avatar
cznic committed
279
			return fmt.Errorf("sqlite: Next: have %v destination values, expected %v", g, e)
cznic's avatar
cznic committed
280 281 282
		}

		for i := range dest {
cznic's avatar
cznic committed
283
			ct, err := r.c.columnType(r.pstmt, i)
cznic's avatar
cznic committed
284 285 286 287 288
			if err != nil {
				return err
			}

			switch ct {
cznic's avatar
cznic committed
289
			case sqlite3.SQLITE_INTEGER:
cznic's avatar
cznic committed
290
				v, err := r.c.columnInt64(r.pstmt, i)
cznic's avatar
cznic committed
291 292 293 294 295
				if err != nil {
					return err
				}

				dest[i] = v
cznic's avatar
cznic committed
296
			case sqlite3.SQLITE_FLOAT:
cznic's avatar
cznic committed
297
				v, err := r.c.columnDouble(r.pstmt, i)
cznic's avatar
cznic committed
298 299 300 301 302
				if err != nil {
					return err
				}

				dest[i] = v
cznic's avatar
cznic committed
303
			case sqlite3.SQLITE_TEXT:
cznic's avatar
cznic committed
304
				v, err := r.c.columnText(r.pstmt, i)
cznic's avatar
cznic committed
305 306 307 308 309
				if err != nil {
					return err
				}

				dest[i] = v
cznic's avatar
cznic committed
310
			case sqlite3.SQLITE_BLOB:
cznic's avatar
cznic committed
311
				v, err := r.c.columnBlob(r.pstmt, i)
cznic's avatar
cznic committed
312 313 314 315 316
				if err != nil {
					return err
				}

				dest[i] = v
cznic's avatar
cznic committed
317
			case sqlite3.SQLITE_NULL:
cznic's avatar
cznic committed
318 319
				dest[i] = nil
			default:
cznic's avatar
cznic committed
320
				return fmt.Errorf("internal error: rc %d", rc)
cznic's avatar
cznic committed
321 322 323
			}
		}
		return nil
cznic's avatar
cznic committed
324
	case sqlite3.SQLITE_DONE:
cznic's avatar
cznic committed
325 326
		return io.EOF
	default:
cznic's avatar
cznic committed
327
		return r.c.errstr(int32(rc))
cznic's avatar
cznic committed
328 329 330 331
	}
}

type stmt struct {
cznic's avatar
cznic committed
332
	c    *conn
cznic's avatar
cznic committed
333
	psql uintptr
cznic's avatar
cznic committed
334 335
}

336
func newStmt(c *conn, sql string) (*stmt, error) {
cznic's avatar
cznic committed
337
	p, err := crt.CString(sql)
cznic's avatar
cznic committed
338 339 340 341
	if err != nil {
		return nil, err
	}

cznic's avatar
cznic committed
342
	return &stmt{c: c, psql: p}, nil
cznic's avatar
cznic committed
343 344 345 346 347 348
}

// Close closes the statement.
//
// As of Go 1.1, a Stmt will not be closed if it's in use by any queries.
func (s *stmt) Close() (err error) {
cznic's avatar
cznic committed
349 350 351
	s.c.free(s.psql)
	s.psql = 0
	return nil
cznic's avatar
cznic committed
352 353
}

cznic's avatar
cznic committed
354
// Exec executes a query that doesn't return rows, such as an INSERT or UPDATE.
cznic's avatar
cznic committed
355 356
//
//
cznic's avatar
cznic committed
357 358 359 360
// Deprecated: Drivers should implement StmtExecContext instead (or
// additionally).
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { //TODO StmtExecContext
	return s.exec(context.Background(), toNamedValues(args))
cznic's avatar
cznic committed
361 362
}

cznic's avatar
cznic committed
363 364 365 366 367 368 369
// toNamedValues converts []driver.Value to []driver.NamedValue
func toNamedValues(vals []driver.Value) (r []driver.NamedValue) {
	r = make([]driver.NamedValue, len(vals))
	for i, val := range vals {
		r[i] = driver.NamedValue{Value: val, Ordinal: i + 1}
	}
	return r
370 371
}

cznic's avatar
cznic committed
372
func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) {
cznic's avatar
cznic committed
373
	var pstmt uintptr
374 375

	donech := make(chan struct{})
cznic's avatar
cznic committed
376

377 378 379
	go func() {
		select {
		case <-ctx.Done():
cznic's avatar
cznic committed
380
			if pstmt != 0 {
cznic's avatar
cznic committed
381
				s.c.interrupt(s.c.db)
382 383 384 385 386
			}
		case <-donech:
		}
	}()

cznic's avatar
cznic committed
387 388 389 390 391 392 393
	defer func() {
		pstmt = 0
		close(donech)
	}()

	for psql := s.psql; *(*byte)(unsafe.Pointer(uintptr(psql))) != 0; {
		if pstmt, err = s.c.prepareV2(&psql); err != nil {
cznic's avatar
cznic committed
394 395 396
			return nil, err
		}

cznic's avatar
cznic committed
397
		if pstmt == 0 {
cznic's avatar
cznic committed
398 399 400
			continue
		}

cznic's avatar
cznic committed
401 402 403 404 405 406
		if err := func() (err error) {
			defer func() {
				if e := s.c.finalize(pstmt); e != nil && err == nil {
					err = e
				}
			}()
cznic's avatar
cznic committed
407

cznic's avatar
cznic committed
408 409 410
			n, err := s.c.bindParameterCount(pstmt)
			if err != nil {
				return err
cznic's avatar
cznic committed
411
			}
412

cznic's avatar
cznic committed
413 414 415 416 417
			if n != 0 {
				allocs, err := s.c.bind(pstmt, n, args)
				if err != nil {
					return err
				}
cznic's avatar
cznic committed
418

cznic's avatar
cznic committed
419 420 421 422 423 424 425
				if len(allocs) != 0 {
					defer func() {
						for _, v := range allocs {
							s.c.free(v)
						}
					}()
				}
cznic's avatar
cznic committed
426
			}
cznic's avatar
cznic committed
427 428 429 430 431 432 433

			rc, err := s.c.step(pstmt)
			if err != nil {
				return err
			}

			switch rc & 0xff {
cznic's avatar
cznic committed
434
			case sqlite3.SQLITE_DONE, sqlite3.SQLITE_ROW:
cznic's avatar
cznic committed
435 436 437 438 439 440 441
				// nop
			default:
				return s.c.errstr(int32(rc))
			}

			return nil
		}(); err != nil {
cznic's avatar
cznic committed
442 443 444
			return nil, err
		}
	}
cznic's avatar
cznic committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458
	return newResult(s.c)
}

// NumInput returns the number of placeholder parameters.
//
// If NumInput returns >= 0, the sql package will sanity check argument counts
// from callers and return errors to the caller before the statement's Exec or
// Query methods are called.
//
// NumInput may also return -1, if the driver doesn't know its number of
// placeholders. In that case, the sql package will not sanity check Exec or
// Query argument counts.
func (s *stmt) NumInput() (n int) {
	return -1
cznic's avatar
cznic committed
459 460
}

cznic's avatar
cznic committed
461 462 463 464 465 466
// Query executes a query that may return rows, such as a
// SELECT.
//
// Deprecated: Drivers should implement StmtQueryContext instead (or
// additionally).
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { //TODO StmtQueryContext
467 468 469
	return s.query(context.Background(), toNamedValues(args))
}

cznic's avatar
cznic committed
470
func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Rows, err error) {
cznic's avatar
cznic committed
471
	var pstmt uintptr
472 473

	donech := make(chan struct{})
cznic's avatar
cznic committed
474

475 476 477
	go func() {
		select {
		case <-ctx.Done():
cznic's avatar
cznic committed
478
			if pstmt != 0 {
cznic's avatar
cznic committed
479
				s.c.interrupt(s.c.db)
480 481 482 483 484
			}
		case <-donech:
		}
	}()

cznic's avatar
cznic committed
485 486 487 488 489
	defer func() {
		pstmt = 0
		close(donech)
	}()

cznic's avatar
cznic committed
490 491
	var allocs []uintptr
	for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0; {
cznic's avatar
cznic committed
492
		if pstmt, err = s.c.prepareV2(&psql); err != nil {
cznic's avatar
cznic committed
493 494 495
			return nil, err
		}

cznic's avatar
cznic committed
496
		if pstmt == 0 {
cznic's avatar
cznic committed
497 498 499
			continue
		}

cznic's avatar
cznic committed
500 501 502 503 504 505
		if err = func() (err error) {
			defer func() {
				if e := s.c.finalize(pstmt); e != nil && err == nil {
					err = e
				}
			}()
cznic's avatar
cznic committed
506

cznic's avatar
cznic committed
507 508 509
			n, err := s.c.bindParameterCount(pstmt)
			if err != nil {
				return err
cznic's avatar
cznic committed
510
			}
511

cznic's avatar
cznic committed
512 513 514 515 516
			if n != 0 {
				a, err := s.c.bind(pstmt, n, args)
				if err != nil {
					return err
				}
cznic's avatar
cznic committed
517

cznic's avatar
cznic committed
518 519
				if len(a) != 0 {
					allocs = append(allocs, a...)
cznic's avatar
cznic committed
520
				}
cznic's avatar
cznic committed
521
			}
cznic's avatar
cznic committed
522

cznic's avatar
cznic committed
523 524 525
			rc, err := s.c.step(pstmt)
			if err != nil {
				return err
cznic's avatar
cznic committed
526 527
			}

cznic's avatar
cznic committed
528
			switch rc & 0xff {
cznic's avatar
cznic committed
529
			case sqlite3.SQLITE_ROW:
cznic's avatar
cznic committed
530
				if r, err = newRows(s.c, pstmt, allocs, false); err != nil {
cznic's avatar
cznic committed
531 532 533 534 535
					return err
				}

				pstmt = 0
				return nil
cznic's avatar
cznic committed
536
			case sqlite3.SQLITE_DONE:
cznic's avatar
cznic committed
537 538 539
				// nop
			default:
				return s.c.errstr(int32(rc))
cznic's avatar
cznic committed
540
			}
cznic's avatar
cznic committed
541

cznic's avatar
cznic committed
542 543 544 545 546 547 548
			if *(*byte)(unsafe.Pointer(uintptr(psql))) == 0 {
				if r, err = newRows(s.c, pstmt, allocs, true); err != nil {
					return err
				}

				pstmt = 0
			}
cznic's avatar
cznic committed
549 550
			return nil
		}(); err != nil {
cznic's avatar
cznic committed
551 552 553
			return nil, err
		}
	}
cznic's avatar
cznic committed
554
	return r, err
cznic's avatar
cznic committed
555 556
}

cznic's avatar
cznic committed
557 558 559 560 561 562 563 564
type tx struct {
	c *conn
}

func newTx(c *conn) (*tx, error) {
	r := &tx{c: c}
	if err := r.exec(context.Background(), "begin"); err != nil {
		return nil, err
cznic's avatar
cznic committed
565 566
	}

cznic's avatar
cznic committed
567
	return r, nil
cznic's avatar
cznic committed
568 569
}

cznic's avatar
cznic committed
570 571 572 573
// Commit implements driver.Tx.
func (t *tx) Commit() (err error) {
	return t.exec(context.Background(), "commit")
}
cznic's avatar
cznic committed
574

cznic's avatar
cznic committed
575 576 577
// Rollback implements driver.Tx.
func (t *tx) Rollback() (err error) {
	return t.exec(context.Background(), "rollback")
cznic's avatar
cznic committed
578 579
}

cznic's avatar
cznic committed
580 581
func (t *tx) exec(ctx context.Context, sql string) (err error) {
	psql, err := crt.CString(sql)
cznic's avatar
cznic committed
582 583 584 585
	if err != nil {
		return err
	}

cznic's avatar
cznic committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
	defer t.c.free(psql)

	//TODO use t.conn.ExecContext() instead
	donech := make(chan struct{})

	defer close(donech)

	go func() {
		select {
		case <-ctx.Done():
			t.c.interrupt(t.c.db)
		case <-donech:
		}
	}()

cznic's avatar
cznic committed
601
	if rc := sqlite3.Xsqlite3_exec(t.c.tls, t.c.db, psql, 0, 0, 0); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
602
		return t.c.errstr(rc)
cznic's avatar
cznic committed
603 604 605 606 607
	}

	return nil
}

cznic's avatar
cznic committed
608
type conn struct {
cznic's avatar
cznic committed
609
	db  uintptr // *sqlite3.Xsqlite3
cznic's avatar
cznic committed
610 611 612 613 614 615 616
	tls *crt.TLS
}

func newConn(name string) (*conn, error) {
	c := &conn{tls: crt.NewTLS()}
	db, err := c.openV2(
		name,
cznic's avatar
cznic committed
617 618 619
		sqlite3.SQLITE_OPEN_READWRITE|sqlite3.SQLITE_OPEN_CREATE|
			sqlite3.SQLITE_OPEN_FULLMUTEX|
			sqlite3.SQLITE_OPEN_URI,
cznic's avatar
cznic committed
620
	)
cznic's avatar
cznic committed
621
	if err != nil {
cznic's avatar
cznic committed
622
		return nil, err
cznic's avatar
cznic committed
623 624
	}

cznic's avatar
cznic committed
625 626 627
	c.db = db
	if err = c.extendedResultCodes(true); err != nil {
		return nil, err
cznic's avatar
cznic committed
628 629
	}

630 631 632
	// Default is set at compile time to 1024 because testfixture hard codes that value, fix it at runtime.
	_, err = c.Exec("PRAGMA page_size = 4096;", nil)
	return c, err
cznic's avatar
cznic committed
633 634
}

cznic's avatar
cznic committed
635
// const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
cznic's avatar
cznic committed
636 637
func (c *conn) columnBlob(pstmt uintptr, iCol int) (v []byte, err error) {
	p := sqlite3.Xsqlite3_column_blob(c.tls, pstmt, int32(iCol))
cznic's avatar
cznic committed
638 639 640 641
	len, err := c.columnBytes(pstmt, iCol)
	if err != nil {
		return nil, err
	}
642

cznic's avatar
cznic committed
643 644 645 646 647 648 649 650 651 652
	if p == 0 || len == 0 {
		return nil, nil
	}

	v = make([]byte, len)
	copy(v, (*crt.RawMem)(unsafe.Pointer(uintptr(p)))[:len])
	return v, nil
}

// int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
cznic's avatar
cznic committed
653 654
func (c *conn) columnBytes(pstmt uintptr, iCol int) (_ int, err error) {
	v := sqlite3.Xsqlite3_column_bytes(c.tls, pstmt, int32(iCol))
cznic's avatar
cznic committed
655 656 657 658
	return int(v), nil
}

// const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
cznic's avatar
cznic committed
659 660
func (c *conn) columnText(pstmt uintptr, iCol int) (v string, err error) {
	p := sqlite3.Xsqlite3_column_text(c.tls, pstmt, int32(iCol))
cznic's avatar
cznic committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
	len, err := c.columnBytes(pstmt, iCol)
	if err != nil {
		return "", err
	}

	if p == 0 || len == 0 {
		return "", nil
	}

	b := make([]byte, len)
	copy(b, (*crt.RawMem)(unsafe.Pointer(uintptr(p)))[:len])
	return string(b), nil
}

// double sqlite3_column_double(sqlite3_stmt*, int iCol);
cznic's avatar
cznic committed
676 677
func (c *conn) columnDouble(pstmt uintptr, iCol int) (v float64, err error) {
	v = sqlite3.Xsqlite3_column_double(c.tls, pstmt, int32(iCol))
cznic's avatar
cznic committed
678 679 680 681
	return v, nil
}

// sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
cznic's avatar
cznic committed
682 683
func (c *conn) columnInt64(pstmt uintptr, iCol int) (v int64, err error) {
	v = sqlite3.Xsqlite3_column_int64(c.tls, pstmt, int32(iCol))
cznic's avatar
cznic committed
684 685 686 687
	return v, nil
}

// int sqlite3_column_type(sqlite3_stmt*, int iCol);
cznic's avatar
cznic committed
688 689
func (c *conn) columnType(pstmt uintptr, iCol int) (_ int, err error) {
	v := sqlite3.Xsqlite3_column_type(c.tls, pstmt, int32(iCol))
cznic's avatar
cznic committed
690 691 692 693
	return int(v), nil
}

// const char *sqlite3_column_name(sqlite3_stmt*, int N);
cznic's avatar
cznic committed
694 695
func (c *conn) columnName(pstmt uintptr, n int) (string, error) {
	p := sqlite3.Xsqlite3_column_name(c.tls, pstmt, int32(n))
cznic's avatar
cznic committed
696 697 698 699
	return crt.GoString(p), nil
}

// int sqlite3_column_count(sqlite3_stmt *pStmt);
cznic's avatar
cznic committed
700 701
func (c *conn) columnCount(pstmt uintptr) (_ int, err error) {
	v := sqlite3.Xsqlite3_column_count(c.tls, pstmt)
cznic's avatar
cznic committed
702 703 704 705 706
	return int(v), nil
}

// sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
func (c *conn) lastInsertRowID() (v int64, _ error) {
cznic's avatar
cznic committed
707
	return sqlite3.Xsqlite3_last_insert_rowid(c.tls, c.db), nil
cznic's avatar
cznic committed
708 709 710 711
}

// int sqlite3_changes(sqlite3*);
func (c *conn) changes() (int, error) {
cznic's avatar
cznic committed
712
	v := sqlite3.Xsqlite3_changes(c.tls, c.db)
cznic's avatar
cznic committed
713 714 715 716
	return int(v), nil
}

// int sqlite3_step(sqlite3_stmt*);
cznic's avatar
cznic committed
717
func (c *conn) step(pstmt uintptr) (int, error) {
cznic's avatar
cznic committed
718
	for {
cznic's avatar
cznic committed
719 720
		switch rc := sqlite3.Xsqlite3_step(c.tls, pstmt); rc {
		case sqliteLockedSharedcache, sqlite3.SQLITE_BUSY:
cznic's avatar
cznic committed
721
			if err := c.retry(pstmt); err != nil {
cznic's avatar
cznic committed
722
				return sqlite3.SQLITE_LOCKED, err
cznic's avatar
cznic committed
723 724 725 726 727 728 729
			}
		default:
			return int(rc), nil
		}
	}
}

cznic's avatar
cznic committed
730 731
func (c *conn) retry(pstmt uintptr) error {
	mu := mutexAlloc(c.tls, sqlite3.SQLITE_MUTEX_FAST)
cznic's avatar
cznic committed
732
	(*mutex)(unsafe.Pointer(uintptr(mu))).enter(c.tls.ID) // Block
cznic's avatar
cznic committed
733
	rc := sqlite3.Xsqlite3_unlock_notify(
cznic's avatar
cznic committed
734 735
		c.tls,
		c.db,
cznic's avatar
cznic committed
736
		*(*uintptr)(unsafe.Pointer(&struct {
cznic's avatar
cznic committed
737 738 739 740
			f func(*crt.TLS, crt.Intptr, int32)
		}{unlockNotify})),
		mu,
	)
cznic's avatar
cznic committed
741 742
	if rc == sqlite3.SQLITE_LOCKED { // Deadlock, see https://www.sqlite.org/c3ref/unlock_notify.html
		(*mutex)(unsafe.Pointer(uintptr(mu))).leave(c.tls.ID) // Clear
cznic's avatar
cznic committed
743 744 745 746 747
		mutexFree(c.tls, mu)
		return c.errstr(rc)
	}

	(*mutex)(unsafe.Pointer(uintptr(mu))).enter(c.tls.ID) // Wait
cznic's avatar
cznic committed
748
	(*mutex)(unsafe.Pointer(uintptr(mu))).leave(c.tls.ID) // Clear
cznic's avatar
cznic committed
749 750
	mutexFree(c.tls, mu)
	if pstmt != 0 {
cznic's avatar
cznic committed
751
		sqlite3.Xsqlite3_reset(c.tls, pstmt)
cznic's avatar
cznic committed
752 753 754 755 756 757 758
	}
	return nil
}

func unlockNotify(t *crt.TLS, ppArg crt.Intptr, nArg int32) {
	for i := int32(0); i < nArg; i++ {
		mu := *(*crt.Intptr)(unsafe.Pointer(uintptr(ppArg)))
cznic's avatar
cznic committed
759
		(*mutex)(unsafe.Pointer(uintptr(mu))).leave(t.ID) // Signal
cznic's avatar
cznic committed
760 761 762 763
		ppArg += crt.Intptr(ptrSize)
	}
}

cznic's avatar
cznic committed
764
func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []uintptr, err error) {
cznic's avatar
cznic committed
765 766 767 768 769 770 771 772 773 774 775 776
	defer func() {
		if err == nil {
			return
		}

		for _, v := range allocs {
			c.free(v)
		}
		allocs = nil
	}()

	for i := 1; i <= n; i++ {
cznic's avatar
cznic committed
777
		var p uintptr
cznic's avatar
cznic committed
778 779 780 781 782 783 784 785 786 787 788
		name, err := c.bindParameterName(pstmt, i)
		if err != nil {
			return allocs, err
		}

		var v driver.NamedValue
		for _, v = range args {
			if name != "" {
				// sqlite supports '$', '@' and ':' prefixes for string
				// identifiers and '?' for numeric, so we cannot
				// combine different prefixes with the same name
789 790 791 792 793 794 795 796 797 798 799 800 801 802
				// because `database/sql` requires variable names
				// to start with a letter
				if name[1:] == v.Name[:] {
					break
				}
			} else {
				if v.Ordinal == i {
					break
				}
			}
		}

		if v.Ordinal == 0 {
			if name != "" {
cznic's avatar
cznic committed
803
				return allocs, fmt.Errorf("missing named argument %q", name[1:])
804
			}
805

cznic's avatar
cznic committed
806
			return allocs, fmt.Errorf("missing argument with %d index", i)
807
		}
cznic's avatar
cznic committed
808

809
		switch x := v.Value.(type) {
cznic's avatar
cznic committed
810
		case int64:
cznic's avatar
cznic committed
811 812
			if err := c.bindInt64(pstmt, i, x); err != nil {
				return allocs, err
cznic's avatar
cznic committed
813 814
			}
		case float64:
cznic's avatar
cznic committed
815 816
			if err := c.bindDouble(pstmt, i, x); err != nil {
				return allocs, err
cznic's avatar
cznic committed
817 818 819 820 821 822
			}
		case bool:
			v := 0
			if x {
				v = 1
			}
cznic's avatar
cznic committed
823 824
			if err := c.bindInt(pstmt, i, v); err != nil {
				return allocs, err
cznic's avatar
cznic committed
825 826
			}
		case []byte:
cznic's avatar
cznic committed
827 828
			if p, err = c.bindBlob(pstmt, i, x); err != nil {
				return allocs, err
cznic's avatar
cznic committed
829 830
			}
		case string:
cznic's avatar
cznic committed
831 832
			if p, err = c.bindText(pstmt, i, x); err != nil {
				return allocs, err
cznic's avatar
cznic committed
833 834
			}
		case time.Time:
cznic's avatar
cznic committed
835 836
			if p, err = c.bindText(pstmt, i, x.String()); err != nil {
				return allocs, err
cznic's avatar
cznic committed
837 838
			}
		default:
cznic's avatar
cznic committed
839 840 841 842
			return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x)
		}
		if p != 0 {
			allocs = append(allocs, p)
cznic's avatar
cznic committed
843 844
		}
	}
cznic's avatar
cznic committed
845 846 847 848
	return allocs, nil
}

// int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
cznic's avatar
cznic committed
849
func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) {
cznic's avatar
cznic committed
850 851 852 853 854
	p, err := crt.CString(value)
	if err != nil {
		return 0, err
	}

cznic's avatar
cznic committed
855
	if rc := sqlite3.Xsqlite3_bind_text(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
856 857 858 859 860 861 862 863
		c.free(p)
		return 0, c.errstr(rc)
	}

	return p, nil
}

// int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
cznic's avatar
cznic committed
864
func (c *conn) bindBlob(pstmt uintptr, idx1 int, value []byte) (uintptr, error) {
cznic's avatar
cznic committed
865 866 867 868 869 870
	p, err := c.malloc(len(value))
	if err != nil {
		return 0, err
	}

	copy((*crt.RawMem)(unsafe.Pointer(uintptr(p)))[:len(value)], value)
cznic's avatar
cznic committed
871
	if rc := sqlite3.Xsqlite3_bind_blob(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
872 873 874 875 876 877 878 879
		c.free(p)
		return 0, c.errstr(rc)
	}

	return p, nil
}

// int sqlite3_bind_int(sqlite3_stmt*, int, int);
cznic's avatar
cznic committed
880 881
func (c *conn) bindInt(pstmt uintptr, idx1, value int) (err error) {
	if rc := sqlite3.Xsqlite3_bind_int(c.tls, pstmt, int32(idx1), int32(value)); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
882 883 884
		return c.errstr(rc)
	}

cznic's avatar
cznic committed
885 886 887
	return nil
}

cznic's avatar
cznic committed
888
// int sqlite3_bind_double(sqlite3_stmt*, int, double);
cznic's avatar
cznic committed
889 890
func (c *conn) bindDouble(pstmt uintptr, idx1 int, value float64) (err error) {
	if rc := sqlite3.Xsqlite3_bind_double(c.tls, pstmt, int32(idx1), value); rc != 0 {
cznic's avatar
cznic committed
891 892 893 894 895 896 897
		return c.errstr(rc)
	}

	return nil
}

// int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
cznic's avatar
cznic committed
898 899
func (c *conn) bindInt64(pstmt uintptr, idx1 int, value int64) (err error) {
	if rc := sqlite3.Xsqlite3_bind_int64(c.tls, pstmt, int32(idx1), value); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
900 901 902 903
		return c.errstr(rc)
	}

	return nil
cznic's avatar
cznic committed
904 905
}

906
// const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
cznic's avatar
cznic committed
907 908
func (c *conn) bindParameterName(pstmt uintptr, i int) (string, error) {
	p := sqlite3.Xsqlite3_bind_parameter_name(c.tls, pstmt, int32(i))
909
	return crt.GoString(p), nil
910 911
}

cznic's avatar
cznic committed
912
// int sqlite3_bind_parameter_count(sqlite3_stmt*);
cznic's avatar
cznic committed
913 914
func (c *conn) bindParameterCount(pstmt uintptr) (_ int, err error) {
	r := sqlite3.Xsqlite3_bind_parameter_count(c.tls, pstmt)
cznic's avatar
cznic committed
915 916 917
	return int(r), nil
}

cznic's avatar
cznic committed
918
// int sqlite3_finalize(sqlite3_stmt *pStmt);
cznic's avatar
cznic committed
919 920
func (c *conn) finalize(pstmt uintptr) error {
	if rc := sqlite3.Xsqlite3_finalize(c.tls, pstmt); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
921
		return c.errstr(rc)
cznic's avatar
cznic committed
922 923 924 925 926 927 928 929 930 931 932 933
	}

	return nil
}

// int sqlite3_prepare_v2(
//   sqlite3 *db,            /* Database handle */
//   const char *zSql,       /* SQL statement, UTF-8 encoded */
//   int nByte,              /* Maximum length of zSql in bytes. */
//   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
//   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
// );
cznic's avatar
cznic committed
934 935
func (c *conn) prepareV2(zSQL *uintptr) (pstmt uintptr, err error) {
	var ppstmt, pptail uintptr
cznic's avatar
cznic committed
936

cznic's avatar
cznic committed
937 938 939 940
	defer func() {
		c.free(ppstmt)
		c.free(pptail)
	}()
cznic's avatar
cznic committed
941

cznic's avatar
cznic committed
942
	if ppstmt, err = c.malloc(int(ptrSize)); err != nil {
cznic's avatar
cznic committed
943 944
		return 0, err
	}
cznic's avatar
cznic committed
945

cznic's avatar
cznic committed
946
	if pptail, err = c.malloc(int(ptrSize)); err != nil {
cznic's avatar
cznic committed
947
		return 0, err
cznic's avatar
cznic committed
948 949
	}

cznic's avatar
cznic committed
950
	for {
cznic's avatar
cznic committed
951 952 953 954 955
		switch rc := sqlite3.Xsqlite3_prepare_v2(c.tls, c.db, *zSQL, -1, ppstmt, pptail); rc {
		case sqlite3.SQLITE_OK:
			*zSQL = *(*uintptr)(unsafe.Pointer(pptail))
			return *(*uintptr)(unsafe.Pointer(ppstmt)), nil
		case sqliteLockedSharedcache, sqlite3.SQLITE_BUSY:
cznic's avatar
cznic committed
956 957 958 959 960 961 962
			if err := c.retry(0); err != nil {
				return 0, err
			}
		default:
			return 0, c.errstr(rc)
		}
	}
cznic's avatar
cznic committed
963 964
}

cznic's avatar
cznic committed
965
// void sqlite3_interrupt(sqlite3*);
cznic's avatar
cznic committed
966 967
func (c *conn) interrupt(pdb uintptr) (err error) {
	sqlite3.Xsqlite3_interrupt(c.tls, pdb)
cznic's avatar
cznic committed
968
	return nil
cznic's avatar
cznic committed
969 970
}

cznic's avatar
cznic committed
971 972
// int sqlite3_extended_result_codes(sqlite3*, int onoff);
func (c *conn) extendedResultCodes(on bool) error {
cznic's avatar
cznic committed
973
	if rc := sqlite3.Xsqlite3_extended_result_codes(c.tls, c.db, crt.Bool32(on)); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
974 975 976 977
		return c.errstr(rc)
	}

	return nil
cznic's avatar
cznic committed
978 979
}

cznic's avatar
cznic committed
980 981 982 983 984
// int sqlite3_open_v2(
//   const char *filename,   /* Database filename (UTF-8) */
//   sqlite3 **ppDb,         /* OUT: SQLite db handle */
//   int flags,              /* Flags */
//   const char *zVfs        /* Name of VFS module to use */
cznic's avatar
cznic committed
985
// );
cznic's avatar
cznic committed
986 987
func (c *conn) openV2(name string, flags int32) (uintptr, error) {
	var p, s uintptr
cznic's avatar
cznic committed
988

cznic's avatar
cznic committed
989 990 991 992 993 994
	defer func() {
		if p != 0 {
			c.free(p)
		}
		if s != 0 {
			c.free(s)
995 996 997
		}
	}()

cznic's avatar
cznic committed
998
	p, err := c.malloc(int(ptrSize))
cznic's avatar
cznic committed
999 1000 1001 1002 1003 1004
	if err != nil {
		return 0, err
	}

	if s, err = crt.CString(name); err != nil {
		return 0, err
cznic's avatar
cznic committed
1005 1006
	}

cznic's avatar
cznic committed
1007
	if rc := sqlite3.Xsqlite3_open_v2(c.tls, s, p, flags, 0); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
1008 1009
		return 0, c.errstr(rc)
	}
cznic's avatar
cznic committed
1010

cznic's avatar
cznic committed
1011
	return *(*uintptr)(unsafe.Pointer(uintptr(p))), nil
cznic's avatar
cznic committed
1012 1013
}

cznic's avatar
cznic committed
1014 1015
func (c *conn) malloc(n int) (uintptr, error) {
	if p := crt.Xmalloc(c.tls, crt.Size_t(n)); p != 0 {
cznic's avatar
cznic committed
1016 1017
		return p, nil
	}
cznic's avatar
cznic committed
1018

cznic's avatar
cznic committed
1019 1020
	return 0, fmt.Errorf("sqlite: cannot allocate %d bytes of memory", n)
}
cznic's avatar
cznic committed
1021

cznic's avatar
cznic committed
1022
func (c *conn) free(p uintptr) {
cznic's avatar
cznic committed
1023 1024
	if p != 0 {
		crt.Xfree(c.tls, p)
cznic's avatar
cznic committed
1025
	}
cznic's avatar
cznic committed
1026
}
cznic's avatar
cznic committed
1027

cznic's avatar
cznic committed
1028 1029
// const char *sqlite3_errstr(int);
func (c *conn) errstr(rc int32) error {
cznic's avatar
cznic committed
1030
	p := sqlite3.Xsqlite3_errstr(c.tls, rc)
cznic's avatar
cznic committed
1031
	str := crt.GoString(p)
cznic's avatar
cznic committed
1032
	p = sqlite3.Xsqlite3_errmsg(c.tls, c.db)
cznic's avatar
cznic committed
1033 1034 1035 1036 1037
	switch msg := crt.GoString(p); {
	case msg == str:
		return &Error{msg: fmt.Sprintf("%s (%v)", str, rc), code: int(rc)}
	default:
		return &Error{msg: fmt.Sprintf("%s: %s (%v)", str, msg, rc), code: int(rc)}
cznic's avatar
cznic committed
1038 1039 1040
	}
}

cznic's avatar
cznic committed
1041 1042 1043 1044 1045
// Begin starts a transaction.
//
// Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
func (c *conn) Begin() (driver.Tx, error) {
	return c.begin(context.Background(), driver.TxOptions{})
1046 1047
}

cznic's avatar
cznic committed
1048 1049
func (c *conn) begin(ctx context.Context, opts driver.TxOptions) (t driver.Tx, err error) {
	return newTx(c)
cznic's avatar
cznic committed
1050 1051 1052 1053 1054 1055 1056 1057
}

// Close invalidates and potentially stops any current prepared statements and
// transactions, marking this connection as no longer in use.
//
// Because the sql package maintains a free pool of connections and only calls
// Close when there's a surplus of idle connections, it shouldn't be necessary
// for drivers to do their own connection caching.
cznic's avatar
cznic committed
1058 1059 1060 1061 1062
func (c *conn) Close() error {
	if c.db != 0 {
		if err := c.closeV2(c.db); err != nil {
			return err
		}
cznic's avatar
cznic committed
1063

cznic's avatar
cznic committed
1064 1065 1066
		c.db = 0
	}
	return nil
1067 1068
}

cznic's avatar
cznic committed
1069
// int sqlite3_close_v2(sqlite3*);
cznic's avatar
cznic committed
1070 1071
func (c *conn) closeV2(db uintptr) error {
	if rc := sqlite3.Xsqlite3_close_v2(c.tls, db); rc != sqlite3.SQLITE_OK {
cznic's avatar
cznic committed
1072 1073
		return c.errstr(rc)
	}
1074

cznic's avatar
cznic committed
1075
	return nil
cznic's avatar
cznic committed
1076 1077 1078 1079 1080 1081 1082 1083
}

// Execer is an optional interface that may be implemented by a Conn.
//
// If a Conn does not implement Execer, the sql package's DB.Exec will first
// prepare a query, execute the statement, and then close the statement.
//
// Exec may return ErrSkip.
cznic's avatar
cznic committed
1084 1085
//
// Deprecated: Drivers should implement ExecerContext instead.
1086 1087 1088 1089
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
	return c.exec(context.Background(), query, toNamedValues(args))
}

cznic's avatar
cznic committed
1090
func (c *conn) exec(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
1091
	s, err := c.prepare(ctx, query)
cznic's avatar
cznic committed
1092