mirror of https://github.com/go-gorm/gorm.git
Add QuoteIfPossible for Scope
This commit is contained in:
parent
fa86433142
commit
8a88d665d5
8
scope.go
8
scope.go
|
@ -3,6 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -87,6 +88,13 @@ func (scope *Scope) Quote(str string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (scope *Scope) QuoteIfPossible(str string) string {
|
||||||
|
if regexp.MustCompile("^[a-zA-Z]+(.[a-zA-Z]+)*$").MatchString(str) {
|
||||||
|
return scope.Quote(str)
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
// Dialect get dialect
|
// Dialect get dialect
|
||||||
func (scope *Scope) Dialect() Dialect {
|
func (scope *Scope) Dialect() Dialect {
|
||||||
return scope.db.parent.dialect
|
return scope.db.parent.dialect
|
||||||
|
|
|
@ -531,11 +531,7 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||||
|
|
||||||
var columns []string
|
var columns []string
|
||||||
for _, name := range column {
|
for _, name := range column {
|
||||||
if regexp.MustCompile("^[a-zA-Z]+$").MatchString(name) {
|
columns = append(columns, scope.QuoteIfPossible(name))
|
||||||
columns = append(columns, scope.Quote(name))
|
|
||||||
} else {
|
|
||||||
columns = append(columns, name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlCreate := "CREATE INDEX"
|
sqlCreate := "CREATE INDEX"
|
||||||
|
@ -550,7 +546,7 @@ func (scope *Scope) addForeignKey(field string, dest string, onDelete string, on
|
||||||
var table = scope.TableName()
|
var table = scope.TableName()
|
||||||
var keyName = fmt.Sprintf("%s_%s_foreign", table, field)
|
var keyName = fmt.Sprintf("%s_%s_foreign", table, field)
|
||||||
var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;`
|
var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;`
|
||||||
scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.Quote(keyName), scope.Quote(field), scope.Quote(dest), onDelete, onUpdate)).Exec()
|
scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.QuoteIfPossible(keyName), scope.QuoteIfPossible(field), scope.QuoteIfPossible(dest), onDelete, onUpdate)).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) removeIndex(indexName string) {
|
func (scope *Scope) removeIndex(indexName string) {
|
||||||
|
|
Loading…
Reference in New Issue