Source file src/database/sql/fakedb_test.go

     1  // Copyright 2011 The Go 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  package sql
     6  
     7  import (
     8  	"context"
     9  	"database/sql/driver"
    10  	"errors"
    11  	"fmt"
    12  	"io"
    13  	"reflect"
    14  	"sort"
    15  	"strconv"
    16  	"strings"
    17  	"sync"
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  // fakeDriver is a fake database that implements Go's driver.Driver
    23  // interface, just for testing.
    24  //
    25  // It speaks a query language that's semantically similar to but
    26  // syntactically different and simpler than SQL.  The syntax is as
    27  // follows:
    28  //
    29  //   WIPE
    30  //   CREATE|<tablename>|<col>=<type>,<col>=<type>,...
    31  //     where types are: "string", [u]int{8,16,32,64}, "bool"
    32  //   INSERT|<tablename>|col=val,col2=val2,col3=?
    33  //   SELECT|<tablename>|projectcol1,projectcol2|filtercol=?,filtercol2=?
    34  //   SELECT|<tablename>|projectcol1,projectcol2|filtercol=?param1,filtercol2=?param2
    35  //
    36  // Any of these can be preceded by PANIC|<method>|, to cause the
    37  // named method on fakeStmt to panic.
    38  //
    39  // Any of these can be proceeded by WAIT|<duration>|, to cause the
    40  // named method on fakeStmt to sleep for the specified duration.
    41  //
    42  // Multiple of these can be combined when separated with a semicolon.
    43  //
    44  // When opening a fakeDriver's database, it starts empty with no
    45  // tables. All tables and data are stored in memory only.
    46  type fakeDriver struct {
    47  	mu         sync.Mutex // guards 3 following fields
    48  	openCount  int        // conn opens
    49  	closeCount int        // conn closes
    50  	waitCh     chan struct{}
    51  	waitingCh  chan struct{}
    52  	dbs        map[string]*fakeDB
    53  }
    54  
    55  type fakeConnector struct {
    56  	name string
    57  
    58  	waiter func(context.Context)
    59  	closed bool
    60  }
    61  
    62  func (c *fakeConnector) Connect(context.Context) (driver.Conn, error) {
    63  	conn, err := fdriver.Open(c.name)
    64  	conn.(*fakeConn).waiter = c.waiter
    65  	return conn, err
    66  }
    67  
    68  func (c *fakeConnector) Driver() driver.Driver {
    69  	return fdriver
    70  }
    71  
    72  func (c *fakeConnector) Close() error {
    73  	if c.closed {
    74  		return errors.New("fakedb: connector is closed")
    75  	}
    76  	c.closed = true
    77  	return nil
    78  }
    79  
    80  type fakeDriverCtx struct {
    81  	fakeDriver
    82  }
    83  
    84  var _ driver.DriverContext = &fakeDriverCtx{}
    85  
    86  func (cc *fakeDriverCtx) OpenConnector(name string) (driver.Connector, error) {
    87  	return &fakeConnector{name: name}, nil
    88  }
    89  
    90  type fakeDB struct {
    91  	name string
    92  
    93  	mu       sync.Mutex
    94  	tables   map[string]*table
    95  	badConn  bool
    96  	allowAny bool
    97  }
    98  
    99  type fakeError struct {
   100  	Message string
   101  	Wrapped error
   102  }
   103  
   104  func (err fakeError) Error() string {
   105  	return err.Message
   106  }
   107  
   108  func (err fakeError) Unwrap() error {
   109  	return err.Wrapped
   110  }
   111  
   112  type table struct {
   113  	mu      sync.Mutex
   114  	colname []string
   115  	coltype []string
   116  	rows    []*row
   117  }
   118  
   119  func (t *table) columnIndex(name string) int {
   120  	for n, nname := range t.colname {
   121  		if name == nname {
   122  			return n
   123  		}
   124  	}
   125  	return -1
   126  }
   127  
   128  type row struct {
   129  	cols []any // must be same size as its table colname + coltype
   130  }
   131  
   132  type memToucher interface {
   133  	// touchMem reads & writes some memory, to help find data races.
   134  	touchMem()
   135  }
   136  
   137  type fakeConn struct {
   138  	db *fakeDB // where to return ourselves to
   139  
   140  	currTx *fakeTx
   141  
   142  	// Every operation writes to line to enable the race detector
   143  	// check for data races.
   144  	line int64
   145  
   146  	// Stats for tests:
   147  	mu          sync.Mutex
   148  	stmtsMade   int
   149  	stmtsClosed int
   150  	numPrepare  int
   151  
   152  	// bad connection tests; see isBad()
   153  	bad       bool
   154  	stickyBad bool
   155  
   156  	skipDirtySession bool // tests that use Conn should set this to true.
   157  
   158  	// dirtySession tests ResetSession, true if a query has executed
   159  	// until ResetSession is called.
   160  	dirtySession bool
   161  
   162  	// The waiter is called before each query. May be used in place of the "WAIT"
   163  	// directive.
   164  	waiter func(context.Context)
   165  }
   166  
   167  func (c *fakeConn) touchMem() {
   168  	c.line++
   169  }
   170  
   171  func (c *fakeConn) incrStat(v *int) {
   172  	c.mu.Lock()
   173  	*v++
   174  	c.mu.Unlock()
   175  }
   176  
   177  type fakeTx struct {
   178  	c *fakeConn
   179  }
   180  
   181  type boundCol struct {
   182  	Column      string
   183  	Placeholder string
   184  	Ordinal     int
   185  }
   186  
   187  type fakeStmt struct {
   188  	memToucher
   189  	c *fakeConn
   190  	q string // just for debugging
   191  
   192  	cmd   string
   193  	table string
   194  	panic string
   195  	wait  time.Duration
   196  
   197  	next *fakeStmt // used for returning multiple results.
   198  
   199  	closed bool
   200  
   201  	colName      []string // used by CREATE, INSERT, SELECT (selected columns)
   202  	colType      []string // used by CREATE
   203  	colValue     []any    // used by INSERT (mix of strings and "?" for bound params)
   204  	placeholders int      // used by INSERT/SELECT: number of ? params
   205  
   206  	whereCol []boundCol // used by SELECT (all placeholders)
   207  
   208  	placeholderConverter []driver.ValueConverter // used by INSERT
   209  }
   210  
   211  var fdriver driver.Driver = &fakeDriver{}
   212  
   213  func init() {
   214  	Register("test", fdriver)
   215  }
   216  
   217  func contains(list []string, y string) bool {
   218  	for _, x := range list {
   219  		if x == y {
   220  			return true
   221  		}
   222  	}
   223  	return false
   224  }
   225  
   226  type Dummy struct {
   227  	driver.Driver
   228  }
   229  
   230  func TestDrivers(t *testing.T) {
   231  	unregisterAllDrivers()
   232  	Register("test", fdriver)
   233  	Register("invalid", Dummy{})
   234  	all := Drivers()
   235  	if len(all) < 2 || !sort.StringsAreSorted(all) || !contains(all, "test") || !contains(all, "invalid") {
   236  		t.Fatalf("Drivers = %v, want sorted list with at least [invalid, test]", all)
   237  	}
   238  }
   239  
   240  // hook to simulate connection failures
   241  var hookOpenErr struct {
   242  	sync.Mutex
   243  	fn func() error
   244  }
   245  
   246  func setHookOpenErr(fn func() error) {
   247  	hookOpenErr.Lock()
   248  	defer hookOpenErr.Unlock()
   249  	hookOpenErr.fn = fn
   250  }
   251  
   252  // Supports dsn forms:
   253  //    <dbname>
   254  //    <dbname>;<opts>  (only currently supported option is `badConn`,
   255  //                      which causes driver.ErrBadConn to be returned on
   256  //                      every other conn.Begin())
   257  func (d *fakeDriver) Open(dsn string) (driver.Conn, error) {
   258  	hookOpenErr.Lock()
   259  	fn := hookOpenErr.fn
   260  	hookOpenErr.Unlock()
   261  	if fn != nil {
   262  		if err := fn(); err != nil {
   263  			return nil, err
   264  		}
   265  	}
   266  	parts := strings.Split(dsn, ";")
   267  	if len(parts) < 1 {
   268  		return nil, errors.New("fakedb: no database name")
   269  	}
   270  	name := parts[0]
   271  
   272  	db := d.getDB(name)
   273  
   274  	d.mu.Lock()
   275  	d.openCount++
   276  	d.mu.Unlock()
   277  	conn := &fakeConn{db: db}
   278  
   279  	if len(parts) >= 2 && parts[1] == "badConn" {
   280  		conn.bad = true
   281  	}
   282  	if d.waitCh != nil {
   283  		d.waitingCh <- struct{}{}
   284  		<-d.waitCh
   285  		d.waitCh = nil
   286  		d.waitingCh = nil
   287  	}
   288  	return conn, nil
   289  }
   290  
   291  func (d *fakeDriver) getDB(name string) *fakeDB {
   292  	d.mu.Lock()
   293  	defer d.mu.Unlock()
   294  	if d.dbs == nil {
   295  		d.dbs = make(map[string]*fakeDB)
   296  	}
   297  	db, ok := d.dbs[name]
   298  	if !ok {
   299  		db = &fakeDB{name: name}
   300  		d.dbs[name] = db
   301  	}
   302  	return db
   303  }
   304  
   305  func (db *fakeDB) wipe() {
   306  	db.mu.Lock()
   307  	defer db.mu.Unlock()
   308  	db.tables = nil
   309  }
   310  
   311  func (db *fakeDB) createTable(name string, columnNames, columnTypes []string) error {
   312  	db.mu.Lock()
   313  	defer db.mu.Unlock()
   314  	if db.tables == nil {
   315  		db.tables = make(map[string]*table)
   316  	}
   317  	if _, exist := db.tables[name]; exist {
   318  		return fmt.Errorf("fakedb: table %q already exists", name)
   319  	}
   320  	if len(columnNames) != len(columnTypes) {
   321  		return fmt.Errorf("fakedb: create table of %q len(names) != len(types): %d vs %d",
   322  			name, len(columnNames), len(columnTypes))
   323  	}
   324  	db.tables[name] = &table{colname: columnNames, coltype: columnTypes}
   325  	return nil
   326  }
   327  
   328  // must be called with db.mu lock held
   329  func (db *fakeDB) table(table string) (*table, bool) {
   330  	if db.tables == nil {
   331  		return nil, false
   332  	}
   333  	t, ok := db.tables[table]
   334  	return t, ok
   335  }
   336  
   337  func (db *fakeDB) columnType(table, column string) (typ string, ok bool) {
   338  	db.mu.Lock()
   339  	defer db.mu.Unlock()
   340  	t, ok := db.table(table)
   341  	if !ok {
   342  		return
   343  	}
   344  	for n, cname := range t.colname {
   345  		if cname == column {
   346  			return t.coltype[n], true
   347  		}
   348  	}
   349  	return "", false
   350  }
   351  
   352  func (c *fakeConn) isBad() bool {
   353  	if c.stickyBad {
   354  		return true
   355  	} else if c.bad {
   356  		if c.db == nil {
   357  			return false
   358  		}
   359  		// alternate between bad conn and not bad conn
   360  		c.db.badConn = !c.db.badConn
   361  		return c.db.badConn
   362  	} else {
   363  		return false
   364  	}
   365  }
   366  
   367  func (c *fakeConn) isDirtyAndMark() bool {
   368  	if c.skipDirtySession {
   369  		return false
   370  	}
   371  	if c.currTx != nil {
   372  		c.dirtySession = true
   373  		return false
   374  	}
   375  	if c.dirtySession {
   376  		return true
   377  	}
   378  	c.dirtySession = true
   379  	return false
   380  }
   381  
   382  func (c *fakeConn) Begin() (driver.Tx, error) {
   383  	if c.isBad() {
   384  		return nil, fakeError{Wrapped: driver.ErrBadConn}
   385  	}
   386  	if c.currTx != nil {
   387  		return nil, errors.New("fakedb: already in a transaction")
   388  	}
   389  	c.touchMem()
   390  	c.currTx = &fakeTx{c: c}
   391  	return c.currTx, nil
   392  }
   393  
   394  var hookPostCloseConn struct {
   395  	sync.Mutex
   396  	fn func(*fakeConn, error)
   397  }
   398  
   399  func setHookpostCloseConn(fn func(*fakeConn, error)) {
   400  	hookPostCloseConn.Lock()
   401  	defer hookPostCloseConn.Unlock()
   402  	hookPostCloseConn.fn = fn
   403  }
   404  
   405  var testStrictClose *testing.T
   406  
   407  // setStrictFakeConnClose sets the t to Errorf on when fakeConn.Close
   408  // fails to close. If nil, the check is disabled.
   409  func setStrictFakeConnClose(t *testing.T) {
   410  	testStrictClose = t
   411  }
   412  
   413  func (c *fakeConn) ResetSession(ctx context.Context) error {
   414  	c.dirtySession = false
   415  	c.currTx = nil
   416  	if c.isBad() {
   417  		return fakeError{Message: "Reset Session: bad conn", Wrapped: driver.ErrBadConn}
   418  	}
   419  	return nil
   420  }
   421  
   422  var _ driver.Validator = (*fakeConn)(nil)
   423  
   424  func (c *fakeConn) IsValid() bool {
   425  	return !c.isBad()
   426  }
   427  
   428  func (c *fakeConn) Close() (err error) {
   429  	drv := fdriver.(*fakeDriver)
   430  	defer func() {
   431  		if err != nil && testStrictClose != nil {
   432  			testStrictClose.Errorf("failed to close a test fakeConn: %v", err)
   433  		}
   434  		hookPostCloseConn.Lock()
   435  		fn := hookPostCloseConn.fn
   436  		hookPostCloseConn.Unlock()
   437  		if fn != nil {
   438  			fn(c, err)
   439  		}
   440  		if err == nil {
   441  			drv.mu.Lock()
   442  			drv.closeCount++
   443  			drv.mu.Unlock()
   444  		}
   445  	}()
   446  	c.touchMem()
   447  	if c.currTx != nil {
   448  		return errors.New("fakedb: can't close fakeConn; in a Transaction")
   449  	}
   450  	if c.db == nil {
   451  		return errors.New("fakedb: can't close fakeConn; already closed")
   452  	}
   453  	if c.stmtsMade > c.stmtsClosed {
   454  		return errors.New("fakedb: can't close; dangling statement(s)")
   455  	}
   456  	c.db = nil
   457  	return nil
   458  }
   459  
   460  func checkSubsetTypes(allowAny bool, args []driver.NamedValue) error {
   461  	for _, arg := range args {
   462  		switch arg.Value.(type) {
   463  		case int64, float64, bool, nil, []byte, string, time.Time:
   464  		default:
   465  			if !allowAny {
   466  				return fmt.Errorf("fakedb: invalid argument ordinal %[1]d: %[2]v, type %[2]T", arg.Ordinal, arg.Value)
   467  			}
   468  		}
   469  	}
   470  	return nil
   471  }
   472  
   473  func (c *fakeConn) Exec(query string, args []driver.Value) (driver.Result, error) {
   474  	// Ensure that ExecContext is called if available.
   475  	panic("ExecContext was not called.")
   476  }
   477  
   478  func (c *fakeConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
   479  	// This is an optional interface, but it's implemented here
   480  	// just to check that all the args are of the proper types.
   481  	// ErrSkip is returned so the caller acts as if we didn't
   482  	// implement this at all.
   483  	err := checkSubsetTypes(c.db.allowAny, args)
   484  	if err != nil {
   485  		return nil, err
   486  	}
   487  	return nil, driver.ErrSkip
   488  }
   489  
   490  func (c *fakeConn) Query(query string, args []driver.Value) (driver.Rows, error) {
   491  	// Ensure that ExecContext is called if available.
   492  	panic("QueryContext was not called.")
   493  }
   494  
   495  func (c *fakeConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
   496  	// This is an optional interface, but it's implemented here
   497  	// just to check that all the args are of the proper types.
   498  	// ErrSkip is returned so the caller acts as if we didn't
   499  	// implement this at all.
   500  	err := checkSubsetTypes(c.db.allowAny, args)
   501  	if err != nil {
   502  		return nil, err
   503  	}
   504  	return nil, driver.ErrSkip
   505  }
   506  
   507  func errf(msg string, args ...any) error {
   508  	return errors.New("fakedb: " + fmt.Sprintf(msg, args...))
   509  }
   510  
   511  // parts are table|selectCol1,selectCol2|whereCol=?,whereCol2=?
   512  // (note that where columns must always contain ? marks,
   513  //  just a limitation for fakedb)
   514  func (c *fakeConn) prepareSelect(stmt *fakeStmt, parts []string) (*fakeStmt, error) {
   515  	if len(parts) != 3 {
   516  		stmt.Close()
   517  		return nil, errf("invalid SELECT syntax with %d parts; want 3", len(parts))
   518  	}
   519  	stmt.table = parts[0]
   520  
   521  	stmt.colName = strings.Split(parts[1], ",")
   522  	for n, colspec := range strings.Split(parts[2], ",") {
   523  		if colspec == "" {
   524  			continue
   525  		}
   526  		nameVal := strings.Split(colspec, "=")
   527  		if len(nameVal) != 2 {
   528  			stmt.Close()
   529  			return nil, errf("SELECT on table %q has invalid column spec of %q (index %d)", stmt.table, colspec, n)
   530  		}
   531  		column, value := nameVal[0], nameVal[1]
   532  		_, ok := c.db.columnType(stmt.table, column)
   533  		if !ok {
   534  			stmt.Close()
   535  			return nil, errf("SELECT on table %q references non-existent column %q", stmt.table, column)
   536  		}
   537  		if !strings.HasPrefix(value, "?") {
   538  			stmt.Close()
   539  			return nil, errf("SELECT on table %q has pre-bound value for where column %q; need a question mark",
   540  				stmt.table, column)
   541  		}
   542  		stmt.placeholders++
   543  		stmt.whereCol = append(stmt.whereCol, boundCol{Column: column, Placeholder: value, Ordinal: stmt.placeholders})
   544  	}
   545  	return stmt, nil
   546  }
   547  
   548  // parts are table|col=type,col2=type2
   549  func (c *fakeConn) prepareCreate(stmt *fakeStmt, parts []string) (*fakeStmt, error) {
   550  	if len(parts) != 2 {
   551  		stmt.Close()
   552  		return nil, errf("invalid CREATE syntax with %d parts; want 2", len(parts))
   553  	}
   554  	stmt.table = parts[0]
   555  	for n, colspec := range strings.Split(parts[1], ",") {
   556  		nameType := strings.Split(colspec, "=")
   557  		if len(nameType) != 2 {
   558  			stmt.Close()
   559  			return nil, errf("CREATE table %q has invalid column spec of %q (index %d)", stmt.table, colspec, n)
   560  		}
   561  		stmt.colName = append(stmt.colName, nameType[0])
   562  		stmt.colType = append(stmt.colType, nameType[1])
   563  	}
   564  	return stmt, nil
   565  }
   566  
   567  // parts are table|col=?,col2=val
   568  func (c *fakeConn) prepareInsert(ctx context.Context, stmt *fakeStmt, parts []string) (*fakeStmt, error) {
   569  	if len(parts) != 2 {
   570  		stmt.Close()
   571  		return nil, errf("invalid INSERT syntax with %d parts; want 2", len(parts))
   572  	}
   573  	stmt.table = parts[0]
   574  	for n, colspec := range strings.Split(parts[1], ",") {
   575  		nameVal := strings.Split(colspec, "=")
   576  		if len(nameVal) != 2 {
   577  			stmt.Close()
   578  			return nil, errf("INSERT table %q has invalid column spec of %q (index %d)", stmt.table, colspec, n)
   579  		}
   580  		column, value := nameVal[0], nameVal[1]
   581  		ctype, ok := c.db.columnType(stmt.table, column)
   582  		if !ok {
   583  			stmt.Close()
   584  			return nil, errf("INSERT table %q references non-existent column %q", stmt.table, column)
   585  		}
   586  		stmt.colName = append(stmt.colName, column)
   587  
   588  		if !strings.HasPrefix(value, "?") {
   589  			var subsetVal any
   590  			// Convert to driver subset type
   591  			switch ctype {
   592  			case "string":
   593  				subsetVal = []byte(value)
   594  			case "blob":
   595  				subsetVal = []byte(value)
   596  			case "int32":
   597  				i, err := strconv.Atoi(value)
   598  				if err != nil {
   599  					stmt.Close()
   600  					return nil, errf("invalid conversion to int32 from %q", value)
   601  				}
   602  				subsetVal = int64(i) // int64 is a subset type, but not int32
   603  			case "table": // For testing cursor reads.
   604  				c.skipDirtySession = true
   605  				vparts := strings.Split(value, "!")
   606  
   607  				substmt, err := c.PrepareContext(ctx, fmt.Sprintf("SELECT|%s|%s|", vparts[0], strings.Join(vparts[1:], ",")))
   608  				if err != nil {
   609  					return nil, err
   610  				}
   611  				cursor, err := (substmt.(driver.StmtQueryContext)).QueryContext(ctx, []driver.NamedValue{})
   612  				substmt.Close()
   613  				if err != nil {
   614  					return nil, err
   615  				}
   616  				subsetVal = cursor
   617  			default:
   618  				stmt.Close()
   619  				return nil, errf("unsupported conversion for pre-bound parameter %q to type %q", value, ctype)
   620  			}
   621  			stmt.colValue = append(stmt.colValue, subsetVal)
   622  		} else {
   623  			stmt.placeholders++
   624  			stmt.placeholderConverter = append(stmt.placeholderConverter, converterForType(ctype))
   625  			stmt.colValue = append(stmt.colValue, value)
   626  		}
   627  	}
   628  	return stmt, nil
   629  }
   630  
   631  // hook to simulate broken connections
   632  var hookPrepareBadConn func() bool
   633  
   634  func (c *fakeConn) Prepare(query string) (driver.Stmt, error) {
   635  	panic("use PrepareContext")
   636  }
   637  
   638  func (c *fakeConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
   639  	c.numPrepare++
   640  	if c.db == nil {
   641  		panic("nil c.db; conn = " + fmt.Sprintf("%#v", c))
   642  	}
   643  
   644  	if c.stickyBad || (hookPrepareBadConn != nil && hookPrepareBadConn()) {
   645  		return nil, fakeError{Message: "Preapre: Sticky Bad", Wrapped: driver.ErrBadConn}
   646  	}
   647  
   648  	c.touchMem()
   649  	var firstStmt, prev *fakeStmt
   650  	for _, query := range strings.Split(query, ";") {
   651  		parts := strings.Split(query, "|")
   652  		if len(parts) < 1 {
   653  			return nil, errf("empty query")
   654  		}
   655  		stmt := &fakeStmt{q: query, c: c, memToucher: c}
   656  		if firstStmt == nil {
   657  			firstStmt = stmt
   658  		}
   659  		if len(parts) >= 3 {
   660  			switch parts[0] {
   661  			case "PANIC":
   662  				stmt.panic = parts[1]
   663  				parts = parts[2:]
   664  			case "WAIT":
   665  				wait, err := time.ParseDuration(parts[1])
   666  				if err != nil {
   667  					return nil, errf("expected section after WAIT to be a duration, got %q %v", parts[1], err)
   668  				}
   669  				parts = parts[2:]
   670  				stmt.wait = wait
   671  			}
   672  		}
   673  		cmd := parts[0]
   674  		stmt.cmd = cmd
   675  		parts = parts[1:]
   676  
   677  		if c.waiter != nil {
   678  			c.waiter(ctx)
   679  			if err := ctx.Err(); err != nil {
   680  				return nil, err
   681  			}
   682  		}
   683  
   684  		if stmt.wait > 0 {
   685  			wait := time.NewTimer(stmt.wait)
   686  			select {
   687  			case <-wait.C:
   688  			case <-ctx.Done():
   689  				wait.Stop()
   690  				return nil, ctx.Err()
   691  			}
   692  		}
   693  
   694  		c.incrStat(&c.stmtsMade)
   695  		var err error
   696  		switch cmd {
   697  		case "WIPE":
   698  			// Nothing
   699  		case "SELECT":
   700  			stmt, err = c.prepareSelect(stmt, parts)
   701  		case "CREATE":
   702  			stmt, err = c.prepareCreate(stmt, parts)
   703  		case "INSERT":
   704  			stmt, err = c.prepareInsert(ctx, stmt, parts)
   705  		case "NOSERT":
   706  			// Do all the prep-work like for an INSERT but don't actually insert the row.
   707  			// Used for some of the concurrent tests.
   708  			stmt, err = c.prepareInsert(ctx, stmt, parts)
   709  		default:
   710  			stmt.Close()
   711  			return nil, errf("unsupported command type %q", cmd)
   712  		}
   713  		if err != nil {
   714  			return nil, err
   715  		}
   716  		if prev != nil {
   717  			prev.next = stmt
   718  		}
   719  		prev = stmt
   720  	}
   721  	return firstStmt, nil
   722  }
   723  
   724  func (s *fakeStmt) ColumnConverter(idx int) driver.ValueConverter {
   725  	if s.panic == "ColumnConverter" {
   726  		panic(s.panic)
   727  	}
   728  	if len(s.placeholderConverter) == 0 {
   729  		return driver.DefaultParameterConverter
   730  	}
   731  	return s.placeholderConverter[idx]
   732  }
   733  
   734  func (s *fakeStmt) Close() error {
   735  	if s.panic == "Close" {
   736  		panic(s.panic)
   737  	}
   738  	if s.c == nil {
   739  		panic("nil conn in fakeStmt.Close")
   740  	}
   741  	if s.c.db == nil {
   742  		panic("in fakeStmt.Close, conn's db is nil (already closed)")
   743  	}
   744  	s.touchMem()
   745  	if !s.closed {
   746  		s.c.incrStat(&s.c.stmtsClosed)
   747  		s.closed = true
   748  	}
   749  	if s.next != nil {
   750  		s.next.Close()
   751  	}
   752  	return nil
   753  }
   754  
   755  var errClosed = errors.New("fakedb: statement has been closed")
   756  
   757  // hook to simulate broken connections
   758  var hookExecBadConn func() bool
   759  
   760  func (s *fakeStmt) Exec(args []driver.Value) (driver.Result, error) {
   761  	panic("Using ExecContext")
   762  }
   763  
   764  var errFakeConnSessionDirty = errors.New("fakedb: session is dirty")
   765  
   766  func (s *fakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
   767  	if s.panic == "Exec" {
   768  		panic(s.panic)
   769  	}
   770  	if s.closed {
   771  		return nil, errClosed
   772  	}
   773  
   774  	if s.c.stickyBad || (hookExecBadConn != nil && hookExecBadConn()) {
   775  		return nil, fakeError{Message: "Exec: Sticky Bad", Wrapped: driver.ErrBadConn}
   776  	}
   777  	if s.c.isDirtyAndMark() {
   778  		return nil, errFakeConnSessionDirty
   779  	}
   780  
   781  	err := checkSubsetTypes(s.c.db.allowAny, args)
   782  	if err != nil {
   783  		return nil, err
   784  	}
   785  	s.touchMem()
   786  
   787  	if s.wait > 0 {
   788  		time.Sleep(s.wait)
   789  	}
   790  
   791  	select {
   792  	default:
   793  	case <-ctx.Done():
   794  		return nil, ctx.Err()
   795  	}
   796  
   797  	db := s.c.db
   798  	switch s.cmd {
   799  	case "WIPE":
   800  		db.wipe()
   801  		return driver.ResultNoRows, nil
   802  	case "CREATE":
   803  		if err := db.createTable(s.table, s.colName, s.colType); err != nil {
   804  			return nil, err
   805  		}
   806  		return driver.ResultNoRows, nil
   807  	case "INSERT":
   808  		return s.execInsert(args, true)
   809  	case "NOSERT":
   810  		// Do all the prep-work like for an INSERT but don't actually insert the row.
   811  		// Used for some of the concurrent tests.
   812  		return s.execInsert(args, false)
   813  	}
   814  	return nil, fmt.Errorf("fakedb: unimplemented statement Exec command type of %q", s.cmd)
   815  }
   816  
   817  // When doInsert is true, add the row to the table.
   818  // When doInsert is false do prep-work and error checking, but don't
   819  // actually add the row to the table.
   820  func (s *fakeStmt) execInsert(args []driver.NamedValue, doInsert bool) (driver.Result, error) {
   821  	db := s.c.db
   822  	if len(args) != s.placeholders {
   823  		panic("error in pkg db; should only get here if size is correct")
   824  	}
   825  	db.mu.Lock()
   826  	t, ok := db.table(s.table)
   827  	db.mu.Unlock()
   828  	if !ok {
   829  		return nil, fmt.Errorf("fakedb: table %q doesn't exist", s.table)
   830  	}
   831  
   832  	t.mu.Lock()
   833  	defer t.mu.Unlock()
   834  
   835  	var cols []any
   836  	if doInsert {
   837  		cols = make([]any, len(t.colname))
   838  	}
   839  	argPos := 0
   840  	for n, colname := range s.colName {
   841  		colidx := t.columnIndex(colname)
   842  		if colidx == -1 {
   843  			return nil, fmt.Errorf("fakedb: column %q doesn't exist or dropped since prepared statement was created", colname)
   844  		}
   845  		var val any
   846  		if strvalue, ok := s.colValue[n].(string); ok && strings.HasPrefix(strvalue, "?") {
   847  			if strvalue == "?" {
   848  				val = args[argPos].Value
   849  			} else {
   850  				// Assign value from argument placeholder name.
   851  				for _, a := range args {
   852  					if a.Name == strvalue[1:] {
   853  						val = a.Value
   854  						break
   855  					}
   856  				}
   857  			}
   858  			argPos++
   859  		} else {
   860  			val = s.colValue[n]
   861  		}
   862  		if doInsert {
   863  			cols[colidx] = val
   864  		}
   865  	}
   866  
   867  	if doInsert {
   868  		t.rows = append(t.rows, &row{cols: cols})
   869  	}
   870  	return driver.RowsAffected(1), nil
   871  }
   872  
   873  // hook to simulate broken connections
   874  var hookQueryBadConn func() bool
   875  
   876  func (s *fakeStmt) Query(args []driver.Value) (driver.Rows, error) {
   877  	panic("Use QueryContext")
   878  }
   879  
   880  func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
   881  	if s.panic == "Query" {
   882  		panic(s.panic)
   883  	}
   884  	if s.closed {
   885  		return nil, errClosed
   886  	}
   887  
   888  	if s.c.stickyBad || (hookQueryBadConn != nil && hookQueryBadConn()) {
   889  		return nil, fakeError{Message: "Query: Sticky Bad", Wrapped: driver.ErrBadConn}
   890  	}
   891  	if s.c.isDirtyAndMark() {
   892  		return nil, errFakeConnSessionDirty
   893  	}
   894  
   895  	err := checkSubsetTypes(s.c.db.allowAny, args)
   896  	if err != nil {
   897  		return nil, err
   898  	}
   899  
   900  	s.touchMem()
   901  	db := s.c.db
   902  	if len(args) != s.placeholders {
   903  		panic("error in pkg db; should only get here if size is correct")
   904  	}
   905  
   906  	setMRows := make([][]*row, 0, 1)
   907  	setColumns := make([][]string, 0, 1)
   908  	setColType := make([][]string, 0, 1)
   909  
   910  	for {
   911  		db.mu.Lock()
   912  		t, ok := db.table(s.table)
   913  		db.mu.Unlock()
   914  		if !ok {
   915  			return nil, fmt.Errorf("fakedb: table %q doesn't exist", s.table)
   916  		}
   917  
   918  		if s.table == "magicquery" {
   919  			if len(s.whereCol) == 2 && s.whereCol[0].Column == "op" && s.whereCol[1].Column == "millis" {
   920  				if args[0].Value == "sleep" {
   921  					time.Sleep(time.Duration(args[1].Value.(int64)) * time.Millisecond)
   922  				}
   923  			}
   924  		}
   925  		if s.table == "tx_status" && s.colName[0] == "tx_status" {
   926  			txStatus := "autocommit"
   927  			if s.c.currTx != nil {
   928  				txStatus = "transaction"
   929  			}
   930  			cursor := &rowsCursor{
   931  				parentMem: s.c,
   932  				posRow:    -1,
   933  				rows: [][]*row{
   934  					{
   935  						{
   936  							cols: []any{
   937  								txStatus,
   938  							},
   939  						},
   940  					},
   941  				},
   942  				cols: [][]string{
   943  					{
   944  						"tx_status",
   945  					},
   946  				},
   947  				colType: [][]string{
   948  					{
   949  						"string",
   950  					},
   951  				},
   952  				errPos: -1,
   953  			}
   954  			return cursor, nil
   955  		}
   956  
   957  		t.mu.Lock()
   958  
   959  		colIdx := make(map[string]int) // select column name -> column index in table
   960  		for _, name := range s.colName {
   961  			idx := t.columnIndex(name)
   962  			if idx == -1 {
   963  				t.mu.Unlock()
   964  				return nil, fmt.Errorf("fakedb: unknown column name %q", name)
   965  			}
   966  			colIdx[name] = idx
   967  		}
   968  
   969  		mrows := []*row{}
   970  	rows:
   971  		for _, trow := range t.rows {
   972  			// Process the where clause, skipping non-match rows. This is lazy
   973  			// and just uses fmt.Sprintf("%v") to test equality. Good enough
   974  			// for test code.
   975  			for _, wcol := range s.whereCol {
   976  				idx := t.columnIndex(wcol.Column)
   977  				if idx == -1 {
   978  					t.mu.Unlock()
   979  					return nil, fmt.Errorf("fakedb: invalid where clause column %q", wcol)
   980  				}
   981  				tcol := trow.cols[idx]
   982  				if bs, ok := tcol.([]byte); ok {
   983  					// lazy hack to avoid sprintf %v on a []byte
   984  					tcol = string(bs)
   985  				}
   986  				var argValue any
   987  				if wcol.Placeholder == "?" {
   988  					argValue = args[wcol.Ordinal-1].Value
   989  				} else {
   990  					// Assign arg value from placeholder name.
   991  					for _, a := range args {
   992  						if a.Name == wcol.Placeholder[1:] {
   993  							argValue = a.Value
   994  							break
   995  						}
   996  					}
   997  				}
   998  				if fmt.Sprintf("%v", tcol) != fmt.Sprintf("%v", argValue) {
   999  					continue rows
  1000  				}
  1001  			}
  1002  			mrow := &row{cols: make([]any, len(s.colName))}
  1003  			for seli, name := range s.colName {
  1004  				mrow.cols[seli] = trow.cols[colIdx[name]]
  1005  			}
  1006  			mrows = append(mrows, mrow)
  1007  		}
  1008  
  1009  		var colType []string
  1010  		for _, column := range s.colName {
  1011  			colType = append(colType, t.coltype[t.columnIndex(column)])
  1012  		}
  1013  
  1014  		t.mu.Unlock()
  1015  
  1016  		setMRows = append(setMRows, mrows)
  1017  		setColumns = append(setColumns, s.colName)
  1018  		setColType = append(setColType, colType)
  1019  
  1020  		if s.next == nil {
  1021  			break
  1022  		}
  1023  		s = s.next
  1024  	}
  1025  
  1026  	cursor := &rowsCursor{
  1027  		parentMem: s.c,
  1028  		posRow:    -1,
  1029  		rows:      setMRows,
  1030  		cols:      setColumns,
  1031  		colType:   setColType,
  1032  		errPos:    -1,
  1033  	}
  1034  	return cursor, nil
  1035  }
  1036  
  1037  func (s *fakeStmt) NumInput() int {
  1038  	if s.panic == "NumInput" {
  1039  		panic(s.panic)
  1040  	}
  1041  	return s.placeholders
  1042  }
  1043  
  1044  // hook to simulate broken connections
  1045  var hookCommitBadConn func() bool
  1046  
  1047  func (tx *fakeTx) Commit() error {
  1048  	tx.c.currTx = nil
  1049  	if hookCommitBadConn != nil && hookCommitBadConn() {
  1050  		return fakeError{Message: "Commit: Hook Bad Conn", Wrapped: driver.ErrBadConn}
  1051  	}
  1052  	tx.c.touchMem()
  1053  	return nil
  1054  }
  1055  
  1056  // hook to simulate broken connections
  1057  var hookRollbackBadConn func() bool
  1058  
  1059  func (tx *fakeTx) Rollback() error {
  1060  	tx.c.currTx = nil
  1061  	if hookRollbackBadConn != nil && hookRollbackBadConn() {
  1062  		return fakeError{Message: "Rollback: Hook Bad Conn", Wrapped: driver.ErrBadConn}
  1063  	}
  1064  	tx.c.touchMem()
  1065  	return nil
  1066  }
  1067  
  1068  type rowsCursor struct {
  1069  	parentMem memToucher
  1070  	cols      [][]string
  1071  	colType   [][]string
  1072  	posSet    int
  1073  	posRow    int
  1074  	rows      [][]*row
  1075  	closed    bool
  1076  
  1077  	// errPos and err are for making Next return early with error.
  1078  	errPos int
  1079  	err    error
  1080  
  1081  	// a clone of slices to give out to clients, indexed by the
  1082  	// original slice's first byte address.  we clone them
  1083  	// just so we're able to corrupt them on close.
  1084  	bytesClone map[*byte][]byte
  1085  
  1086  	// Every operation writes to line to enable the race detector
  1087  	// check for data races.
  1088  	// This is separate from the fakeConn.line to allow for drivers that
  1089  	// can start multiple queries on the same transaction at the same time.
  1090  	line int64
  1091  }
  1092  
  1093  func (rc *rowsCursor) touchMem() {
  1094  	rc.parentMem.touchMem()
  1095  	rc.line++
  1096  }
  1097  
  1098  func (rc *rowsCursor) Close() error {
  1099  	rc.touchMem()
  1100  	rc.parentMem.touchMem()
  1101  	rc.closed = true
  1102  	return nil
  1103  }
  1104  
  1105  func (rc *rowsCursor) Columns() []string {
  1106  	return rc.cols[rc.posSet]
  1107  }
  1108  
  1109  func (rc *rowsCursor) ColumnTypeScanType(index int) reflect.Type {
  1110  	return colTypeToReflectType(rc.colType[rc.posSet][index])
  1111  }
  1112  
  1113  var rowsCursorNextHook func(dest []driver.Value) error
  1114  
  1115  func (rc *rowsCursor) Next(dest []driver.Value) error {
  1116  	if rowsCursorNextHook != nil {
  1117  		return rowsCursorNextHook(dest)
  1118  	}
  1119  
  1120  	if rc.closed {
  1121  		return errors.New("fakedb: cursor is closed")
  1122  	}
  1123  	rc.touchMem()
  1124  	rc.posRow++
  1125  	if rc.posRow == rc.errPos {
  1126  		return rc.err
  1127  	}
  1128  	if rc.posRow >= len(rc.rows[rc.posSet]) {
  1129  		return io.EOF // per interface spec
  1130  	}
  1131  	for i, v := range rc.rows[rc.posSet][rc.posRow].cols {
  1132  		// TODO(bradfitz): convert to subset types? naah, I
  1133  		// think the subset types should only be input to
  1134  		// driver, but the sql package should be able to handle
  1135  		// a wider range of types coming out of drivers. all
  1136  		// for ease of drivers, and to prevent drivers from
  1137  		// messing up conversions or doing them differently.
  1138  		dest[i] = v
  1139  
  1140  		if bs, ok := v.([]byte); ok {
  1141  			if rc.bytesClone == nil {
  1142  				rc.bytesClone = make(map[*byte][]byte)
  1143  			}
  1144  			clone, ok := rc.bytesClone[&bs[0]]
  1145  			if !ok {
  1146  				clone = make([]byte, len(bs))
  1147  				copy(clone, bs)
  1148  				rc.bytesClone[&bs[0]] = clone
  1149  			}
  1150  			dest[i] = clone
  1151  		}
  1152  	}
  1153  	return nil
  1154  }
  1155  
  1156  func (rc *rowsCursor) HasNextResultSet() bool {
  1157  	rc.touchMem()
  1158  	return rc.posSet < len(rc.rows)-1
  1159  }
  1160  
  1161  func (rc *rowsCursor) NextResultSet() error {
  1162  	rc.touchMem()
  1163  	if rc.HasNextResultSet() {
  1164  		rc.posSet++
  1165  		rc.posRow = -1
  1166  		return nil
  1167  	}
  1168  	return io.EOF // Per interface spec.
  1169  }
  1170  
  1171  // fakeDriverString is like driver.String, but indirects pointers like
  1172  // DefaultValueConverter.
  1173  //
  1174  // This could be surprising behavior to retroactively apply to
  1175  // driver.String now that Go1 is out, but this is convenient for
  1176  // our TestPointerParamsAndScans.
  1177  //
  1178  type fakeDriverString struct{}
  1179  
  1180  func (fakeDriverString) ConvertValue(v any) (driver.Value, error) {
  1181  	switch c := v.(type) {
  1182  	case string, []byte:
  1183  		return v, nil
  1184  	case *string:
  1185  		if c == nil {
  1186  			return nil, nil
  1187  		}
  1188  		return *c, nil
  1189  	}
  1190  	return fmt.Sprintf("%v", v), nil
  1191  }
  1192  
  1193  type anyTypeConverter struct{}
  1194  
  1195  func (anyTypeConverter) ConvertValue(v any) (driver.Value, error) {
  1196  	return v, nil
  1197  }
  1198  
  1199  func converterForType(typ string) driver.ValueConverter {
  1200  	switch typ {
  1201  	case "bool":
  1202  		return driver.Bool
  1203  	case "nullbool":
  1204  		return driver.Null{Converter: driver.Bool}
  1205  	case "byte", "int16":
  1206  		return driver.NotNull{Converter: driver.DefaultParameterConverter}
  1207  	case "int32":
  1208  		return driver.Int32
  1209  	case "nullbyte", "nullint32", "nullint16":
  1210  		return driver.Null{Converter: driver.DefaultParameterConverter}
  1211  	case "string":
  1212  		return driver.NotNull{Converter: fakeDriverString{}}
  1213  	case "nullstring":
  1214  		return driver.Null{Converter: fakeDriverString{}}
  1215  	case "int64":
  1216  		// TODO(coopernurse): add type-specific converter
  1217  		return driver.NotNull{Converter: driver.DefaultParameterConverter}
  1218  	case "nullint64":
  1219  		// TODO(coopernurse): add type-specific converter
  1220  		return driver.Null{Converter: driver.DefaultParameterConverter}
  1221  	case "float64":
  1222  		// TODO(coopernurse): add type-specific converter
  1223  		return driver.NotNull{Converter: driver.DefaultParameterConverter}
  1224  	case "nullfloat64":
  1225  		// TODO(coopernurse): add type-specific converter
  1226  		return driver.Null{Converter: driver.DefaultParameterConverter}
  1227  	case "datetime":
  1228  		return driver.NotNull{Converter: driver.DefaultParameterConverter}
  1229  	case "nulldatetime":
  1230  		return driver.Null{Converter: driver.DefaultParameterConverter}
  1231  	case "any":
  1232  		return anyTypeConverter{}
  1233  	}
  1234  	panic("invalid fakedb column type of " + typ)
  1235  }
  1236  
  1237  func colTypeToReflectType(typ string) reflect.Type {
  1238  	switch typ {
  1239  	case "bool":
  1240  		return reflect.TypeOf(false)
  1241  	case "nullbool":
  1242  		return reflect.TypeOf(NullBool{})
  1243  	case "int16":
  1244  		return reflect.TypeOf(int16(0))
  1245  	case "nullint16":
  1246  		return reflect.TypeOf(NullInt16{})
  1247  	case "int32":
  1248  		return reflect.TypeOf(int32(0))
  1249  	case "nullint32":
  1250  		return reflect.TypeOf(NullInt32{})
  1251  	case "string":
  1252  		return reflect.TypeOf("")
  1253  	case "nullstring":
  1254  		return reflect.TypeOf(NullString{})
  1255  	case "int64":
  1256  		return reflect.TypeOf(int64(0))
  1257  	case "nullint64":
  1258  		return reflect.TypeOf(NullInt64{})
  1259  	case "float64":
  1260  		return reflect.TypeOf(float64(0))
  1261  	case "nullfloat64":
  1262  		return reflect.TypeOf(NullFloat64{})
  1263  	case "datetime":
  1264  		return reflect.TypeOf(time.Time{})
  1265  	case "any":
  1266  		return reflect.TypeOf(new(any)).Elem()
  1267  	}
  1268  	panic("invalid fakedb column type of " + typ)
  1269  }
  1270  

View as plain text