forked from omixen/gorm-snowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake.go
183 lines (157 loc) · 4.12 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package snowflake
import (
"database/sql"
"fmt"
"log"
"strconv"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
_ "github.com/snowflakedb/gosnowflake"
)
const (
SnowflakeDriverName = "snowflake"
)
type Dialector struct {
*Config
}
type Config struct {
DriverName string
DSN string
Conn gorm.ConnPool
}
func (dialector Dialector) Name() string {
return SnowflakeDriverName
}
func Open(dsn string) gorm.Dialector {
return &Dialector{
Config: &Config{
DSN: dsn,
DriverName: SnowflakeDriverName,
},
}
}
func New(config Config) gorm.Dialector {
return &Dialector{Config: &config}
}
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
log.Println("creating connection...")
// register callbacks
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{})
_ = db.Callback().Create().Replace("gorm:create", Create)
if dialector.DriverName == "" {
dialector.DriverName = SnowflakeDriverName
}
if dialector.Conn != nil {
db.ConnPool = dialector.Conn
} else {
db.ConnPool, err = sql.Open(dialector.DriverName, dialector.DSN)
if err != nil {
return err
}
}
for k, v := range dialector.ClauseBuilders() {
db.ClauseBuilders[k] = v
}
return
}
func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder {
return map[string]clause.ClauseBuilder{
"LIMIT": func(c clause.Clause, builder clause.Builder) {
if limit, ok := c.Expression.(clause.Limit); ok {
if stmt, ok := builder.(*gorm.Statement); ok {
if _, ok := stmt.Clauses["ORDER BY"]; !ok {
if stmt.Schema != nil && stmt.Schema.PrioritizedPrimaryField != nil {
builder.WriteString("ORDER BY ")
builder.WriteQuoted(stmt.Schema.PrioritizedPrimaryField.DBName)
builder.WriteByte(' ')
} else {
builder.WriteString("ORDER BY (SELECT NULL) ")
}
}
}
if limit.Offset > 0 {
builder.WriteString("OFFSET ")
builder.WriteString(strconv.Itoa(limit.Offset))
builder.WriteString(" ROWS")
}
if limit.Limit != nil && *limit.Limit > 0 {
if limit.Offset == 0 {
builder.WriteString("OFFSET 0 ROW")
}
builder.WriteString(" FETCH NEXT ")
builder.WriteString(strconv.Itoa(*limit.Limit))
builder.WriteString(" ROWS ONLY")
}
}
},
}
}
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression {
return clause.Expr{SQL: "NULL"}
}
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
return Migrator{migrator.Migrator{Config: migrator.Config{
DB: db,
Dialector: dialector,
}}}
}
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}
// no quotes, quotes cause everything needing quotes
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
writer.WriteString(strings.ToLower(str))
}
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
return logger.ExplainSQL(sql, nil, `'`, vars...)
}
func (dialector Dialector) DataTypeOf(field *schema.Field) string {
switch field.DataType {
case schema.Bool:
return "BOOLEAN"
case schema.Int, schema.Uint:
var sqlType string
switch {
case field.Size < 16:
sqlType = "SMALLINT"
case field.Size < 31:
sqlType = "INT"
default:
sqlType = "BIGINT"
}
if field.AutoIncrement {
return sqlType + " IDENTITY(1,1)"
}
return sqlType
case schema.Float:
return "FLOAT"
case schema.String:
size := field.Size
hasIndex := field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUE"] != ""
if (field.PrimaryKey || hasIndex) && size == 0 {
size = 256
}
if size > 0 && size <= 4000 {
return fmt.Sprintf("VARCHAR(%d)", size)
}
return "VARCHAR"
case schema.Time:
return "TIMESTAMP_NTZ"
case schema.Bytes:
return "VARBINARY"
}
return string(field.DataType)
}
// no support for savepoint
func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error {
return nil
}
func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error {
tx.Exec("ROLLBACK TRANSACTION " + name)
return nil
}