Skip to content

Commit 5e5ca5e

Browse files
committed
修改template set where函数,column将不再自动添加关键字转义符,需在template文件中自行添加
1 parent 68d0adb commit 5e5ca5e

File tree

9 files changed

+83
-53
lines changed

9 files changed

+83
-53
lines changed

README.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,41 +258,49 @@ gobatis.RegisterTemplateFile(filePath)
258258

259259
```
260260
{{define "selectTestTable"}}
261-
{{$COLUMNS := "id, username, password"}}
262-
SELECT {{$COLUMNS}} FROM test_table
263-
{{where (ne .Username "") "AND" "username" .Username "" | where (ne .Password "pw") "AND" "password" .Password}}
261+
SELECT "id","username","password","createtime" FROM "test_table"
262+
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
263+
{{end}}
264+
265+
{{define "selectTestTableCount"}}
266+
SELECT COUNT(*) FROM "test_table"
267+
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
264268
{{end}}
265269
266270
{{define "insertTestTable"}}
267-
{{$COLUMNS := "id, username, password"}}
268-
INSERT INTO test_table ({{$COLUMNS}})
269-
VALUES(
270-
{{.Id}},
271-
'{{.Username}}',
272-
'{{.Password}}'
273-
)
271+
INSERT INTO "test_table"("id","username","password","createtime")
272+
VALUES(
273+
{{arg .Id}}, {{arg .Username}}, {{arg .Password}}, {{arg .Createtime}})
274+
{{end}}
275+
276+
{{define "insertBatchTestTable"}}
277+
{{$size := len . | add -1}}
278+
INSERT INTO "test_table"("username","password","createtime")
279+
VALUES {{range $i, $v := .}}
280+
({{arg $v.Username}}, {{arg $v.Password}}, {{arg $v.Createtime}}){{if lt $i $size}},{{end}}
281+
{{end}}
274282
{{end}}
275283
276284
{{define "updateTestTable"}}
277-
UPDATE test_table
278-
{{set (ne .Username "") "username" .Username "" | set (ne .Password "") "password" .Password}}
279-
{{where (ne .Id 0) "AND" "id" .Id ""}}
285+
UPDATE "test_table"
286+
{{set .Id "\"id\" = " (arg .Id) "" | set .Username "\"username\" = " (arg .Username) | set .Password "\"password\" = " (arg .Password) | set .Createtime "\"createtime\" = " (arg .Createtime)}}
287+
{{where .Id "AND" "\"id\" = " (arg .Id) ""}}
280288
{{end}}
281289
282290
{{define "deleteTestTable"}}
283-
DELETE FROM test_table
284-
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "username" .Username | where (ne .Password "") "AND" "password" .Password}}
291+
DELETE FROM "test_table"
292+
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
285293
{{end}}
286294
```
287295
其中where、set、arg是gobatis的自定义函数,用于智能生成动态sql
288296

289297
arg用于将对象动态转换为占位符,并保存为SQL参数,如:
290298
```cassandraql
291-
SELECT * FROM TABLENAME WHERE name = {{arg .Name}}
299+
SELECT * FROM TABLE_NAME WHERE name = {{arg .Name}}
292300
```
293301
以mysql为例,将解析为:
294302
```cassandraql
295-
SELECT * FROM TABLENAME WHERE name = ?
303+
SELECT * FROM TABLE_NAME WHERE name = ?
296304
```
297305
同时Name的值将自动保存为SQL参数,自动传入,起到类似xml中#{MODEL.Name}的效果。
298306

parsing/template/dynamic.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (d *MysqlDynamic) getParam() []interface{} {
8888
return nil
8989
}
9090

