/
store.go
146 lines (114 loc) · 4.35 KB
/
store.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
package sqlstore
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/SKF/go-eventsource/v2/eventsource"
"github.com/SKF/go-eventsource/v2/eventsource/stores/sqlstore/driver"
)
type EventDB interface {
Load(ctx context.Context, query string, args []interface{}) ([]eventsource.Record, error)
NewTransaction(ctx context.Context, query string, records ...eventsource.Record) (eventsource.StoreTransaction, error)
}
type PGXStore interface {
eventsource.Store
WithPostgresNotify() PGXStore
}
type store struct {
db EventDB
tableName string
}
var (
columns = []column{columnAggregateID, columnSequenceID, columnCreatedAt, columnUserID, columnType, columnData}
saveSQL = "INSERT INTO %s (aggregate_id, sequence_id, created_at, user_id, type, data) VALUES ($1, $2, $3, $4, $5, $6)"
loadSQL = "SELECT aggregate_id, sequence_id, created_at, user_id, type, data FROM %s"
)
// New creates a new event source store.
func New(db *sql.DB, tableName string) eventsource.Store {
return &store{
db: &driver.Generic{DB: db},
tableName: tableName,
}
}
// NewPgx creates a new event source store.
func NewPgx(db driver.PgxPool, tableName string) PGXStore {
return &store{
db: &driver.PGX{DB: db, NotificationChannel: nil},
tableName: tableName,
}
}
func columnExist(key column) bool {
for _, column := range columns {
if key == column {
return true
}
}
return false
}
func (s *store) WithPostgresNotify() PGXStore {
if db, ok := s.db.(*driver.PGX); ok {
db.NotificationChannel = &s.tableName
}
return s
}
func (s *store) NewTransaction(ctx context.Context, records ...eventsource.Record) (eventsource.StoreTransaction, error) {
return s.db.NewTransaction(ctx, fmt.Sprintf(saveSQL, s.tableName), records...) // nolint:wrapcheck
}
func (s *store) buildQuery(queryOpts []eventsource.QueryOption, query string) (returnedQuery string, args []interface{}, err error) {
fullQuery := []string{fmt.Sprintf(query, s.tableName)}
opts := evaluateQueryOptions(queryOpts)
if len(opts.where) > 0 {
whereStatements := make([]string, 0, len(opts.where))
for key, data := range opts.where {
if !columnExist(key) {
err = errors.Errorf("column '%s' cannot be applied to", key)
return
}
args = append(args, data.value)
whereStatements = append(whereStatements, fmt.Sprintf("%s %s $%d", key, data.operator, len(args)))
}
whereQuery := strings.Join(whereStatements, " AND ")
fullQuery = append(fullQuery, "WHERE", whereQuery)
}
if opts.descending {
fullQuery = append(fullQuery, "ORDER BY sequence_id DESC")
} else {
fullQuery = append(fullQuery, "ORDER BY sequence_id ASC")
}
if opts.limit != nil {
fullQuery = append(fullQuery, fmt.Sprintf("LIMIT %d", *opts.limit))
}
if opts.offset != nil {
fullQuery = append(fullQuery, fmt.Sprintf("OFFSET %d", *opts.offset))
}
returnedQuery = strings.Join(fullQuery, " ")
return returnedQuery, args, nil
}
func (s *store) fetchRecords(ctx context.Context, queryOpts []eventsource.QueryOption, query string) (records []eventsource.Record, err error) {
fullQuery, args, err := s.buildQuery(queryOpts, query)
if err != nil {
return
}
return s.db.Load(ctx, fullQuery, args) // nolint:wrapcheck
}
// Load will load records based on specified query options.
func (s *store) Load(ctx context.Context, opts ...eventsource.QueryOption) (records []eventsource.Record, err error) {
return s.fetchRecords(ctx, opts, loadSQL)
}
func (s *store) LoadByAggregate(ctx context.Context, aggregateID string, opts ...eventsource.QueryOption) (records []eventsource.Record, err error) {
return s.Load(ctx, append(opts, equals(columnAggregateID, aggregateID))...)
}
// Deprecated.
func (s *store) LoadBySequenceID(ctx context.Context, sequenceID string, opts ...eventsource.QueryOption) (records []eventsource.Record, err error) {
return s.Load(ctx, append(opts, BySequenceID(sequenceID))...)
}
// Deprecated.
func (s *store) LoadBySequenceIDAndType(ctx context.Context, sequenceID string, eventType string, opts ...eventsource.QueryOption) (records []eventsource.Record, err error) {
return s.Load(ctx, append(opts, BySequenceID(sequenceID), ByType(eventType))...)
}
// Deprecated.
func (s *store) LoadByTimestamp(ctx context.Context, timestamp int64, opts ...eventsource.QueryOption) (records []eventsource.Record, err error) {
return s.Load(ctx, append(opts, ByTimestamp(timestamp))...)
}