Skip to content

Commit 2e54e99

Browse files
committed
添加sqlBuilder
1 parent 3fc6257 commit 2e54e99

File tree

6 files changed

+112
-74
lines changed

6 files changed

+112
-74
lines changed

http/context/context.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ type key int
1414

1515
const defaultKey = key(1)
1616

17-
// ReqContext context
18-
type ReqContext struct {
19-
PS httprouter.Params
20-
AuthInfo interface{}
21-
}
22-
2317
// WithContext 携带请求上下文
2418
func WithContext(req *http.Request, rctx *ReqContext) *http.Request {
2519
ctx := context.WithValue(req.Context(), defaultKey, rctx)
@@ -34,3 +28,14 @@ func GetContext(req *http.Request) *ReqContext {
3428

3529
return new(ReqContext)
3630
}
31+
32+
// NewReqContext 创建请假上下文实例
33+
func NewReqContext() *ReqContext {
34+
return &ReqContext{}
35+
}
36+
37+
// ReqContext context
38+
type ReqContext struct {
39+
PS httprouter.Params
40+
AuthInfo interface{}
41+
}

http/context/context_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package context_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGet(t *testing.T) {
8+
9+
}

sqlbuilder/builder.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package sqlbuilder
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// NewMySQLQuery new 实例
9+
func NewMySQLQuery(querySQL string) *Query {
10+
return &Query{
11+
query: querySQL,
12+
whereOr: []string{},
13+
whereAnd: []string{},
14+
argsOr: []interface{}{},
15+
argsAnd: []interface{}{},
16+
argsLimit: []interface{}{},
17+
}
18+
}
19+
20+
// Query 查询mysql数据库
21+
type Query struct {
22+
query string
23+
whereOr []string
24+
whereAnd []string
25+
argsOr []interface{}
26+
argsAnd []interface{}
27+
limit string
28+
argsLimit []interface{}
29+
desc string
30+
}
31+
32+
// AddOrArg 添加参数
33+
func (q *Query) AddOrArg(v ...interface{}) {
34+
q.argsOr = append(q.argsOr, v...)
35+
}
36+
37+
// AddOrWhere 添加Where语句
38+
func (q *Query) AddOrWhere(v ...string) {
39+
q.whereOr = append(q.whereOr, v...)
40+
}
41+
42+
// AddAndArg 添加参数
43+
func (q *Query) AddAndArg(v ...interface{}) {
44+
q.argsAnd = append(q.argsAnd, v...)
45+
}
46+
47+
// AddAndWhere 添加Where语句
48+
func (q *Query) AddAndWhere(v ...string) {
49+
q.whereAnd = append(q.whereAnd, v...)
50+
}
51+
52+
// Limit 这周Limit
53+
func (q *Query) Limit(offset int64, limit uint) {
54+
q.limit = "LIMIT ?,? "
55+
q.argsLimit = append(q.argsLimit, offset, limit)
56+
}
57+
58+
// Desc todo
59+
func (q *Query) Desc(d string) {
60+
q.desc = fmt.Sprintf("ORDER BY %s DESC ", d)
61+
}
62+
63+
func (q *Query) whereBuild() string {
64+
if len(q.whereAnd) == 0 && len(q.whereOr) == 0 {
65+
return ""
66+
}
67+
68+
where := []string{}
69+
if len(q.whereAnd) > 0 {
70+
where = append(where, strings.Join(q.whereAnd, " AND "))
71+
}
72+
if len(q.whereOr) > 0 {
73+
where = append(where, " ( "+strings.Join(q.whereOr, " OR ")+" ) ")
74+
}
75+
76+
return "WHERE " + strings.Join(where, " AND ")
77+
}
78+
79+
// Build 组件SQL
80+
func (q *Query) Build() string {
81+
return q.query + q.whereBuild() + q.desc + q.limit + ";"
82+
}
83+
84+
// Args sql参数
85+
func (q *Query) Args() []interface{} {
86+
args := []interface{}{}
87+
args = append(args, q.argsAnd...)
88+
args = append(args, q.argsOr...)
89+
args = append(args, q.argsLimit...)
90+
return args
91+
}

sqlbuilder/builder_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package sqlbuilder_test

sqlpool/default.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

sqlpool/pool.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)