Skip to content

Commit e93b084

Browse files
committed
增加keyword参数支持
1 parent 989e059 commit e93b084

File tree

6 files changed

+99
-15
lines changed

6 files changed

+99
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ gobatis-cmd -driver=mysql -host=localhost -port=3306 -user=test -pw=test -db=tes
3838
数据库的用户名
3939
-mapper string
4040
mapper文件类型: xml | template | go (默认xml)
41+
-keyword bool
42+
是否自动添加转义符,默认false,如果为true则会根据driver名称辨识添加
4143
```
4244

4345
会在当前目录下生成1个目录及3个文件,分别为:

cmd_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,24 @@ func TestMode(t *testing.T) {
129129
}
130130

131131
func TestXml(t *testing.T) {
132+
t.Run("no keyword", func(t *testing.T) {
133+
config := Config{
134+
PackageName: "test_package",
135+
Path: "c:/tmp/",
136+
TagName: "xfield",
137+
MapperFile: "xml",
138+
}
139+
genXml(config, "test_table", *createModeInfo())
140+
})
141+
132142
t.Run("mysql", func(t *testing.T) {
133143
config := Config{
134144
PackageName: "test_package",
135145
Path: "c:/tmp/",
136146
TagName: "xfield",
137147
MapperFile: "xml",
148+
Driver: "mysql",
149+
Keyword: true,
138150
}
139151
genXml(config, "test_table", *createModeInfo())
140152
})
@@ -146,18 +158,43 @@ func TestXml(t *testing.T) {
146158
TagName: "xfield",
147159
MapperFile: "xml",
148160
Driver: "postgres",
161+
Keyword: true,
162+
}
163+
genXml(config, "test_table", *createModeInfo())
164+
})
165+
166+
t.Run("sqlserver", func(t *testing.T) {
167+
config := Config{
168+
PackageName: "test_package",
169+
Path: "c:/tmp/",
170+
TagName: "xfield",
171+
MapperFile: "xml",
172+
Driver: "mssql",
173+
Keyword: true,
149174
}
150175
genXml(config, "test_table", *createModeInfo())
151176
})
152177
}
153178

154179
func TestTemplate(t *testing.T) {
180+
t.Run("no keyword", func(t *testing.T) {
181+
config := Config{
182+
PackageName: "test_package",
183+
Path: "c:/tmp/",
184+
TagName: "xfield",
185+
MapperFile: "template",
186+
}
187+
genTemplate(config, "test_table", *createModeInfo())
188+
})
189+
155190
t.Run("mysql", func(t *testing.T) {
156191
config := Config{
157192
PackageName: "test_package",
158193
Path: "c:/tmp/",
159194
TagName: "xfield",
160195
MapperFile: "template",
196+
Driver: "mysql",
197+
Keyword: true,
161198
}
162199
genTemplate(config, "test_table", *createModeInfo())
163200
})
@@ -169,6 +206,19 @@ func TestTemplate(t *testing.T) {
169206
TagName: "xfield",
170207
MapperFile: "template",
171208
Driver: "postgres",
209+
Keyword: true,
210+
}
211+
genTemplate(config, "test_table", *createModeInfo())
212+
})
213+
214+
t.Run("sqlserver", func(t *testing.T) {
215+
config := Config{
216+
PackageName: "test_package",
217+
Path: "c:/tmp/",
218+
TagName: "xfield",
219+
MapperFile: "template",
220+
Driver: "mssql",
221+
Keyword: true,
172222
}
173223
genTemplate(config, "test_table", *createModeInfo())
174224
})

common/formatter.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,59 @@
66
package common
77

88
const (
9-
MysqlKeywordEscapeChar = "`"
10-
PostgresKeywordEscapeChar = `"`
9+
MysqlKeywordEscapeCharStart = "`"
10+
MysqlKeywordEscapeCharEnd = "`"
11+
MysqlEscapeKeywordEscapeCharStart = "`"
12+
MysqlEscapeKeywordEscapeCharEnd = "`"
1113

12-
MysqlEscapeKeywordEscapeChar = "`"
13-
PostgresEscapeKeywordEscapeChar = `\"`
14+
PostgresKeywordEscapeCharStart = `"`
15+
PostgresKeywordEscapeCharEnd = `"`
16+
PostgresEscapeKeywordEscapeCharStart = `\"`
17+
PostgresEscapeKeywordEscapeCharEnd = `\"`
18+
19+
SqlServerKeywordEscapeCharStart = `[`
20+
SqlServerKeywordEscapeCharEnd = `]`
21+
SqlServerEscapeKeywordEscapeCharStart = `[`
22+
SqlServerEscapeKeywordEscapeCharEnd = `]`
23+
24+
DummyKeywordEscapeCharStart = ""
25+
DummyKeywordEscapeCharEnd = ""
26+
DummyEscapeKeywordEscapeCharStart = ""
27+
DummyEscapeKeywordEscapeCharEnd = ""
1428
)
1529

