-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_select.go
More file actions
51 lines (42 loc) · 1001 Bytes
/
query_select.go
File metadata and controls
51 lines (42 loc) · 1001 Bytes
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
package query
import "github.com/epkgs/query/clause"
// SelectQuery SELECT查询结构体
type SelectQuery struct {
table string
errorRecord
fields []string
*orderbys[*SelectQuery]
*pagination[*SelectQuery]
*where[*SelectQuery]
}
// Select 设置SELECT查询的字段
func (q *SelectQuery) Select(fields ...string) *SelectQuery {
q.fields = append(q.fields, fields...)
return q
}
// Build 构建SELECT查询的SQL语句
func (q *SelectQuery) Build(builder clause.Builder) {
// 构建 SELECT 部分
builder.WriteString("SELECT ")
if len(q.fields) > 0 {
for i, field := range q.fields {
if i > 0 {
builder.WriteString(", ")
}
builder.WriteQuoted(field)
}
} else {
builder.WriteString("*")
}
// 构建 FROM 部分
if q.table != "" {
builder.WriteString(" FROM ")
builder.WriteQuoted(q.table)
}
// 构建 WHERE 部分
q.where.Build(builder)
// 构建 ORDER BY 部分
q.orderbys.Build(builder)
// 构建 Pagination 部分
q.pagination.Build(builder)
}