-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (62 loc) · 1.5 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
package main
import (
"bufio"
"database/sql"
"encoding/csv"
"flag"
"fmt"
"io"
"os"
"github.com/pkg/errors"
// DB drivers
_ "github.com/lib/pq" // postgres
_ "github.com/mattn/go-sqlite3" // sqlite
)
// Run an arbitrary query where the parameter markers in the query should line up
// in order with the values in each line of the CSV file.
//
// Also, don't include a header line in the CSV file.
func main() {
var (
file = flag.String("file", "", "CSV file with data")
connStr = flag.String("conn_str", "", "database connection string")
driver = flag.String("db_driver", "postgres", "the database driver")
query = flag.String("query", "", "the query to execute")
)
flag.Parse()
db, err := sql.Open(*driver, *connStr)
if err != nil {
fmt.Println(errors.Wrap(err, "couldn't connect to database"))
os.Exit(1)
}
f, err := os.Open(*file)
if err != nil {
fmt.Println(errors.Wrap(err, "couldn't open file"))
os.Exit(1)
}
reader := csv.NewReader(bufio.NewReader(f))
i := 1
for {
fmt.Println(i)
line, err := reader.Read()
// we have to convert []string -> []interface for db.Exec, below
vals := make([]interface{}, len(line))
for i, v := range line {
vals[i] = v
}
switch err {
case nil:
_, err := db.Exec(*query, vals...)
if err != nil {
fmt.Println(errors.Wrap(err, "query failed"))
}
case io.EOF:
fmt.Println("finished")
os.Exit(0)
default:
fmt.Println(errors.Wrap(err, "error reading line from CSV file"))
continue
}
i++
}
}