-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivercaps.go
238 lines (203 loc) · 4.67 KB
/
drivercaps.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package drivercaps
import (
"database/sql"
"fmt"
"log"
"math"
"math/rand"
"os"
"strconv"
"time"
"github.com/jimsmart/schema"
// . "github.com/onsi/ginkgo"
// . "github.com/onsi/gomega"
)
const (
csvFilename = "/report.csv"
asciiFilename = "/report.txt"
markdownFilename = "/README.md"
)
type DriverTest struct {
Database string
PkgURL string
PkgName string
DrvName string
ConnStr string
Columns []*ColumnDefn
CreateOpt string
PKType string
}
type ColumnDefn struct {
DDL string // e.g. VARCHAR(255)
// *sql.ColumnType return values.
// Name string
// DatabaseTypeName string
// DecimalSizeOk bool
// DecimalSizePrecision int64
// DecimalSizeScale int64
// LengthOk bool
// Length int64
// NullableOk bool
// Nullable bool
// // ScanType reflect.Type
}
func ReportForDriver(test *DriverTest) error {
results, err := exerciseDriver(test)
if err == nil && len(results) > 1 {
writeCSVFile(results, test)
// renderCSV(results, test)
}
// if err != nil {
// return err
// }
// TODO Write new CSV on successful results, but always regenerate the other views from existing csv
results, tim, err2 := readCSVFile()
if os.IsNotExist(err2) {
return nil
}
if err2 != nil {
return err2
}
if len(results) < 2 {
return nil
}
renderASCII(results, test, tim)
writeASCIIFile(results, test, tim)
// renderMarkdown(results, test, tim)
writeMarkdownFile(results, test, tim)
return nil
}
func exerciseDriver(test *DriverTest) ([][]string, error) {
if len(test.Columns) == 0 {
return nil, nil
}
title := fmt.Sprintf("sql.Driver %s %q", test.PkgName, test.DrvName)
log.Printf(title)
db, err := sql.Open(test.DrvName, test.ConnStr)
if err != nil {
return nil, err
}
defer func() {
err = db.Close()
if err != nil {
log.Printf("db.Close error %v", err)
}
}()
types := make([]string, len(test.Columns)*2)
cis := make([]*sql.ColumnType, len(test.Columns)*2)
i := 0
testType := func(typeDDL string) {
j := i
i++
tname := "drivercaps_" + randomString(8)
// log.Println(tname)
create := fmt.Sprintf("CREATE TABLE %s (\n\tpk %s PRIMARY KEY,\n", tname, test.PKType)
s := fmt.Sprintf("t_%d %s", j, typeDDL)
types[j] = s
create += fmt.Sprintf("\t%s\n)", s)
err := exec(db, create)
if err != nil {
log.Printf("create error %v", err)
return
}
// query caps
ci, err := schema.Table(db, tname)
if err != nil {
log.Printf("metadata query error %v", err)
return
}
cis[j] = ci[1]
err = exec(db, "DROP TABLE "+tname)
if err != nil {
log.Printf("drop error %v", err)
}
}
for _, t := range test.Columns {
testType(t.DDL)
}
for _, t := range test.Columns {
testType(t.DDL + " NOT NULL")
}
return assembleResults(types, cis), nil
}
func assembleResults(types []string, ci []*sql.ColumnType) [][]string {
var results [][]string
header := []string{
"DDL Definition",
".Name",
".DBTypeName",
".Nullable",
".DecimalSize",
".Length",
".ScanType",
}
results = append(results, header)
for i := range ci {
nullable, nullableok := ci[i].Nullable()
nullableStr := "-"
if nullableok {
nullableStr = strconv.FormatBool(nullable)
}
precision, scale, decsizeok := ci[i].DecimalSize()
decsizeStr := "-"
if decsizeok {
decsizeStr = fmt.Sprintf("(%s,%s)", formatInt64(precision), formatInt64(scale))
}
length, lengthok := ci[i].Length()
lengthStr := "-"
if lengthok {
lengthStr = formatInt64(length)
}
scantype := ci[i].ScanType()
scantypeStr := "nil"
if scantype != nil {
scantypeStr = scantype.String()
}
row := []string{
// strconv.Quote(types[i]),
types[i],
ci[i].Name(),
ci[i].DatabaseTypeName(),
nullableStr,
decsizeStr,
lengthStr,
scantypeStr,
}
results = append(results, row)
}
return results
}
func formatInt64(n int64) string {
if n == math.MaxInt64 {
return "MaxInt64"
} else {
return strconv.FormatInt(n, 10)
}
}
func localFilename(filename string) string {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return wd + filename
}
func exec(db *sql.DB, ddl string) error {
// log.Printf("executing %s", ddl)
_, err := db.Exec(ddl)
if err != nil {
log.Printf("db.Exec error %v for %s", err, ddl)
return err
}
// log.Println("executed ok")
return nil
}
// const unmistakableChars = "23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz"
const unmistakableChars = "23456789abcdefghijkmnopqrstuvwxyz"
func randomString(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, n)
for i := range b {
b[i] = unmistakableChars[r.Int63()%int64(len(unmistakableChars))]
}
return string(b)
}