This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
/
main.go
219 lines (196 loc) · 4.4 KB
/
main.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
// Copyright 2014 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command ql is a utility to explore a database, prototype a schema or test
// drive a query, etc.
//
// Installation:
//
// $ go get github.com/cznic/ql/ql
//
// Usage:
//
// ql [-db name] [-schema regexp] [-tables regexp] [-fld] statement_list
//
// Options:
//
// -db name Name of the database to use. Defaults to "ql.db".
// If the DB file does not exists it is created automatically.
//
// -schema re If re != "" show the CREATE statements of matching tables and exit.
//
// -tables re If re != "" show the matching table names and exit.
//
// -fld First row of a query result set will show field names.
//
// statement_list QL statements to execute.
// If no non flag arguments are present, ql reads from stdin.
// The list is wrapped into an automatic transaction.
//
// -t Report and measure time to execute, including creating/opening and closing the DB.
//
// Example:
//
// $ ql 'create table t (i int, s string)'
// $ ql << EOF
// > insert into t values
// > (1, "a"),
// > (2, "b"),
// > (3, "c"),
// > EOF
// $ ql 'select * from t'
// 3, "c"
// 2, "b"
// 1, "a"
// $ ql -fld 'select * from t where i != 2 order by s'
// "i", "s"
// 1, "a"
// 3, "c"
// $
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/cznic/ql"
)
func str(data []interface{}) string {
a := make([]string, len(data))
for i, v := range data {
switch x := v.(type) {
case string:
a[i] = fmt.Sprintf("%q", x)
default:
a[i] = fmt.Sprint(x)
}
}
return strings.Join(a, ", ")
}
func main() {
if err := do(); err != nil {
log.Fatal(err)
}
}
func do() (err error) {
oDB := flag.String("db", "ql.db", "The DB file to open. It'll be created if missing.")
oFlds := flag.Bool("fld", false, "Show recordset's field names.")
oSchema := flag.String("schema", "", "If non empty, show the CREATE statements of matching tables and exit.")
oTables := flag.String("tables", "", "If non empty, list matching table names and exit.")
oTime := flag.Bool("t", false, "Measure and report time to execute the statement(s) including DB create/open/close.")
flag.Parse()
t0 := time.Now()
if *oTime {
defer func() {
fmt.Fprintf(os.Stderr, "%s\n", time.Since(t0))
}()
}
db, err := ql.OpenFile(*oDB, &ql.Options{CanCreate: true})
if err != nil {
return err
}
defer func() {
ec := db.Close()
switch {
case ec != nil && err != nil:
log.Println(ec)
case ec != nil:
err = ec
}
}()
if pat := *oSchema; pat != "" {
re, err := regexp.Compile(pat)
if err != nil {
return err
}
nfo, err := db.Info()
if err != nil {
return err
}
r := []string{}
for _, ti := range nfo.Tables {
if !re.MatchString(ti.Name) {
continue
}
a := []string{}
for _, ci := range ti.Columns {
a = append(a, fmt.Sprintf("%s %s", ci.Name, ci.Type))
}
r = append(r, fmt.Sprintf("CREATE TABLE %s (%s);", ti.Name, strings.Join(a, ", ")))
}
sort.Strings(r)
if len(r) != 0 {
fmt.Println(strings.Join(r, "\n"))
}
return nil
}
if pat := *oTables; pat != "" {
re, err := regexp.Compile(pat)
if err != nil {
return err
}
nfo, err := db.Info()
if err != nil {
return err
}
r := []string{}
for _, ti := range nfo.Tables {
if !re.MatchString(ti.Name) {
continue
}
r = append(r, ti.Name)
}
sort.Strings(r)
if len(r) != 0 {
fmt.Println(strings.Join(r, "\n"))
}
return nil
}
var src string
switch n := flag.NArg(); n {
case 0:
b, err := ioutil.ReadAll(bufio.NewReader(os.Stdin))
if err != nil {
return err
}
src = string(b)
default:
a := make([]string, n)
for i := range a {
a[i] = flag.Arg(i)
}
src = strings.Join(a, " ")
}
src = "BEGIN TRANSACTION; " + src + "; COMMIT;"
l, err := ql.Compile(src)
if err != nil {
log.Println(src)
return err
}
rs, i, err := db.Execute(ql.NewRWCtx(), l)
if err != nil {
a := strings.Split(strings.TrimSpace(fmt.Sprint(l)), "\n")
return fmt.Errorf("%v: %s", err, a[i])
}
if len(rs) == 0 {
return
}
switch {
case l.IsExplainStmt():
return rs[len(rs)-1].Do(*oFlds, func(data []interface{}) (bool, error) {
fmt.Println(data[0])
return true, nil
})
default:
return rs[len(rs)-1].Do(*oFlds, func(data []interface{}) (bool, error) {
fmt.Println(str(data))
return true, nil
})
}
}