From b6fa663315e1bf7a280857a94edc57a9b8b8e7ae Mon Sep 17 00:00:00 2001 From: Andrei-hub11 Date: Sun, 20 Jul 2025 14:23:18 -0300 Subject: [PATCH] fix: quote identifiers starting with numbers --- internal/jet/sql_builder.go | 16 +++++++++++++--- internal/jet/sql_builder_test.go | 7 +++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/internal/jet/sql_builder.go b/internal/jet/sql_builder.go index 7077af4..e404527 100644 --- a/internal/jet/sql_builder.go +++ b/internal/jet/sql_builder.go @@ -4,15 +4,16 @@ import ( "bytes" "database/sql/driver" "fmt" - "github.com/go-jet/jet/v2/internal/3rdparty/pq" - "github.com/go-jet/jet/v2/internal/utils/is" - "github.com/google/uuid" "reflect" "sort" "strconv" "strings" "time" "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 @@ -302,12 +303,21 @@ func integerTypesToString(value interface{}) string { } func shouldQuoteIdentifier(identifier string) bool { + if len(identifier) == 0 { + return true + } + _, err := strconv.ParseInt(identifier, 10, 64) if err == nil { // if it is a number we should quote it return true } + firstChar := rune(identifier[0]) + if unicode.IsNumber(firstChar) { + return true + } + // check if contains non ascii characters for _, c := range identifier { if unicode.IsNumber(c) || c == '_' { diff --git a/internal/jet/sql_builder_test.go b/internal/jet/sql_builder_test.go index ebcf83f..f283da6 100644 --- a/internal/jet/sql_builder_test.go +++ b/internal/jet/sql_builder_test.go @@ -1,10 +1,11 @@ package jet import ( - "github.com/google/uuid" - "github.com/stretchr/testify/require" "testing" "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/require" ) 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"), true) require.Equal(t, shouldQuoteIdentifier("DŽƜĐǶ"), true) + require.Equal(t, shouldQuoteIdentifier("1test"), true) + require.Equal(t, shouldQuoteIdentifier(""), true) }