Debian Codegraph @

_example/json/json.go

raw
1 package main
2
3 import (
4 "database/sql"
5 "database/sql/driver"
6 "encoding/json"
7 "fmt"
8 _ "github.com/mattn/go-sqlite3"
9 "log"
10 "os"
11 )
12
13 type Tag struct {
14 Name string `json:"name"`
15 Country string `json:"country"`
16 }
17
18 func (t *Tag) Scan(value interface{}) error {
19 switch v := value.(type) {
20 case nil:
21 *t = Tag{}
22 return nil
23 case string:
24 return json.Unmarshal([]byte(v), t)
25 case []byte:
26 return json.Unmarshal(v, t)
27 default:
28 return fmt.Errorf("unsupported type %T for Tag", value)
29 }
30 }
31
32 func (t *Tag) Value() (driver.Value, error) {
33 b, err := json.Marshal(t)
34 return string(b), err
35 }
36
37 func main() {
38 os.Remove("./foo.db")
39
40 db, err := sql.Open("sqlite3", "./foo.db")
41 if err != nil {
42 log.Fatal(err)
43 }
44 defer db.Close()
45
46 // Using a json-typed column
47 // Verify type: `create table foo (tag text) strict`
48 _, err = db.Exec(`create table foo (tag json)`)
49 if err != nil {
50 log.Fatal(err)
51 }
52
53 stmt, err := db.Prepare("insert into foo(tag) values(?)")
54 if err != nil {
55 log.Fatal(err)
56 }
57 defer stmt.Close()
58 _, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
59 if err != nil {
60 log.Fatal(err)
61 }
62 _, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
63 if err != nil {
64 log.Fatal(err)
65 }
66
67 var country string
68 err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
69 if err != nil {
70 log.Fatal(err)
71 }
72 fmt.Println(country)
73
74 var tag Tag
75 err = db.QueryRow("select tag from foo where tag->>'name' = 'mattn'").Scan(&tag)
76 if err != nil {
77 log.Fatal(err)
78 }
79
80 fmt.Println(tag.Name)
81
82 tag.Country = "日本"
83 _, err = db.Exec(`update foo set tag = ? where tag->>'name' == 'mattn'`, &tag)
84 if err != nil {
85 log.Fatal(err)
86 }
87
88 err = db.QueryRow("select tag->>'country' from foo where tag->>'name' = 'mattn'").Scan(&country)
89 if err != nil {
90 log.Fatal(err)
91 }
92 fmt.Println(country)
93
94 // Using a jsonb-typed column
95 // Verify type: `create table bar (tag blob) strict`
96 _, err = db.Exec(`create table bar (tag jsonb)`)
97 if err != nil {
98 log.Fatal(err)
99 }
100
101 stmt, err = db.Prepare("insert into bar(tag) values(jsonb(?))")
102 if err != nil {
103 log.Fatal(err)
104 }
105 defer stmt.Close()
106 _, err = stmt.Exec(`{"name": "mattn", "country": "japan"}`)
107 if err != nil {
108 log.Fatal(err)
109 }
110 _, err = stmt.Exec(`{"name": "michael", "country": "usa"}`)
111 if err != nil {
112 log.Fatal(err)
113 }
114
115 err = db.QueryRow("select tag->>'country' from bar where tag->>'name' = 'mattn'").Scan(&country)
116 if err != nil {
117 log.Fatal(err)
118 }
119 fmt.Println(country)
120
121 err = db.QueryRow("select json(tag) from bar where tag->>'name' = 'mattn'").Scan(&tag)
122 if err != nil {
123 log.Fatal(err)
124 }
125
126 fmt.Println(tag.Name)
127
128 tag.Country = "日本"
129 _, err = db.Exec(`update bar set tag = jsonb(?) where tag->>'name' == 'mattn'`, &tag)
130 if err != nil {
131 log.Fatal(err)
132 }
133
134 err = db.QueryRow("select tag->>'country' from bar where tag->>'name' = 'mattn'").Scan(&country)
135 if err != nil {
136 log.Fatal(err)
137 }
138 fmt.Println(country)
139 }