-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelation.go
175 lines (144 loc) · 3.71 KB
/
relation.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
package ayaorm
import (
"database/sql"
"log"
"github.com/okazaki-kk/ayaorm/query"
"github.com/okazaki-kk/ayaorm/utils"
)
type Relation struct {
query.Table
db *sql.DB
}
func NewRelation(db *sql.DB) *Relation {
r := &Relation{db: db, Table: query.Table{}}
return r
}
func (r *Relation) SetTable(tableName string) *Relation {
r.Table.SetTable(tableName)
return r
}
func (r *Relation) SetColumns(columns ...string) *Relation {
r.Table.SetColumns(columns...)
return r
}
func (r *Relation) Count(column ...string) int {
var count int
if err := r.SetColumns("COUNT(*)").QueryRow(&count); err != nil {
log.Print("ERROR IN COUNTER ", err)
return 0
}
return count
}
func (r *Relation) Pluck(column string) ([]interface{}, error) {
var res []interface{}
rows, err := r.SetColumns(column).Query()
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var tmp interface{}
err := rows.Scan(&tmp)
if err != nil {
return nil, err
}
res = append(res, tmp)
}
return res, nil
}
func (r *Relation) Limit(lim int) *Relation {
r.Table.Limit(lim)
return r
}
func (r *Relation) Order(key, order string) *Relation {
r.Table.Order(key, order)
return r
}
func (r *Relation) Where(column string, conditions ...interface{}) *Relation {
r.Table.Where(column, conditions...)
return r
}
func (r *Relation) Or(column string, conditions ...interface{}) *Relation {
r.Table.Or(column, conditions...)
return r
}
func (r *Relation) GroupBy(column ...string) *Relation {
r.Table.GroupBy(column...)
return r
}
func (r *Relation) Having(column string, conditions ...interface{}) *Relation {
r.Table.Having(column, conditions...)
return r
}
func (r *Relation) Save(id int, fieldMap map[string]interface{}) (int, error) {
r.Table.Insert(fieldMap)
var query string
var args []interface{}
if utils.IsZero(id) {
query, args = r.Table.BuildInsert(r.GetTable())
} else {
r.Table.Update(fieldMap)
query, args = r.Table.BuildUpdate(r.Table.GetTable(), id)
}
log.Print("execute query: ", query, " ", InterfaceJoin(args, ", "))
res, err := r.db.Exec(query, args...)
if err != nil {
return 0, err
}
lastId, err := res.LastInsertId()
if err != nil {
return 0, err
}
return int(lastId), err
}
func (r *Relation) CreateAll(fieldMaps map[string][]interface{}) error {
r.Table.CreateAll(fieldMaps)
var query string
var args []interface{}
query, args = r.Table.BuildCreateAll(r.Table.GetTable())
log.Println("execute query: ", query, " ", InterfaceJoin(args, ", "))
_, err := r.db.Exec(query, args...)
if err != nil {
return err
}
return nil
}
func (r *Relation) Delete(id int) error {
query := r.Table.BuildDelete(r.Table.GetTable(), id)
log.Print("execute query: ", query)
_, err := r.db.Exec(query)
return err
}
func (r *Relation) First() *Relation {
r.Limit(1).Order("id", "asc")
return r
}
func (r *Relation) Last() *Relation {
r.Limit(1).Order("id", "desc")
return r
}
func (r *Relation) Find(id int) *Relation {
r.Where("id", id)
r.Limit(1)
return r
}
func (r *Relation) FindBy(column string, value interface{}) *Relation {
r.Where(column, value)
r.Limit(1)
return r
}
func (r *Relation) InnerJoin(left, right string, hasMany bool) *Relation {
r.Table.InnerJoin(left, right, hasMany)
return r
}
func (r *Relation) QueryRow(dest ...interface{}) error {
query, args := r.Table.BuildQuery(r.Table.GetColumns(), r.GetTable())
log.Print("execute query: ", query, " ", InterfaceJoin(args, ", "))
return r.db.QueryRow(query, args...).Scan(dest...)
}
func (r *Relation) Query() (*sql.Rows, error) {
query, args := r.Table.BuildQuery(r.Table.GetColumns(), r.GetTable())
log.Print("execute query: ", query, " ", InterfaceJoin(args, ", "))
rows, err := r.db.Query(query, args...)
return rows, err
}