Skip to content

Commit

Permalink
func
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed May 2, 2024
1 parent 59aa085 commit 7b8b7ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 3 additions & 11 deletions lib/destination/dml/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ func (m *MergeArgument) GetParts() ([]string, error) {
}

var equalitySQLParts []string
for _, primaryKey := range m.PrimaryKeys {
// We'll need to escape the primary key as well.
escapedPrimaryKey := m.Dialect.QuoteIdentifier(primaryKey.RawName())
for _, escapedPrimaryKey := range columns.QuoteColumns(m.PrimaryKeys, m.Dialect) {
equalitySQL := fmt.Sprintf("c.%s = cc.%s", escapedPrimaryKey, escapedPrimaryKey)
equalitySQLParts = append(equalitySQLParts, equalitySQL)
}
Expand Down Expand Up @@ -141,11 +139,6 @@ func (m *MergeArgument) GetParts() ([]string, error) {
return nil, errors.New("artie delete flag doesn't exist")
}

var pks []string
for _, pk := range m.PrimaryKeys {
pks = append(pks, m.Dialect.QuoteIdentifier(pk.RawName()))
}

parts := []string{
// INSERT
fmt.Sprintf(`INSERT INTO %s (%s) SELECT %s FROM %s as cc LEFT JOIN %s as c on %s WHERE c.%s IS NULL;`,
Expand All @@ -171,6 +164,7 @@ func (m *MergeArgument) GetParts() ([]string, error) {
}

if *m.ContainsHardDeletes {
var pks []string = columns.QuoteColumns(m.PrimaryKeys, m.Dialect)
parts = append(parts,
// DELETE
fmt.Sprintf(`DELETE FROM %s WHERE (%s) IN (SELECT %s FROM %s as cc WHERE cc.%s = true);`,
Expand Down Expand Up @@ -296,9 +290,7 @@ func (m *MergeArgument) GetMSSQLStatement() (string, error) {
}

var equalitySQLParts []string
for _, primaryKey := range m.PrimaryKeys {
// We'll need to escape the primary key as well.
escapedPrimaryKey := m.Dialect.QuoteIdentifier(primaryKey.RawName())
for _, escapedPrimaryKey := range columns.QuoteColumns(m.PrimaryKeys, m.Dialect) {
equalitySQL := fmt.Sprintf("c.%s = cc.%s", escapedPrimaryKey, escapedPrimaryKey)
equalitySQLParts = append(equalitySQLParts, equalitySQL)
}
Expand Down
8 changes: 8 additions & 0 deletions lib/typing/columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,11 @@ func processToastCol(colName string, dialect sql.Dialect) string {
colName, colName, constants.ToastUnavailableValuePlaceholder, colName, colName)
}
}

func QuoteColumns(columns []Column, dialect sql.Dialect) []string {
result := make([]string, len(columns))
for i, columns := range columns {
result[i] = dialect.QuoteIdentifier(columns.RawName())
}
return result
}

0 comments on commit 7b8b7ea

Please sign in to comment.