-
Notifications
You must be signed in to change notification settings - Fork 2
/
testtoast.go
80 lines (66 loc) · 1.39 KB
/
testtoast.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"github.com/pkg/errors"
)
func testtoast(db *sql.DB) (err error) {
_, _, dataParsed, err := loadJsonDataFromDisk()
if err != nil {
return err
}
err = truncate(db, "tbl_test_toast_seed")
if err != nil {
return
}
err = truncate(db, "tbl_test_toast")
if err != nil {
return
}
qa := "INSERT INTO tbl_test_toast_seed(data) VALUES ($1)"
for i := range dataParsed {
fmt.Println("i:", i)
s := make([]any, 10)
for j := 1; j < 10; j += 1 {
s[j] = dataParsed[i+j]
}
dataParsed[i]["additional"] = s
var tmp []byte
tmp, err = json.Marshal(dataParsed[i])
if err != nil {
err = errors.Wrap(err, "fail to marshal data")
return
}
_, err = db.Exec(qa, tmp)
if err != nil {
err = errors.Wrap(err, "fail to run insert a")
return
}
if i >= len(dataParsed)-20 {
break
}
}
qb := `
INSERT INTO tbl_test_toast(title, year, score, data)
SELECT data->>'title'
, CAST(data->>'year' AS INT)
, CAST(data->>'score' AS FLOAT)
, data
FROM tbl_test_toast_seed
`
_, err = db.Exec(qb)
if err != nil {
err = errors.Wrap(err, "fail to run insert b")
return
}
return
}
func truncate(db *sql.DB, table string) (err error) {
_, err = db.Exec(fmt.Sprintf("TRUNCATE %s", table))
if err != nil {
return errors.Wrapf(err, "fail to truncate table '%s'", table)
}
return
}