-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres.go
242 lines (200 loc) · 5.38 KB
/
postgres.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package pg_dao
import (
"context"
"database/sql"
goerr "errors"
"fmt"
"reflect"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/fatih/structs"
"gitlab.com/distributed_lab/kit/pgdb"
"gitlab.com/distributed_lab/logan/v3/errors"
)
type dao struct {
tableName string
db *pgdb.DB
sql sq.SelectBuilder
upd sq.UpdateBuilder
dlt sq.DeleteBuilder
}
func NewDAO(db *pgdb.DB, tableName string) DAO {
return &dao{
tableName: tableName,
db: db,
sql: sq.Select(tableName + ".*").From(tableName),
upd: sq.Update(tableName),
dlt: sq.Delete(tableName),
}
}
func (d *dao) Clone() DAO {
return &dao{
tableName: d.tableName,
db: d.db.Clone(),
sql: sq.Select(d.tableName + ".*").From(d.tableName),
upd: sq.Update(d.tableName),
dlt: sq.Delete(d.tableName),
}
}
func (d *dao) New() DAO {
return &dao{
tableName: d.tableName,
db: d.db,
sql: sq.Select(d.tableName + ".*").From(d.tableName),
upd: sq.Update(d.tableName),
dlt: sq.Delete(d.tableName),
}
}
func (d *dao) Count() DAO {
return &dao{
tableName: d.tableName,
db: d.db,
sql: sq.Select("count(*)").From(d.tableName),
upd: sq.Update(d.tableName),
dlt: sq.Delete(d.tableName),
}
}
func (d *dao) Create(dto interface{}) (int64, error) {
return d.CreateCtx(context.TODO(), dto)
}
func (d *dao) CreateCtx(ctx context.Context, dto interface{}) (int64, error) {
clauses := structs.Map(dto)
var id int64
stmt := sq.Insert(d.tableName).SetMap(clauses).Suffix("returning id")
err := d.db.GetContext(ctx, &id, stmt)
return id, err
}
func (d *dao) Get(dto interface{}) (bool, error) {
return d.GetCtx(context.TODO(), dto)
}
func (d *dao) GetCtx(ctx context.Context, dto interface{}) (bool, error) {
if reflect.ValueOf(dto).Type().Kind() != reflect.Ptr {
return false, errors.New("argument is not a pointer")
}
err := d.db.GetContext(ctx, dto, d.sql)
if goerr.Is(err, sql.ErrNoRows) {
return false, nil
}
return true, err
}
func (d *dao) Select(list interface{}) error {
return d.SelectCtx(context.TODO(), list)
}
func (d *dao) SelectCtx(ctx context.Context, list interface{}) error {
if reflect.ValueOf(list).Type().Kind() != reflect.Ptr {
return errors.New("argument is not a slice pointer")
}
err := d.db.SelectContext(ctx, list, d.sql)
if goerr.Is(err, sql.ErrNoRows) {
return nil
}
return err
}
func (d *dao) FilterByID(id int64) DAO {
d.sql = d.sql.Where(sq.Eq{IdColumn: id})
return d
}
func (d *dao) FilterOnlyAfter(time time.Time) DAO {
d.sql = d.sql.Where(sq.Gt{CreatedAtColumn: time})
return d
}
func (d *dao) FilterOnlyBefore(time time.Time) DAO {
d.sql = d.sql.Where(sq.Lt{CreatedAtColumn: time})
return d
}
func (d *dao) FilterGreater(col string, val interface{}) DAO {
d.sql = d.sql.Where(sq.Gt{col: val})
return d
}
func (d *dao) FilterLess(col string, val interface{}) DAO {
d.sql = d.sql.Where(sq.Lt{col: val})
return d
}
func (d *dao) FilterByColumn(col string, val interface{}) DAO {
d.sql = d.sql.Where(sq.Eq{col: val})
return d
}
func (d *dao) Limit(limit uint64) DAO {
d.sql = d.sql.Limit(limit)
return d
}
func (d *dao) OrderByDesc(col string) DAO {
d.sql = d.sql.OrderBy(fmt.Sprintf("%s %s", col, OrderDescending))
return d
}
func (d *dao) OrderByAsc(col string) DAO {
d.sql = d.sql.OrderBy(fmt.Sprintf("%s %s", col, OrderAscending))
return d
}
func (d *dao) UpdateWhereID(id int64) DAO {
d.upd = d.upd.Where(sq.Eq{IdColumn: id})
return d
}
func (d *dao) UpdateColumn(col string, val interface{}) DAO {
d.upd = d.upd.Set(col, val)
return d
}
func (d *dao) Update() error {
return d.UpdateCtx(context.TODO())
}
func (d *dao) UpdateCtx(ctx context.Context) error {
res, err := d.db.ExecWithResultContext(ctx, d.upd)
if err != nil {
return errors.Wrap(err, "unable to update row")
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return errors.Wrap(err, "unable to get affected rows")
}
if rowsAffected == 0 {
return ErrNotFound
}
return nil
}
func (d *dao) DeleteWhereVal(col string, val interface{}) DAO {
d.dlt = d.dlt.Where(sq.Eq{col: val})
return d
}
func (d *dao) DeleteWhereID(id int64) DAO {
d.dlt = d.dlt.Where(sq.Eq{IdColumn: id})
return d
}
func (d *dao) Delete() error {
return d.DeleteCtx(context.TODO())
}
func (d *dao) DeleteCtx(ctx context.Context) error {
err := d.db.ExecContext(ctx, d.dlt)
if err != nil {
return errors.Wrap(err, "unable to delete row")
}
return nil
}
func (d *dao) Page(params pgdb.OffsetPageParams, column string) DAO {
d.sql = params.ApplyTo(d.sql, column)
return d
}
func (d *dao) Cursor(params pgdb.CursorPageParams, column string) DAO {
d.sql = params.ApplyTo(d.sql, column)
return d
}
func (d *dao) Transaction(fn func(q DAO) error) (err error) {
return d.db.Transaction(func() error {
return fn(d)
})
}
func (d *dao) TransactionSerializable(fn func(q DAO) error) error {
return d.db.TransactionWithOptions(&sql.TxOptions{Isolation: sql.LevelSerializable}, func() error {
return fn(d)
})
}
func (d *dao) TransactionWithLevel(level sql.IsolationLevel, fn func(q DAO) error) error {
return d.db.TransactionWithOptions(&sql.TxOptions{Isolation: level}, func() error {
return fn(d)
})
}
func (d *dao) ExecRaw(fn func(raw *pgdb.DB) error) error {
return fn(d.db)
}
func (d *dao) ExecRawCtx(ctx context.Context, fn func(ctx context.Context, raw *pgdb.DB) error) error {
return fn(ctx, d.db)
}