Skip to content

Commit

Permalink
Fix join table name quote bug (#1534)
Browse files Browse the repository at this point in the history
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 <tkusana@morisawa.co.jp>
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 <guillep2k@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
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 <guillep2k@noreply.gitea.io>
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 <guillep2k@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
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
  • Loading branch information
lunny committed Feb 25, 2020
1 parent 3df7714 commit cc33b2d
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 200 deletions.
52 changes: 18 additions & 34 deletions dialects/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ type Dialect interface {
DataSourceName() string

IsReserved(string) bool
Quote(string) string
Quoter() schemas.Quoter

AndStr() string
OrStr() string
EqStr() string
RollBackStr() string
AutoIncrStr() string

Expand Down Expand Up @@ -101,7 +98,7 @@ type Base struct {

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

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

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

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

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

Expand Down Expand Up @@ -186,18 +183,6 @@ func (b *Base) DataSourceName() string {
return b.dataSourceName
}

func (b *Base) AndStr() string {
return "AND"
}

func (b *Base) OrStr() string {
return "OR"
}

func (b *Base) EqStr() string {
return "="
}

func (db *Base) RollBackStr() string {
return "ROLL BACK"
}
Expand All @@ -207,7 +192,7 @@ func (db *Base) SupportDropIfExists() bool {
}

func (db *Base) DropTableSQL(tableName string) string {
quote := db.dialect.Quote
quote := db.dialect.Quoter().Quote
return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName))
}

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

func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
quote := db.dialect.Quoter().Quote
query := fmt.Sprintf(
"SELECT %v FROM %v.%v WHERE %v = ? AND %v = ? AND %v = ?",
db.dialect.Quote("COLUMN_NAME"),
db.dialect.Quote("INFORMATION_SCHEMA"),
db.dialect.Quote("COLUMNS"),
db.dialect.Quote("TABLE_SCHEMA"),
db.dialect.Quote("TABLE_NAME"),
db.dialect.Quote("COLUMN_NAME"),
quote("COLUMN_NAME"),
quote("INFORMATION_SCHEMA"),
quote("COLUMNS"),
quote("TABLE_SCHEMA"),
quote("TABLE_NAME"),
quote("COLUMN_NAME"),
)
return db.HasRecords(query, db.uri.DBName, tableName, colName)
}
Expand Down Expand Up @@ -263,21 +249,20 @@ func (db *Base) CreateTableIfNotExists(table *Table, tableName, storeEngine, cha
}*/

func (db *Base) CreateIndexSQL(tableName string, index *schemas.Index) string {
quotes := db.dialect.Quote("")
quote := db.dialect.Quote
quoter := db.dialect.Quoter()
var unique string
var idxName string
if index.Type == schemas.UniqueType {
unique = " UNIQUE"
}
idxName = index.XName(tableName)
return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v)", unique,
quote(idxName), quote(tableName),
quote(strings.Join(index.Cols, fmt.Sprintf("%c,%c", quotes[1], quotes[0]))))
quoter.Quote(idxName), quoter.Quote(tableName),
quoter.Quote(strings.Join(index.Cols, quoter.ReverseQuote(","))))
}

