Skip to content

Commit 7f2e6e8

Browse files
committed
适配sqlserver、oracle占位符
1 parent b6e3adf commit 7f2e6e8

File tree

4 files changed

+154
-40
lines changed

4 files changed

+154
-40
lines changed

parsing/sqlparser/parse.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ func ParseWithParamMap(driverName, sql string, params map[string]interface{}) (*
183183
type holder func(int) string
184184

185185
var gHolderMap = map[string]holder{
186-
"mysql": mysqlHolder,
187-
"postgres": postgresHolder,
186+
"mysql": mysqlHolder, //mysql
187+
"postgres": postgresHolder, //postgresql
188+
"oci8": oci8Holder, //oracle
189+
"adodb": mysqlHolder, //sqlserver
188190
}
189191

190192
func selectHolder(driverName string) holder {
@@ -202,6 +204,10 @@ func postgresHolder(i int) string {
202204
return "$" + strconv.Itoa(i)
203205
}
204206

207+
func oci8Holder(i int) string {
208+
return ":" + strconv.Itoa(i)
209+
}
210+
205211
func interface2String(i interface{}) string {
206212
return fmt.Sprintf("%v", i)
207213
}

parsing/template/dynamic.go

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
const (
1717
argPlaceHolder = "_xfali_Arg_Holder"
1818
argPlaceHolderLen = 17
19+
argPlaceHolderFormat = "%s%08d"
1920
)
2021

2122
type Dynamic interface {
@@ -24,6 +25,8 @@ type Dynamic interface {
2425
format(string) (string, []interface{})
2526
}
2627

28+
var ArgPlaceHolderFormat = argPlaceHolderFormat
29+
2730
func dummyUpdateSet(b interface{}, column string, value interface{}, origin string) string {
2831
return origin
2932
}
@@ -271,9 +274,114 @@ func (d *PostgresDynamic) format(s string) (string, []interface{}) {
271274
return s, params
272275
}
273276

277+
//oracle
278+
type Oci8Dynamic struct {
279+
index int
280+
keys [] string
281+
paramMap map[string]interface{}
282+
}
283+
284+
func (d *Oci8Dynamic) getFuncMap() template.FuncMap {
285+
return template.FuncMap{
286+
"set": d.UpdateSet,
287+
"where": d.Where,
288+
"arg": d.Param,
289+
290+
"add": commonAdd,
291+
}
292+
}
293+
294+
func (d *Oci8Dynamic) UpdateSet(b interface{}, columnDesc string, value interface{}, origin string) string {
295+
if !IsTrue(b) {
296+
return origin
297+
}
298+
299+
buf := strings.Builder{}
300+
if origin == "" {
301+
buf.WriteString(" SET ")
302+
} else {
303+
origin = strings.TrimSpace(origin)
304+
buf.WriteString(origin)
305+
if origin[:len(origin)-1] != "," {
306+
buf.WriteString(",")
307+
}
308+
}
309+
buf.WriteString(columnDesc)
310+
if s, ok := value.(string); ok {
311+
if _, ok := d.paramMap[s]; ok {
312+
buf.WriteString(s)
313+
} else {
314+
buf.WriteString(`'`)
315+
buf.WriteString(s)
316+
buf.WriteString(`'`)
317+
}
318+
} else {
319+
buf.WriteString(fmt.Sprint(value))
320+
}
321+
return buf.String()
322+
}
323+
324+
func (d *Oci8Dynamic) Where(b interface{}, cond, columnDesc string, value interface{}, origin string) string {
325+
if !IsTrue(b) {
326+
return origin
327+
}
328+
329+
buf := strings.Builder{}
330+
if origin == "" {
331+
buf.WriteString(" WHERE ")
332+
cond = ""
333+
} else {
334+
buf.WriteString(strings.TrimSpace(origin))
335+
buf.WriteString(" ")
336+
buf.WriteString(cond)
337+
buf.WriteString(" ")
338+
}
339+
340+
buf.WriteString(columnDesc)
341+
if s, ok := value.(string); ok {
342+
if _, ok := d.paramMap[s]; ok {
343+
buf.WriteString(s)
344+
} else {
345+
buf.WriteString(`'`)
346+
buf.WriteString(s)
347+
buf.WriteString(`'`)
348+
}
349+
} else {
350+
buf.WriteString(fmt.Sprint(value))
351+
}
352+
return buf.String()
353+
}
354+
355+
func (d *Oci8Dynamic) getParam() []interface{} {
356+
return nil
357+
}
358+
359+
func (d *Oci8Dynamic) Param(p interface{}) string {
360+
d.index++
361+
key := getPlaceHolderKey(d.index)
362+
d.paramMap[key] = p
363+
d.keys = append(d.keys, key)
364+
return key
365+
}
366+
367+
func (d *Oci8Dynamic) format(s string) (string, []interface{}) {
368+
i, index := 0, 1
369+
var params []interface{}
370+
for _, k := range d.keys {
371+
s, i = replace(s, k, ":"+strconv.Itoa(index), -1)
372+
if i > 0 {
373+
params = append(params, d.paramMap[k])
374+
index++
375+
}
376+
}
377+
return s, params
378+
}
379+
274380
var dynamicMap = map[string]Dynamic{
275381
"mysql": &MysqlDynamic{paramMap: map[string]interface{}{}},
382+
"adodb": &MysqlDynamic{paramMap: map[string]interface{}{}},
276383
"postgres": &PostgresDynamic{paramMap: map[string]interface{}{}},
384+
"oci8": &MysqlDynamic{paramMap: map[string]interface{}{}},
277385
}
278386

279387
func selectDynamic(driverName string) Dynamic {
@@ -340,5 +448,5 @@ func IsTrue(i interface{}) bool {
340448
}
341449

342450
func getPlaceHolderKey(index int) string {
343-
return fmt.Sprintf("%s%06d", argPlaceHolder, index)
451+
return fmt.Sprintf(ArgPlaceHolderFormat, argPlaceHolder, index)
344452
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
{{/*This file was generated by xfali/gobatis-cmd at*/}}
2-
{{/*2020-02-12 10:02:30.5793955 +0800 CST m=+0.098005601*/}}
2+
{{/*2020-02-12 15:10:06.7050255 +0800 CST m=+0.085004901*/}}
33

44
{{define "selectTestTable"}}
5-
SELECT "id","username","password","createtime" FROM "test_table"
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)}}
5+
SELECT id,username,password,createtime FROM test_table
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)}}
77
{{end}}
88

