forked from go-gorm/bigquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptor.go
70 lines (53 loc) · 1.38 KB
/
adaptor.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
package bigquery
import (
"database/sql/driver"
"gorm.io/driver/bigquery/adaptor"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"reflect"
)
type bigQuerySchemaAdaptor struct {
schema *schema.Schema
db *gorm.DB
}
func (schemaAdaptor *bigQuerySchemaAdaptor) GetColumnAdaptor(name string) adaptor.SchemaColumnAdaptor {
if schema := schemaAdaptor.schema; schema != nil {
field := schema.FieldsByDBName[name]
if field == nil {
return nil
}
switch field.DataType {
case adaptor.RecordType, adaptor.ArrayType:
return &bigQueryColumnAdaptor{field: field, rootDB: schemaAdaptor.db}
}
}
return nil
}
type bigQueryColumnAdaptor struct {
field *schema.Field
rootDB *gorm.DB
}
func (columnAdaptor *bigQueryColumnAdaptor) AdaptValue(value driver.Value) (driver.Value, error) {
instance := reflect.New(columnAdaptor.field.IndirectFieldType).Interface()
db := columnAdaptor.rootDB.Raw(adaptor.RerouteQuery, value)
err := db.Statement.Parse(instance)
if err != nil {
return nil, err
}
applyStatementSchemaContext(db, columnAdaptor.rootDB)
err = db.Scan(instance).Error
if err != nil {
return nil, err
}
return instance, err
}
func (columnAdaptor *bigQueryColumnAdaptor) GetSchemaAdaptor() adaptor.SchemaAdaptor {
schema := columnAdaptor.field.Schema
if schema == nil {
return nil
}
return &bigQuerySchemaAdaptor{
schema: schema,
db: columnAdaptor.rootDB,
}
}