forked from augmentable-dev/flite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (52 loc) · 1009 Bytes
/
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
package main
import (
"database/sql"
"encoding/json"
"log"
"os"
_ "github.com/augmentable-dev/flite/internal/sqlite"
_ "github.com/augmentable-dev/flite/pkg/ext"
_ "github.com/mattn/go-sqlite3"
)
func main() {
var query string
if len(os.Args) > 0 {
query = os.Args[1]
}
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatal(err)
}
rows, err := db.Query(query)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
log.Fatal(err)
}
values := make([]interface{}, len(columns))
for i := range values {
values[i] = new(interface{})
}
enc := json.NewEncoder(os.Stdout)
for rows.Next() {
err = rows.Scan(values...)
if err != nil {
log.Fatal(err)
}
dest := make(map[string]interface{})
for i, column := range columns {
dest[column] = *(values[i].(*interface{}))
}
err := enc.Encode(dest)
if err != nil {
log.Fatal(err)
}
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}