-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathjson.go
129 lines (109 loc) · 2.72 KB
/
json.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
package main
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
)
type Tag struct {
Name string `json:"name"`
Country string `json:"country"`
}
func (t *Tag) Scan(value interface{}) error {
return json.Unmarshal([]byte(value.(string)), t)
}
func (t *Tag) Value() (driver.Value, error) {
b, err := json.Marshal(t)
return string(b), err
}
func main() {
os.Remove("./foo.db")
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Using a json-typed column
// Verify type: `create table foo (tag text) strict`
_, err = db.Exec(`create table foo (tag json)`)
if err != nil {
log.Fatal(err)
}
stmt, err := db.Prepare("insert into foo(tag) values(?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
if err != nil {
log.Fatal(err)
}
var country string
err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)
var tag Tag
err = db.QueryRow("select tag from foo where tag->>'name' = 'mattn'").Scan(&tag)
if err != nil {
log.Fatal(err)
}
fmt.Println(tag.Name)
tag.Country = "日本"
_, err = db.Exec(`update foo set tag = ? where tag->>'name' == 'mattn'`, &tag)
if err != nil {
log.Fatal(err)
}
err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)
// Using a jsonb-typed column
// Verify type: `create table bar (tag blob) strict`
_, err = db.Exec(`create table bar (tag jsonb)`)
if err != nil {
log.Fatal(err)
}
stmt, err = db.Prepare("insert into bar(tag) values(jsonb(?))")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
if err != nil {
log.Fatal(err)
}
err = db.QueryRow("select tag->>'country' from bar where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)
err = db.QueryRow("select json(tag) from bar where tag->>'name' = 'mattn'").Scan(&tag)
if err != nil {
log.Fatal(err)
}
fmt.Println(tag.Name)
tag.Country = "日本"
_, err = db.Exec(`update bar set tag = jsonb(?) where tag->>'name' == 'mattn'`, &tag)
if err != nil {
log.Fatal(err)
}
err = db.QueryRow("select tag->>'country' from bar where tag->>'name' = 'mattn'").Scan(&country)
if err != nil {
log.Fatal(err)
}
fmt.Println(country)
}