fix: quote identifiers starting with numbers

This commit is contained in:
Andrei-hub11 2025-07-20 14:23:18 -03:00
parent 8667bca8fc
commit b6fa663315
2 changed files with 18 additions and 5 deletions

View file

@ -4,15 +4,16 @@ import (
"bytes" "bytes"
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"github.com/go-jet/jet/v2/internal/3rdparty/pq"
"github.com/go-jet/jet/v2/internal/utils/is"
"github.com/google/uuid"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode" "unicode"
"github.com/go-jet/jet/v2/internal/3rdparty/pq"
"github.com/go-jet/jet/v2/internal/utils/is"
"github.com/google/uuid"
) )
// SQLBuilder generates output SQL // SQLBuilder generates output SQL
@ -302,12 +303,21 @@ func integerTypesToString(value interface{}) string {
} }
func shouldQuoteIdentifier(identifier string) bool { func shouldQuoteIdentifier(identifier string) bool {
if len(identifier) == 0 {
return true
}
_, err := strconv.ParseInt(identifier, 10, 64) _, err := strconv.ParseInt(identifier, 10, 64)
if err == nil { // if it is a number we should quote it if err == nil { // if it is a number we should quote it
return true return true
} }
firstChar := rune(identifier[0])
if unicode.IsNumber(firstChar) {
return true
}
// check if contains non ascii characters // check if contains non ascii characters
for _, c := range identifier { for _, c := range identifier {
if unicode.IsNumber(c) || c == '_' { if unicode.IsNumber(c) || c == '_' {

View file

@ -1,10 +1,11 @@
package jet package jet
import ( import (
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"testing" "testing"
"time" "time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
) )
func TestArgToString(t *testing.T) { func TestArgToString(t *testing.T) {
@ -58,4 +59,6 @@ func TestShouldQuote(t *testing.T) {
require.Equal(t, shouldQuoteIdentifier("abc_123"), false) require.Equal(t, shouldQuoteIdentifier("abc_123"), false)
require.Equal(t, shouldQuoteIdentifier("Abc_123"), true) require.Equal(t, shouldQuoteIdentifier("Abc_123"), true)
require.Equal(t, shouldQuoteIdentifier("DŽƜĐǶ"), true) require.Equal(t, shouldQuoteIdentifier("DŽƜĐǶ"), true)
require.Equal(t, shouldQuoteIdentifier("1test"), true)
require.Equal(t, shouldQuoteIdentifier(""), true)
} }