-
Notifications
You must be signed in to change notification settings - Fork 15
/
csvq-replace-io-example.go
106 lines (94 loc) · 2.27 KB
/
csvq-replace-io-example.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
// Usage:
// $ cd $GOPATH/src/github.com/mithrandie/csvq-driver
// $ go build ./example/csvq-driver/csvq-replace-io-example.go
// $ ./csvq-replace-io-example
//
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"strings"
"time"
"github.com/mithrandie/csvq/lib/query"
"github.com/mithrandie/csvq-driver"
"github.com/mithrandie/csvq-driver/example"
)
func main() {
flag.Parse()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
db, err := sql.Open("csvq", "")
if err != nil {
example.ExitWithError(err)
}
defer func() {
if err := db.Close(); err != nil {
example.ExitWithError(err)
}
}()
fmt.Println("######## Opened")
fmt.Printf("\n######## Replace I/O files\n")
data := `
[
{
"id": 1,
"first_name": "Louis",
"country_code": "PS"
},
{
"id": 2,
"first_name": "Sean",
"country_code": "SE"
},
{
"id": 3,
"first_name": "Mildred",
"country_code": "ID"
}
]`
stdin := query.NewInput(strings.NewReader(data))
stdout := query.NewOutput()
outfile := query.NewOutput()
if err := csvq.SetStdin(stdin); err != nil {
panic(err)
}
csvq.SetStdout(stdout)
csvq.SetOutFile(outfile)
queryString := "" +
"SET @@IMPORT_FORMAT TO JSON;" +
"SET @@FORMAT TO JSON;" +
"SET @@PRETTY_PRINT TO TRUE;"
fmt.Printf("\n######## Exec Set Flag Statements: %s\n", queryString)
_, err = db.ExecContext(ctx, queryString)
if err != nil {
example.ExitWithError(err)
}
queryString = "UPDATE stdin SET first_name = 'foo' WHERE id = 2;"
fmt.Printf("\n######## Exec Update Query: %s\n", queryString)
ret, err := db.ExecContext(ctx, queryString)
if err != nil {
example.ExitWithError(err)
}
affected, _ := ret.RowsAffected()
fmt.Printf("RowsAffected: %d\n", affected)
queryString = "SELECT id, first_name, country_code FROM stdin;"
fmt.Printf("\n######## Exec Select Query: %s\n", queryString)
rs, err := db.QueryContext(ctx, queryString)
if err != nil {
example.ExitWithError(err)
}
defer func() {
if err := rs.Close(); err != nil {
println(err.Error())
}
}()
if err := example.ScanUsers(rs); err != nil {
println(err.Error())
}
fmt.Println("\n######## Print Stdout")
fmt.Println(stdout.String())
fmt.Println("\n######## Print OutFile")
fmt.Println(outfile.String())
}