|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +var ( |
| 9 | + // reserved keywords https://www.alibabacloud.com/help/en/maxcompute/user-guide/reserved-words-and-keywords |
| 10 | + reservedKeywords = []string{ |
| 11 | + "add", "after", "all", "alter", "analyze", "and", "archive", "array", "as", "asc", |
| 12 | + "before", "between", "bigint", "binary", "blob", "boolean", "both", "decimal", |
| 13 | + "bucket", "buckets", "by", "cascade", "case", "cast", "cfile", "change", "cluster", |
| 14 | + "clustered", "clusterstatus", "collection", "column", "columns", "comment", "compute", |
| 15 | + "concatenate", "continue", "create", "cross", "current", "cursor", "data", "database", |
| 16 | + "databases", "date", "datetime", "dbproperties", "deferred", "delete", "delimited", |
| 17 | + "desc", "describe", "directory", "disable", "distinct", "distribute", "double", "drop", |
| 18 | + "else", "enable", "end", "except", "escaped", "exclusive", "exists", "explain", "export", |
| 19 | + "extended", "external", "false", "fetch", "fields", "fileformat", "first", "float", |
| 20 | + "following", "format", "formatted", "from", "full", "function", "functions", "grant", |
| 21 | + "group", "having", "hold_ddltime", "idxproperties", "if", "import", "in", "index", |
| 22 | + "indexes", "inpath", "inputdriver", "inputformat", "insert", "int", "intersect", "into", |
| 23 | + "is", "items", "join", "keys", "lateral", "left", "lifecycle", "like", "limit", "lines", |
| 24 | + "load", "local", "location", "lock", "locks", "long", "map", "mapjoin", "materialized", |
| 25 | + "minus", "msck", "not", "no_drop", "null", "of", "offline", "offset", "on", "option", |
| 26 | + "or", "order", "out", "outer", "outputdriver", "outputformat", "over", "overwrite", |
| 27 | + "partition", "partitioned", "partitionproperties", "partitions", "percent", "plus", |
| 28 | + "preceding", "preserve", "procedure", "purge", "range", "rcfile", "read", "readonly", |
| 29 | + "reads", "rebuild", "recordreader", "recordwriter", "reduce", "regexp", "rename", |
| 30 | + "repair", "replace", "restrict", "revoke", "right", "rlike", "row", "rows", "schema", |
| 31 | + "schemas", "select", "semi", "sequencefile", "serde", "serdeproperties", "set", "shared", |
| 32 | + "show", "show_database", "smallint", "sort", "sorted", "ssl", "statistics", "status", |
| 33 | + "stored", "streamtable", "string", "struct", "table", "tables", "tablesample", |
| 34 | + "tblproperties", "temporary", "terminated", "textfile", "then", "timestamp", "tinyint", |
| 35 | + "to", "touch", "transform", "trigger", "true", "type", "unarchive", "unbounded", "undo", |
| 36 | + "union", "uniontype", "uniquejoin", "unlock", "unsigned", "update", "use", "using", |
| 37 | + "utc", "utc_timestamp", "view", "when", "where", "while", "div", |
| 38 | + } |
| 39 | + |
| 40 | + reservedKeywordsMap map[string]bool |
| 41 | +) |
| 42 | + |
| 43 | +func init() { |
| 44 | + reservedKeywordsMap = make(map[string]bool, len(reservedKeywords)) |
| 45 | + for _, keyword := range reservedKeywords { |
| 46 | + reservedKeywordsMap[keyword] = true |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func sanitizeColumnName(columnName string) string { |
| 51 | + // if column name is a reserved keyword, add backticks around it |
| 52 | + if _, ok := reservedKeywordsMap[strings.ToLower(columnName)]; ok { |
| 53 | + return fmt.Sprintf("`%s`", columnName) |
| 54 | + } |
| 55 | + |
| 56 | + return columnName |
| 57 | +} |
0 commit comments