Skip to content

Commit 3e63403

Browse files
committed
add inner template functions
1 parent 8ce3bb7 commit 3e63403

File tree

7 files changed

+272
-54
lines changed

7 files changed

+272
-54
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.11
44

55
require (
66
github.com/go-sql-driver/mysql v1.4.1 // indirect
7-
github.com/lib/pq v1.3.0 // indirect
7+
github.com/lib/pq v1.3.0
88
github.com/mattn/go-sqlite3 v1.10.0 // indirect
99
)

parsing/template/dynamic.go

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Copyright (C) 2019-2020, Xiongfa Li.
2+
// @author xiongfa.li
3+
// @version V1.0
4+
// Description:
5+
6+
package template
7+
8+
import (
9+
"fmt"
10+
"strings"
11+
"text/template"
12+
)
13+
14+
func dummyUpdateSet(b bool, column string, value interface{}, origin string) string {
15+
return origin
16+
}
17+
18+
func dummyWhere(b bool, cond, column string, value interface{}, origin string) string {
19+
return origin
20+
}
21+
22+
func mysqlUpdateSet(b bool, column string, value interface{}, origin string) string {
23+
if !b {
24+
return origin
25+
}
26+
27+
buf := strings.Builder{}
28+
if origin == "" {
29+
buf.WriteString(" SET ")
30+
} else {
31+
origin = strings.TrimSpace(origin)
32+
buf.WriteString(origin)
33+
if origin[:len(origin)-1] != "," {
34+
buf.WriteString(",")
35+
}
36+
}
37+
buf.WriteString("`")
38+
buf.WriteString(column)
39+
buf.WriteString("` = ")
40+
if s, ok := value.(string); ok {
41+
buf.WriteString(`'`)
42+
buf.WriteString(s)
43+
buf.WriteString(`'`)
44+
} else {
45+
buf.WriteString(fmt.Sprint(value))
46+
}
47+
return buf.String()
48+
}
49+
50+
func postgresUpdateSet(b bool, column string, value interface{}, origin string) string {
51+
if !b {
52+
return origin
53+
}
54+
55+
buf := strings.Builder{}
56+
if origin == "" {
57+
buf.WriteString(" SET ")
58+
} else {
59+
origin = strings.TrimSpace(origin)
60+
buf.WriteString(origin)
61+
if origin[:len(origin)-1] != "," {
62+
buf.WriteString(",")
63+
}
64+
}
65+
buf.WriteString(column)
66+
buf.WriteString(" = ")
67+
if s, ok := value.(string); ok {
68+
buf.WriteString(`'`)
69+
buf.WriteString(s)
70+
buf.WriteString(`'`)
71+
} else {
72+
buf.WriteString(fmt.Sprint(value))
73+
}
74+
return buf.String()
75+
}
76+
77+
func mysqlWhere(b bool, cond, column string, value interface{}, origin string) string {
78+
if !b {
79+
return origin
80+
}
81+
82+
buf := strings.Builder{}
83+
if origin == "" {
84+
buf.WriteString(" WHERE ")
85+
cond = ""
86+
} else {
87+
buf.WriteString(strings.TrimSpace(origin))
88+
buf.WriteString(" ")
89+
buf.WriteString(cond)
90+
buf.WriteString(" ")
91+
}
92+
93+
buf.WriteString("`")
94+
buf.WriteString(column)
95+
buf.WriteString("` = ")
96+
if s, ok := value.(string); ok {
97+
buf.WriteString(`'`)
98+
buf.WriteString(s)
99+
buf.WriteString(`'`)
100+
} else {
101+
buf.WriteString(fmt.Sprint(value))
102+
}
103+
return buf.String()
104+
}
105+
106+
func postgresWhere(b bool, cond, column string, value interface{}, origin string) string {
107+
if !b {
108+
return origin
109+
}
110+
111+
buf := strings.Builder{}
112+
if origin == "" {
113+
buf.WriteString(" WHERE ")
114+
cond = ""
115+
} else {
116+
buf.WriteString(strings.TrimSpace(origin))
117+
buf.WriteString(" ")
118+
buf.WriteString(cond)
119+
buf.WriteString(" ")
120+
}
121+
122+
buf.WriteString(column)
123+
buf.WriteString(" = ")
124+
if s, ok := value.(string); ok {
125+
buf.WriteString(`'`)
126+
buf.WriteString(s)
127+
buf.WriteString(`'`)
128+
} else {
129+
buf.WriteString(fmt.Sprint(value))
130+
}
131+
return buf.String()
132+
}
133+
134+
var mysqlFuncMap = template.FuncMap{
135+
"set": mysqlUpdateSet,
136+
"where": mysqlWhere,
137+
}
138+
139+
var postgresFuncMap = template.FuncMap{
140+
"set": postgresUpdateSet,
141+
"where": postgresWhere,
142+
}
143+
144+
var dummyFuncMap = template.FuncMap{
145+
"set": dummyUpdateSet,
146+
"where": dummyWhere,
147+
}
148+
149+
var funcMap = map[string]template.FuncMap{
150+
"mysql": mysqlFuncMap,
151+
"postgres": postgresFuncMap,
152+
}
153+
154+
func selectFuncMap(driverName string) template.FuncMap {
155+
if v, ok := funcMap[driverName]; ok {
156+
return v
157+
}
158+
return dummyFuncMap
159+
}

