forked from guypeled76/go-bigquery-driver
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
callbacks.go
39 lines (30 loc) · 830 Bytes
/
callbacks.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
package bigquery
import (
"gorm.io/driver/bigquery/adaptor"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
)
func initializeCallbacks(db *gorm.DB) {
// register callbacks
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
CreateClauses: []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"},
})
c := &bigQueryCallbacks{db}
queryCallback := db.Callback().Query()
queryCallback.Replace("gorm:query", c.queryCallback)
}
type bigQueryCallbacks struct {
root *gorm.DB
}
func (c *bigQueryCallbacks) queryCallback(db *gorm.DB) {
if !db.DryRun {
applyStatementSchemaContext(db, c.root)
}
callbacks.Query(db)
}
func applyStatementSchemaContext(db *gorm.DB, rootDB *gorm.DB) {
db.Statement.Context = adaptor.SetSchemaAdaptor(db.Statement.Context, &bigQuerySchemaAdaptor{
db.Statement.Schema,
rootDB,
})
}