/
dsquery.go
219 lines (212 loc) · 5.74 KB
/
dsquery.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
package dataset
import (
"bytes"
"database/sql"
"encoding/csv"
"fmt"
"io"
"path"
"strings"
"time"
)
type DSQuery struct {
CName string `json:"c_name,omitempty"`
Stmt string `json:"stmt,omitempty"`
Pretty bool `json:"pretty,omitempty"`
AsGrid bool `json:"as_grid,omitempty"`
AsCSV bool `json:"csv,omitempty"`
Attributes []string `json:"attributes,omitempty"`
PTIndex bool `json:"pt_index,omitempty"`
ds *Collection
db *sql.DB
}
// MakeGrid takes JSON source holding an array of objects and uses the attribute list to
// render a 2D grid of values where the columns match the attribute name list provided.
// If an attribute is missing a nil is inserted. MakeGrid returns the grid as JSON source
// along with an error value.
func MakeGrid(src []byte, attributes []string) ([]byte, error) {
listOfObjects := []map[string]interface{}{}
if err := JSONUnmarshal(src, &listOfObjects); err != nil {
return nil, err
}
outerArray := []interface{}{}
// Now that we know the column names scan the list of objects and build grid
for _, obj := range listOfObjects {
innerArray := []interface{}{}
for _, attr := range attributes {
if val, ok := obj[attr]; ok {
innerArray = append(innerArray, val)
} else {
innerArray = append(innerArray, nil)
}
}
outerArray = append(outerArray, innerArray)
}
return JSONMarshal(outerArray)
}
// MakeCSV takes JSON source holding an array of objects and uses the attribute list to
// render a CSV file from the list. It returns the CSV content as a byte slice along
// with an error.
func MakeCSV(src []byte, attributes []string) ([]byte, error) {
listOfObjects := []map[string]interface{}{}
if err := JSONUnmarshal(src, &listOfObjects); err != nil {
return nil, err
}
// Write header row based on our attribute list.
buf := []byte{}
out := bytes.NewBuffer(buf)
w := csv.NewWriter(out)
if err := w.Write(attributes); err != nil {
return nil, err
}
// Now that we know the column names scan the list of objects and build grid
for _, obj := range listOfObjects {
innerArray := []string{}
for _, attr := range attributes {
if val, ok := obj[attr]; ok {
switch val.(type) {
case string:
cell := val.(string)
innerArray = append(innerArray, cell)
default:
data, _ := JSONMarshal(val)
innerArray = append(innerArray, fmt.Sprintf("%s", data))
}
} else {
innerArray = append(innerArray, "")
}
}
if err := w.Write(innerArray); err != nil {
return nil, err
}
}
w.Flush()
err := w.Error()
if err != nil {
return nil, err
}
src = out.Bytes()
return src, nil
}
// indexCollection creates a SQL replica of a dataset collection as a SQLite 3
// database called index.db inside the collection's root folder. This allows
// pairtree implementations to be queried using SQLite 3's SQL dialect.
func indexCollection(ds *Collection, index *sql.DB) error {
// Clear the existing index if exists, create a new one.
tName := strings.TrimSuffix(ds.Name, ".ds")
stmt := fmt.Sprintf(`drop table if exists %s;`, tName)
_, err := index.Exec(stmt)
if err != nil {
return fmt.Errorf("stmt:\n%s\n\t%s", stmt, err)
}
stmt = fmt.Sprintf(`create table %s (_key string, src json, updated datetime, created timestamp);`, tName)
_, err = index.Exec(stmt)
if err != nil {
return fmt.Errorf("stmt:\n%s\n\t%s", stmt, err)
}
keys, err := ds.Keys()
if err != nil {
return err
}
tStamp := time.Now()
stmt = fmt.Sprintf(`insert into %s (_key, src, updated, created) values (?, ?, ?, ?)`, tName)
for _, key := range keys {
src, err := ds.ReadJSON(key)
if err != nil {
return err
}
_, err = index.Exec(stmt, key, fmt.Sprintf("%s", src), tStamp, tStamp)
if err != nil {
return fmt.Errorf("stmt:\n%s\n\t$1 = %+v $2 = %+v, %s", stmt, tStamp, tStamp, err)
}
}
return nil
}
func (app *DSQuery) Run(in io.Reader, out io.Writer, eout io.Writer, cName string, stmt string, params []string) error {
app.CName = cName
app.Stmt = stmt
ds, err := Open(cName)
if err != nil {
return err
}
defer ds.Close()
app.ds = ds
if strings.Compare(ds.StoreType, SQLSTORE) == 0 {
if ds.SQLStore == nil {
return fmt.Errorf("sqlstore failed to open")
}
app.db = ds.SQLStore.db
} else {
wPath := cName
if ds.workPath != "" {
wPath = ds.workPath
}
indexDSN := path.Join(wPath, "index.db")
index, err := sql.Open("sqlite", indexDSN)
if err != nil {
return fmt.Errorf("failed to open index %s, %s", indexDSN, err)
}
defer index.Close()
if index == nil {
return fmt.Errorf("index failed to open")
}
if app.PTIndex {
// Index the collection into a SQLite3 database. This would normally used to allow
// dsquery to support querring a pairtree collection.
if err = indexCollection(ds, index); err != nil {
return fmt.Errorf("failed to index %q, %s", cName, err)
}
}
app.db = index
}
var rows *sql.Rows
if len(params) > 0 {
args := []interface{}{}
for _, val := range params {
args = append(args, val)
}
rows, err = app.db.Query(app.Stmt, args...)
} else {
rows, err = app.db.Query(app.Stmt)
}
if err != nil {
return fmt.Errorf("stmt: %s, %s", app.Stmt, err)
}
src := []byte(`[`)
i := 0
for rows.Next() {
// Get our row values
obj := []byte{}
if err := rows.Scan(&obj); err != nil {
return err
}
if i > 0 {
src = append(src, ',')
}
src = append(src, obj...)
i++
}
src = append(src, ']')
err = rows.Err()
if err != nil {
return err
}
if app.AsGrid {
src, err = MakeGrid(src, app.Attributes)
if err != nil {
return err
}
}
if app.AsCSV {
src, err = MakeCSV(src, app.Attributes)
if err != nil {
return err
}
}
if app.Pretty {
fmt.Fprintf(out, "%s\n", JSONIndent(src, "", " "))
return nil
}
fmt.Fprintf(out, "%s\n", src)
return nil
}