fakedb_test.go 27.9 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sql

import (
8
	"context"
9
	"database/sql/driver"
10
	"errors"
11
	"fmt"
12
	"io"
13
	"log"
14
	"reflect"
15
	"sort"
16 17 18
	"strconv"
	"strings"
	"sync"
19
	"testing"
20
	"time"
21 22 23 24 25 26 27 28
)

var _ = log.Printf

// fakeDriver is a fake database that implements Go's driver.Driver
// interface, just for testing.
//
// It speaks a query language that's semantically similar to but
29
// syntactically different and simpler than SQL.  The syntax is as
30 31 32 33 34 35 36
// follows:
//
//   WIPE
//   CREATE|<tablename>|<col>=<type>,<col>=<type>,...
//     where types are: "string", [u]int{8,16,32,64}, "bool"
//   INSERT|<tablename>|col=val,col2=val2,col3=?
//   SELECT|<tablename>|projectcol1,projectcol2|filtercol=?,filtercol2=?
37
//   SELECT|<tablename>|projectcol1,projectcol2|filtercol=?param1,filtercol2=?param2
38
//
39 40 41
// Any of these can be preceded by PANIC|<method>|, to cause the
// named method on fakeStmt to panic.
//
42 43 44
// Any of these can be proceeded by WAIT|<duration>|, to cause the
// named method on fakeStmt to sleep for the specified duration.
//
45 46
// Multiple of these can be combined when separated with a semicolon.
//
47
// When opening a fakeDriver's database, it starts empty with no
48
// tables. All tables and data are stored in memory only.
49
type fakeDriver struct {
50 51 52
	mu         sync.Mutex // guards 3 following fields
	openCount  int        // conn opens
	closeCount int        // conn closes
53 54
	waitCh     chan struct{}
	waitingCh  chan struct{}
55
	dbs        map[string]*fakeDB
56 57
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
type fakeConnector struct {
	name string

	waiter func(context.Context)
}

func (c *fakeConnector) Connect(context.Context) (driver.Conn, error) {
	conn, err := fdriver.Open(c.name)
	conn.(*fakeConn).waiter = c.waiter
	return conn, err
}

func (c *fakeConnector) Driver() driver.Driver {
	return fdriver
}

74 75 76 77 78 79 80 81 82 83
type fakeDriverCtx struct {
	fakeDriver
}

var _ driver.DriverContext = &fakeDriverCtx{}

func (cc *fakeDriverCtx) OpenConnector(name string) (driver.Connector, error) {
	return &fakeConnector{name: name}, nil
}

84 85 86
type fakeDB struct {
	name string

87 88 89 90
	mu       sync.Mutex
	tables   map[string]*table
	badConn  bool
	allowAny bool
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

type table struct {
	mu      sync.Mutex
	colname []string
	coltype []string
	rows    []*row
}

func (t *table) columnIndex(name string) int {
	for n, nname := range t.colname {
		if name == nname {
			return n
		}
	}
	return -1
}

type row struct {
	cols []interface{} // must be same size as its table colname + coltype
}

113 114 115 116 117
type memToucher interface {
	// touchMem reads & writes some memory, to help find data races.
	touchMem()
}

118 119 120 121
type fakeConn struct {
	db *fakeDB // where to return ourselves to

	currTx *fakeTx
122

123 124 125 126
	// Every operation writes to line to enable the race detector
	// check for data races.
	line int64

127 128 129 130
	// Stats for tests:
	mu          sync.Mutex
	stmtsMade   int
	stmtsClosed int
131
	numPrepare  int
132 133 134 135

	// bad connection tests; see isBad()
	bad       bool
	stickyBad bool
136 137 138 139 140 141 142 143 144 145

	skipDirtySession bool // tests that use Conn should set this to true.

	// dirtySession tests ResetSession, true if a query has executed
	// until ResetSession is called.
	dirtySession bool

	// The waiter is called before each query. May be used in place of the "WAIT"
	// directive.
	waiter func(context.Context)
146 147
}

148 149 150 151
func (c *fakeConn) touchMem() {
	c.line++
}

152 153 154 155
func (c *fakeConn) incrStat(v *int) {
	c.mu.Lock()
	*v++
	c.mu.Unlock()
156 157 158 159 160 161
}

type fakeTx struct {
	c *fakeConn
}

162 163 164 165 166 167
type boundCol struct {
	Column      string
	Placeholder string
	Ordinal     int
}

168
type fakeStmt struct {
169
	memToucher
170 171 172 173 174
	c *fakeConn
	q string // just for debugging

	cmd   string
	table string
175
	panic string
176
	wait  time.Duration
177

178 179
	next *fakeStmt // used for returning multiple results.

180 181
	closed bool

182 183 184 185 186
	colName      []string      // used by CREATE, INSERT, SELECT (selected columns)
	colType      []string      // used by CREATE
	colValue     []interface{} // used by INSERT (mix of strings and "?" for bound params)
	placeholders int           // used by INSERT/SELECT: number of ? params

187
	whereCol []boundCol // used by SELECT (all placeholders)
188 189 190 191 192 193 194 195 196 197

	placeholderConverter []driver.ValueConverter // used by INSERT
}

var fdriver driver.Driver = &fakeDriver{}

func init() {
	Register("test", fdriver)
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211
func contains(list []string, y string) bool {
	for _, x := range list {
		if x == y {
			return true
		}
	}
	return false
}

type Dummy struct {
	driver.Driver
}

func TestDrivers(t *testing.T) {
212 213
	unregisterAllDrivers()
	Register("test", fdriver)
214 215 216 217 218 219 220
	Register("invalid", Dummy{})
	all := Drivers()
	if len(all) < 2 || !sort.StringsAreSorted(all) || !contains(all, "test") || !contains(all, "invalid") {
		t.Fatalf("Drivers = %v, want sorted list with at least [invalid, test]", all)
	}
}

221 222 223 224 225 226 227 228 229 230 231 232
// hook to simulate connection failures
var hookOpenErr struct {
	sync.Mutex
	fn func() error
}

func setHookOpenErr(fn func() error) {
	hookOpenErr.Lock()
	defer hookOpenErr.Unlock()
	hookOpenErr.fn = fn
}

233 234
// Supports dsn forms:
//    <dbname>
235 236 237
//    <dbname>;<opts>  (only currently supported option is `badConn`,
//                      which causes driver.ErrBadConn to be returned on
//                      every other conn.Begin())
238
func (d *fakeDriver) Open(dsn string) (driver.Conn, error) {
239 240 241 242 243 244 245 246
	hookOpenErr.Lock()
	fn := hookOpenErr.fn
	hookOpenErr.Unlock()
	if fn != nil {
		if err := fn(); err != nil {
			return nil, err
		}
	}
247 248
	parts := strings.Split(dsn, ";")
	if len(parts) < 1 {
249
		return nil, errors.New("fakedb: no database name")
250 251
	}
	name := parts[0]
252 253 254 255 256 257

	db := d.getDB(name)

	d.mu.Lock()
	d.openCount++
	d.mu.Unlock()
258 259 260 261 262
	conn := &fakeConn{db: db}

	if len(parts) >= 2 && parts[1] == "badConn" {
		conn.bad = true
	}
263 264 265
	if d.waitCh != nil {
		d.waitingCh <- struct{}{}
		<-d.waitCh
266 267
		d.waitCh = nil
		d.waitingCh = nil
268
	}
269
	return conn, nil
270 271 272 273 274 275 276 277
}

func (d *fakeDriver) getDB(name string) *fakeDB {
	d.mu.Lock()
	defer d.mu.Unlock()
	if d.dbs == nil {
		d.dbs = make(map[string]*fakeDB)
	}
278 279 280 281 282
	db, ok := d.dbs[name]
	if !ok {
		db = &fakeDB{name: name}
		d.dbs[name] = db
	}
283
	return db
284 285 286 287 288 289 290 291
}

func (db *fakeDB) wipe() {
	db.mu.Lock()
	defer db.mu.Unlock()
	db.tables = nil
}

292
func (db *fakeDB) createTable(name string, columnNames, columnTypes []string) error {
293 294 295 296 297 298
	db.mu.Lock()
	defer db.mu.Unlock()
	if db.tables == nil {
		db.tables = make(map[string]*table)
	}
	if _, exist := db.tables[name]; exist {
299
		return fmt.Errorf("fakedb: table %q already exists", name)
300 301
	}
	if len(columnNames) != len(columnTypes) {
302
		return fmt.Errorf("fakedb: create table of %q len(names) != len(types): %d vs %d",
303
			name, len(columnNames), len(columnTypes))
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	}
	db.tables[name] = &table{colname: columnNames, coltype: columnTypes}
	return nil
}

// must be called with db.mu lock held
func (db *fakeDB) table(table string) (*table, bool) {
	if db.tables == nil {
		return nil, false
	}
	t, ok := db.tables[table]
	return t, ok
}

func (db *fakeDB) columnType(table, column string) (typ string, ok bool) {
	db.mu.Lock()
	defer db.mu.Unlock()
	t, ok := db.table(table)
	if !ok {
		return
	}
	for n, cname := range t.colname {
		if cname == column {
			return t.coltype[n], true
		}
	}
	return "", false
}

333
func (c *fakeConn) isBad() bool {
334 335 336
	if c.stickyBad {
		return true
	} else if c.bad {
337 338 339
		if c.db == nil {
			return false
		}
340 341 342 343
		// alternate between bad conn and not bad conn
		c.db.badConn = !c.db.badConn
		return c.db.badConn
	} else {
344 345 346 347
		return false
	}
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
func (c *fakeConn) isDirtyAndMark() bool {
	if c.skipDirtySession {
		return false
	}
	if c.currTx != nil {
		c.dirtySession = true
		return false
	}
	if c.dirtySession {
		return true
	}
	c.dirtySession = true
	return false
}

363
func (c *fakeConn) Begin() (driver.Tx, error) {
364 365 366
	if c.isBad() {
		return nil, driver.ErrBadConn
	}
367
	if c.currTx != nil {
368
		return nil, errors.New("fakedb: already in a transaction")
369
	}
370
	c.touchMem()
371 372 373 374
	c.currTx = &fakeTx{c: c}
	return c.currTx, nil
}

375 376 377 378 379 380 381 382 383 384 385
var hookPostCloseConn struct {
	sync.Mutex
	fn func(*fakeConn, error)
}

func setHookpostCloseConn(fn func(*fakeConn, error)) {
	hookPostCloseConn.Lock()
	defer hookPostCloseConn.Unlock()
	hookPostCloseConn.fn = fn
}

386 387 388 389 390 391 392 393
var testStrictClose *testing.T

// setStrictFakeConnClose sets the t to Errorf on when fakeConn.Close
// fails to close. If nil, the check is disabled.
func setStrictFakeConnClose(t *testing.T) {
	testStrictClose = t
}

394 395 396 397 398 399 400 401
func (c *fakeConn) ResetSession(ctx context.Context) error {
	c.dirtySession = false
	if c.isBad() {
		return driver.ErrBadConn
	}
	return nil
}

402
func (c *fakeConn) Close() (err error) {
403
	drv := fdriver.(*fakeDriver)
404
	defer func() {
405 406 407
		if err != nil && testStrictClose != nil {
			testStrictClose.Errorf("failed to close a test fakeConn: %v", err)
		}
408 409 410 411 412 413
		hookPostCloseConn.Lock()
		fn := hookPostCloseConn.fn
		hookPostCloseConn.Unlock()
		if fn != nil {
			fn(c, err)
		}
414 415 416 417 418
		if err == nil {
			drv.mu.Lock()
			drv.closeCount++
			drv.mu.Unlock()
		}
419
	}()
420
	c.touchMem()
421
	if c.currTx != nil {
422
		return errors.New("fakedb: can't close fakeConn; in a Transaction")
423 424
	}
	if c.db == nil {
425
		return errors.New("fakedb: can't close fakeConn; already closed")
426
	}
427
	if c.stmtsMade > c.stmtsClosed {
428
		return errors.New("fakedb: can't close; dangling statement(s)")
429
	}
430 431 432 433
	c.db = nil
	return nil
}

434
func checkSubsetTypes(allowAny bool, args []driver.NamedValue) error {
435 436
	for _, arg := range args {
		switch arg.Value.(type) {
437
		case int64, float64, bool, nil, []byte, string, time.Time:
438
		default:
439
			if !allowAny {
440
				return fmt.Errorf("fakedb: invalid argument ordinal %[1]d: %[2]v, type %[2]T", arg.Ordinal, arg.Value)
441
			}
442 443 444 445 446
		}
	}
	return nil
}

447
func (c *fakeConn) Exec(query string, args []driver.Value) (driver.Result, error) {
448 449 450 451 452
	// Ensure that ExecContext is called if available.
	panic("ExecContext was not called.")
}

func (c *fakeConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
453
	// This is an optional interface, but it's implemented here
454
	// just to check that all the args are of the proper types.
455 456
	// ErrSkip is returned so the caller acts as if we didn't
	// implement this at all.
457
	err := checkSubsetTypes(c.db.allowAny, args)
458 459 460 461 462 463
	if err != nil {
		return nil, err
	}
	return nil, driver.ErrSkip
}

464
func (c *fakeConn) Query(query string, args []driver.Value) (driver.Rows, error) {
465 466 467 468 469
	// Ensure that ExecContext is called if available.
	panic("QueryContext was not called.")
}

func (c *fakeConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
470 471 472 473
	// This is an optional interface, but it's implemented here
	// just to check that all the args are of the proper types.
	// ErrSkip is returned so the caller acts as if we didn't
	// implement this at all.
474
	err := checkSubsetTypes(c.db.allowAny, args)
475 476 477 478 479 480
	if err != nil {
		return nil, err
	}
	return nil, driver.ErrSkip
}

481 482
func errf(msg string, args ...interface{}) error {
	return errors.New("fakedb: " + fmt.Sprintf(msg, args...))
483 484 485
}

// parts are table|selectCol1,selectCol2|whereCol=?,whereCol2=?
486
// (note that where columns must always contain ? marks,
487
//  just a limitation for fakedb)
488
func (c *fakeConn) prepareSelect(stmt *fakeStmt, parts []string) (*fakeStmt, error) {
489
	if len(parts) != 3 {
490
		stmt.Close()
491 492 493
		return nil, errf("invalid SELECT syntax with %d parts; want 3", len(parts))
	}
	stmt.table = parts[0]
494

495 496
	stmt.colName = strings.Split(parts[1], ",")
	for n, colspec := range strings.Split(parts[2], ",") {
497 498 499
		if colspec == "" {
			continue
		}
500 501
		nameVal := strings.Split(colspec, "=")
		if len(nameVal) != 2 {
502
			stmt.Close()
503 504 505 506 507
			return nil, errf("SELECT on table %q has invalid column spec of %q (index %d)", stmt.table, colspec, n)
		}
		column, value := nameVal[0], nameVal[1]
		_, ok := c.db.columnType(stmt.table, column)
		if !ok {
508
			stmt.Close()
509 510
			return nil, errf("SELECT on table %q references non-existent column %q", stmt.table, column)
		}
511
		if !strings.HasPrefix(value, "?") {
512
			stmt.Close()
513 514 515 516
			return nil, errf("SELECT on table %q has pre-bound value for where column %q; need a question mark",
				stmt.table, column)
		}
		stmt.placeholders++
517
		stmt.whereCol = append(stmt.whereCol, boundCol{Column: column, Placeholder: value, Ordinal: stmt.placeholders})
518 519 520 521 522
	}
	return stmt, nil
}

// parts are table|col=type,col2=type2
523
func (c *fakeConn) prepareCreate(stmt *fakeStmt, parts []string) (*fakeStmt, error) {
524
	if len(parts) != 2 {
525
		stmt.Close()
526 527 528 529 530 531
		return nil, errf("invalid CREATE syntax with %d parts; want 2", len(parts))
	}
	stmt.table = parts[0]
	for n, colspec := range strings.Split(parts[1], ",") {
		nameType := strings.Split(colspec, "=")
		if len(nameType) != 2 {
532
			stmt.Close()
533 534 535 536 537 538 539 540 541
			return nil, errf("CREATE table %q has invalid column spec of %q (index %d)", stmt.table, colspec, n)
		}
		stmt.colName = append(stmt.colName, nameType[0])
		stmt.colType = append(stmt.colType, nameType[1])
	}
	return stmt, nil
}

// parts are table|col=?,col2=val
542
func (c *fakeConn) prepareInsert(stmt *fakeStmt, parts []string) (*fakeStmt, error) {
543
	if len(parts) != 2 {
544
		stmt.Close()
545 546 547 548 549 550
		return nil, errf("invalid INSERT syntax with %d parts; want 2", len(parts))
	}
	stmt.table = parts[0]
	for n, colspec := range strings.Split(parts[1], ",") {
		nameVal := strings.Split(colspec, "=")
		if len(nameVal) != 2 {
551
			stmt.Close()
552 553 554 555 556
			return nil, errf("INSERT table %q has invalid column spec of %q (index %d)", stmt.table, colspec, n)
		}
		column, value := nameVal[0], nameVal[1]
		ctype, ok := c.db.columnType(stmt.table, column)
		if !ok {
557
			stmt.Close()
558 559 560 561
			return nil, errf("INSERT table %q references non-existent column %q", stmt.table, column)
		}
		stmt.colName = append(stmt.colName, column)

562
		if !strings.HasPrefix(value, "?") {
563 564 565 566 567
			var subsetVal interface{}
			// Convert to driver subset type
			switch ctype {
			case "string":
				subsetVal = []byte(value)
568 569
			case "blob":
				subsetVal = []byte(value)
570 571 572
			case "int32":
				i, err := strconv.Atoi(value)
				if err != nil {
573
					stmt.Close()
574 575 576 577
					return nil, errf("invalid conversion to int32 from %q", value)
				}
				subsetVal = int64(i) // int64 is a subset type, but not int32
			default:
578
				stmt.Close()
579 580 581 582 583 584
				return nil, errf("unsupported conversion for pre-bound parameter %q to type %q", value, ctype)
			}
			stmt.colValue = append(stmt.colValue, subsetVal)
		} else {
			stmt.placeholders++
			stmt.placeholderConverter = append(stmt.placeholderConverter, converterForType(ctype))
585
			stmt.colValue = append(stmt.colValue, value)
586 587 588 589 590
		}
	}
	return stmt, nil
}

591 592 593
// hook to simulate broken connections
var hookPrepareBadConn func() bool

594
func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
595 596 597 598
	panic("use PrepareContext")
}

func (c *fakeConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
599
	c.numPrepare++
600 601 602
	if c.db == nil {
		panic("nil c.db; conn = " + fmt.Sprintf("%#v", c))
	}
603

604
	if c.stickyBad || (hookPrepareBadConn != nil && hookPrepareBadConn()) {
605 606 607
		return nil, driver.ErrBadConn
	}

608
	c.touchMem()
609 610 611 612 613 614
	var firstStmt, prev *fakeStmt
	for _, query := range strings.Split(query, ";") {
		parts := strings.Split(query, "|")
		if len(parts) < 1 {
			return nil, errf("empty query")
		}
615
		stmt := &fakeStmt{q: query, c: c, memToucher: c}
616 617 618
		if firstStmt == nil {
			firstStmt = stmt
		}
619 620 621 622 623 624 625 626 627 628 629 630 631
		if len(parts) >= 3 {
			switch parts[0] {
			case "PANIC":
				stmt.panic = parts[1]
				parts = parts[2:]
			case "WAIT":
				wait, err := time.ParseDuration(parts[1])
				if err != nil {
					return nil, errf("expected section after WAIT to be a duration, got %q %v", parts[1], err)
				}
				parts = parts[2:]
				stmt.wait = wait
			}
632 633 634 635 636
		}
		cmd := parts[0]
		stmt.cmd = cmd
		parts = parts[1:]

637 638 639 640
		if c.waiter != nil {
			c.waiter(ctx)
		}

641
		if stmt.wait > 0 {
642 643 644 645 646 647 648
			wait := time.NewTimer(stmt.wait)
			select {
			case <-wait.C:
			case <-ctx.Done():
				wait.Stop()
				return nil, ctx.Err()
			}
649 650
		}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
		c.incrStat(&c.stmtsMade)
		var err error
		switch cmd {
		case "WIPE":
			// Nothing
		case "SELECT":
			stmt, err = c.prepareSelect(stmt, parts)
		case "CREATE":
			stmt, err = c.prepareCreate(stmt, parts)
		case "INSERT":
			stmt, err = c.prepareInsert(stmt, parts)
		case "NOSERT":
			// Do all the prep-work like for an INSERT but don't actually insert the row.
			// Used for some of the concurrent tests.
			stmt, err = c.prepareInsert(stmt, parts)
		default:
			stmt.Close()
			return nil, errf("unsupported command type %q", cmd)
		}
		if err != nil {
			return nil, err
		}
		if prev != nil {
			prev.next = stmt
		}
		prev = stmt
677
	}
678
	return firstStmt, nil
679 680 681
}

func (s *fakeStmt) ColumnConverter(idx int) driver.ValueConverter {
682 683 684
	if s.panic == "ColumnConverter" {
		panic(s.panic)
	}
685 686 687
	if len(s.placeholderConverter) == 0 {
		return driver.DefaultParameterConverter
	}
688 689 690
	return s.placeholderConverter[idx]
}

691
func (s *fakeStmt) Close() error {
692 693 694
	if s.panic == "Close" {
		panic(s.panic)
	}
695 696 697 698
	if s.c == nil {
		panic("nil conn in fakeStmt.Close")
	}
	if s.c.db == nil {
699
		panic("in fakeStmt.Close, conn's db is nil (already closed)")
700
	}
701
	s.touchMem()
702 703 704 705
	if !s.closed {
		s.c.incrStat(&s.c.stmtsClosed)
		s.closed = true
	}
706 707 708
	if s.next != nil {
		s.next.Close()
	}
709 710 711
	return nil
}

712 713
var errClosed = errors.New("fakedb: statement has been closed")

714 715 716
// hook to simulate broken connections
var hookExecBadConn func() bool

717
func (s *fakeStmt) Exec(args []driver.Value) (driver.Result, error) {
718 719 720
	panic("Using ExecContext")
}
func (s *fakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
721 722 723
	if s.panic == "Exec" {
		panic(s.panic)
	}
724 725 726
	if s.closed {
		return nil, errClosed
	}
727

728
	if s.c.stickyBad || (hookExecBadConn != nil && hookExecBadConn()) {
729 730
		return nil, driver.ErrBadConn
	}
731
	if s.c.isDirtyAndMark() {
732
		return nil, errors.New("fakedb: session is dirty")
733
	}
734

735
	err := checkSubsetTypes(s.c.db.allowAny, args)
736 737 738
	if err != nil {
		return nil, err
	}
739
	s.touchMem()
740

741 742 743 744 745 746 747 748 749 750
	if s.wait > 0 {
		time.Sleep(s.wait)
	}

	select {
	default:
	case <-ctx.Done():
		return nil, ctx.Err()
	}

751 752 753 754
	db := s.c.db
	switch s.cmd {
	case "WIPE":
		db.wipe()
755
		return driver.ResultNoRows, nil
756 757 758 759
	case "CREATE":
		if err := db.createTable(s.table, s.colName, s.colType); err != nil {
			return nil, err
		}
760
		return driver.ResultNoRows, nil
761
	case "INSERT":
762 763 764 765 766
		return s.execInsert(args, true)
	case "NOSERT":
		// Do all the prep-work like for an INSERT but don't actually insert the row.
		// Used for some of the concurrent tests.
		return s.execInsert(args, false)
767
	}
768
	return nil, fmt.Errorf("fakedb: unimplemented statement Exec command type of %q", s.cmd)
769 770
}

771 772 773
// When doInsert is true, add the row to the table.
// When doInsert is false do prep-work and error checking, but don't
// actually add the row to the table.
774
func (s *fakeStmt) execInsert(args []driver.NamedValue, doInsert bool) (driver.Result, error) {
775 776 777 778 779 780 781 782 783 784 785 786 787 788
	db := s.c.db
	if len(args) != s.placeholders {
		panic("error in pkg db; should only get here if size is correct")
	}
	db.mu.Lock()
	t, ok := db.table(s.table)
	db.mu.Unlock()
	if !ok {
		return nil, fmt.Errorf("fakedb: table %q doesn't exist", s.table)
	}

	t.mu.Lock()
	defer t.mu.Unlock()

789 790 791 792
	var cols []interface{}
	if doInsert {
		cols = make([]interface{}, len(t.colname))
	}
793 794 795 796 797 798 799
	argPos := 0
	for n, colname := range s.colName {
		colidx := t.columnIndex(colname)
		if colidx == -1 {
			return nil, fmt.Errorf("fakedb: column %q doesn't exist or dropped since prepared statement was created", colname)
		}
		var val interface{}
800 801 802 803 804 805
		if strvalue, ok := s.colValue[n].(string); ok && strings.HasPrefix(strvalue, "?") {
			if strvalue == "?" {
				val = args[argPos].Value
			} else {
				// Assign value from argument placeholder name.
				for _, a := range args {
806
					if a.Name == strvalue[1:] {
807 808 809 810 811
						val = a.Value
						break
					}
				}
			}
812 813 814 815
			argPos++
		} else {
			val = s.colValue[n]
		}
816 817 818
		if doInsert {
			cols[colidx] = val
		}
819 820
	}

821 822 823
	if doInsert {
		t.rows = append(t.rows, &row{cols: cols})
	}
824 825 826
	return driver.RowsAffected(1), nil
}

827 828 829
// hook to simulate broken connections
var hookQueryBadConn func() bool

830
func (s *fakeStmt) Query(args []driver.Value) (driver.Rows, error) {
831 832 833 834
	panic("Use QueryContext")
}

func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
835 836 837
	if s.panic == "Query" {
		panic(s.panic)
	}
838 839 840
	if s.closed {
		return nil, errClosed
	}
841

842
	if s.c.stickyBad || (hookQueryBadConn != nil && hookQueryBadConn()) {
843 844
		return nil, driver.ErrBadConn
	}
845
	if s.c.isDirtyAndMark() {
846
		return nil, errors.New("fakedb: session is dirty")
847
	}
848

849
	err := checkSubsetTypes(s.c.db.allowAny, args)
850 851 852 853
	if err != nil {
		return nil, err
	}

854
	s.touchMem()
855 856 857 858 859
	db := s.c.db
	if len(args) != s.placeholders {
		panic("error in pkg db; should only get here if size is correct")
	}

860 861
	setMRows := make([][]*row, 0, 1)
	setColumns := make([][]string, 0, 1)
862
	setColType := make([][]string, 0, 1)
863

864 865 866 867 868 869
	for {
		db.mu.Lock()
		t, ok := db.table(s.table)
		db.mu.Unlock()
		if !ok {
			return nil, fmt.Errorf("fakedb: table %q doesn't exist", s.table)
870 871
		}

872
		if s.table == "magicquery" {
873 874 875
			if len(s.whereCol) == 2 && s.whereCol[0].Column == "op" && s.whereCol[1].Column == "millis" {
				if args[0].Value == "sleep" {
					time.Sleep(time.Duration(args[1].Value.(int64)) * time.Millisecond)
876 877
				}
			}
878 879
		}

880 881 882 883 884
		t.mu.Lock()

		colIdx := make(map[string]int) // select column name -> column index in table
		for _, name := range s.colName {
			idx := t.columnIndex(name)
885
			if idx == -1 {
886 887
				t.mu.Unlock()
				return nil, fmt.Errorf("fakedb: unknown column name %q", name)
888
			}
889 890 891 892 893 894 895 896 897
			colIdx[name] = idx
		}

		mrows := []*row{}
	rows:
		for _, trow := range t.rows {
			// Process the where clause, skipping non-match rows. This is lazy
			// and just uses fmt.Sprintf("%v") to test equality. Good enough
			// for test code.
898 899
			for _, wcol := range s.whereCol {
				idx := t.columnIndex(wcol.Column)
900 901
				if idx == -1 {
					t.mu.Unlock()
902
					return nil, fmt.Errorf("fakedb: invalid where clause column %q", wcol)
903 904 905 906 907 908
				}
				tcol := trow.cols[idx]
				if bs, ok := tcol.([]byte); ok {
					// lazy hack to avoid sprintf %v on a []byte
					tcol = string(bs)
				}
909 910 911 912 913 914
				var argValue interface{}
				if wcol.Placeholder == "?" {
					argValue = args[wcol.Ordinal-1].Value
				} else {
					// Assign arg value from placeholder name.
					for _, a := range args {
915
						if a.Name == wcol.Placeholder[1:] {
916 917 918 919 920 921
							argValue = a.Value
							break
						}
					}
				}
				if fmt.Sprintf("%v", tcol) != fmt.Sprintf("%v", argValue) {
922 923
					continue rows
				}
924
			}
925 926 927
			mrow := &row{cols: make([]interface{}, len(s.colName))}
			for seli, name := range s.colName {
				mrow.cols[seli] = trow.cols[colIdx[name]]
928
			}
929
			mrows = append(mrows, mrow)
930
		}
931

932 933 934 935 936
		var colType []string
		for _, column := range s.colName {
			colType = append(colType, t.coltype[t.columnIndex(column)])
		}

937 938 939 940
		t.mu.Unlock()

		setMRows = append(setMRows, mrows)
		setColumns = append(setColumns, s.colName)
941
		setColType = append(setColType, colType)
942 943 944

		if s.next == nil {
			break
945
		}
946
		s = s.next
947 948 949
	}

	cursor := &rowsCursor{
950 951 952 953 954 955
		parentMem: s.c,
		posRow:    -1,
		rows:      setMRows,
		cols:      setColumns,
		colType:   setColType,
		errPos:    -1,
956 957 958 959 960
	}
	return cursor, nil
}

func (s *fakeStmt) NumInput() int {
961 962 963
	if s.panic == "NumInput" {
		panic(s.panic)
	}
964 965 966
	return s.placeholders
}

967 968 969
// hook to simulate broken connections
var hookCommitBadConn func() bool

970
func (tx *fakeTx) Commit() error {
971
	tx.c.currTx = nil
972 973 974
	if hookCommitBadConn != nil && hookCommitBadConn() {
		return driver.ErrBadConn
	}
975
	tx.c.touchMem()
976 977 978
	return nil
}

979 980 981
// hook to simulate broken connections
var hookRollbackBadConn func() bool

982
func (tx *fakeTx) Rollback() error {
983
	tx.c.currTx = nil
984 985 986
	if hookRollbackBadConn != nil && hookRollbackBadConn() {
		return driver.ErrBadConn
	}
987
	tx.c.touchMem()
988 989 990 991
	return nil
}

type rowsCursor struct {
992 993 994 995 996 997 998
	parentMem memToucher
	cols      [][]string
	colType   [][]string
	posSet    int
	posRow    int
	rows      [][]*row
	closed    bool
999

1000 1001 1002 1003
	// errPos and err are for making Next return early with error.
	errPos int
	err    error

1004
	// a clone of slices to give out to clients, indexed by the
1005
	// original slice's first byte address.  we clone them
1006 1007
	// just so we're able to corrupt them on close.
	bytesClone map[*byte][]byte
1008 1009 1010 1011 1012 1013 1014 1015 1016

	// Every operation writes to line to enable the race detector
	// check for data races.
	// This is separate from the fakeConn.line to allow for drivers that
	// can start multiple queries on the same transaction at the same time.
	line int64
}

func (rc *rowsCursor) touchMem() {
1017
	rc.parentMem.touchMem()
1018
	rc.line++
1019 1020
}

1021
func (rc *rowsCursor) Close() error {
1022 1023
	rc.touchMem()
	rc.parentMem.touchMem()
1024 1025 1026 1027 1028
	rc.closed = true
	return nil
}

func (rc *rowsCursor) Columns() []string {
1029
	return rc.cols[rc.posSet]
1030 1031
}

1032 1033 1034 1035
func (rc *rowsCursor) ColumnTypeScanType(index int) reflect.Type {
	return colTypeToReflectType(rc.colType[rc.posSet][index])
}

1036 1037
var rowsCursorNextHook func(dest []driver.Value) error

1038
func (rc *rowsCursor) Next(dest []driver.Value) error {
1039 1040 1041 1042
	if rowsCursorNextHook != nil {
		return rowsCursorNextHook(dest)
	}

1043
	if rc.closed {
1044
		return errors.New("fakedb: cursor is closed")
1045
	}
1046
	rc.touchMem()
1047 1048
	rc.posRow++
	if rc.posRow == rc.errPos {
1049 1050
		return rc.err
	}
1051
	if rc.posRow >= len(rc.rows[rc.posSet]) {
1052
		return io.EOF // per interface spec
1053
	}
1054
	for i, v := range rc.rows[rc.posSet][rc.posRow].cols {
1055 1056
		// TODO(bradfitz): convert to subset types? naah, I
		// think the subset types should only be input to
1057
		// driver, but the sql package should be able to handle
1058 1059 1060 1061
		// a wider range of types coming out of drivers. all
		// for ease of drivers, and to prevent drivers from
		// messing up conversions or doing them differently.
		dest[i] = v
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

		if bs, ok := v.([]byte); ok {
			if rc.bytesClone == nil {
				rc.bytesClone = make(map[*byte][]byte)
			}
			clone, ok := rc.bytesClone[&bs[0]]
			if !ok {
				clone = make([]byte, len(bs))
				copy(clone, bs)
				rc.bytesClone[&bs[0]] = clone
			}
			dest[i] = clone
		}
1075 1076 1077 1078
	}
	return nil
}

1079
func (rc *rowsCursor) HasNextResultSet() bool {
1080
	rc.touchMem()
1081 1082 1083 1084
	return rc.posSet < len(rc.rows)-1
}

func (rc *rowsCursor) NextResultSet() error {
1085
	rc.touchMem()
1086 1087 1088 1089 1090 1091 1092 1093
	if rc.HasNextResultSet() {
		rc.posSet++
		rc.posRow = -1
		return nil
	}
	return io.EOF // Per interface spec.
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
// fakeDriverString is like driver.String, but indirects pointers like
// DefaultValueConverter.
//
// This could be surprising behavior to retroactively apply to
// driver.String now that Go1 is out, but this is convenient for
// our TestPointerParamsAndScans.
//
type fakeDriverString struct{}

func (fakeDriverString) ConvertValue(v interface{}) (driver.Value, error) {
	switch c := v.(type) {
	case string, []byte:
		return v, nil
	case *string:
		if c == nil {
			return nil, nil
		}
		return *c, nil
	}
	return fmt.Sprintf("%v", v), nil
}

1116 1117 1118 1119 1120 1121
type anyTypeConverter struct{}

func (anyTypeConverter) ConvertValue(v interface{}) (driver.Value, error) {
	return v, nil
}

1122 1123 1124 1125
func converterForType(typ string) driver.ValueConverter {
	switch typ {
	case "bool":
		return driver.Bool
1126
	case "nullbool":
1127
		return driver.Null{Converter: driver.Bool}
1128 1129 1130
	case "int32":
		return driver.Int32
	case "string":
1131
		return driver.NotNull{Converter: fakeDriverString{}}
1132
	case "nullstring":
1133
		return driver.Null{Converter: fakeDriverString{}}
1134 1135
	case "int64":
		// TODO(coopernurse): add type-specific converter
1136
		return driver.NotNull{Converter: driver.DefaultParameterConverter}
1137 1138
	case "nullint64":
		// TODO(coopernurse): add type-specific converter
1139
		return driver.Null{Converter: driver.DefaultParameterConverter}
1140 1141
	case "float64":
		// TODO(coopernurse): add type-specific converter
1142
		return driver.NotNull{Converter: driver.DefaultParameterConverter}
1143 1144
	case "nullfloat64":
		// TODO(coopernurse): add type-specific converter
1145
		return driver.Null{Converter: driver.DefaultParameterConverter}
1146 1147
	case "datetime":
		return driver.DefaultParameterConverter
1148 1149
	case "any":
		return anyTypeConverter{}
1150 1151 1152
	}
	panic("invalid fakedb column type of " + typ)
}
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

func colTypeToReflectType(typ string) reflect.Type {
	switch typ {
	case "bool":
		return reflect.TypeOf(false)
	case "nullbool":
		return reflect.TypeOf(NullBool{})
	case "int32":
		return reflect.TypeOf(int32(0))
	case "string":
		return reflect.TypeOf("")
	case "nullstring":
		return reflect.TypeOf(NullString{})
	case "int64":
		return reflect.TypeOf(int64(0))
	case "nullint64":
		return reflect.TypeOf(NullInt64{})
	case "float64":
		return reflect.TypeOf(float64(0))
	case "nullfloat64":
		return reflect.TypeOf(NullFloat64{})
	case "datetime":
		return reflect.TypeOf(time.Time{})
1176 1177
	case "any":
		return reflect.TypeOf(new(interface{})).Elem()
1178 1179 1180
	}
	panic("invalid fakedb column type of " + typ)
}