-
Notifications
You must be signed in to change notification settings - Fork 3
/
q.go
55 lines (47 loc) · 1.33 KB
/
q.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
package db
import (
"context"
"errors"
"strconv"
"strings"
)
// Q 查询
type Q struct {
Sql string `json:"sql"`
Filters []F `json:"filters"`
ReplaceOrderBy string `json:"replace_order_by"`
ReplaceGroupBy string `json:"replace_group_by"`
CurrentPage int64 `json:"current_page"`
PageSize int64 `json:"page_size"`
}
// BuildSql 构建SQL
func (m Q) BuildSql(ctx context.Context) (sql string, err error) {
if m.Sql == "" {
return "", errors.New("params SQL is null")
}
// 追加的排序条件
if m.ReplaceOrderBy != "" {
var b strings.Builder
b.WriteString(" order by ")
b.WriteString(m.ReplaceOrderBy)
m.Sql = strings.ReplaceAll(m.Sql, "{{.replace_order_by}}", b.String())
}
// 追加的分组条件
if m.ReplaceGroupBy != "" {
var b strings.Builder
b.WriteString(" group by ")
b.WriteString(m.ReplaceGroupBy)
m.Sql = strings.ReplaceAll(m.Sql, "{{.replace_group_by}}", b.String())
}
// 追加的分页条件
if m.PageSize > 0 && m.CurrentPage >= 0 {
var b strings.Builder
b.WriteString(" limit ")
b.WriteString(strconv.FormatInt((m.CurrentPage-1)*m.PageSize, 10))
b.WriteString(",")
b.WriteString(strconv.FormatInt(m.PageSize, 10))
m.Sql = strings.ReplaceAll(m.Sql, "{{.replace_limit}}", b.String())
}
m.Sql, err = F{}.BuildFilter(ctx, m.Filters, m.Sql)
return m.Sql, err
}