-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
61 lines (53 loc) · 1.42 KB
/
model.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
package sequel
import (
"database/sql"
"time"
"go.step.sm/qb"
)
// Model is the interface implemented by all the database models.
type Model interface {
GetID() string
SetID(id string)
SetCreatedAt(t time.Time)
SetUpdatedAt(t time.Time)
SetDeletedAt(t time.Time)
Select() string
Insert() string
Update() string
Delete() string
}
// ModelWithHardDelete is the interface implemented by a model that can be hard
// deleted.
type ModelWithHardDelete interface {
Model
HardDelete() string
}
// ModelWithExecInsert is the interface implemented by a model which inserts
// already contains the id and are not returning one.
type ModelWithExecInsert interface {
Model
WithExecInsert()
}
type Base struct {
ID string `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt sql.NullTime `db:"deleted_at"`
}
func (m Base) GetID() string { return m.ID }
func (m *Base) SetID(id string) { m.ID = id }
func (m *Base) SetCreatedAt(t time.Time) { m.CreatedAt = t }
func (m *Base) SetUpdatedAt(t time.Time) { m.UpdatedAt = t }
func (m *Base) SetDeletedAt(t time.Time) {
m.DeletedAt = sql.NullTime{
Valid: !t.IsZero(),
Time: t,
}
}
func Queries(builder *qb.QueryBuilder) (selectQ, insertQ, updateQ, deleteQ string) {
selectQ = builder.Select()
insertQ = builder.NamedInsertWithReturning()
updateQ = builder.NamedUpdate()
deleteQ = builder.Delete()
return
}