-
Notifications
You must be signed in to change notification settings - Fork 27
/
adaptor.go
56 lines (44 loc) · 1.17 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
package bigquery
import (
"database/sql/driver"
"github.com/go-gorm/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, db: schemaAdaptor.db}
}
}
return nil
}
type bigQueryColumnAdaptor struct {
field *schema.Field
db *gorm.DB
}
func (columnAdaptor bigQueryColumnAdaptor) AdaptValue(value driver.Value) driver.Value {
instance := reflect.New(columnAdaptor.field.IndirectFieldType).Interface()
columnAdaptor.db.Raw(adaptor.RerouteQuery, value).Scan(instance)
return instance
}
func (columnAdaptor bigQueryColumnAdaptor) GetSchemaAdaptor() adaptor.SchemaAdaptor {
schema := columnAdaptor.field.Schema
if schema == nil {
return nil
}
return &bigQuerySchemaAdaptor{
schema: schema,
db: columnAdaptor.db,
}
}