Skip to content

Commit

Permalink
[sql] Add QuoteLiterals function (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed May 17, 2024
1 parent 7066de7 commit ed4bf7b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
4 changes: 2 additions & 2 deletions clients/bigquery/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package bigquery

import (
"fmt"
"strings"

"cloud.google.com/go/bigquery"

"github.com/artie-labs/transfer/clients/shared"
"github.com/artie-labs/transfer/lib/array"
"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/destination/types"
"github.com/artie-labs/transfer/lib/kafkalib/partition"
Expand Down Expand Up @@ -73,7 +73,7 @@ func generateMergeString(bqSettings *partition.BigQuerySettings, dialect sql.Dia
columns.NewColumn(bqSettings.PartitionField, typing.Invalid),
dialect,
),
array.StringsJoinAddSingleQuotes(values)), nil
strings.Join(sql.QuoteLiterals(values), ",")), nil
}
}

Expand Down
10 changes: 0 additions & 10 deletions lib/array/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/artie-labs/transfer/lib/stringutil"
)
Expand Down Expand Up @@ -54,15 +53,6 @@ func InterfaceToArrayString(val any, recastAsArray bool) ([]string, error) {
return vals, nil
}

func StringsJoinAddSingleQuotes(values []string) string {
var vals []string
for _, value := range values {
vals = append(vals, fmt.Sprintf(`'%s'`, value))
}

return strings.Join(vals, ",")
}

// Empty will iterate over a list, if one of the item in the list is empty, it will return true
// This is useful to check the presence of a setting.
func Empty(list []string) bool {
Expand Down
10 changes: 0 additions & 10 deletions lib/array/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestStringsJoinAddSingleQuotes(t *testing.T) {
foo := []string{
"abc",
"def",
"ggg",
}

assert.Equal(t, "'abc','def','ggg'", StringsJoinAddSingleQuotes(foo))
}

func TestToArrayString(t *testing.T) {
type _testCase struct {
name string
Expand Down
8 changes: 8 additions & 0 deletions lib/sql/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func QuoteLiteral(value string) string {
return fmt.Sprintf("'%s'", strings.ReplaceAll(stringutil.EscapeBackslashes(value), "'", `\'`))
}

func QuoteLiterals(values []string) []string {
result := make([]string, len(values))
for i, value := range values {
result[i] = QuoteLiteral(value)
}
return result
}

func QuoteIdentifiers(identifiers []string, dialect Dialect) []string {
result := make([]string, len(identifiers))
for i, identifier := range identifiers {
Expand Down
7 changes: 7 additions & 0 deletions lib/sql/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func TestQuoteLiteral(t *testing.T) {
}
}

func TestQuoteLiterals(t *testing.T) {
assert.Empty(t, QuoteLiterals(nil))
assert.Empty(t, QuoteLiterals([]string{}))
assert.Equal(t, []string{"'a'"}, QuoteLiterals([]string{"a"}))
assert.Equal(t, []string{"'a'", "'b'", "'c\\'c'"}, QuoteLiterals([]string{"a", "b", "c'c"}))
}

func TestParseDataTypeDefinition(t *testing.T) {
{
dataType, parameters, err := ParseDataTypeDefinition("number")
Expand Down

0 comments on commit ed4bf7b

Please sign in to comment.