From 2291477bc22815334c9ce05ef672d077f95d18e8 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 1 May 2024 23:00:22 -0700 Subject: [PATCH] [sql] Add `QuoteIdentifiers` function (#545) --- clients/snowflake/snowflake.go | 5 +---- lib/sql/util.go | 8 ++++++++ lib/sql/util_test.go | 5 +++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/clients/snowflake/snowflake.go b/clients/snowflake/snowflake.go index 8c62c08be..3cf716612 100644 --- a/clients/snowflake/snowflake.go +++ b/clients/snowflake/snowflake.go @@ -128,10 +128,7 @@ func (s *Store) reestablishConnection() error { } func (s *Store) generateDedupeQueries(tableID, stagingTableID types.TableIdentifier, primaryKeys []string, topicConfig kafkalib.TopicConfig) []string { - var primaryKeysEscaped []string - for _, pk := range primaryKeys { - primaryKeysEscaped = append(primaryKeysEscaped, s.Dialect().QuoteIdentifier(pk)) - } + primaryKeysEscaped := sql.QuoteIdentifiers(primaryKeys, s.Dialect()) orderColsToIterate := primaryKeysEscaped if topicConfig.IncludeArtieUpdatedAt { diff --git a/lib/sql/util.go b/lib/sql/util.go index 9a8150f89..b7dab812e 100644 --- a/lib/sql/util.go +++ b/lib/sql/util.go @@ -13,3 +13,11 @@ import ( func QuoteLiteral(value string) string { return fmt.Sprintf("'%s'", strings.ReplaceAll(stringutil.EscapeBackslashes(value), "'", `\'`)) } + +func QuoteIdentifiers(identifiers []string, dialect Dialect) []string { + result := make([]string, len(identifiers)) + for i, identifier := range identifiers { + result[i] = dialect.QuoteIdentifier(identifier) + } + return result +} diff --git a/lib/sql/util_test.go b/lib/sql/util_test.go index 89ea11320..1be71dfb9 100644 --- a/lib/sql/util_test.go +++ b/lib/sql/util_test.go @@ -38,3 +38,8 @@ func TestQuoteLiteral(t *testing.T) { assert.Equal(t, testCase.expected, QuoteLiteral(testCase.colVal), testCase.name) } } + +func TestQuoteIdentifiers(t *testing.T) { + assert.Equal(t, []string{}, QuoteIdentifiers([]string{}, BigQueryDialect{})) + assert.Equal(t, []string{"`a`", "`b`", "`c`"}, QuoteIdentifiers([]string{"a", "b", "c"}, BigQueryDialect{})) +}