github.com/dolthub/go-mysql-server@v0.18.0/driver/helpers_test.go (about)

     1  package driver_test
     2  
     3  import (
     4  	"database/sql"
     5  	"reflect"
     6  	"sync"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/dolthub/go-mysql-server/driver"
    12  )
    13  
    14  var driverMu sync.Mutex
    15  var drivers = map[driver.Provider]*driver.Driver{}
    16  
    17  func sqlOpen(t *testing.T, provider driver.Provider, dsn string) *sql.DB {
    18  	driverMu.Lock()
    19  	drv, ok := drivers[provider]
    20  	if !ok {
    21  		drv = driver.New(provider, nil)
    22  		drivers[provider] = drv
    23  	}
    24  	driverMu.Unlock()
    25  
    26  	conn, err := drv.OpenConnector(dsn)
    27  	require.NoError(t, err)
    28  	return sql.OpenDB(conn)
    29  }
    30  
    31  type Pointers []any
    32  
    33  func (ptrs Pointers) Values() []any {
    34  	values := make([]any, len(ptrs))
    35  	for i := range values {
    36  		values[i] = reflect.ValueOf(ptrs[i]).Elem().Interface()
    37  	}
    38  	return values
    39  }
    40  
    41  type Records [][]any
    42  
    43  func (records Records) Rows(rows ...int) Records {
    44  	result := make(Records, len(rows))
    45  
    46  	for i := range rows {
    47  		result[i] = records[rows[i]]
    48  	}
    49  
    50  	return result
    51  }
    52  
    53  func (records Records) Columns(cols ...int) Records {
    54  	result := make(Records, len(records))
    55  
    56  	for i := range records {
    57  		result[i] = make([]any, len(cols))
    58  		for j := range cols {
    59  			result[i][j] = records[i][cols[j]]
    60  		}
    61  	}
    62  
    63  	return result
    64  }