Should quote identifier improvement.
This commit is contained in:
parent
de57a52acc
commit
ec21a2ad35
1 changed files with 14 additions and 3 deletions
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqlBuilder struct {
|
type SqlBuilder struct {
|
||||||
|
|
@ -86,9 +87,7 @@ func (s *SqlBuilder) WriteString(str string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlBuilder) WriteIdentifier(name string, alwaysQuote ...bool) {
|
func (s *SqlBuilder) WriteIdentifier(name string, alwaysQuote ...bool) {
|
||||||
quoteWrap := name != strings.ToLower(name) || strings.ContainsAny(name, ". -")
|
if shouldQuoteIdentifier(name) || len(alwaysQuote) > 0 {
|
||||||
|
|
||||||
if quoteWrap || len(alwaysQuote) > 0 {
|
|
||||||
identQuoteChar := string(s.Dialect.IdentifierQuoteChar())
|
identQuoteChar := string(s.Dialect.IdentifierQuoteChar())
|
||||||
s.WriteString(identQuoteChar + name + identQuoteChar)
|
s.WriteString(identQuoteChar + name + identQuoteChar)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -171,6 +170,18 @@ func argToString(value interface{}) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shouldQuoteIdentifier(identifier string) bool {
|
||||||
|
for _, c := range identifier {
|
||||||
|
if unicode.IsNumber(c) || c == '_' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c > unicode.MaxASCII || !unicode.IsLetter(c) || unicode.IsUpper(c) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func stringQuote(value string) string {
|
func stringQuote(value string) string {
|
||||||
return `'` + strings.Replace(value, "'", "''", -1) + `'`
|
return `'` + strings.Replace(value, "'", "''", -1) + `'`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue