1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
package testdb
import (
"crypto/sha1"
"database/sql"
"database/sql/driver"
"encoding/csv"
"io"
"regexp"
"strings"
"time"
)
var d *testDriver
func init() {
d = newDriver()
sql.Register("testdb", d)
}
type testDriver struct {
openFunc func(dsn string) (driver.Conn, error)
conn *conn
enableTimeParsing bool
}
type query struct {
rows driver.Rows
result *Result
err error
}
func newDriver() *testDriver {
return &testDriver{
conn: newConn(),
}
}
func EnableTimeParsing(flag bool) {
d.enableTimeParsing = flag
}
func (d *testDriver) Open(dsn string) (driver.Conn, error) {
if d.openFunc != nil {
conn, err := d.openFunc(dsn)
return conn, err
}
if d.conn == nil {
d.conn = newConn()
}
return d.conn, nil
}
var whitespaceRegexp = regexp.MustCompile("\\s")
func getQueryHash(query string) string {
// Remove whitespace and lowercase to make stubbing less brittle
query = strings.ToLower(whitespaceRegexp.ReplaceAllString(query, ""))
h := sha1.New()
io.WriteString(h, query)
return string(h.Sum(nil))
}
// Set your own function to be executed when db.Query() is called. As with StubQuery() you can use the RowsFromCSVString() method to easily generate the driver.Rows, or you can return your own.
func SetQueryFunc(f func(query string) (result driver.Rows, err error)) {
SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) {
return f(query)
})
}
// Set your own function to be executed when db.Query() is called. As with StubQuery() you can use the RowsFromCSVString() method to easily generate the driver.Rows, or you can return your own.
func SetQueryWithArgsFunc(f func(query string, args []driver.Value) (result driver.Rows, err error)) {
d.conn.queryFunc = f
}
// Stubs the global driver.Conn to return the supplied driver.Rows when db.Query() is called, query stubbing is case insensitive, and whitespace is also ignored.
func StubQuery(q string, rows driver.Rows) {
d.conn.queries[getQueryHash(q)] = query{
rows: rows,
}
}
// Stubs the global driver.Conn to return the supplied error when db.Query() is called, query stubbing is case insensitive, and whitespace is also ignored.
func StubQueryError(q string, err error) {
d.conn.queries[getQueryHash(q)] = query{
err: err,
}
}
// Set your own function to be executed when db.Open() is called. You can either hand back a valid connection, or an error. Conn() can be used to grab the global Conn object containing stubbed queries.
func SetOpenFunc(f func(dsn string) (driver.Conn, error)) {
d.openFunc = f
}
// Set your own function to be executed when db.Exec is called. You can return an error or a Result object with the LastInsertId and RowsAffected
func SetExecFunc(f func(query string) (driver.Result, error)) {
SetExecWithArgsFunc(func(query string, args []driver.Value) (driver.Result, error) {
return f(query)
})
}
// Set your own function to be executed when db.Exec is called. You can return an error or a Result object with the LastInsertId and RowsAffected
func SetExecWithArgsFunc(f func(query string, args []driver.Value) (driver.Result, error)) {
d.conn.execFunc = f
}
// Stubs the global driver.Conn to return the supplied Result when db.Exec is called, query stubbing is case insensitive, and whitespace is also ignored.
func StubExec(q string, r *Result) {
d.conn.queries[getQueryHash(q)] = query{
result: r,
}
}
// Stubs the global driver.Conn to return the supplied error when db.Exec() is called, query stubbing is case insensitive, and whitespace is also ignored.
func StubExecError(q string, err error) {
StubQueryError(q, err)
}
// Set your own function to be executed when db.Begin() is called. You can either hand back a valid transaction, or an error. Conn() can be used to grab the global Conn object containing stubbed queries.
func SetBeginFunc(f func() (driver.Tx, error)) {
d.conn.beginFunc = f
}
// Stubs the global driver.Conn to return the supplied tx and error when db.Begin() is called.
func StubBegin(tx driver.Tx, err error) {
SetBeginFunc(func() (driver.Tx, error) {
return tx, err
})
}
// Set your own function to be executed when tx.Commit() is called on the default transcation. Conn() can be used to grab the global Conn object containing stubbed queries.
func SetCommitFunc(f func() error) {
d.conn.commitFunc = f
}
// Stubs the default transaction to return the supplied error when tx.Commit() is called.
func StubCommitError(err error) {
SetCommitFunc(func() error {
return err
})
}
// Set your own function to be executed when tx.Rollback() is called on the default transcation. Conn() can be used to grab the global Conn object containing stubbed queries.
func SetRollbackFunc(f func() error) {
d.conn.rollbackFunc = f
}
// Stubs the default transaction to return the supplied error when tx.Rollback() is called.
func StubRollbackError(err error) {
SetRollbackFunc(func() error {
return err
})
}
// Clears all stubbed queries, and replaced functions.
func Reset() {
d.conn = newConn()
d.openFunc = nil
}
// Returns a pointer to the global conn object associated with this driver.
func Conn() driver.Conn {
return d.conn
}
func RowsFromCSVString(columns []string, s string, c ...rune) driver.Rows {
r := strings.NewReader(strings.TrimSpace(s))
csvReader := csv.NewReader(r)
if len(c) > 0 {
csvReader.Comma = c[0]
}
rows := [][]driver.Value{}
for {
r, err := csvReader.Read()
if err != nil || r == nil {
break
}
row := make([]driver.Value, len(columns))
for i, v := range r {
v := strings.TrimSpace(v)
// If enableTimeParsing is on, check to see if this is a
// time in RFC33339 format
if d.enableTimeParsing {
if time, err := time.Parse(time.RFC3339, v); err == nil {
row[i] = time
} else {
row[i] = v
}
} else {
row[i] = v
}
}
rows = append(rows, row)
}
return RowsFromSlice(columns, rows)
}
func RowsFromSlice(columns []string, data [][]driver.Value) driver.Rows {
return &rows{
closed: false,
columns: columns,
rows: data,
pos: 0,
}
}
|