-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
146 lines (122 loc) · 2.99 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
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 orm
import (
"errors"
"reflect"
"github.com/ueffort/goutils/event"
)
type Model struct {
name string
schema *Schema
executor *executor
event event.Event
}
func NewModel(name string, record interface{}, executor *executor) (*Model, error) {
schema, err := NewSchema(record)
if err != nil {
return nil, err
}
return NewModelWithSchema(name, schema, executor), nil
}
func NewModelWithSchema(name string, schema *Schema, executor *executor) *Model {
if executor == nil {
executor = defaultExecutor
}
model := &Model{
name: name,
schema: schema,
executor: executor,
}
return model
}
func (m *Model) Query() ModelQuery {
return m.bind(m.executor.getQuery())
}
func (m *Model) bind(query *query) *query {
query.Table(m.name)
query.setModel(m)
auto := m.schema.AutoIncrement
if auto != "" {
query.Id(m.schema.fields[auto])
}
return query
}
func (m *Model) Bind(query Query) Query {
return query.Table(m.name)
}
func (m *Model) PK(key ...interface{}) ModelQuery {
query := m.executor.getQuery()
if m.schema.Primary == nil {
return query.error(errors.New("Model do not have primary"))
}
for k, v := range m.schema.Primary {
query.Where(m.schema.fields[v], "=", key[k])
}
return m.bind(query)
}
func (m *Model) Find(query Query) ([]interface{}, error) {
if m.schema.softDelete {
query.Where(m.schema.deleteTime, "=", 0)
}
return m.FindByQuery(query)
}
func (m *Model) One(query Query) (interface{}, error) {
query.Limit(0, 1)
if m.schema.softDelete {
query.Where(m.schema.deleteTime, "=", 0)
}
result, err := m.FindByQuery(query)
if err != nil {
return nil, err
}
return result[0], nil
}
func (m *Model) FindByQuery(query Query) ([]interface{}, error) {
value := reflect.MakeSlice(m.schema.schemaPtrType, 0, 0).Interface()
err := m.Bind(query).Find(value)
if err != nil {
return nil, err
}
return value.([]interface{}), nil
}
func (m *Model) Rows(query Query) (Rows, error) {
rows, err := m.Bind(query).Rows()
if err != nil {
return nil, err
}
rows.With(m.schema)
return rows, nil
}
func (m *Model) Insert(record interface{})(int64, error){
return m.Query().Save(record)
}
func (m *Model) Update(query Query, record interface{}) (int64, error) {
if m.schema.softDelete {
query.Where(m.schema.deleteTime, "=", 0)
}
return m.UpdateByQuery(query, record)
}
func (m *Model) UpdateByQuery(query Query, record interface{}) (int64, error) {
data, err := interface2map(m.schema, record)
if err != nil {
return 0, err
}
return m.Bind(query).Update(data)
}
func (m *Model) Delete(query Query) (int64, error) {
if m.schema.softDelete {
query.Where(m.schema.deleteTime, "=", 0)
}
return m.DeleteByQuery(query)
}
func (m *Model) DeleteByQuery(query Query) (int64, error) {
return m.Bind(query).Delete()
}
func (m *Model) Count(query Query) (int64, error) {
if m.schema.softDelete {
query.Where(m.schema.deleteTime, "=", 0)
}
return m.Bind(query).Count()
}
func (m *Model) CountByQuery(query Query) (int64, error) {
return m.Bind(query).Count()
}