-
Notifications
You must be signed in to change notification settings - Fork 413
/
mysql.go
98 lines (80 loc) · 2.32 KB
/
mysql.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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"strings"
"time"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql"
"github.com/ThreeDotsLabs/watermill/message"
)
type mysqlUser struct {
ID int64
User string
FirstName string
LastName string
CreatedAt time.Time
}
type mysqlSchemaAdapter struct {
sql.DefaultMySQLSchema
}
func (m mysqlSchemaAdapter) SchemaInitializingQueries(topic string) []sql.Query {
createQuery := `
CREATE TABLE IF NOT EXISTS ` + topic + ` (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
user VARCHAR(36) NOT NULL,
first_name VARCHAR(36) NOT NULL,
last_name VARCHAR(36) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
`
return []sql.Query{{Query: createQuery}}
}
func (m mysqlSchemaAdapter) InsertQuery(topic string, msgs message.Messages) (sql.Query, error) {
insertQuery := fmt.Sprintf(
`INSERT INTO %s (user, first_name, last_name, created_at) VALUES %s`,
topic,
strings.TrimRight(strings.Repeat(`(?,?,?,?),`, len(msgs)), ","),
)
var args []interface{}
for _, msg := range msgs {
user := mysqlUser{}
decoder := gob.NewDecoder(bytes.NewBuffer(msg.Payload))
err := decoder.Decode(&user)
if err != nil {
return sql.Query{}, err
}
args = append(args, user.User, user.FirstName, user.LastName, user.CreatedAt)
}
return sql.Query{Query: insertQuery, Args: args}, nil
}
func (m mysqlSchemaAdapter) SelectQuery(topic string, consumerGroup string, offsetsAdapter sql.OffsetsAdapter) sql.Query {
nextOffsetQuery := offsetsAdapter.NextOffsetQuery(topic, consumerGroup)
selectQuery := `
SELECT id, user, first_name, last_name, created_at FROM ` + topic + `
WHERE
id > (` + nextOffsetQuery.Query + `)
ORDER BY
id ASC
LIMIT 1`
return sql.Query{Query: selectQuery, Args: nextOffsetQuery.Args}
}
func (m mysqlSchemaAdapter) UnmarshalMessage(row sql.Scanner) (_ sql.Row, err error) {
user := mysqlUser{}
err = row.Scan(&user.ID, &user.User, &user.FirstName, &user.LastName, &user.CreatedAt)
if err != nil {
return sql.Row{}, err
}
var payload bytes.Buffer
encoder := gob.NewEncoder(&payload)
err = encoder.Encode(user)
if err != nil {
return sql.Row{}, err
}
msg := message.NewMessage(watermill.NewULID(), payload.Bytes())
return sql.Row{
Offset: user.ID,
Msg: msg,
}, nil
}