Skip to content

Commit 68d0adb

Browse files
committed
增加template对time参数判空
1 parent 6dc0c04 commit 68d0adb

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

parsing/template/dynamic.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212
"text/template"
13+
"time"
1314
)
1415

1516
const (
@@ -23,18 +24,22 @@ type Dynamic interface {
2324
format(string) (string, []interface{})
2425
}
2526

26-
func dummyUpdateSet(b bool, column string, value interface{}, origin string) string {
27+
func dummyUpdateSet(b interface{}, column string, value interface{}, origin string) string {
2728
return origin
2829
}
2930

30-
func dummyWhere(b bool, cond, column string, value interface{}, origin string) string {
31+
func dummyWhere(b interface{}, cond, column string, value interface{}, origin string) string {
3132
return origin
3233
}
3334

3435
func dummyParam(p interface{}) string {
3536
return fmt.Sprint(p)
3637
}
3738

39+
func dummyNil(p interface{}) bool {
40+
return true
41+
}
42+
3843
func commonAdd(a, b int) int {
3944
return a + b
4045
}
@@ -44,8 +49,9 @@ type DummyDynamic struct{}
4449
var dummyFuncMap = template.FuncMap{
4550
"set": dummyUpdateSet,
4651
"where": dummyWhere,
47-
"add": commonAdd,
4852
"arg": dummyParam,
53+
54+
"add": commonAdd,
4955
}
5056

5157
var gDummyDynamic = &DummyDynamic{}
@@ -72,17 +78,18 @@ func (d *MysqlDynamic) getFuncMap() template.FuncMap {
7278
return template.FuncMap{
7379
"set": d.mysqlUpdateSet,
7480
"where": d.mysqlWhere,
75-
"add": commonAdd,
7681
"arg": d.Param,
82+
83+
"add": commonAdd,
7784
}
7885
}
7986

8087
func (d *MysqlDynamic) getParam() []interface{} {
8188
return nil
8289
}
8390

84-
func (d *MysqlDynamic) mysqlUpdateSet(b bool, column string, value interface{}, origin string) string {
85-
if !b {
91+
func (d *MysqlDynamic) mysqlUpdateSet(b interface{}, column string, value interface{}, origin string) string {
92+
if !IsTrue(b) {
8693
return origin
8794
}
8895

@@ -113,8 +120,8 @@ func (d *MysqlDynamic) mysqlUpdateSet(b bool, column string, value interface{},
113120
return buf.String()
114121
}
115122

116-
func (d *MysqlDynamic) mysqlWhere(b bool, cond, column string, value interface{}, origin string) string {
117-
if !b {
123+
func (d *MysqlDynamic) mysqlWhere(b interface{}, cond, column string, value interface{}, origin string) string {
124+
if !IsTrue(b) {
118125
return origin
119126
}
120127

@@ -176,13 +183,14 @@ func (d *PostgresDynamic) getFuncMap() template.FuncMap {
176183
return template.FuncMap{
177184
"set": d.postgresUpdateSet,
178185
"where": d.postgresWhere,
179-
"add": commonAdd,
180186
"arg": d.Param,
187+
188+
"add": commonAdd,
181189
}
182190
}
183191

184-
func (d *PostgresDynamic) postgresUpdateSet(b bool, column string, value interface{}, origin string) string {
185-
if !b {
192+
func (d *PostgresDynamic) postgresUpdateSet(b interface{}, column string, value interface{}, origin string) string {
193+
if !IsTrue(b) {
186194
return origin
187195
}
188196

@@ -214,8 +222,8 @@ func (d *PostgresDynamic) postgresUpdateSet(b bool, column string, value interfa
214222
return buf.String()
215223
}
216224

217-
func (d *PostgresDynamic) postgresWhere(b bool, cond, column string, value interface{}, origin string) string {
218-
if !b {
225+
func (d *PostgresDynamic) postgresWhere(b interface{}, cond, column string, value interface{}, origin string) string {
226+
if !IsTrue(b) {
219227
return origin
220228
}
221229

@@ -326,3 +334,17 @@ func replace(s, old, new string, n int) (string, int) {
326334
w += copy(t[w:], s[start:])
327335
return string(t[0:w]), count
328336
}
337+
338+
func IsTrue(i interface{}) bool {
339+
t, _ := template.IsTrue(i)
340+
if !t {
341+
return t
342+
}
343+
344+
if ti, ok := i.(time.Time); ok {
345+
if ti.IsZero() {
346+
return false
347+
}
348+
}
349+
return t
350+
}

test/template/sql.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{define "selectTestTable"}}
22
{{$COLUMNS := "`id`, `username`, `password`"}}
33
SELECT {{$COLUMNS}} FROM `TEST_TABLE`
4-
{{where (ne .UserName "") "AND" "username" (arg .UserName) "" | where (ne .Password "pw") "AND" "password" (arg .Password) | where (ne .Status -1) "AND" "status" (arg .Status) }}
4+
{{where (ne .UserName "") "AND" "username" (arg .UserName) "" | where (ne .Password "pw") "AND" "password" (arg .Password) | where (ne .Status -1) "AND" "status" (arg .Status) | where .Time "AND" "time" (arg .Time) }}
55
{{end}}
66

77
{{define "insertTestTable"}}

test/template/temp_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
"os"
1212
"testing"
1313
"text/template"
14+
"time"
1415
)
1516

1617
type TestTable struct {
1718
Id int
1819
UserName string
1920
Password string
2021
Status int
22+
Time time.Time
2123
}
2224

2325
var driverName = "mysql"
@@ -86,7 +88,7 @@ func TestTemplate(t *testing.T) {
8688
func TestParser(t *testing.T) {
8789
mgr := tmp2.NewManager()
8890
mgr.RegisterFile("./sql.tpl")
89-
var param = TestTable{Id: 1, UserName: "user", Password: "pw"}
91+
var param = TestTable{Id: 1, UserName: "user", Password: "pw", Time: time.Now()}
9092
t.Run("select", func(t *testing.T) {
9193
tmp, _ := mgr.FindSqlParser("selectTestTable")
9294
md, err := tmp.ParseMetadata(driverName, param)
@@ -169,7 +171,7 @@ func TestParser2(t *testing.T) {
169171
if !ok {
170172
t.Fatal(ok)
171173
}
172-
md, err := p.ParseMetadata("mysql",[]TestTable{
174+
md, err := p.ParseMetadata("mysql", []TestTable{
173175
{Id: 11, UserName: "user11", Password: "pw11"},
174176
{Id: 12, UserName: "user12", Password: "pw12"},
175177
})

0 commit comments

Comments
 (0)