Skip to content

Commit

Permalink
[sql] Add Dialect.EscapeStruct method (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored May 2, 2024
1 parent 4b77f47 commit 4153191
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
18 changes: 18 additions & 0 deletions lib/sql/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"strings"

"github.com/artie-labs/transfer/lib/config/constants"
"github.com/artie-labs/transfer/lib/stringutil"
)

type Dialect interface {
NeedsEscaping(identifier string) bool // TODO: Remove this when we escape everything
QuoteIdentifier(identifier string) string
EscapeStruct(value any) string
}

type BigQueryDialect struct{}
Expand All @@ -24,6 +26,10 @@ func (BigQueryDialect) QuoteIdentifier(identifier string) string {
return fmt.Sprintf("`%s`", identifier)
}

func (BigQueryDialect) EscapeStruct(value any) string {
return "JSON" + stringutil.Wrap(value, false)
}

type MSSQLDialect struct{}

func (MSSQLDialect) NeedsEscaping(_ string) bool { return true }
Expand All @@ -32,6 +38,10 @@ func (MSSQLDialect) QuoteIdentifier(identifier string) string {
return fmt.Sprintf(`"%s"`, identifier)
}

func (MSSQLDialect) EscapeStruct(value any) string {
panic("not implemented") // We don't currently support backfills for MS SQL.
}

type RedshiftDialect struct{}

func (RedshiftDialect) NeedsEscaping(_ string) bool { return true }
Expand All @@ -41,6 +51,10 @@ func (rd RedshiftDialect) QuoteIdentifier(identifier string) string {
return fmt.Sprintf(`"%s"`, strings.ToLower(identifier))
}

func (RedshiftDialect) EscapeStruct(value any) string {
return fmt.Sprintf("JSON_PARSE(%s)", stringutil.Wrap(value, false))
}

type SnowflakeDialect struct {
UppercaseEscNames bool
}
Expand Down Expand Up @@ -75,3 +89,7 @@ func (sd SnowflakeDialect) QuoteIdentifier(identifier string) string {

return fmt.Sprintf(`"%s"`, identifier)
}

func (SnowflakeDialect) EscapeStruct(value any) string {
return stringutil.Wrap(value, false)
}
12 changes: 1 addition & 11 deletions lib/typing/columns/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ func (c *Column) DefaultValue(dialect sql.Dialect, additionalDateFmts []string)

switch c.KindDetails.Kind {
case typing.Struct.Kind, typing.Array.Kind:
switch dialect.(type) {
case sql.BigQueryDialect:
return "JSON" + stringutil.Wrap(c.defaultValue, false), nil
case sql.RedshiftDialect:
return fmt.Sprintf("JSON_PARSE(%s)", stringutil.Wrap(c.defaultValue, false)), nil
case sql.SnowflakeDialect:
return stringutil.Wrap(c.defaultValue, false), nil
default:
// Note that we don't currently support backfills for MS SQL.
return nil, fmt.Errorf("not implemented for %v dialect", dialect)
}
return dialect.EscapeStruct(c.defaultValue), nil
case typing.ETime.Kind:
if c.KindDetails.ExtendedTimeDetails == nil {
return nil, fmt.Errorf("column kind details for extended time is nil")
Expand Down

0 comments on commit 4153191

Please sign in to comment.