forked from odedniv/terraform-provider-sql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snowflake.go
128 lines (105 loc) · 2.78 KB
/
snowflake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package dialect
import (
"fmt"
"log"
"reflect"
"strings"
"gopkg.in/gorp.v1"
)
///////////////////////////////////////////////////////
// Snowflake //
////////////////
type SnowflakeDialect struct {
suffix string
}
func (d SnowflakeDialect) QuerySuffix() string { return ";" }
func (d SnowflakeDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), maxsize, isAutoIncr)
case reflect.Bool:
return "BOOLEAN"
case reflect.Int:
return "NUMBER"
case reflect.Int8, reflect.Uint8:
return "NUMBER"
case reflect.Int16, reflect.Uint16:
return "NUMBER"
case reflect.Int32, reflect.Uint32:
return "NUMBER"
case reflect.Int64, reflect.Uint64:
return "NUMBER"
case reflect.Float64:
return "FLOAT"
case reflect.Float32:
return "FLOAT"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "BINARY"
}
}
switch val.Name() {
case "NullInt64":
return "NUMBER"
case "NullFloat64":
return "FLOAT"
case "NullBool":
return "BOOLEAN"
case "Time":
return "TIMESTAMP_TZ"
}
if maxsize > 0 {
return fmt.Sprintf("VARCHAR(%d)", maxsize)
} else {
return "TEXT"
}
}
// Returns empty string
func (d SnowflakeDialect) AutoIncrStr() string {
return "AUTOINCREMENT"
}
func (d SnowflakeDialect) AutoIncrBindValue() string {
return ""
}
func (d SnowflakeDialect) AutoIncrInsertSuffix(col *gorp.ColumnMap) string {
return ""
}
// Returns suffix
func (d SnowflakeDialect) CreateTableSuffix() string {
return d.suffix
}
func (d SnowflakeDialect) TruncateClause() string {
return "truncate"
}
func (d SnowflakeDialect) BindVar(i int) string {
return "?"
}
func standardInsertAutoIncr(exec gorp.SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
log.Printf("EXEC standardInsertAutoIncr: q: %s, params: %+v", insertSql, params)
res, err := exec.Exec(insertSql, params...)
if err != nil {
return 0, err
}
return res.LastInsertId()
}
func (d SnowflakeDialect) InsertAutoIncr(exec gorp.SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(exec, insertSql, params...)
}
func (d SnowflakeDialect) QuoteField(f string) string {
return `"` + strings.ToLower(f) + `"`
}
func (d SnowflakeDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return table
}
return schema + "." + table
}
func (d SnowflakeDialect) IfSchemaNotExists(command, schema string) string {
return fmt.Sprintf("%s if not exists", command)
}
func (d SnowflakeDialect) IfTableExists(command, schema, table string) string {
return fmt.Sprintf("%s if exists", command)
}
func (d SnowflakeDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}