-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (158 loc) · 4.38 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
var migrations = []string{
`CREATE TABLE IF NOT EXISTS events_log (
code text not null,
description text not null,
metadata json not null,
created_at timestamp(6) not null default current_timestamp(6)
)`,
}
// Event is a simple struct that represents an event.
type Event struct {
Code string
Description string
Metadata Metadata
CreatedAt time.Time
}
func (e Event) String() string {
return fmt.Sprintf("Code: %s\nDescription: %s\nMetadata: %v\nCreatedAt: %v\n", e.Code, e.Description, e.Metadata, e.CreatedAt.Format(time.RFC3339))
}
// KV is a simple key value struct
type KV struct {
Key string `json:"key"`
Val string `json:"val"`
}
// Value implements the driver Valuer interface for KV.
func (kv KV) Value() (driver.Value, error) {
return json.Marshal(kv)
}
// Scan implements the Scanner interface for KV.
func (kv *KV) Scan(src any) error {
switch src := src.(type) {
case []byte:
return json.Unmarshal(src, kv)
default:
return fmt.Errorf("incompatible type for KV (%T)", src)
}
}
// Metadata is a custom type that implements the driver.Valuer and sql.Scanner
type Metadata []KV
// Value implements the driver Valuer interface.
func (m Metadata) Value() (driver.Value, error) {
return json.Marshal(m)
}
// Scan implements the Scanner interface.
func (m *Metadata) Scan(src any) error {
switch src := src.(type) {
case []byte:
return json.Unmarshal(src, m)
default:
return fmt.Errorf("incompatible type for Metadata (%T)", src)
}
}
func main() {
db := setupDB()
// Create some events
events := []Event{
{
Code: "golangdorset.meetup.begin",
Description: "Golang Dorset Meetup has begun.",
Metadata: Metadata{
KV{Key: "location", Val: "Bournemouth"},
KV{Key: "attendees", Val: "10"},
KV{Key: "organiser", Val: "Golang Dorset"},
KV{Key: "talks", Val: "2"},
},
},
{
Code: "golangdorset.meetup.end",
Description: "Golang Dorset Meetup has finished.",
Metadata: Metadata{
KV{Key: "location", Val: "Bournemouth"},
KV{Key: "attendees", Val: "10"},
KV{Key: "organiser", Val: "Golang Dorset"},
KV{Key: "talks", Val: "2"},
},
},
}
// Insert the events into the database
const stmt = `
INSERT INTO events_log (code, description, metadata)
VALUES (?, ?, ?)`
for _, event := range events {
_, err := db.Exec(stmt, event.Code, event.Description, event.Metadata)
if err != nil {
log.Fatalf("Error inserting event: %v", err)
}
}
// Query the database for the first event found
const query = `
SELECT code, description, metadata, created_at
FROM events_log
WHERE code = ?
LIMIT 1`
var e Event
row := db.QueryRow(query, "golangdorset.meetup.begin")
if err := row.Scan(&e.Code, &e.Description, &e.Metadata, &e.CreatedAt); err != nil {
log.Fatalf("Error querying event: %v", err)
}
// Print the event
fmt.Printf("%+v\n", e)
// Query the database for all events where the location is Discord
const query2 = `
SELECT code, description, metadata, created_at
FROM events_log
WHERE JSON_CONTAINS(metadata, ?)`
rows, err := db.Query(query2, KV{Key: "location", Val: "Bournemouth"})
if err != nil {
log.Fatalf("Error querying events: %v", err)
}
defer rows.Close()
// Print the events
for rows.Next() {
var e Event
if err := rows.Scan(&e.Code, &e.Description, &e.Metadata, &e.CreatedAt); err != nil {
log.Printf("Error scanning event: %v", err)
}
fmt.Printf("%+v\n", e)
}
if err := rows.Err(); err != nil {
log.Printf("Error iterating over events: %v", err)
}
}
func setupDB() *sql.DB {
db, err := sql.Open("mysql", "root:password@tcp(0.0.0.0:3306)/golangdorset?charset=utf8&parseTime=true")
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
if err := db.Ping(); err != nil {
log.Fatalf("Error pinging database: %v", err)
}
for i, v := range migrations {
_, err := db.Exec(v)
if err != nil {
log.Fatalf("Error executing migration %d: %v", i, err)
}
}
return db
}
// insertEvent inserts an event into the database.
func insertEvent(db *sql.DB, event Event) error {
const stmt = `
INSERT INTO events_log (code, description, metadata)
VALUES (?, ?, ?)`
_, err := db.Exec(stmt, event.Code, event.Description, event.Metadata)
if err != nil {
return fmt.Errorf("error inserting event: %v", err)
}
return nil
}