-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_go1.18.go
201 lines (152 loc) · 4.4 KB
/
storage_go1.18.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//go:build go1.18
// +build go1.18
package sqluct
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
"github.com/Masterminds/squirrel"
)
// SerialID is the name of field tag to indicate integer serial (auto increment) ID of the table.
const SerialID = "serialIdentity"
// Get retrieves a single row from database storage.
func Get[V any](ctx context.Context, s *Storage, qb ToSQL) (V, error) {
var v V
err := s.Select(ctx, qb, &v)
return v, err
}
// List retrieves a collection of rows from database storage.
func List[V any](ctx context.Context, s *Storage, qb ToSQL) ([]V, error) {
var v []V
err := s.Select(ctx, qb, &v)
return v, err
}
// StorageOf is a type-safe facade to work with rows of specific type.
type StorageOf[V any] struct {
*Referencer
R *V
s *Storage
tableName string
id string
}
// Table configures and returns StorageOf in a table.
func Table[V any](storage *Storage, tableName string) StorageOf[V] {
ar := StorageOf[V]{}
ar.s = storage
ar.tableName = tableName
var v V
ar.R = &v
tm := mapper(ar.s.Mapper).typeMap(reflect.TypeOf(v))
for _, fi := range tm.Index {
if fi.Embedded {
continue
}
if _, ok := fi.Options[SerialID]; ok {
ar.id = fi.Name
break
}
}
ar.Referencer = storage.MakeReferencer()
ar.Referencer.AddTableAlias(ar.R, tableName)
return ar
}
// List retrieves a collection of rows from database storage.
func (s *StorageOf[V]) List(ctx context.Context, qb ToSQL) ([]V, error) {
var v []V
err := s.s.Select(ctx, qb, &v)
return v, err
}
// Get retrieves a single row from database storage.
func (s *StorageOf[V]) Get(ctx context.Context, qb ToSQL) (V, error) {
var v V
err := s.s.Select(ctx, qb, &v)
return v, err
}
// SelectStmt creates query statement with table name and row columns.
func (s *StorageOf[V]) SelectStmt(options ...func(*Options)) squirrel.SelectBuilder {
if len(options) == 0 {
options = []func(*Options){
s.ColumnsOf(s.R),
}
}
return s.s.SelectStmt(s.tableName, s.R, options...)
}
// DeleteStmt creates delete statement with table name.
func (s *StorageOf[V]) DeleteStmt() squirrel.DeleteBuilder {
return s.s.DeleteStmt(s.tableName)
}
// UpdateStmt creates update statement with table name and updated value (can be nil).
func (s *StorageOf[V]) UpdateStmt(value any, options ...func(*Options)) squirrel.UpdateBuilder {
return s.s.UpdateStmt(s.tableName, value, options...)
}
// InsertRow inserts single row database table.
func (s *StorageOf[V]) InsertRow(ctx context.Context, row V, options ...func(o *Options)) (int64, error) {
q := s.s.InsertStmt(s.tableName, row, options...)
if mapper(s.s.Mapper).Dialect == DialectPostgres && s.id != "" {
q = q.Suffix("RETURNING " + s.id)
query, args, err := q.ToSql()
if err != nil {
return 0, fmt.Errorf("building insert statement: %w", err)
}
var id int64
if err = s.s.db.QueryRowContext(ctx, query, args...).Scan(&id); err != nil {
return 0, fmt.Errorf("insert: %w", err)
}
return id, nil
}
res, err := s.s.Exec(ctx, q)
if err != nil {
return 0, fmt.Errorf("insert: %w", err)
}
if s.id == "" {
return 0, nil
}
id, err := res.LastInsertId()
if err != nil {
return id, fmt.Errorf("insert last id: %w", err)
}
return id, nil
}
// InsertRows inserts multiple rows in database table.
func (s *StorageOf[V]) InsertRows(ctx context.Context, rows []V, options ...func(o *Options)) (sql.Result, error) {
q := s.s.InsertStmt(s.tableName, rows, options...)
res, err := s.s.Exec(ctx, q)
if err != nil {
return nil, fmt.Errorf("insert: %w", err)
}
return res, nil
}
// JSON is a generic container to a serialized db column.
type JSON[V any] struct {
Val V
}
// UnmarshalJSON decodes JSON into container.
func (s *JSON[V]) UnmarshalJSON(bytes []byte) error {
return json.Unmarshal(bytes, &s.Val)
}
// MarshalJSON encodes container value as JSON.
func (s JSON[V]) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Val)
}
// Scan decodes json value from a db column.
func (s *JSON[V]) Scan(src any) error {
if src == nil {
return nil
}
switch v := src.(type) {
case []byte:
return json.Unmarshal(v, &s.Val)
case string:
return json.Unmarshal([]byte(v), &s.Val)
default:
return fmt.Errorf("unsupported type %T", src) //nolint:goerr113
}
}
// Value encodes value as json for a db column.
func (s JSON[V]) Value() (driver.Value, error) {
j, err := json.Marshal(s.Val)
return string(j), err
}