forked from cockroachdb/cockroach
/
sql_util.go
393 lines (353 loc) · 9.63 KB
/
sql_util.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Marc berhault (marc@cockroachlabs.com)
package cli
import (
"bytes"
"database/sql/driver"
"fmt"
"io"
"strings"
"text/tabwriter"
"unicode"
"unicode/utf8"
"golang.org/x/net/context"
"github.com/olekukonko/tablewriter"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log"
"github.com/cockroachdb/pq"
)
type sqlConnI interface {
driver.Conn
driver.Execer
driver.Queryer
Next() (driver.Rows, error)
}
type sqlConn struct {
url string
conn sqlConnI
}
func (c *sqlConn) ensureConn() error {
if c.conn == nil {
conn, err := pq.Open(c.url)
if err != nil {
return err
}
c.conn = conn.(sqlConnI)
}
return nil
}
func (c *sqlConn) Exec(query string, args []driver.Value) error {
if err := c.ensureConn(); err != nil {
return err
}
_, err := c.conn.Exec(query, args)
return err
}
func (c *sqlConn) Query(query string, args []driver.Value) (*sqlRows, error) {
if err := c.ensureConn(); err != nil {
return nil, err
}
rows, err := c.conn.Query(query, args)
if err == driver.ErrBadConn {
c.Close()
}
if err != nil {
return nil, err
}
return &sqlRows{rows: rows.(sqlRowsI), conn: c}, nil
}
func (c *sqlConn) Next() (*sqlRows, error) {
if c.conn == nil {
return nil, driver.ErrBadConn
}
rows, err := c.conn.Next()
if err == driver.ErrBadConn {
c.Close()
}
if err != nil {
return nil, err
}
return &sqlRows{rows: rows.(sqlRowsI), conn: c}, nil
}
func (c *sqlConn) QueryRow(query string, args []driver.Value) ([]driver.Value, error) {
rows, err := makeQuery(query, args...)(c)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
vals := make([]driver.Value, len(rows.Columns()))
err = rows.Next(vals)
return vals, err
}
func (c *sqlConn) Close() {
if c.conn != nil {
err := c.conn.Close()
if err != nil && err != driver.ErrBadConn {
log.Info(context.TODO(), err)
}
c.conn = nil
}
}
type sqlRowsI interface {
driver.Rows
Result() driver.Result
Tag() string
}
type sqlRows struct {
rows sqlRowsI
conn *sqlConn
}
func (r *sqlRows) Columns() []string {
return r.rows.Columns()
}
func (r *sqlRows) Result() driver.Result {
return r.rows.Result()
}
func (r *sqlRows) Tag() string {
return r.rows.Tag()
}
func (r *sqlRows) Close() error {
err := r.rows.Close()
if err == driver.ErrBadConn {
r.conn.Close()
}
return err
}
// Next populates values with the next row of results. []byte values are copied
// so that subsequent calls to Next and Close do not mutate values. This
// makes it slower than theoretically possible but the safety concerns
// (since this is unobvious and unexpected behavior) outweigh.
func (r *sqlRows) Next(values []driver.Value) error {
err := r.rows.Next(values)
if err == driver.ErrBadConn {
r.conn.Close()
}
for i, v := range values {
if b, ok := v.([]byte); ok {
values[i] = append([]byte{}, b...)
}
}
return err
}
func makeSQLConn(url string) *sqlConn {
return &sqlConn{
url: url,
}
}
func makeSQLClient() (*sqlConn, error) {
sqlURL := connURL
if len(connURL) == 0 {
u, err := sqlCtx.PGURL(connUser)
if err != nil {
return nil, err
}
u.Path = connDBName
sqlURL = u.String()
}
return makeSQLConn(sqlURL), nil
}
type queryFunc func(conn *sqlConn) (*sqlRows, error)
func nextResult(conn *sqlConn) (*sqlRows, error) {
return conn.Next()
}
func makeQuery(query string, parameters ...driver.Value) queryFunc {
return func(conn *sqlConn) (*sqlRows, error) {
// driver.Value is an alias for interface{}, but must adhere to a restricted
// set of types when being passed to driver.Queryer.Query (see
// driver.IsValue). We use driver.DefaultParameterConverter to perform the
// necessary conversion. This is usually taken care of by the sql package,
// but we have to do so manually because we're talking directly to the
// driver.
for i := range parameters {
var err error
parameters[i], err = driver.DefaultParameterConverter.ConvertValue(parameters[i])
if err != nil {
return nil, err
}
}
return conn.Query(query, parameters)
}
}
// runQuery takes a 'query' with optional 'parameters'.
// It runs the sql query and returns a list of columns names and a list of rows.
func runQuery(conn *sqlConn, fn queryFunc, pretty bool) ([]string, [][]string, string, error) {
rows, err := fn(conn)
if err != nil {
return nil, nil, "", err
}
defer func() { _ = rows.Close() }()
return sqlRowsToStrings(rows, pretty)
}
// runQueryAndFormatResults takes a 'query' with optional 'parameters'.
// It runs the sql query and writes output to 'w'.
func runQueryAndFormatResults(
conn *sqlConn, w io.Writer, fn queryFunc, pretty bool,
) error {
for {
cols, allRows, result, err := runQuery(conn, fn, pretty)
if err != nil {
if err == pq.ErrNoMoreResults {
return nil
}
return err
}
printQueryOutput(w, cols, allRows, result, pretty)
fn = nextResult
}
}
// sqlRowsToStrings turns 'rows' into a list of rows, each of which
// is a list of column values.
// 'rows' should be closed by the caller.
// It returns the header row followed by all data rows.
// If both the header row and list of rows are empty, it means no row
// information was returned (eg: statement was not a query).
// If pretty is true, then more characters are not escaped.
func sqlRowsToStrings(rows *sqlRows, pretty bool) ([]string, [][]string, string, error) {
srcCols := rows.Columns()
cols := make([]string, len(srcCols))
for i, c := range srcCols {
cols[i] = formatVal(c, pretty, false)
}
var allRows [][]string
var vals []driver.Value
if len(cols) > 0 {
vals = make([]driver.Value, len(cols))
}
for {
err := rows.Next(vals)
if err == io.EOF {
break
}
if err != nil {
return nil, nil, "", err
}
rowStrings := make([]string, len(cols))
for i, v := range vals {
rowStrings[i] = formatVal(v, pretty, pretty)
}
allRows = append(allRows, rowStrings)
}
result := rows.Result()
tag := rows.Tag()
switch tag {
case "":
tag = "OK"
case "DELETE", "INSERT", "UPDATE":
if n, err := result.RowsAffected(); err == nil {
tag = fmt.Sprintf("%s %d", tag, n)
}
}
return cols, allRows, tag, nil
}
// expandTabsAndNewLines ensures that multi-line row strings that may
// contain tabs are properly formatted: tabs are expanded to spaces,
// and newline characters are marked visually. Marking newline
// characters is especially important in single-column results where
// the underlying TableWriter would not otherwise show the difference
// between one multi-line row and two one-line rows.
func expandTabsAndNewLines(s string) string {
var buf bytes.Buffer
// 4-wide columns, 1 character minimum width.
w := tabwriter.NewWriter(&buf, 4, 0, 1, ' ', 0)
fmt.Fprint(w, strings.Replace(s, "\n", "\n", -1))
_ = w.Flush()
return buf.String()
}
// printQueryOutput takes a list of column names and a list of row contents
// writes a pretty table to 'w', or "OK" if empty.
func printQueryOutput(
w io.Writer, cols []string, allRows [][]string, tag string, pretty bool,
) {
if len(cols) == 0 {
// This operation did not return rows, just show the tag.
fmt.Fprintln(w, tag)
return
}
if pretty {
// Initialize tablewriter and set column names as the header row.
table := tablewriter.NewWriter(w)
table.SetAutoFormatHeaders(false)
table.SetAutoWrapText(false)
table.SetHeader(cols)
for _, row := range allRows {
for i, r := range row {
row[i] = expandTabsAndNewLines(r)
}
table.Append(row)
}
table.Render()
nRows := len(allRows)
fmt.Fprintf(w, "(%d row%s)\n", nRows, util.Pluralize(int64(nRows)))
} else {
if len(cols) == 0 {
// No result selected, inform the user.
fmt.Fprintln(w, tag)
} else {
// Some results selected, inform the user about how much data to expect.
fmt.Fprintf(w, "%d row%s\n", len(allRows),
util.Pluralize(int64(len(allRows))))
// Then print the results themselves.
fmt.Fprintln(w, strings.Join(cols, "\t"))
for _, row := range allRows {
fmt.Fprintln(w, strings.Join(row, "\t"))
}
}
}
}
func isNotPrintableASCII(r rune) bool { return r < 0x20 || r > 0x7e || r == '"' || r == '\\' }
func isNotGraphicUnicode(r rune) bool { return !unicode.IsGraphic(r) }
func isNotGraphicUnicodeOrTabOrNewline(r rune) bool {
return r != '\t' && r != '\n' && !unicode.IsGraphic(r)
}
func formatVal(
val driver.Value, showPrintableUnicode bool, showNewLinesAndTabs bool,
) string {
switch t := val.(type) {
case nil:
return "NULL"
case string:
if showPrintableUnicode {
pred := isNotGraphicUnicode
if showNewLinesAndTabs {
pred = isNotGraphicUnicodeOrTabOrNewline
}
if utf8.ValidString(t) && strings.IndexFunc(t, pred) == -1 {
return t
}
} else {
if strings.IndexFunc(t, isNotPrintableASCII) == -1 {
return t
}
}
return fmt.Sprintf("%+q", t)
case []byte:
if showPrintableUnicode {
pred := isNotGraphicUnicode
if showNewLinesAndTabs {
pred = isNotGraphicUnicodeOrTabOrNewline
}
if utf8.Valid(t) && bytes.IndexFunc(t, pred) == -1 {
return string(t)
}
} else {
if bytes.IndexFunc(t, isNotPrintableASCII) == -1 {
return string(t)
}
}
return fmt.Sprintf("%+q", t)
}
return fmt.Sprint(val)
}