Skip to content

Commit 3127417

Browse files
committed
增加template insertBatch支持
1 parent b43f060 commit 3127417

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

parsing/template/dynamic.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func dummyWhere(b bool, cond, column string, value interface{}, origin string) s
1919
return origin
2020
}
2121

22+
func commonAdd(a, b int) int {
23+
return a + b
24+
}
25+
2226
func mysqlUpdateSet(b bool, column string, value interface{}, origin string) string {
2327
if !b {
2428
return origin
@@ -138,16 +142,19 @@ func postgresWhere(b bool, cond, column string, value interface{}, origin string
138142
var mysqlFuncMap = template.FuncMap{
139143
"set": mysqlUpdateSet,
140144
"where": mysqlWhere,
145+
"add": commonAdd,
141146
}
142147

143148
var postgresFuncMap = template.FuncMap{
144149
"set": postgresUpdateSet,
145150
"where": postgresWhere,
151+
"add": commonAdd,
146152
}
147153

148154
var dummyFuncMap = template.FuncMap{
149155
"set": dummyUpdateSet,
150156
"where": dummyWhere,
157+
"add": commonAdd,
151158
}
152159

153160
var funcMap = map[string]template.FuncMap{

test/postgresql/postgresql.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ INSERT INTO test_table ({{$COLUMNS}})
1414
)
1515
{{end}}
1616

17+
{{define "insertBatchTestTable"}}
18+
{{$size := len . | add -1}}
19+
INSERT INTO "test_table"("id","username","password")
20+
VALUES {{range $i, $v := .}}
21+
({{$v.Id}}, '{{$v.Username}}', '{{$v.Password}}'){{if lt $i $size}},{{end}}
22+
{{end}}
23+
{{end}}
24+
1725
{{define "updateTestTable"}}
1826
UPDATE test_table
1927
{{set (ne .Username "") "username" .Username "" | set (ne .Password "") "password" .Password}}

test/postgresql/session_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ func TestSessionTpl(t *testing.T) {
3030
t.Log(ret)
3131
})
3232

33+
t.Run("insertBatch", func(t *testing.T) {
34+
mgr := gobatis.NewSessionManager(connect())
35+
sess := mgr.NewSession()
36+
var ret int
37+
err := sess.Insert("insertBatchTestTable").Param([]TestTable{
38+
{Id: 13, Username: "user13", Password: "pw13"},
39+
{Id: 14, Username: "user14", Password: "pw14"},
40+
}).Result(&ret)
41+
t.Log(err)
42+
t.Log(ret)
43+
})
44+
3345
t.Run("update", func(t *testing.T) {
3446
mgr := gobatis.NewSessionManager(connect())
3547
sess := mgr.NewSession()

test/template/sql.tpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ INSERT INTO `TEST_TABLE` ({{$COLUMNS}})
1313
)
1414
{{end}}
1515

16+
{{define "insertBatchTestTable"}}
17+
{{$COLUMNS := "id, username, password"}}
18+
{{$size := len . | add -1}}
19+
INSERT INTO test_table ({{$COLUMNS}})
20+
VALUES {{range $i, $v := .}}
21+
({{$v.Id}}, '{{$v.UserName}}', '{{$v.Password}}'){{if lt $i $size}},{{end}}
22+
{{end}}
23+
{{end}}
24+
1625
{{define "updateTestTable"}}
1726
UPDATE `TEST_TABLE`
1827
{{set (ne .UserName "") "username" .UserName "" | set (ne .Password "") "password" .Password | set (ne .Status -1) "status" .Status}}

test/template/temp_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@ func TestParser2(t *testing.T) {
152152
t.Log(md)
153153
})
154154

155+
t.Run("insertBatch", func(t *testing.T) {
156+
p, ok := gobatis.FindTemplateSqlParser("insertBatchTestTable")
157+
if !ok {
158+
t.Fatal(ok)
159+
}
160+
md, err := p.ParseMetadata("mysql",[]TestTable{
161+
{Id: 11, UserName: "user11", Password: "pw11"},
162+
{Id: 12, UserName: "user12", Password: "pw12"},
163+
})
164+
if err != nil {
165+
t.Fatal(err)
166+
}
167+
168+
t.Log(md)
169+
})
170+
155171
t.Run("update", func(t *testing.T) {
156172
p, ok := gobatis.FindTemplateSqlParser("updateTestTable")
157173
if !ok {

0 commit comments

Comments
 (0)