/
dialect_bigquery.go
233 lines (202 loc) · 5.29 KB
/
dialect_bigquery.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
229
230
231
232
233
package bigquery
import (
bigquery2 "cloud.google.com/go/bigquery"
"context"
"fmt"
_ "github.com/infobloxopen/protoc-gen-gorm/types"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
_ "github.com/solcates/go-sql-bigquery"
bigquery "github.com/solcates/go-sql-bigquery"
"google.golang.org/api/googleapi"
"os"
"reflect"
"strconv"
"strings"
"time"
)
type Dialect struct {
db gorm.SQLCommon
gorm.DefaultForeignKeyNamer
cfg *bigquery.Config
client *bigquery2.Client
}
func init() {
client, cfg := getClient()
gorm.RegisterDialect("bigquery", &Dialect{
cfg: cfg,
client: client,
})
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return fmt.Sprintf("%s.%s", cfg.DataSet, defaultTableName)
}
}
func getClient() (*bigquery2.Client, *bigquery.Config) {
uri := os.Getenv(bigquery.ConnectionStringEnvKey)
cfg, err := bigquery.ConfigFromConnString(uri)
if err != nil {
panic(err)
}
client, err := bigquery2.NewClient(context.TODO(), cfg.ProjectID)
if err != nil {
panic(err)
}
return client, cfg
}
func (b *Dialect) GetName() string {
return "bigquery"
}
func (b *Dialect) SetDB(db gorm.SQLCommon) {
b.db = db
}
func (b *Dialect) BindVar(i int) string {
return "$$$" // ?
}
func (b *Dialect) Quote(key string) string {
return fmt.Sprintf("`%s`", key)
}
func (b *Dialect) DataTypeOf(field *gorm.StructField) string {
var dataValue, sqlType, _, additionalType = gorm.ParseFieldStructForDialect(field, b)
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool:
sqlType = "BOOL"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if b.fieldCanAutoIncrement(field) {
sqlType = "INT64 AUTO_INCREMENT"
} else {
sqlType = "INT64"
}
case reflect.Int64, reflect.Uint64:
if b.fieldCanAutoIncrement(field) {
sqlType = "INT64 AUTO_INCREMENT"
} else {
sqlType = "INT64"
}
case reflect.Float32, reflect.Float64:
sqlType = "FLOAT64"
case reflect.String:
sqlType = "STRING"
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "TIMESTAMP"
}
case reflect.Array:
if _, ok := dataValue.Interface().(uuid.UUID); ok {
sqlType = "STRING"
}
default:
if _, ok := dataValue.Interface().([]byte); ok {
sqlType = "BYTES"
}
}
}
if sqlType == "uuid" {
sqlType = "STRING"
}
logrus.Debugf("sqlType: %s", sqlType)
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for commonDialect", dataValue.Type().Name(), dataValue.Kind().String()))
}
if strings.TrimSpace(additionalType) == "" {
return sqlType
}
return fmt.Sprintf("%v %v", sqlType, additionalType)
}
func (b *Dialect) HasIndex(tableName string, indexName string) bool {
panic("implement me")
}
func (b Dialect) HasForeignKey(tableName string, foreignKeyName string) bool {
panic("implement me")
}
func (b Dialect) RemoveIndex(tableName string, indexName string) error {
panic("implement me")
}
func (b *Dialect) HasTable(in string) bool {
logrus.Debugf("HasTable| Asking for Table: %s", in)
ds := strings.Split(in, ".")
var tableName string
switch len(ds) {
case 2:
tableName = ds[1]
case 1:
tableName = in
default:
panic("HasTable| invalid tablename")
}
client, cfg := getClient()
logrus.Debugf("HasTable| Dataset: %s", cfg.DataSet)
d := client.Dataset(cfg.DataSet)
t := d.Table(tableName)
md, err := t.Metadata(context.TODO())
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok {
if gerr.Code == 404 {
return false
}
logrus.Debugf("Google Error: %s", gerr.Error())
} else {
logrus.Debugf("Unhandled Error from .Metadata: %s", err)
panic(err)
}
}
if md != nil {
return true
}
return false
}
func (b *Dialect) HasColumn(tableName string, columnName string) bool {
query := fmt.Sprintf("SELECT * FROM %s LIMIT 0", tableName)
rows, err := b.db.Query(query)
if err != nil {
return false
}
columns, err := rows.Columns()
if err != nil {
return false
}
for _, column := range columns {
if column == columnName {
return true
}
}
return false
}
func (b Dialect) ModifyColumn(tableName string, columnName string, typ string) error {
panic("implement me")
}
func (b Dialect) LimitAndOffsetSQL(limit, offset interface{}) (sql string) {
if limit != nil {
if parsedLimit, err := strconv.ParseInt(fmt.Sprint(limit), 0, 0); err == nil && parsedLimit >= 0 {
sql += fmt.Sprintf(" LIMIT %d", parsedLimit)
}
}
if offset != nil {
if parsedOffset, err := strconv.ParseInt(fmt.Sprint(offset), 0, 0); err == nil && parsedOffset >= 0 {
sql += fmt.Sprintf(" OFFSET %d", parsedOffset)
}
}
return
}
func (b Dialect) SelectFromDummyTable() string {
return ""
}
func (b Dialect) LastInsertIDReturningSuffix(tableName, columnName string) string {
return ""
}
func (b Dialect) DefaultValueStr() string {
return "DEFAULT VALUES"
}
func (b Dialect) NormalizeIndexAndColumn(indexName, columnName string) (string, string) {
return indexName, columnName
}
func (b Dialect) CurrentDatabase() string {
panic("implement me")
}
func (b *Dialect) fieldCanAutoIncrement(field *gorm.StructField) bool {
if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
return strings.ToLower(value) != "false"
}
return field.IsPrimaryKey
}