91-
func (d *MysqlDynamic) mysqlUpdateSet(b interface{}, column string, value interface{}, origin string) string {
91+
func (d *MysqlDynamic) mysqlUpdateSet(b interface{}, columnDesc string, value interface{}, origin string) string {
9292
if !IsTrue(b) {
9393
return origin
9494
}
@@ -103,9 +103,7 @@ func (d *MysqlDynamic) mysqlUpdateSet(b interface{}, column string, value interf
103103
buf.WriteString(",")
104104
}
105105
}
106-
buf.WriteString("`")
107-
buf.WriteString(column)
108-
buf.WriteString("` = ")
106+
buf.WriteString(columnDesc)
109107
if s, ok := value.(string); ok {
110108
if _, ok := d.paramMap[s]; ok {
111109
buf.WriteString(s)
@@ -120,7 +118,7 @@ func (d *MysqlDynamic) mysqlUpdateSet(b interface{}, column string, value interf
120118
return buf.String()
121119
}
122120

123-
func (d *MysqlDynamic) mysqlWhere(b interface{}, cond, column string, value interface{}, origin string) string {
121+
func (d *MysqlDynamic) mysqlWhere(b interface{}, cond, columnDesc string, value interface{}, origin string) string {
124122
if !IsTrue(b) {
125123
return origin
126124
}
@@ -136,9 +134,7 @@ func (d *MysqlDynamic) mysqlWhere(b interface{}, cond, column string, value inte
136134
buf.WriteString(" ")
137135
}
138136

139-
buf.WriteString("`")
140-
buf.WriteString(column)
141-
buf.WriteString("` = ")
137+
buf.WriteString(columnDesc)
142138
if s, ok := value.(string); ok {
143139
if _, ok := d.paramMap[s]; ok {
144140
buf.WriteString(s)
@@ -189,7 +185,7 @@ func (d *PostgresDynamic) getFuncMap() template.FuncMap {
189185
}
190186
}
191187

192-
func (d *PostgresDynamic) postgresUpdateSet(b interface{}, column string, value interface{}, origin string) string {
188+
func (d *PostgresDynamic) postgresUpdateSet(b interface{}, columnDesc string, value interface{}, origin string) string {
193189
if !IsTrue(b) {
194190
return origin
195191
}
@@ -204,10 +200,7 @@ func (d *PostgresDynamic) postgresUpdateSet(b interface{}, column string, value
204200
buf.WriteString(",")
205201
}
206202
}
207-
buf.WriteString(`"`)
208-
buf.WriteString(column)
209-
buf.WriteString(`"`)
210-
buf.WriteString(" = ")
203+
buf.WriteString(columnDesc)
211204
if s, ok := value.(string); ok {
212205
if _, ok := d.paramMap[s]; ok {
213206
buf.WriteString(s)
@@ -222,7 +215,7 @@ func (d *PostgresDynamic) postgresUpdateSet(b interface{}, column string, value
222215
return buf.String()
223216
}
224217

225-
func (d *PostgresDynamic) postgresWhere(b interface{}, cond, column string, value interface{}, origin string) string {
218+
func (d *PostgresDynamic) postgresWhere(b interface{}, cond, columnDesc string, value interface{}, origin string) string {
226219
if !IsTrue(b) {
227220
return origin
228221
}
@@ -238,10 +231,7 @@ func (d *PostgresDynamic) postgresWhere(b interface{}, cond, column string, valu
238231
buf.WriteString(" ")
239232
}
240233

241-
buf.WriteString(`"`)
242-
buf.WriteString(column)
243-
buf.WriteString(`"`)
244-
buf.WriteString(" = ")
234+
buf.WriteString(columnDesc)
245235
if s, ok := value.(string); ok {
246236
if _, ok := d.paramMap[s]; ok {
247237
buf.WriteString(s)

test/cmd/pg_cmd_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func TestInsert(t *testing.T) {
6565
func TestInsertBatch(t *testing.T) {
6666
ret, id, err := InsertBatchTestTable(sessionMgr.NewSession(), []TestTable{
6767
{Username: "test_insert_user4", Password: "test_pw4"},
68+
{Username: "test_insert_user5", Password: "test_pw5"},
6869
})
6970
if err != nil {
7071
t.Log(err)
@@ -95,7 +96,14 @@ func TestSessionTpl(t *testing.T) {
9596
t.Run("select", func(t *testing.T) {
9697
sess := sessionMgr.NewSession()
9798
var ret []TestTable
98-
sess.Select("selectTestTable").Param(TestTable{}).Result(&ret)
99+
sess.Select("selectTestTable").Param(TestTable{Id: 1}).Result(&ret)
100+
t.Log(ret)
101+
})
102+
103+
t.Run("count", func(t *testing.T) {
104+
sess := sessionMgr.NewSession()
105+
var ret int
106+
sess.Select("selectTestTableCount").Param(TestTable{}).Result(&ret)
99107
t.Log(ret)
100108
})
101109

@@ -107,6 +115,17 @@ func TestSessionTpl(t *testing.T) {
107115
t.Log(ret)
108116
})
109117

118+
t.Run("insertBatch", func(t *testing.T) {
119+
sess := sessionMgr.NewSession()
120+
var ret int
121+
err := sess.Insert("insertBatchTestTable").Param([]TestTable{
122+
{Username: "test_insert_user14", Password: "test_pw14"},
123+
{Username: "test_insert_user15", Password: "test_pw15"},
124+
}).Result(&ret)
125+
t.Log(err)
126+
t.Log(ret)
127+
})
128+
110129
t.Run("update", func(t *testing.T) {
111130
sess := sessionMgr.NewSession()
112131
var ret int
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
{{/*This file was generated by xfali/gobatis-cmd at*/}}
2-
{{/*2020-02-07 11:43:44.4379882 +0800 CST m=+0.092005301*/}}
2+
{{/*2020-02-12 10:02:30.5793955 +0800 CST m=+0.098005601*/}}
33

44
{{define "selectTestTable"}}
55
SELECT "id","username","password","createtime" FROM "test_table"
6-
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "username" .Username | where (ne .Password "") "AND" "password" .Password | where (ne .Createtime ) "AND" "createtime" .Createtime}}
6+
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
7+
{{end}}
8+
9+
{{define "selectTestTableCount"}}
10+
SELECT COUNT(*) FROM "test_table"
11+
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
712
{{end}}
813

914
{{define "insertTestTable"}}
1015
INSERT INTO "test_table"("id","username","password","createtime")
1116
VALUES(
12-
{{.Id}}, '{{.Username}}', '{{.Password}}', {{.Createtime}})
17+
{{arg .Id}}, {{arg .Username}}, {{arg .Password}}, {{arg .Createtime}})
18+
{{end}}
19+
20+
{{define "insertBatchTestTable"}}
21+
{{$size := len . | add -1}}
22+
INSERT INTO "test_table"("username","password","createtime")
23+
VALUES {{range $i, $v := .}}
24+
({{arg $v.Username}}, {{arg $v.Password}}, {{arg $v.Createtime}}){{if lt $i $size}},{{end}}
25+
{{end}}
1326
{{end}}
1427

1528
{{define "updateTestTable"}}
1629
UPDATE "test_table"
17-
{{set (ne .Id 0) "id" .Id "" | set (ne .Username "") "username" .Username | set (ne .Password "") "password" .Password | set (ne .Createtime ) "createtime" .Createtime}}
18-
{{where (ne .Id 0) "AND" "id" .Id ""}}
30+
{{set .Id "\"id\" = " (arg .Id) "" | set .Username "\"username\" = " (arg .Username) | set .Password "\"password\" = " (arg .Password) | set .Createtime "\"createtime\" = " (arg .Createtime)}}
31+
{{where .Id "AND" "\"id\" = " (arg .Id) ""}}
1932
{{end}}
2033

2134
{{define "deleteTestTable"}}
2235
DELETE FROM "test_table"
23-
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "username" .Username | where (ne .Password "") "AND" "password" .Password | where (ne .Createtime ) "AND" "createtime" .Createtime}}
36+
{{where .Id "AND" "\"id\" = " (arg .Id) "" | where .Username "AND" "\"username\" = " (arg .Username) | where .Password "AND" "\"password\" = " (arg .Password) | where .Createtime "AND" "\"createtime\" = " (arg .Createtime)}}
2437
{{end}}
2538

test/cmd/test_table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//This file was generated by xfali/gobatis-cmd at
2-
//2020-02-07 11:43:44.4189871 +0800 CST m=+0.073004201
2+
//2020-02-12 10:02:30.5593944 +0800 CST m=+0.078004501
33

44
package test_package
55

test/cmd/test_table_proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//This file was generated by xfali/gobatis-cmd at
2-
//2020-02-07 17:21:31.6652073 +0800 CST m=+0.105006001
2+
//2020-02-12 10:02:30.588396 +0800 CST m=+0.107006101
33

44
package test_package
55

@@ -10,7 +10,7 @@ import (
1010
func init() {
1111
modelV := TestTable{}
1212
gobatis.RegisterModel(&modelV)
13-
gobatis.RegisterMapperFile("./xml/test_table_mapper.xml")
13+
//gobatis.RegisterTemplateFile("./template/test_table_mapper.tmpl")
1414
}
1515

1616
func SelectTestTable(sess *gobatis.Session, model TestTable) ([]TestTable, error) {

test/cmd/xml/test_table_mapper.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--This file was generated by xfali/gobatis-cmd at -->
2-
<!--2020-02-07 17:33:24.9580053 +0800 CST m=+0.099005701-->
2+
<!--2020-02-12 10:07:07.5302362 +0800 CST m=+0.094005401-->
33

44
<mapper namespace="test_package.TestTable">
55
<sql id="columns_id">"id","username","password","createtime"</sql>

test/postgresql/postgresql.tpl

Lines changed: 4 additions & 4 deletions
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" .Username "" | where (ne .Password "pw") "AND" "password" .Password}}
4+
{{where (ne .Username "") "AND" "\"username\" =" .Username "" | where (ne .Password "pw") "AND" "\"password\"=" .Password}}
55
{{end}}
66

77
{{define "insertTestTable"}}
@@ -24,11 +24,11 @@ VALUES {{range $i, $v := .}}
2424

2525
{{define "updateTestTable"}}
2626
UPDATE test_table
27-
{{set (ne .Username "") "username" .Username "" | set (ne .Password "") "password" .Password}}
28-
{{where (ne .Id 0) "AND" "id" .Id ""}}
27+
{{set (ne .Username "") "\"username\" =" .Username "" | set (ne .Password "") "\"password\" =" .Password}}
28+
{{where (ne .Id 0) "AND" "\"id\"=" .Id ""}}
2929
{{end}}
3030

3131
{{define "deleteTestTable"}}
3232
DELETE FROM test_table
33-
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "username" .Username | where (ne .Password "") "AND" "password" .Password}}
33+
{{where (ne .Id 0) "AND" "id" .Id "" | where (ne .Username "") "AND" "\"username\"=" .Username | where (ne .Password "") "AND" "\"password\"=" .Password}}
3434
{{end}}

test/template/sql.tpl

Lines changed: 3 additions & 3 deletions
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) | where .Time "AND" "time" (arg .Time) }}
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"}}
@@ -24,13 +24,13 @@ INSERT INTO test_table ({{$COLUMNS}})
2424

2525
{{define "updateTestTable"}}
2626
UPDATE `TEST_TABLE`
27-
{{set (ne .UserName "") "username" .UserName "" | set (ne .Password "") "password" .Password | set (ne .Status -1) "status" .Status}}
27+
{{set (ne .UserName "") "\"username\"=" .UserName "" | set (ne .Password "") "\"password\"=" .Password | set (ne .Status -1) "\"status\"=" .Status}}
2828
{{if (ne .Id 0)}} WHERE `id` = {{.Id}} {{end}}
2929
{{end}}
3030

3131
{{define "deleteTestTable"}}
3232
DELETE FROM `TEST_TABLE`
33-
{{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 }}
33+
{{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 }}
3434
{{end}}
3535

3636
{{template "selectTestTable"}}

0 commit comments

Comments
 (0)