forked from thda/tds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsql.go
415 lines (369 loc) · 10.4 KB
/
gsql.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// test package
// not for production use
package main
import (
"bufio"
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io"
"net/url"
"os"
"os/signal"
"os/user"
"regexp"
"strings"
"syscall"
"github.com/neweric2021/tds"
"github.com/xo/tblfmt"
"github.com/chzyer/readline"
)
var (
version = "0.01"
noHeader = false
echoInput = false
noPSInInput = false
printVersion = false
chained = false
packetSize = 512
terminator = ";|^go"
database = "master"
hostname string
inputFile string
charset string
loginTimeout = 60
outputFile string
password string
columnSeparator = " "
server string
commandTimeout = 0
pageSize = 3000
userName string
locale string
width int
tlsEnable = false //Enforce TLS use
tlsHostname string //Remote hostname to validate against SANs
tlsSkipValidation = false //Skip TLS validation - accepts any TLS certificate
tlsCAFile string //Path to CA file to validate server certificate against
theme = "UtfCompact"
outFormat = "table"
re *regexp.Regexp
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: example -stderrthreshold=[INFO|WARN|FATAL] -log_dir=[string]\n")
flag.PrintDefaults()
os.Exit(2)
}
func init() {
hostname, _ = os.Hostname()
flag.Usage = usage
flag.BoolVar(&noHeader, "b", false, "disable headers")
flag.BoolVar(&echoInput, "e", false, "print commands before execution")
flag.BoolVar(&noPSInInput, "n", false, "no prompt is printed when displaying commands")
flag.BoolVar(&printVersion, "v", false, "print version and exit")
flag.BoolVar(&chained, "Y", false, "use chained mode. Can break lots of stored procedures")
flag.IntVar(&packetSize, "A", 0, "custom network packet size. Zero to let the server handle it.")
flag.StringVar(&terminator, "c", ";|^go", "the terminator used to determine the end of a command. Can contain regex.")
flag.StringVar(&database, "D", database, "database to use.")
flag.StringVar(&hostname, "H", "system hostname", "client's host name to send to the server.")
flag.StringVar(&inputFile, "i", "/gsqlnone/", "file to read commands from")
flag.StringVar(&charset, "J", charset, "character set")
flag.StringVar(&theme, "T", theme, "display theme, can be ASCIICompact or UtfCompact")
flag.IntVar(&loginTimeout, "l", 0, "login Timeout")
flag.StringVar(&outputFile, "o", "/gsqlnone/", "file to output to")
flag.StringVar(&password, "P", "none", "password")
flag.IntVar(&pageSize, "p", pageSize, "paging size")
flag.StringVar(&columnSeparator, "s", columnSeparator, "column separator")
flag.StringVar(&server, "S", " ", "host:port")
flag.IntVar(&commandTimeout, "t", 0, "command Timeout")
flag.IntVar(&width, "w", 0, "line width")
flag.StringVar(&userName, "U", "none", "user name")
flag.BoolVar(&tlsEnable, "tls-enable", tlsEnable, "Enforce TLS use")
flag.StringVar(&tlsHostname, "tls-hostname", " ", "Remote hostname to validate against SANs")
flag.BoolVar(&tlsSkipValidation, "tls-skip-validation", tlsSkipValidation, "Skip TLS validation - accepts any TLS certificate")
flag.StringVar(&tlsCAFile, "tls-ca-file", " ", "Path to CA file to validate server certificate against")
flag.StringVar(&locale, "z", "none", "locale name")
flag.StringVar(&outFormat, "f", outFormat, "output format. Can be 'table','json', or 'csv'")
flag.Parse()
re = regexp.MustCompile("(" + terminator + ")\n?$")
// check for mandatory parameters
if userName == "" || server == "" {
fmt.Fprintf(os.Stderr, "usage: example -stderrthreshold=[INFO|WARN|FATAL] -log_dir=[string]\n")
flag.PrintDefaults()
os.Exit(1)
}
}
// build the connection string
func buildCnxStr() string {
// build the url
v := url.Values{}
if chained {
v.Set("mode", "chained")
}
if packetSize != 0 {
v.Set("packetSize", fmt.Sprintf("%d", packetSize))
}
v.Set("hostname", hostname)
v.Set("readTimeout", "10")
if charset != "" {
v.Set("charset", charset)
}
return "tds://" + url.QueryEscape(userName) + ":" + url.QueryEscape(password) +
"@" + server + "/" + url.QueryEscape(database) + "?" + v.Encode()
}
// find the string terminator in a line and add it to the current batch if needed
func processLine(terminator string, line string, batch string) (batchOut string, found bool) {
// continue till we get a the terminator
if match := re.MatchString(line); !match {
if batch == "" {
batchOut = line
} else {
// add the line to the batch
batchOut = batch + "\n" + line
}
return batchOut, false
}
return batch + re.ReplaceAllString(line, ""), true
}
type SQLBatchReader interface {
ReadBatch(terminator string) (batch string, err error)
Close() error
}
type fileBatchReader struct {
io.ReadCloser
scanner *bufio.Reader
w *bufio.Writer
}
// get an instance of readline with the proper settings
func newFileBatchReader(inputFile string, w *bufio.Writer) (r *fileBatchReader, err error) {
r = &fileBatchReader{w: w}
if inputFile == "-" {
r.ReadCloser = os.Stdin
} else if r.ReadCloser, err = os.Open(inputFile); err != nil {
return nil, err
}
r.scanner = bufio.NewReader(r.ReadCloser)
return r, nil
}
func (r *fileBatchReader) ReadBatch(terminator string) (batch string, err error) {
found := false
lineNo := 1
batch = ""
for {
line, err := r.scanner.ReadString('\n')
if err != nil && (err != io.EOF || line == "") {
return batch, err
}
batch, found = processLine(terminator, line, batch)
if echoInput {
fmt.Fprintf(r.w, "%d> %s", lineNo, line)
}
lineNo++
// found the separator
if found {
lineNo = 1
return batch, nil
}
}
}
type readLineBatchReader struct {
*readline.Instance
server string
conn *sql.DB
}
func (r *readLineBatchReader) ReadBatch(terminator string) (batch string, err error) {
found := false
lineNo := 1
for {
var prompt string
row := r.conn.QueryRow("select @@servername")
if err == nil {
row.Scan(&r.server)
}
prompt = fmt.Sprintf("%d $ ", lineNo)
if r.server != "" {
prompt = fmt.Sprintf("%s %d $ ", r.server, lineNo)
}
r.SetPrompt(prompt)
line, err := r.Readline()
if err == readline.ErrInterrupt {
lineNo = 1
batch = ""
continue
}
if err != nil {
return "", err
}
batch, found = processLine(terminator, line, batch)
if found {
lineNo = 1
r.SaveHistory(batch)
return batch, nil
}
lineNo++
}
}
// get an instance of readline with the proper settings
func newReadLineBatchReader(conn *sql.DB) (SQLBatchReader, error) {
usr, _ := user.Current()
rl, err := readline.NewEx(&readline.Config{
Prompt: "$ ",
HistoryFile: usr.HomeDir + "/.gsql_history.txt",
DisableAutoSaveHistory: true,
})
rl.SetPrompt("1> ")
if err != nil {
return nil, fmt.Errorf("newReadLine: error while initiating readline object (%s)", err)
}
return &readLineBatchReader{Instance: rl, conn: conn}, err
}
// Result is the struct to json encode
type Result struct {
Messages string `json:"messages"`
Results json.RawMessage `json:"results"`
Duration uint64 `json:"duration"`
Error string `json:"error"`
ReturnStatus int `json:"return_status"`
}
func main() {
// defer profile.Start(profile.CPUProfile).Stop()
var batch string
var r SQLBatchReader
var w *bufio.Writer
var mb strings.Builder // message buffer
mo := bufio.NewWriter(os.Stdout)
if outFormat == "json" {
mo = bufio.NewWriter(&mb)
}
// connect
conn, err := sql.Open("tds", buildCnxStr())
if err != nil {
fmt.Println("failed to connect: ", err)
os.Exit(1)
}
// print showplan messages and all
conn.Driver().(tds.ErrorHandler).SetErrorhandler(func(m tds.SybError) bool {
if m.Severity == 10 {
if (m.MsgNumber >= 3612 && m.MsgNumber <= 3615) ||
(m.MsgNumber >= 6201 && m.MsgNumber <= 6299) ||
(m.MsgNumber >= 10201 && m.MsgNumber <= 10299) {
mo.WriteString(m.Message)
} else {
mo.WriteString(strings.TrimRight(m.Message, "\n"))
}
}
if m.Severity > 10 {
mo.WriteString(m.Error())
}
return m.Severity > 10
})
// open outpout
switch outputFile {
default:
var f io.WriteCloser
var _, err = os.Stat(outputFile)
if os.IsNotExist(err) {
// not found...
f, err = os.Create(outputFile)
} else {
// truncate the file if it exists
if err = os.Truncate(outputFile, 0); err == nil {
f, err = os.OpenFile(outputFile, os.O_WRONLY, os.ModePerm)
}
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer f.Close()
w = bufio.NewWriter(f)
defer w.Flush()
case "/gsqlnone/":
w = bufio.NewWriter(os.Stdout)
defer w.Flush()
}
// open input
switch inputFile {
case "/gsqlnone/":
// get readline instance
r, err = newReadLineBatchReader(conn)
default:
r, err = newFileBatchReader(inputFile, w)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer r.Close()
input:
for {
batch, err = r.ReadBatch(terminator)
if err != nil {
if err != io.EOF {
fmt.Println(err)
}
break
}
// handle cancelation
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal)
done := make(chan struct{})
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-c:
cancel()
<-done
case <-done:
}
}()
// send query
rows, err := conn.QueryContext(ctx, batch)
// flush message buffer
mo.Flush()
select {
case <-done:
case done <- struct{}{}:
}
switch outFormat {
case "json":
if err != nil {
out, _ := json.Marshal(&Result{
Messages: mb.String(), ReturnStatus: 1, Error: err.Error()})
fmt.Fprintln(w, string(out))
mb = strings.Builder{}
continue input
}
for {
rb := strings.Builder{}
tblfmt.EncodeJSON(&rb, rows)
results := rb.String()
if results == "" {
results = "{}"
}
if !(results == "{}" && mb.String() == "") {
out, _ := json.Marshal(&Result{Results: json.RawMessage(results),
Messages: mb.String()})
fmt.Fprintln(w, string(out))
}
mb = strings.Builder{}
if !rows.NextResultSet() {
continue input
}
}
default:
if err != nil {
// SQL errors are printed by the error handler
if _, ok := err.(tds.SybError); !ok {
fmt.Println(err)
}
continue input
}
tblfmt.EncodeAll(w, rows, map[string]string{"format": "aligned", "border": "2",
"unicode_border_linestyle": "single", "linestyle": "unicode"})
}
rows.Close()
}
}