99
{{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)}}
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)}}
1212
{{end}}
1313

1414
{{define "insertTestTable"}}
15-
INSERT INTO "test_table"("id","username","password","createtime")
15+
INSERT INTO test_table(id,username,password,createtime)
1616
VALUES(
1717
{{arg .Id}}, {{arg .Username}}, {{arg .Password}}, {{arg .Createtime}})
1818
{{end}}
1919

2020
{{define "insertBatchTestTable"}}
2121
{{$size := len . | add -1}}
22-
INSERT INTO "test_table"("username","password","createtime")
22+
INSERT INTO test_table(id,username,password,createtime)
2323
VALUES {{range $i, $v := .}}
24-
({{arg $v.Username}}, {{arg $v.Password}}, {{arg $v.Createtime}}){{if lt $i $size}},{{end}}
24+
({{arg $v.Id}}, {{arg $v.Username}}, {{arg $v.Password}}, {{arg $v.Createtime}}){{if lt $i $size}},{{end}}
2525
{{end}}
2626
{{end}}
2727

2828
{{define "updateTestTable"}}
29-
UPDATE "test_table"
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) ""}}
29+
UPDATE test_table
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) ""}}
3232
{{end}}
3333

3434
{{define "deleteTestTable"}}
35-
DELETE FROM "test_table"
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)}}
35+
DELETE FROM test_table
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)}}
3737
{{end}}
3838

test/cmd/xml/test_table_mapper.xml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<!--This file was generated by xfali/gobatis-cmd at -->
2-
<!--2020-02-12 10:07:07.5302362 +0800 CST m=+0.094005401-->
2+
<!--2020-02-12 15:09:30.9889827 +0800 CST m=+0.098005601-->
33

