-
Notifications
You must be signed in to change notification settings - Fork 0
/
querybuild.go
84 lines (74 loc) · 1.5 KB
/
querybuild.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
package pagenation
import "fmt"
/**
翻页sql生成器
@author Bill
*/
const (
_pkField = "id"
countField = "count"
ListRow = 20
)
/**
当前sql + LIMIT 0,20 [分页sql]
*/
func QueryBuild(querySql string, currPage int, usePage bool) string {
var sql string
if usePage == true {
sql = querySql + PagenationParse(currPage, ListRow)
}
return sql
}
/**
汇总sql这里直接使用sql
*/
func QueryTotalBuild(tableName string, otherCondi string, alias string) string {
totalQuery := "SELECT count(" + _pkField + ") AS " + countField + " FROM " + tableName + " "
if alias != "" {
totalQuery += alias + " "
}
if otherCondi != "" {
totalQuery += otherCondi
}
return totalQuery
}
/**
页面计算(用于sql解析) 比如 1 页面 20 行 => LIMIT 0,20
比如取第五页 => 80,20
@author Bill
*/
func PagenationParse(page int, limitRow int) string {
pageInfo := map[string]int{
"starRow": 0,
"limitRow": limitRow,
}
var _page = page
if _page <= 0 {
_page = 1
}
switch _page {
case 1:
return fmt.Sprintf("LIMIT %d,%d", pageInfo["starRow"], pageInfo["limitRow"])
default:
pageInfo["starRow"] = PagenationStart(page, limitRow)
}
return fmt.Sprintf("LIMIT %d,%d", pageInfo["starRow"], pageInfo["limitRow"])
}
/**
页面计算(开始于结束步长区间)
@author Bill
*/
func PagenationStart(page int, limitRow int) int {
var start = 0
var _page = page
if _page <= 0 {
_page = 1
}
switch _page {
case 1:
start = 0
default:
start = (page - 1) * limitRow
}
return start
}