func (db *Base) DropIndexSQL(tableName string, index *schemas.Index) string {
quote := db.dialect.Quote
quote := db.dialect.Quoter().Quote
var name string
if index.IsRegular {
name = index.XName(tableName)
Expand All @@ -298,11 +283,10 @@ func (b *Base) CreateTableSQL(table *schemas.Table, tableName, storeEngine, char
tableName = table.Name
}

sql += b.dialect.Quote(tableName)
quoter := b.dialect.Quoter()
sql += quoter.Quote(tableName)
sql += " ("

quotes := b.dialect.Quote("")

if len(table.ColumnsSeq()) > 0 {
pkList := table.PrimaryKeys

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

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

Expand Down
32 changes: 4 additions & 28 deletions dialects/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type QuoteFilter struct {
}

func (s *QuoteFilter) Do(sql string, dialect Dialect, table *schemas.Table) string {
dummy := dialect.Quote("")
if len(dummy) != 2 {
quoter := dialect.Quoter()
if quoter.IsEmpty() {
return sql
}
prefix, suffix := dummy[0], dummy[1]

prefix, suffix := quoter[0][0], quoter[1][0]
raw := []byte(sql)
for i, cnt := 0, 0; i < len(raw); i = i + 1 {
if raw[i] == '`' {
Expand All @@ -38,32 +39,7 @@ func (s *QuoteFilter) Do(sql string, dialect Dialect, table *schemas.Table) stri
}
}
return string(raw)
}

// IdFilter filter SQL replace (id) to primary key column name
type IdFilter struct {
}

type Quoter struct {
dialect Dialect
}

func NewQuoter(dialect Dialect) *Quoter {
return &Quoter{dialect}
}

func (q *Quoter) Quote(content string) string {
return q.dialect.Quote(content)
}

func (i *IdFilter) Do(sql string, dialect Dialect, table *schemas.Table) string {
quoter := NewQuoter(dialect)
if table != nil && len(table.PrimaryKeys) == 1 {
sql = strings.Replace(sql, " `(id)` ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
sql = strings.Replace(sql, " "+quoter.Quote("(id)")+" ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
return strings.Replace(sql, " (id) ", " "+quoter.Quote(table.PrimaryKeys[0])+" ", -1)
}
return sql
}

// SeqFilter filter SQL replace ?, ? ... to $1, $2 ...
Expand Down
8 changes: 4 additions & 4 deletions dialects/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ func (db *mssql) IsReserved(name string) bool {
return ok
}

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

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

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

sql += db.Quote(tableName) + " ("
sql += db.Quoter().Quote(tableName) + " ("

pkList := table.PrimaryKeys

Expand Down Expand Up @@ -534,7 +534,7 @@ func (db *mssql) ForUpdateSQL(query string) string {
}

func (db *mssql) Filters() []Filter {
return []Filter{&IdFilter{}, &QuoteFilter{}}
return []Filter{&QuoteFilter{}}
}

type odbcDriver struct {
Expand Down
12 changes: 6 additions & 6 deletions dialects/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ func (db *mysql) IsReserved(name string) bool {
return ok
}

func (db *mysql) Quote(name string) string {
return "`" + name + "`"
func (db *mysql) Quoter() schemas.Quoter {
return schemas.Quoter{"`", "`"}
}

func (db *mysql) SupportEngine() bool {
Expand Down Expand Up @@ -512,9 +512,9 @@ func (db *mysql) CreateTableSQL(table *schemas.Table, tableName, storeEngine, ch
tableName = table.Name
}

quotes := db.Quote("")
quoter := db.Quoter()

sql += db.Quote(tableName)
sql += quoter.Quote(tableName)
sql += " ("

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

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

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

func (db *mysql) Filters() []Filter {
return []Filter{&IdFilter{}}
return []Filter{}
}

type mymysqlDriver struct {
Expand Down
13 changes: 6 additions & 7 deletions dialects/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ func (db *oracle) IsReserved(name string) bool {
return ok
}

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

func (db *oracle) SupportEngine() bool {
Expand Down Expand Up @@ -582,7 +582,8 @@ func (db *oracle) CreateTableSQL(table *schemas.Table, tableName, storeEngine, c
tableName = table.Name
}

sql += db.Quote(tableName) + " ("
quoter := db.Quoter()
sql += quoter.Quote(tableName) + " ("

pkList := table.PrimaryKeys

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

quotes := db.Quote("")

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

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

func (db *oracle) Filters() []Filter {
return []Filter{&QuoteFilter{}, &SeqFilter{Prefix: ":", Start: 1}, &IdFilter{}}
return []Filter{&QuoteFilter{}, &SeqFilter{Prefix: ":", Start: 1}}
}

type goracleDriver struct {
Expand Down
10 changes: 4 additions & 6 deletions dialects/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,8 @@ func (db *postgres) IsReserved(name string) bool {
return ok
}

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

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

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

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

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

func (db *postgres) Filters() []Filter {
return []Filter{&IdFilter{}, &QuoteFilter{}, &SeqFilter{Prefix: "$", Start: 1}}
return []Filter{&QuoteFilter{}, &SeqFilter{Prefix: "$", Start: 1}}
}

type pqDriver struct {
Expand Down
9 changes: 4 additions & 5 deletions dialects/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ func (db *sqlite3) IsReserved(name string) bool {
return ok
}

func (db *sqlite3) Quote(name string) string {
return "`" + name + "`"
func (db *sqlite3) Quoter() schemas.Quoter {
return schemas.Quoter{"`", "`"}
}

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

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

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

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

func (db *sqlite3) Filters() []Filter {
return []Filter{&IdFilter{}}
return []Filter{}
}

type sqlite3Driver struct {
Expand Down
Loading

0 comments on commit cc33b2d

Please sign in to comment.