From 19221f956cc7be1c6740a1bc198bfadfaee95672 Mon Sep 17 00:00:00 2001 From: Nathan Villaescusa Date: Wed, 24 Apr 2024 13:06:45 -0700 Subject: [PATCH 1/2] [mssql] Escape all columns that do not start with "__artie" --- lib/config/constants/mssql.go | 190 ------------------------ lib/destination/dml/merge_mssql_test.go | 6 +- lib/sql/escape.go | 2 +- 3 files changed, 4 insertions(+), 194 deletions(-) delete mode 100644 lib/config/constants/mssql.go diff --git a/lib/config/constants/mssql.go b/lib/config/constants/mssql.go deleted file mode 100644 index c3f8ca62f..000000000 --- a/lib/config/constants/mssql.go +++ /dev/null @@ -1,190 +0,0 @@ -package constants - -// MSSQLReservedKeywords https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-ver16 -var MSSQLReservedKeywords = []string{ - "add", - "external", - "procedure", - "all", - "fetch", - "public", - "alter", - "file", - "raiserror", - "and", - "fillfactor", - "read", - "any", - "for", - "readtext", - "as", - "foreign", - "reconfigure", - "asc", - "freetext", - "references", - "authorization", - "freetexttable", - "replication", - "backup", - "from", - "restore", - "begin", - "full", - "restrict", - "between", - "function", - "return", - "break", - "goto", - "revert", - "browse", - "grant", - "revoke", - "bulk", - "group", - "right", - "by", - "having", - "rollback", - "cascade", - "holdlock", - "rowcount", - "case", - "identity", - "rowguidcol", - "check", - "identity_insert", - "rule", - "checkpoint", - "identitycol", - "save", - "close", - "if", - "schema", - "clustered", - "in", - "securityaudit", - "coalesce", - "index", - "select", - "collate", - "inner", - "semantickeyphrasetable", - "column", - "insert", - "semanticsimilaritydetailstable", - "commit", - "intersect", - "semanticsimilaritytable", - "compute", - "into", - "session_user", - "constraint", - "is", - "set", - "contains", - "join", - "setuser", - "containstable", - "key", - "shutdown", - "continue", - "kill", - "some", - "convert", - "left", - "statistics", - "create", - "like", - "system_user", - "cross", - "lineno", - "table", - "current", - "load", - "tablesample", - "current_date", - "merge", - "textsize", - "current_time", - "national", - "then", - "current_timestamp", - "nocheck", - "to", - "current_user", - "nonclustered", - "top", - "cursor", - "not", - "tran", - "database", - "null", - "transaction", - "dbcc", - "nullif", - "trigger", - "deallocate", - "of", - "truncate", - "declare", - "off", - "try_convert", - "default", - "offsets", - "tsequal", - "delete", - "on", - "union", - "deny", - "open", - "unique", - "desc", - "opendatasource", - "unpivot", - "disk", - "openquery", - "update", - "distinct", - "openrowset", - "updatetext", - "distributed", - "openxml", - "use", - "double", - "option", - "user", - "drop", - "or", - "values", - "dump", - "order", - "varying", - "else", - "outer", - "view", - "end", - "over", - "waitfor", - "errlvl", - "percent", - "when", - "escape", - "pivot", - "where", - "except", - "plan", - "while", - "exec", - "precision", - "with", - "execute", - "primary", - "within group", - "exists", - "print", - "writetext", - "exit", - "proc", -} diff --git a/lib/destination/dml/merge_mssql_test.go b/lib/destination/dml/merge_mssql_test.go index edc58ef70..25bce32bd 100644 --- a/lib/destination/dml/merge_mssql_test.go +++ b/lib/destination/dml/merge_mssql_test.go @@ -54,11 +54,11 @@ func Test_GetMSSQLStatement(t *testing.T) { mergeSQL, err := mergeArg.GetMSSQLStatement() assert.NoError(t, err) assert.Contains(t, mergeSQL, fmt.Sprintf("MERGE INTO %s", fqTable), mergeSQL) - assert.NotContains(t, mergeSQL, fmt.Sprintf("cc.%s >= c.%s", "updated_at", "updated_at"), fmt.Sprintf("Idempotency key: %s", mergeSQL)) + assert.NotContains(t, mergeSQL, fmt.Sprintf(`cc."%s" >= c."%s"`, "updated_at", "updated_at"), fmt.Sprintf("Idempotency key: %s", mergeSQL)) // Check primary keys clause assert.Contains(t, mergeSQL, "AS cc ON c.id = cc.id", mergeSQL) - assert.Contains(t, mergeSQL, `SET id=cc.id,bar=cc.bar,updated_at=cc.updated_at,start=cc.start`, mergeSQL) + assert.Contains(t, mergeSQL, `SET "id"=cc."id","bar"=cc."bar","updated_at"=cc."updated_at","start"=cc."start"`, mergeSQL) assert.Contains(t, mergeSQL, `id,bar,updated_at,start`, mergeSQL) - assert.Contains(t, mergeSQL, `cc.id,cc.bar,cc.updated_at,cc.start`, mergeSQL) + assert.Contains(t, mergeSQL, `cc."id",cc."bar",cc."updated_at",cc."start"`, mergeSQL) } diff --git a/lib/sql/escape.go b/lib/sql/escape.go index 3e7061103..aa6139699 100644 --- a/lib/sql/escape.go +++ b/lib/sql/escape.go @@ -25,7 +25,7 @@ func NeedsEscaping(name string, destKind constants.DestinationKind) bool { if destKind == constants.Redshift { reservedKeywords = constants.RedshiftReservedKeywords } else if destKind == constants.MSSQL { - reservedKeywords = constants.MSSQLReservedKeywords + return !strings.Contains(name, constants.ArtiePrefix) } else { reservedKeywords = constants.ReservedKeywords } From 890e15b09b7be55d9649f5d87c7723b0100b0825 Mon Sep 17 00:00:00 2001 From: Nathan Villaescusa Date: Wed, 24 Apr 2024 13:08:00 -0700 Subject: [PATCH 2/2] hasprefix --- lib/sql/escape.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sql/escape.go b/lib/sql/escape.go index aa6139699..180f303dc 100644 --- a/lib/sql/escape.go +++ b/lib/sql/escape.go @@ -25,7 +25,7 @@ func NeedsEscaping(name string, destKind constants.DestinationKind) bool { if destKind == constants.Redshift { reservedKeywords = constants.RedshiftReservedKeywords } else if destKind == constants.MSSQL { - return !strings.Contains(name, constants.ArtiePrefix) + return !strings.HasPrefix(name, constants.ArtiePrefix) } else { reservedKeywords = constants.ReservedKeywords }