-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mogi.go
103 lines (92 loc) · 2.6 KB
/
mogi.go
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
package mogi
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"os"
"text/tabwriter"
)
var (
// ErrUnstubbed is returned as the result for unstubbed queries.
ErrUnstubbed = errors.New("mogi: query not stubbed")
// ErrUnresolved is returned as the result of a stub that was matched,
// but whose data could not be resolved. For example, exceeded LIMITs.
ErrUnresolved = errors.New("mogi: query matched but no stub data")
// errNotSet is used for Exec results stubbed as -1.
errNotSet = errors.New("value set to -1")
)
var (
verbose = false
timeLayout = ""
)
func init() {
drv = newDriver()
sql.Register("mogi", drv)
}
// Reset removes all the stubs that have been set
func Reset() {
drv.conn.stubs = nil
drv.conn.execStubs = nil
}
// Verbose turns on unstubbed logging when v is true
func Verbose(v bool) {
verbose = v
}
// ParseTime will configure mogi to convert dates of the given layout
// (e.g. time.RFC3339) to time.Time when using StubCSV.
// Give it an empty string to turn off time parsing.
func ParseTime(layout string) {
timeLayout = layout
}
// Dump prints all the current stubs, in order of priority.
// Helpful for debugging.
func Dump() {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintf(w, ">>\t\tQuery stubs: (%d total)\t\n", len(drv.conn.stubs))
fmt.Fprintf(w, "\t\t=========================\t\n")
for rank, s := range drv.conn.stubs {
for i, c := range s.chain {
if i == 0 {
fmt.Fprintf(w, "#%d\t[%d]\t%s\t[%+d]\n", rank+1, s.priority(), c, c.priority())
continue
}
fmt.Fprintf(w, "\t\t%s\t[%+d]\n", c, c.priority())
}
switch {
case s.err != nil:
fmt.Fprintf(w, "\t\t→ error: %v\t\n", s.err)
case s.data != nil, s.resolve != nil:
fmt.Fprintf(w, "\t\t→ data\t\n")
}
}
fmt.Fprintf(w, "\t\t\t\n")
fmt.Fprintf(w, ">>\t\tExec stubs: (%d total)\t\n", len(drv.conn.execStubs))
fmt.Fprintf(w, "\t\t=========================\t\n")
for rank, s := range drv.conn.execStubs {
for i, c := range s.chain {
if i == 0 {
fmt.Fprintf(w, "#%d\t[%d]\t%s\t[%+d]\n", rank+1, s.priority(), c, c.priority())
continue
}
fmt.Fprintf(w, "\t\t%s\t[%+d]\n", c, c.priority())
}
switch {
case s.err != nil:
fmt.Fprintf(w, "\t\t→ error: %v\t\n", s.err)
case s.result != nil:
if r, ok := s.result.(execResult); ok {
fmt.Fprintf(w, "\t\t→ result ID: %d, rows: %d\t\n", r.lastInsertID, r.rowsAffected)
} else {
fmt.Fprintf(w, "\t\t→ result %T\t\n", s.result)
}
}
}
w.Flush()
}
// func Replace() {
// drv.conn = newConn()
// }
var _ driver.Stmt = &stmt{}
var _ driver.Conn = &conn{}
var _ driver.Driver = &mdriver{}