16-
var KeywordEscapeChar = MysqlKeywordEscapeChar
17-
var EscapeKeywordEscapeChar = MysqlEscapeKeywordEscapeChar
30+
var KeywordEscapeCharStart = DummyKeywordEscapeCharStart
31+
var KeywordEscapeCharEnd = DummyKeywordEscapeCharEnd
32+
var EscapeKeywordEscapeCharStart = DummyEscapeKeywordEscapeCharStart
33+
var EscapeKeywordEscapeCharEnd = DummyEscapeKeywordEscapeCharEnd
1834

1935
type KeywordFormatter func(string) string
2036

2137
var KwFormatter = CommonKeywordFormatter
2238

2339
func CommonKeywordFormatter(src string) string {
24-
return KeywordEscapeChar + src + KeywordEscapeChar
40+
return KeywordEscapeCharStart + src + KeywordEscapeCharEnd
2541
}
2642

2743
func CommonEscapeKeywordFormatter(src string) string {
28-
return EscapeKeywordEscapeChar + src + EscapeKeywordEscapeChar
44+
return EscapeKeywordEscapeCharStart + src + EscapeKeywordEscapeCharEnd
2945
}
3046

3147
func SelectKeywordFormatter(driver string) {
32-
if driver == "postgres" {
33-
KeywordEscapeChar = PostgresKeywordEscapeChar
34-
EscapeKeywordEscapeChar = PostgresEscapeKeywordEscapeChar
48+
if driver == "mysql" {
49+
KeywordEscapeCharStart = MysqlKeywordEscapeCharStart
50+
KeywordEscapeCharEnd = MysqlKeywordEscapeCharEnd
51+
EscapeKeywordEscapeCharStart = MysqlEscapeKeywordEscapeCharStart
52+
EscapeKeywordEscapeCharEnd = MysqlEscapeKeywordEscapeCharEnd
53+
} else if driver == "mssql" || driver == "adodb" {
54+
KeywordEscapeCharStart = SqlServerKeywordEscapeCharStart
55+
KeywordEscapeCharEnd = SqlServerKeywordEscapeCharEnd
56+
EscapeKeywordEscapeCharStart = SqlServerEscapeKeywordEscapeCharStart
57+
EscapeKeywordEscapeCharEnd = SqlServerEscapeKeywordEscapeCharEnd
3558
} else {
36-
KeywordEscapeChar = MysqlKeywordEscapeChar
37-
EscapeKeywordEscapeChar = MysqlEscapeKeywordEscapeChar
59+
KeywordEscapeCharStart = PostgresKeywordEscapeCharStart
60+
KeywordEscapeCharEnd = PostgresKeywordEscapeCharEnd
61+
EscapeKeywordEscapeCharStart = PostgresEscapeKeywordEscapeCharStart
62+
EscapeKeywordEscapeCharEnd = PostgresEscapeKeywordEscapeCharEnd
3863
}
3964
}

gen_template.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
)
1818

1919
func genTemplate(config Config, tableName string, model []common.ModelInfo) {
20-
common.SelectKeywordFormatter(config.Driver)
20+
if config.Keyword {
21+
common.SelectKeywordFormatter(config.Driver)
22+
}
2123
targetDir := config.Path + "template/"
2224
if !io.IsPathExists(targetDir) {
2325
io.Mkdir(targetDir)

gen_xml.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
)
1818

1919
func genXml(config Config, tableName string, model []common.ModelInfo) {
20-
common.SelectKeywordFormatter(config.Driver)
20+
if config.Keyword {
21+
common.SelectKeywordFormatter(config.Driver)
22+
}
2123
if config.MapperFile == "xml" {
2224
xmlDir := config.Path + "xml/"
2325
if !io.IsPathExists(xmlDir) {

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Config struct {
2424
TagName string
2525
MapperFile string
2626
Plugin string
27+
Keyword bool
2728
}
2829

2930
func main() {
@@ -40,6 +41,7 @@ func main() {
4041
tagName := flag.String("tag", "xfield", "the name of field tag,eg: xfield,json xfield,json,yaml")
4142
mapper := flag.String("mapper", "xml", "generate mapper file: xml | template | go")
4243
plugin := flag.String("plugin", "", "path of plugin")
44+
keyword := flag.Bool("keyword", false, "with Keyword escape")
4345
flag.Parse()
4446

4547
db := buildinDrivers[*driver]
@@ -65,6 +67,7 @@ func main() {
6567
TagName: *tagName,
6668
MapperFile: *mapper,
6769
Plugin: *plugin,
70+
Keyword: *keyword,
6871
}
6972

7073
if *tableName == "" {

0 commit comments

Comments
 (0)