forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_snowflake.go
169 lines (145 loc) · 4.19 KB
/
dialect_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gorp
import (
"fmt"
"reflect"
"strings"
"time"
)
// Implementation of Dialect for Snowflake databases.
// Tested with driver: github.com/snowflakedb/gosnowflake
type SnowflakeDialect struct {
suffix string
LowercaseFields bool
}
// Type returns the dialect type
func (d SnowflakeDialect) Type() DialectType {
return Snowflake
}
// QuerySuffix adds a Suffix to any query, usually ";"
func (d SnowflakeDialect) QuerySuffix() string { return ";" }
// ToSqlType returns the SQL column type to use when creating a
// table of the given Go Type. maxsize can be used to switch based on
// size. For example, in Snowflake []byte maps to binary
func (d SnowflakeDialect) ToSqlType(val reflect.Type, opts ColumnOptions) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), opts)
case reflect.Bool:
return "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
if opts.IsAutoIncr {
return "autoincrement"
}
return "integer"
case reflect.Int64, reflect.Uint64:
if opts.IsAutoIncr {
return "autoincrement"
}
return "bigint"
case reflect.Float64:
return "double precision"
case reflect.Float32:
return "real"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "binary"
}
}
switch val.Name() {
case "NullInt64":
return "bigint"
case "NullFloat64":
return "double precision"
case "NullBool":
return "boolean"
case "Time", "NullTime":
return "timestamp with time zone"
}
if opts.MaxSize > 0 {
return fmt.Sprintf("varchar(%d)", opts.MaxSize)
}
return "text"
}
// Returns empty string since Snowflake uses AUTOINCREMENT keyword
func (d SnowflakeDialect) AutoIncrStr() string {
return ""
}
// Returns "default"
func (d SnowflakeDialect) AutoIncrBindValue() string {
return "default"
}
// Returns empty string
func (d SnowflakeDialect) AutoIncrInsertSuffix(col *ColumnMap) string {
return ""
}
// Returns Suffix
func (d SnowflakeDialect) CreateTableSuffix() string {
return d.suffix
}
// Returns empty string
func (d SnowflakeDialect) CreateIndexSuffix() string {
return ""
}
// Returns empty string
func (d SnowflakeDialect) DropIndexSuffix() string {
return ""
}
// Returns "truncate"
func (d SnowflakeDialect) TruncateClause() string {
return "truncate"
}
// Returns call system$wait(s)
func (d SnowflakeDialect) SleepClause(s time.Duration) string {
return fmt.Sprintf("call system$wait(%d)", s.Milliseconds())
}
// Returns "?"
func (d SnowflakeDialect) BindVar(i int) string {
return "?"
}
// Handles auto-increment values for Snowflake
func (d SnowflakeDialect) InsertAutoIncrToTarget(exec SqlExecutor, insertSql string, target interface{}, params ...interface{}) error {
rows, err := exec.Query(insertSql, params...)
if err != nil {
return err
}
defer rows.Close()
if !rows.Next() {
return fmt.Errorf("No serial value returned for insert: %s Encountered error: %s", insertSql, rows.Err())
}
if err := rows.Scan(target); err != nil {
return err
}
if rows.Next() {
return fmt.Errorf("more than two serial value returned for insert: %s", insertSql)
}
return rows.Err()
}
// Returns quoted field name based on LowercaseFields setting
func (d SnowflakeDialect) QuoteField(f string) string {
if d.LowercaseFields {
return `"` + strings.ToLower(f) + `"`
}
return `"` + f + `"`
}
// Returns quoted table name with schema support
func (d SnowflakeDialect) QuotedTableForQuery(schema string, table string) string {
if strings.TrimSpace(schema) == "" {
return d.QuoteField(table)
}
return schema + "." + d.QuoteField(table)
}
// Returns "if not exists"
func (d SnowflakeDialect) IfSchemaNotExists(command, schema string) string {
return fmt.Sprintf("%s if not exists", command)
}
// Returns "if exists"
func (d SnowflakeDialect) IfTableExists(command, schema, table string) string {
return fmt.Sprintf("%s if exists", command)
}
// Returns "if not exists"
func (d SnowflakeDialect) IfTableNotExists(command, schema, table string) string {
return fmt.Sprintf("%s if not exists", command)
}