-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryset.go
141 lines (122 loc) · 2.92 KB
/
queryset.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
package dorm
import (
"reflect"
"bytes"
"fmt"
"strings"
)
type QuerySet struct {
orm *Orm
table string
cols string
where string
limit string
order string
}
func (q *QuerySet) Query(d ...interface{}) *QuerySet {
if len(d) > 0 {
//log.Info("filter :", d, len(d))
q.where = d[0].(string)
}
return q
}
func (q *QuerySet) First(d interface{}) error {
q.setLimit(0, 1)
q.marshal(d)
return q.orm.retrieve(d, q)
}
func (q *QuerySet) Last(d interface{}) error {
// todo
return nil
}
func (q *QuerySet) All(d interface{}) error {
q.marshal(d)
return q.orm.retrieve(d, q)
}
func (q *QuerySet) marshal(d interface{}) {
ind := reflect.Indirect(reflect.ValueOf(d))
typ := ind.Type()
if ind.Kind() == reflect.Slice {
typ = ind.Type().Elem()
}
mi := modelCache.getByName(typ.Name())
// 填充下col
if q.cols == "" {
cols := mi.fields.cols(&fieldFilter{pk: true})
q.setCols(cols...)
}
if q.table == "" {
q.setTableName(mi.table)
}
}
func ptrCheck(d interface{}) {
if reflect.ValueOf(d).Kind() != reflect.Ptr {
panic("参数必须为Ptr")
}
}
func sliceCheck(d interface{}) {
val := reflect.ValueOf(d)
ind := reflect.Indirect(val)
if ind.Kind() != reflect.Slice {
panic("参数必须为slice")
}
}
func (q *QuerySet) Asc(cols ...string) *QuerySet {
return q._order(true, cols...)
}
func (q *QuerySet) Desc(cols ...string) *QuerySet {
return q._order(false, cols...)
}
func (q *QuerySet)_order(asc bool, cols ...string) *QuerySet {
var buf bytes.Buffer
fmt.Fprint(&buf, q.order)
if asc {
fmt.Fprintf(&buf, "%v Asc", strings.Join(cols, " Asc, "))
} else {
fmt.Fprintf(&buf, "%v Desc", strings.Join(cols, " Desc, "))
}
q.order = buf.String()
return q
}
func (q *QuerySet)setCols(cols ...string) {
var buf bytes.Buffer
for i, c := range cols {
buf.WriteString(fmt.Sprintf(" `%s`", c))
if i != len(cols) - 1 {
buf.WriteByte(',')
}
}
q.cols = buf.String()
return
}
func (q *QuerySet)setTableName(t string) {
if len(t) == 0 {
panic("表名不能为空")
}
q.table = fmt.Sprintf("`%s`", t)
return
}
func (q *QuerySet)setLimit(min, max uint) {
q.limit = fmt.Sprintf("%d, %d", min, max)
return
}
func (q *QuerySet) sql() string {
var buf bytes.Buffer
buf.WriteString("select ")
if len(q.cols) == 0 {
buf.WriteByte('*')
} else {
buf.WriteString(q.cols)
}
buf.WriteString(fmt.Sprintf(" from %s ", q.table))
if len(q.where) > 0 {
buf.WriteString(fmt.Sprintf("where %s ", q.where))
}
if len(q.order) > 0 {
buf.WriteString(fmt.Sprintf("order by %s ", q.order))
}
if q.limit != "" {
buf.WriteString(fmt.Sprintf("limit %s ", q.limit))
}
return buf.String()
}