Skip to content

Commit cc33b2d

Browse files
committed
Fix join table name quote bug (#1534)
Fix test Fix test Add new Quoter object to handle quote Fix join table name quote bug Move reserve words related files into dialects sub package (#1544) Move reserve words related files into dialects sub package Reviewed-on: https://gitea.com/xorm/xorm/pulls/1544 Fix mssql quote (#1535) Fix some quotes Fix mssql quote Merge core package back into the main repository and split into serval sub packages. (#1543) Fix test Improve fmt update go.mod Move core as a sub package Reviewed-on: https://gitea.com/xorm/xorm/pulls/1543 Fix int time deleted bug (#1539) Fix panic Fix test Fix test for mssql time Add sql type check on deleted cond Fix int time deleted bug Reviewed-on: https://gitea.com/xorm/xorm/pulls/1539 Add test for mysql8.0 (#1538) Fix pk order on test Add test for mysql8.0 Reviewed-on: https://gitea.com/xorm/xorm/pulls/1538 Add test for join limit (#1536) Add test for join limit Reviewed-on: https://gitea.com/xorm/xorm/pulls/1536 Improve drone (#1537) Fix drone Improve drone * use traditional positional parameters on inser... Reviewed-on: https://gitea.com/xorm/xorm/pulls/1537 Fix slice of struct not cache bug (go-xorm#895) Fix failure caused by nil bean Judge both type of struct and pointer in case of out-of-range Fix issue go-xorm#894 Add test for join subquery (#1528) Fix test Fix subquery with schema Add test for join subquery Add makefile (#1531) Fix drone Fix ci Add deps Improve drone Fix envs Add makefile Reviewed-on: https://gitea.com/xorm/xorm/pulls/1531 Add password for postgres drone image (#1530) Add password for postgres drone image Reviewed-on: https://gitea.com/xorm/xorm/pulls/1530 format time when sqlTypeName is core.Varchar (go-xorm#1026) fix time test add test for time format sign codes according to contributing rules. format time when sqlTypeName is core.Varchar. Same with core.DateTime or core.TimeStamp Add test for second insert error (#1527) Add test for second insert error Reviewed-on: https://gitea.com/xorm/xorm/pulls/1527 Add tests for table name (#1517) add tests for table name Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix test (#1526) Fix test Reviewed-on: https://gitea.com/xorm/xorm/pulls/1526 Fix wrong warning log on autoincrement column when sync table (#1525) improve doc Fix wrong warning log on autoincrement column when sync table Reviewed-on: https://gitea.com/xorm/xorm/pulls/1525 Fixed Join strings on func Exist (#1520) fix test fixed Join strings on func Exist Co-authored-by: Tomofumi Kusana <[email protected]> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1520 For nullable columns, store nil values as NULL (go-xorm#531) Merge branch 'master' into jcsalem/fix/nil_ptr_is_nullable fix bug when buffersize with iterate (go-xorm#941) Merge branch 'master' into lunny/fix_buffer_iterate Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 fix test fix bug fix bug when buffersize with iterate SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (go-xorm#1455) Co-authored-by: Guillermo Prandi <[email protected]> Reviewed-on: https://gitea.com/xorm/xorm/pulls/941 fix update map with version (go-xorm#1448) fix test fix update map with version SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (go-xorm#1455) Reviewed-on: https://gitea.com/xorm/xorm/pulls/1448 Exclude schema from index name (#1505) Merge branch 'master' into fix-schema-idx SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 fix vet fix drone lint remove go1.10 test on drone Exclude schema from the index name Co-authored-by: Guillermo Prandi <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505 SetExpr support more go types (#1499) Improve tests SetExpr support more go types fix vet fix drone lint remove go1.10 test on drone Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499 For nullable columns, store nil values as NULL fix vet fix drone lint remove go1.10 test on drone Fix update with Alias (go-xorm#1455) Improve c... Reviewed-on: https://gitea.com/xorm/xorm/pulls/1534
1 parent 3df7714 commit cc33b2d

18 files changed

+276
-200
lines changed

dialects/dialect.go

+18-34
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ type Dialect interface {
4545
DataSourceName() string
4646

4747
IsReserved(string) bool
48-
Quote(string) string
48+
Quoter() schemas.Quoter
4949

50-
AndStr() string
51-
OrStr() string
52-
EqStr() string
5350
RollBackStr() string
5451
AutoIncrStr() string
5552

@@ -101,7 +98,7 @@ type Base struct {
10198

10299
// String generate column description string according dialect
103100
func String(d Dialect, col *schemas.Column) string {
104-
sql := d.Quote(col.Name) + " "
101+
sql := d.Quoter().Quote(col.Name) + " "
105102

106103
sql += d.SQLType(col) + " "
107104

@@ -129,7 +126,7 @@ func String(d Dialect, col *schemas.Column) string {
129126

130127
// StringNoPk generate column description string according dialect without primary keys
131128
func StringNoPk(d Dialect, col *schemas.Column) string {
132-
sql := d.Quote(col.Name) + " "
129+
sql := d.Quoter().Quote(col.Name) + " "
133130

134131
sql += d.SQLType(col) + " "
135132

@@ -186,18 +183,6 @@ func (b *Base) DataSourceName() string {
186183
return b.dataSourceName
187184
}
188185

189-
func (b *Base) AndStr() string {
190-
return "AND"
191-
}
192-
193-
func (b *Base) OrStr() string {
194-
return "OR"
195-
}
196-
197-
func (b *Base) EqStr() string {
198-
return "="
199-
}
200-
201186
func (db *Base) RollBackStr() string {
202187
return "ROLL BACK"
203188
}
@@ -207,7 +192,7 @@ func (db *Base) SupportDropIfExists() bool {
207192
}
208193

209194
func (db *Base) DropTableSQL(tableName string) string {
210-
quote := db.dialect.Quote
195+
quote := db.dialect.Quoter().Quote
211196
return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName))
212197
}
213198

@@ -226,14 +211,15 @@ func (db *Base) HasRecords(query string, args ...interface{}) (bool, error) {
226211
}
227212

228213
func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
214+
quote := db.dialect.Quoter().Quote
229215
query := fmt.Sprintf(
230216
"SELECT %v FROM %v.%v WHERE %v = ? AND %v = ? AND %v = ?",
231-
db.dialect.Quote("COLUMN_NAME"),
232-
db.dialect.Quote("INFORMATION_SCHEMA"),
233-
db.dialect.Quote("COLUMNS"),
234-
db.dialect.Quote("TABLE_SCHEMA"),
235-
db.dialect.Quote("TABLE_NAME"),
236-
db.dialect.Quote("COLUMN_NAME"),
217+
quote("COLUMN_NAME"),
218+
quote("INFORMATION_SCHEMA"),
219+
quote("COLUMNS"),
220+
quote("TABLE_SCHEMA"),
221+
quote("TABLE_NAME"),
222+
quote("COLUMN_NAME"),
237223
)
238224
return db.HasRecords(query, db.uri.DBName, tableName, colName)
239225
}
@@ -263,21 +249,20 @@ func (db *Base) CreateTableIfNotExists(table *Table, tableName, storeEngine, cha
263249
}*/
264250

265251
func (db *Base) CreateIndexSQL(tableName string, index *schemas.Index) string {
266-
quotes := db.dialect.Quote("")
267-
quote := db.dialect.Quote
252+
quoter := db.dialect.Quoter()
268253
var unique string
269254
var idxName string
270255
if index.Type == schemas.UniqueType {
271256
unique = " UNIQUE"
272257
}
273258
idxName = index.XName(tableName)
274259
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique,
275-
quote(idxName), quote(tableName),
276-
quote(strings.Join(index.Cols, fmt.Sprintf("%c,%c", quotes[1], quotes[0]))))
260+
quoter.Quote(idxName), quoter.Quote(tableName),
261+
quoter.Quote(strings.Join(index.Cols, quoter.ReverseQuote(","))))
277262
}
278263

279264
func (db *Base) DropIndexSQL(tableName string, index *schemas.Index) string {
280-
quote := db.dialect.Quote
265+
quote := db.dialect.Quoter().Quote
281266
var name string
282267
if index.IsRegular {
283268
name = index.XName(tableName)
@@ -298,11 +283,10 @@ func (b *Base) CreateTableSQL(table *schemas.Table, tableName, storeEngine, char
298283
tableName = table.Name
299284
}
300285

301-
sql += b.dialect.Quote(tableName)
286+
quoter := b.dialect.Quoter()
287+
sql += quoter.Quote(tableName)
302288
sql += " ("
303289

304-
quotes := b.dialect.Quote("")
305-
306290
if len(table.ColumnsSeq()) > 0 {
307291
pkList := table.PrimaryKeys
308292

@@ -322,7 +306,7 @@ func (b *Base) CreateTableSQL(table *schemas.Table, tableName, storeEngine, char
322306

323307
if len(pkList) > 1 {
324308
sql += "PRIMARY KEY ( "
325-
sql += b.dialect.Quote(strings.Join(pkList, fmt.Sprintf("%c,%c", quotes[1], quotes[0])))
309+
sql += quoter.Quote(strings.Join(pkList, quoter.ReverseQuote(",")))
326310
sql += " ), "
327311
}
328312

dialects/filter.go

+4-28
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ type QuoteFilter struct {
2121
}
2222

2323
func (s *QuoteFilter) Do(sql string, dialect Dialect, table *schemas.Table) string {
24-
dummy := dialect.Quote("")
25-
if len(dummy) != 2 {
24+
quoter := dialect.Quoter()
25+
if quoter.IsEmpty() {
2626
return sql
2727
}
28-
prefix, suffix := dummy[0], dummy[1]
28+
29+
prefix, suffix := quoter[0][0], quoter[1][0]
2930
raw := []byte(sql)
3031
for i, cnt := 0, 0; i < len(raw); i = i + 1 {
3132
if raw[i] == '`' {
@@ -38,32 +39,7 @@ func (s *QuoteFilter) Do(sql string, dialect Dialect, table *schemas.Table) stri
3839
}
3940
}
4041
return string(raw)
41-
}
42-
43-
// IdFilter filter SQL replace (id) to primary key column name
44-
type IdFilter struct {
45-
}
46-
47-
type Quoter struct {
48-
dialect Dialect
49-
}
5042

51-
func NewQuoter(dialect Dialect) *Quoter {
52-
return &Quoter{dialect}
53-
}
54-
55-
func (q *Quoter) Quote(content string) string {
56-
return q.dialect.Quote(content)
57-
}
58-
59-
func (i *IdFilter) Do(sql string, dialect Dialect, table *schemas.Table) string {
60-
quoter := NewQuoter(dialect)
61-
if table != nil && len(table.PrimaryKeys) == 1 {
62-
sql = strings.Replace(sql, " `(id)` ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
63-
sql = strings.Replace(sql, " "+quoter.Quote("(id)")+" ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
64-
return strings.Replace(sql, " (id) ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
65-
}
66-
return sql
6743
}
6844

6945
// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...

dialects/mssql.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ func (db *mssql) IsReserved(name string) bool {
286286
return ok
287287
}
288288

289-
func (db *mssql) Quote(name string) string {
290-
return "[" + name + "]"
289+
func (db *mssql) Quoter() schemas.Quoter {
290+
return schemas.Quoter{"[", "]"}
291291
}
292292

293293
func (db *mssql) SupportEngine() bool {
@@ -503,7 +503,7 @@ func (db *mssql) CreateTableSQL(table *schemas.Table, tableName, storeEngine, ch
503503

504504
sql = "IF NOT EXISTS (SELECT [name] FROM sys.tables WHERE [name] = '" + tableName + "' ) CREATE TABLE "
505505

506-
sql += db.Quote(tableName) + " ("
506+
sql += db.Quoter().Quote(tableName) + " ("
507507

508508
pkList := table.PrimaryKeys
509509

@@ -534,7 +534,7 @@ func (db *mssql) ForUpdateSQL(query string) string {
534534
}
535535

536536
func (db *mssql) Filters() []Filter {
537-
return []Filter{&IdFilter{}, &QuoteFilter{}}
537+
return []Filter{&QuoteFilter{}}
538538
}
539539

540540
type odbcDriver struct {

dialects/mysql.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ func (db *mysql) IsReserved(name string) bool {
275275
return ok
276276
}
277277

278-
func (db *mysql) Quote(name string) string {
279-
return "`" + name + "`"
278+
func (db *mysql) Quoter() schemas.Quoter {
279+
return schemas.Quoter{"`", "`"}
280280
}
281281

282282
func (db *mysql) SupportEngine() bool {
@@ -512,9 +512,9 @@ func (db *mysql) CreateTableSQL(table *schemas.Table, tableName, storeEngine, ch
512512
tableName = table.Name
513513
}
514514

515-
quotes := db.Quote("")
515+
quoter := db.Quoter()
516516

517-
sql += db.Quote(tableName)
517+
sql += quoter.Quote(tableName)
518518
sql += " ("
519519

520520
if len(table.ColumnsSeq()) > 0 {
@@ -536,7 +536,7 @@ func (db *mysql) CreateTableSQL(table *schemas.Table, tableName, storeEngine, ch
536536

537537
if len(pkList) > 1 {
538538
sql += "PRIMARY KEY ( "
539-
sql += db.Quote(strings.Join(pkList, fmt.Sprintf("%c,%c", quotes[1], quotes[0])))
539+
sql += quoter.Quote(strings.Join(pkList, quoter.ReverseQuote(",")))
540540
sql += " ), "
541541
}
542542

@@ -562,7 +562,7 @@ func (db *mysql) CreateTableSQL(table *schemas.Table, tableName, storeEngine, ch
562562
}
563563

564564
func (db *mysql) Filters() []Filter {
565-
return []Filter{&IdFilter{}}
565+
return []Filter{}
566566
}
567567

568568
type mymysqlDriver struct {

dialects/oracle.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ func (db *oracle) IsReserved(name string) bool {
552552
return ok
553553
}
554554

555-
func (db *oracle) Quote(name string) string {
556-
return "[" + name + "]"
555+
func (db *oracle) Quoter() schemas.Quoter {
556+
return schemas.Quoter{"[", "]"}
557557
}
558558

559559
func (db *oracle) SupportEngine() bool {
@@ -582,7 +582,8 @@ func (db *oracle) CreateTableSQL(table *schemas.Table, tableName, storeEngine, c
582582
tableName = table.Name
583583
}
584584

585-
sql += db.Quote(tableName) + " ("
585+
quoter := db.Quoter()
586+
sql += quoter.Quote(tableName) + " ("
586587

587588
pkList := table.PrimaryKeys
588589

@@ -597,11 +598,9 @@ func (db *oracle) CreateTableSQL(table *schemas.Table, tableName, storeEngine, c
597598
sql += ", "
598599
}
599600

600-
quotes := db.Quote("")
601-
602601
if len(pkList) > 0 {
603602
sql += "PRIMARY KEY ( "
604-
sql += db.Quote(strings.Join(pkList, fmt.Sprintf("%c,%c", quotes[1], quotes[0])))
603+
sql += quoter.Quote(strings.Join(pkList, quoter.ReverseQuote(",")))
605604
sql += " ), "
606605
}
607606

@@ -849,7 +848,7 @@ func (db *oracle) GetIndexes(tableName string) (map[string]*schemas.Index, error
849848
}
850849

851850
func (db *oracle) Filters() []Filter {
852-
return []Filter{&QuoteFilter{}, &SeqFilter{Prefix: ":", Start: 1}, &IdFilter{}}
851+
return []Filter{&QuoteFilter{}, &SeqFilter{Prefix: ":", Start: 1}}
853852
}
854853

855854
type goracleDriver struct {

dialects/postgres.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,8 @@ func (db *postgres) IsReserved(name string) bool {
859859
return ok
860860
}
861861

862-
func (db *postgres) Quote(name string) string {
863-
name = strings.Replace(name, ".", `"."`, -1)
864-
return "\"" + name + "\""
862+
func (db *postgres) Quoter() schemas.Quoter {
863+
return schemas.Quoter{`"`, `"`}
865864
}
866865

867866
func (db *postgres) AutoIncrStr() string {
@@ -911,7 +910,6 @@ func (db *postgres) ModifyColumnSQL(tableName string, col *schemas.Column) strin
911910
}
912911

913912
func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string {
914-
quote := db.Quote
915913
idxName := index.Name
916914

917915
tableParts := strings.Split(strings.Replace(tableName, `"`, "", -1), ".")
@@ -928,7 +926,7 @@ func (db *postgres) DropIndexSQL(tableName string, index *schemas.Index) string
928926
if db.uri.Schema != "" {
929927
idxName = db.uri.Schema + "." + idxName
930928
}
931-
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
929+
return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName))
932930
}
933931

934932
func (db *postgres) IsColumnExist(tableName, colName string) (bool, error) {
@@ -1161,7 +1159,7 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*schemas.Index, err
11611159
}
11621160

11631161
func (db *postgres) Filters() []Filter {
1164-
return []Filter{&IdFilter{}, &QuoteFilter{}, &SeqFilter{Prefix: "$", Start: 1}}
1162+
return []Filter{&QuoteFilter{}, &SeqFilter{Prefix: "$", Start: 1}}
11651163
}
11661164

11671165
type pqDriver struct {

dialects/sqlite3.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func (db *sqlite3) IsReserved(name string) bool {
199199
return ok
200200
}
201201

202-
func (db *sqlite3) Quote(name string) string {
203-
return "`" + name + "`"
202+
func (db *sqlite3) Quoter() schemas.Quoter {
203+
return schemas.Quoter{"`", "`"}
204204
}
205205

206206
func (db *sqlite3) AutoIncrStr() string {
@@ -231,7 +231,6 @@ func (db *sqlite3) TableCheckSQL(tableName string) (string, []interface{}) {
231231

232232
func (db *sqlite3) DropIndexSQL(tableName string, index *schemas.Index) string {
233233
// var unique string
234-
quote := db.Quote
235234
idxName := index.Name
236235

237236
if !strings.HasPrefix(idxName, "UQE_") &&
@@ -242,7 +241,7 @@ func (db *sqlite3) DropIndexSQL(tableName string, index *schemas.Index) string {
242241
idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
243242
}
244243
}
245-
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
244+
return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName))
246245
}
247246

248247
func (db *sqlite3) ForUpdateSQL(query string) string {
@@ -478,7 +477,7 @@ func (db *sqlite3) GetIndexes(tableName string) (map[string]*schemas.Index, erro
478477
}
479478

480479
func (db *sqlite3) Filters() []Filter {
481-
return []Filter{&IdFilter{}}
480+
return []Filter{}
482481
}
483482

484483
type sqlite3Driver struct {

0 commit comments

Comments
 (0)