44
<mapper namespace="test_package.TestTable">
5-
<sql id="columns_id">"id","username","password","createtime"</sql>
5+
<sql id="columns_id">id,username,password,createtime</sql>
66

77
<select id="selectTestTable">
8-
SELECT <include refid="columns_id"> </include> FROM "test_table"
8+
SELECT <include refid="columns_id"> </include> FROM test_table
99
<where>
10-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND "id" = #{TestTable.id} </if>
11-
<if test="{TestTable.username} != nil">AND "username" = #{TestTable.username} </if>
12-
<if test="{TestTable.password} != nil">AND "password" = #{TestTable.password} </if>
13-
<if test="{TestTable.createtime} != nil">AND "createtime" = #{TestTable.createtime} </if>
10+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
11+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
12+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
13+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
1414
</where>
1515
</select>
1616

1717
<select id="selectTestTableCount">
18-
SELECT COUNT(*) FROM "test_table"
18+
SELECT COUNT(*) FROM test_table
1919
<where>
20-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND "id" = #{TestTable.id} </if>
21-
<if test="{TestTable.username} != nil">AND "username" = #{TestTable.username} </if>
22-
<if test="{TestTable.password} != nil">AND "password" = #{TestTable.password} </if>
23-
<if test="{TestTable.createtime} != nil">AND "createtime" = #{TestTable.createtime} </if>
20+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
21+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
22+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
23+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
2424
</where>
2525
</select>
2626

2727
<insert id="insertTestTable">
28-
INSERT INTO "test_table" ("id","username","password","createtime")
28+
INSERT INTO test_table (id,username,password,createtime)
2929
VALUES(
3030
#{TestTable.id},
3131
#{TestTable.username},
@@ -35,30 +35,30 @@
3535
</insert>
3636

3737
<insert id="insertBatchTestTable">
38-
INSERT INTO "test_table" ("id","username","password","createtime")
38+
INSERT INTO test_table (id,username,password,createtime)
3939
VALUES
4040
<foreach item="item" index="index" collection="{0}" open="" separator="," close="">
4141
(#{item.TestTable.id},#{item.TestTable.username},#{item.TestTable.password},#{item.TestTable.createtime})
4242
</foreach>
4343
</insert>
4444

4545
<update id="updateTestTable">
46-
UPDATE "test_table"
46+
UPDATE test_table
4747
<set>
48-
<if test="{TestTable.username} != nil"> "username" = #{TestTable.username} </if>
49-
<if test="{TestTable.password} != nil"> "password" = #{TestTable.password} </if>
50-
<if test="{TestTable.createtime} != nil"> "createtime" = #{TestTable.createtime} </if>
48+
<if test="{TestTable.username} != nil"> username = #{TestTable.username} </if>
49+
<if test="{TestTable.password} != nil"> password = #{TestTable.password} </if>
50+
<if test="{TestTable.createtime} != nil"> createtime = #{TestTable.createtime} </if>
5151
</set>
52-
WHERE "id" = #{TestTable.id}
52+
WHERE id = #{TestTable.id}
5353
</update>
5454

5555
<delete id="deleteTestTable">
56-
DELETE FROM "test_table"
56+
DELETE FROM test_table
5757
<where>
58-
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND "id" = #{TestTable.id} </if>
59-
<if test="{TestTable.username} != nil">AND "username" = #{TestTable.username} </if>
60-
<if test="{TestTable.password} != nil">AND "password" = #{TestTable.password} </if>
61-
<if test="{TestTable.createtime} != nil">AND "createtime" = #{TestTable.createtime} </if>
58+
<if test="{TestTable.id} != nil and {TestTable.id} != 0">AND id = #{TestTable.id} </if>
59+
<if test="{TestTable.username} != nil">AND username = #{TestTable.username} </if>
60+
<if test="{TestTable.password} != nil">AND password = #{TestTable.password} </if>
61+
<if test="{TestTable.createtime} != nil">AND createtime = #{TestTable.createtime} </if>
6262
</where>
6363
</delete>
6464
</mapper>

0 commit comments

Comments
 (0)