From ec21a2ad35c79504e9694e73eb0506436fc7b0b3 Mon Sep 17 00:00:00 2001 From: go-jet Date: Fri, 16 Aug 2019 14:03:24 +0200 Subject: [PATCH] Should quote identifier improvement. --- internal/jet/sql_builder.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/jet/sql_builder.go b/internal/jet/sql_builder.go index 9f9f0a3..c021fd0 100644 --- a/internal/jet/sql_builder.go +++ b/internal/jet/sql_builder.go @@ -7,6 +7,7 @@ import ( "strconv" "strings" "time" + "unicode" ) type SqlBuilder struct { @@ -86,9 +87,7 @@ func (s *SqlBuilder) WriteString(str string) { } func (s *SqlBuilder) WriteIdentifier(name string, alwaysQuote ...bool) { - quoteWrap := name != strings.ToLower(name) || strings.ContainsAny(name, ". -") - - if quoteWrap || len(alwaysQuote) > 0 { + if shouldQuoteIdentifier(name) || len(alwaysQuote) > 0 { identQuoteChar := string(s.Dialect.IdentifierQuoteChar()) s.WriteString(identQuoteChar + name + identQuoteChar) } 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 { return `'` + strings.Replace(value, "'", "''", -1) + `'` }