parsing/template/parse.go

+6-25
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package template
88
import (
99
"github.com/xfali/gobatis/logging"
1010
"github.com/xfali/gobatis/parsing/sqlparser"
11-
"html/template"
1211
"io/ioutil"
1312
"strings"
1413
"sync"
14+
"text/template"
1515
)
1616

1717
type Parser struct {
@@ -25,8 +25,9 @@ func (p *Parser) ParseMetadata(driverName string, params ...interface{}) (*sqlpa
2525
if len(params) > 0 {
2626
param = params[0]
2727
}
28-
29-
err := p.tpl.Execute(&b, param)
28+
fm := selectFuncMap(driverName)
29+
tpl := p.tpl.Funcs(fm)
30+
err := tpl.Execute(&b, param)
3031
if err != nil {
3132
return nil, err
3233
}
@@ -42,22 +43,6 @@ func (p *Parser) ParseMetadata(driverName string, params ...interface{}) (*sqlpa
4243
return ret, nil
4344
}
4445

45-
func updateSet(sets ... string) string {
46-
b := strings.Builder{}
47-
for _, v := range sets {
48-
if len(v) > 0 {
49-
b.WriteString(strings.TrimSpace(v))
50-
b.WriteString(",")
51-
}
52-
}
53-
setStr := b.String()
54-
if len(setStr) == 0 {
55-
return ""
56-
} else {
57-
return " SET " + setStr[:len(setStr)-1]
58-
}
59-
}
60-
6146
type Manager struct {
6247
sqlMap map[string]*Parser
6348
lock sync.Mutex
@@ -74,9 +59,7 @@ func (m *Manager) RegisterData(data []byte) error {
7459
defer m.lock.Unlock()
7560

7661
tpl := template.New("")
77-
tpl = tpl.Funcs(template.FuncMap{
78-
"updateSet": updateSet,
79-
})
62+
tpl = tpl.Funcs(dummyFuncMap)
8063
tpl, err := tpl.Parse(string(data))
8164
if err != nil {
8265
logging.Warn("register template data failed: %s err: %v\n", string(data), err)
@@ -96,14 +79,12 @@ func (m *Manager) RegisterFile(file string) error {
9679
defer m.lock.Unlock()
9780

9881
tpl := template.New("")
99-
tpl = tpl.Funcs(template.FuncMap{
100-
"updateSet": updateSet,
101-
})
10282
data, err := ioutil.ReadFile(file)
10383
if err != nil {
10484
logging.Warn("register template file failed: %s err: %v\n", file, err)
10585
return err
10686
}
87+
tpl = tpl.Funcs(dummyFuncMap)
10788
tpl, err = tpl.Parse(string(data))
10889
if err != nil {
10990
logging.Warn("register template file failed: %s err: %v\n", file, err)

test/postgresql/postgresql.tpl

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{{define "selectTestTable"}}
22
{{$COLUMNS := "id, username, password"}}
3-
SELECT {{$COLUMNS}} FROM test_table WHERE 1=1
4-
{{if (ne .Username "")}} AND username = '{{.Username}}' {{end}}
5-
{{if (ne .Password "")}} AND password = '{{.Password}}' {{end}}
3+
SELECT {{$COLUMNS}} FROM test_table
4+
{{where (ne .Username "") "AND" "username" .Username "" | where (ne .Password "pw") "AND" "password" .Password}}
65
{{end}}
76

87
{{define "insertTestTable"}}
@@ -16,15 +15,12 @@ INSERT INTO test_table ({{$COLUMNS}})
1615
{{end}}
1716

1817
{{define "updateTestTable"}}
19-
UPDATE test_table SET id = id
20-
{{if (ne .Username "")}} , username = '{{.Username}}' {{end}}
21-
{{if (ne .Password "")}} , password = '{{.Password}}' {{end}}
22-
{{if (ne .Id 0)}} WHERE id = {{.Id}} {{end}}
18+
UPDATE test_table
19+
{{set (ne .Username "") "username" .Username "" | set (ne .Password "") "password" .Password}}
20+
{{where (ne .Id 0) "AND" "id" .Id ""}}
2321
{{end}}
2422

2523
{{define "deleteTestTable"}}
26-
DELETE FROM test_table WHERE 1=1
27-
{{if (ne .Id 0)}} AND id = {{.Id}} {{end}}
28-
{{if (ne .Username "")}} AND username = {{.Username}} {{end}}
29-
{{if (ne .Password "")}} AND password = {{.Password}} {{end}}
24+
DELETE FROM test_table
25+
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "username" .Username | where (ne .Password "") "AND" "password" .Password}}
3026
{{end}}

test/postgresql/postgresql2.tpl

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{define "selectTestTable"}}
2+
{{$COLUMNS := "id, username, password"}}
3+
SELECT {{$COLUMNS}} FROM test_table WHERE 1=1
4+
{{if (ne .Username "")}} AND username = '{{.Username}}' {{end}}
5+
{{if (ne .Password "")}} AND password = '{{.Password}}' {{end}}
6+
{{end}}
7+
8+
{{define "insertTestTable"}}
9+
{{$COLUMNS := "id, username, password"}}
10+
INSERT INTO test_table ({{$COLUMNS}})
11+
VALUES(
12+
{{.Id}},
13+
'{{.Username}}',
14+
'{{.Password}}'
15+
)
16+
{{end}}
17+
18+
{{define "updateTestTable"}}
19+
UPDATE test_table SET id = id
20+
{{if (ne .Username "")}} , username = '{{.Username}}' {{end}}
21+
{{if (ne .Password "")}} , password = '{{.Password}}' {{end}}
22+
{{if (ne .Id 0)}} WHERE id = {{.Id}} {{end}}
23+
{{end}}
24+
25+
{{define "deleteTestTable"}}
26+
DELETE FROM test_table WHERE 1=1
27+
{{if (ne .Id 0)}} AND id = {{.Id}} {{end}}
28+
{{if (ne .Username "")}} AND username = {{.Username}} {{end}}
29+
{{if (ne .Password "")}} AND password = {{.Password}} {{end}}
30+
{{end}}

test/template/sql.tpl

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{{define "selectTestTable"}}
22
{{$COLUMNS := "`id`, `username`, `password`"}}
3-
SELECT {{$COLUMNS}} FROM `TEST_TABLE` WHERE 1=1
4-
{{if (ne .UserName "")}} AND `username` = {{.UserName}} {{end}}
5-
{{if (ne .Password "")}} AND `password` = {{.Password}} {{end}}
3+
SELECT {{$COLUMNS}} FROM `TEST_TABLE`
4+
{{where (ne .UserName "") "AND" "username" .UserName "" | where (ne .Password "pw") "AND" "password" .Password | where (ne .Status -1) "AND" "status" .Status }}
65
{{end}}
76

87
{{define "insertTestTable"}}
@@ -15,17 +14,14 @@ INSERT INTO `TEST_TABLE` ({{$COLUMNS}})
1514
{{end}}
1615

1716
{{define "updateTestTable"}}
18-
Update `TEST_TABLE` set
19-
{{if (ne .UserName "")}} `username` = {{.UserName}} {{end}}
20-
{{if (ne .Password "")}} `password` = {{.Password}} {{end}}
17+
UPDATE `TEST_TABLE`
18+
{{set (ne .UserName "") "username" .UserName "" | set (ne .Password "") "password" .Password | set (ne .Status -1) "status" .Status}}
2119
{{if (ne .Id 0)}} WHERE `id` = {{.Id}} {{end}}
2220
{{end}}
2321

2422
{{define "deleteTestTable"}}
25-
DELETE FROM `TEST_TABLE` WHERE 1=1
26-
{{if (ne .Id 0)}} AND `id` = {{.Id}} {{end}}
27-
{{if (ne .UserName "")}} AND `username` = {{.UserName}} {{end}}
28-
{{if (ne .Password "")}} AND `password` = {{.Password}} {{end}}
23+
DELETE FROM `TEST_TABLE`
24+
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .UserName "") "AND" "username" .UserName | where (ne .Password "pw") "AND" "password" .Password | where (ne .Status -1) "AND" "status" .Status }}
2925
{{end}}
3026

3127
{{template "selectTestTable"}}

0 commit comments

Comments
 (0)