forked from googleapis/go-gorm-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spanner.go
228 lines (201 loc) · 5.7 KB
/
spanner.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2023 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gorm
import (
"context"
"database/sql"
"fmt"
"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/googleapis/go-sql-spanner"
)
type Config struct {
DriverName string
DSN string
Conn gorm.ConnPool
// DisableAutoMigrateBatching turns off DDL batching for AutoMigrate calls.
// Cloud Spanner by default uses DDL batching when AutoMigrate is called, as
// executing multiple DDL statements in a single batch is a lot more efficient
// than executing each statement separately. You should only use this option
// if you are experiencing problems with the automatic batching of DDL
// statements when calling AutoMigrate.
DisableAutoMigrateBatching bool
}
type Dialector struct {
*Config
}
func Open(dsn string) gorm.Dialector {
return &Dialector{Config: &Config{DSN: dsn}}
}
func New(config Config) gorm.Dialector {
return &Dialector{Config: &config}
}
func (dialector Dialector) Name() string {
return "spanner"
}
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
CreateClauses: []string{"INSERT", "VALUES", "RETURNING"},
})
if dialector.DriverName == "" {
dialector.DriverName = "spanner"
}
// Register an UPDATE callback that will ensure that primary key columns are
// never included in the SET clause of the statement.
updateCallback := db.Callback().Update()
if err := updateCallback.
After("gorm:before_update").
Before("gorm:update").
Register("gorm:spanner:remove_primary_key_from_update", BeforeUpdate); err != nil {
return err
}
if dialector.Conn != nil {
db.ConnPool = dialector.Conn
} else {
db.ConnPool, err = sql.Open(dialector.DriverName, dialector.DSN)
if err != nil {
return err
}
}
// Spanner DML does not support 'ON CONFLICT' clauses.
db.ClauseBuilders[clause.OnConflict{}.Name()] = func(c clause.Clause, builder clause.Builder) {}
db.ClauseBuilders[clause.Returning{}.Name()] = func(c clause.Clause, builder clause.Builder) {
builder.WriteString("THEN RETURN ")
returning, ok := c.Expression.(clause.Returning)
if ok && len(returning.Columns) > 0 {
for idx, column := range returning.Columns {
if idx > 0 {
builder.WriteByte(',')
}
builder.WriteQuoted(column)
}
} else {
builder.WriteByte('*')
}
}
return
}
func BeforeUpdate(db *gorm.DB) {
// Omit all primary key fields from the SET clause of an UPDATE statement.
db.Statement.Omit(db.Statement.Schema.PrimaryFieldDBNames...)
}
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression {
return clause.Expr{SQL: "NULL"}
}
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
var conn *sql.Conn
if c, ok := db.ConnPool.(*sql.Conn); ok && c != nil {
conn = c
} else {
sqlDB, _ := db.DB()
conn, _ = sqlDB.Conn(context.Background())
}
db.ConnPool = conn
db.Statement.ConnPool = conn
return spannerMigrator{
Migrator: migrator.Migrator{
Config: migrator.Config{
DB: db,
Dialector: dialector,
CreateIndexAfterCreateTable: true,
},
},
Dialector: dialector,
}
}
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteByte('?')
}
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
var (
underQuoted, selfQuoted bool
continuousBacktick int8
shiftDelimiter int8
)
for _, v := range []byte(str) {
switch v {
case '`':
continuousBacktick++
if continuousBacktick == 2 {
writer.WriteString("``")
continuousBacktick = 0
}
case '.':
if continuousBacktick > 0 || !selfQuoted {
shiftDelimiter = 0
underQuoted = false
continuousBacktick = 0
writer.WriteString("`")
}
writer.WriteByte(v)
continue
default:
if shiftDelimiter-continuousBacktick <= 0 && !underQuoted {
writer.WriteByte('`')
underQuoted = true
if selfQuoted = continuousBacktick > 0; selfQuoted {
continuousBacktick -= 1
}
}
for ; continuousBacktick > 0; continuousBacktick -= 1 {
writer.WriteString("``")
}
writer.WriteByte(v)
}
shiftDelimiter++
}
if continuousBacktick > 0 && !selfQuoted {
writer.WriteString("``")
}
writer.WriteString("`")
}
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 "BOOL"
case schema.Int, schema.Uint:
return "INT64"
case schema.Float:
if field.Size == 32 {
return "FLOAT32"
}
return "FLOAT64"
case schema.String:
var size string
if field.Size == 0 || field.Size > 2621440 {
size = "MAX"
} else {
size = fmt.Sprintf("%d", field.Size)
}
return fmt.Sprintf("STRING(%s)", size)
case schema.Bytes:
var size string
if field.Size == 0 || field.Size > 10485760 {
size = "MAX"
} else {
size = fmt.Sprintf("%d", field.Size)
}
return fmt.Sprintf("BYTES(%s)", size)
case schema.Time:
return "TIMESTAMP"
}
return string(field.DataType)
}