/
tablespec.go
162 lines (148 loc) · 4.47 KB
/
tablespec.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
// Copyright 2019 Yunion
//
// 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 db
import (
"context"
"reflect"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/cloudcommon/informer"
"yunion.io/x/onecloud/pkg/util/nopanic"
)
type ITableSpec interface {
Name() string
DataType() reflect.Type
Insert(ctx context.Context, dt interface{}) error
InsertOrUpdate(ctx context.Context, dt interface{}) error
Update(ctx context.Context, dt interface{}, doUpdate func() error) (sqlchemy.UpdateDiffs, error)
Instance() *sqlchemy.STable
ColumnSpec(name string) sqlchemy.IColumnSpec
PrimaryColumns() []sqlchemy.IColumnSpec
Columns() []sqlchemy.IColumnSpec
Fetch(dt interface{}) error
FetchAll(dest interface{}) error
SyncSQL() []string
DropForeignKeySQL() []string
AddIndex(unique bool, cols ...string) bool
}
type sTableSpec struct {
*sqlchemy.STableSpec
}
func newTableSpec(model interface{}, tableName string) ITableSpec {
return &sTableSpec{
STableSpec: sqlchemy.NewTableSpecFromStruct(model, tableName),
}
}
func (ts *sTableSpec) newInformerModel(dt interface{}) (*informer.ModelObject, error) {
obj, ok := dt.(IModel)
if !ok {
return nil, errors.Errorf("informer model is not IModel")
}
if obj.GetVirtualObject() == nil {
return nil, errors.Errorf("object %v virtual object is nil", obj)
}
if obj.GetModelManager() == nil {
return nil, errors.Errorf("object %v model manager is nil", obj)
}
jointObj, isJoint := obj.(IJointModel)
if isJoint {
mObj := jointObj.Master()
sObj := jointObj.Slave()
return informer.NewJointModel(jointObj, jointObj.KeywordPlural(), mObj.GetId(), sObj.GetId()), nil
}
return informer.NewModel(obj, obj.KeywordPlural(), obj.GetId()), nil
}
func (ts *sTableSpec) isMarkDeleted(dt interface{}) (bool, error) {
if vObj, ok := dt.(IVirtualModel); ok {
if vObj.GetPendingDeleted() {
return true, nil
}
}
obj, ok := dt.(IModel)
if !ok {
return false, errors.Errorf("informer model is not IModel")
}
return obj.GetDeleted(), nil
}
func (ts *sTableSpec) Insert(ctx context.Context, dt interface{}) error {
if err := ts.STableSpec.Insert(dt); err != nil {
return err
}
ts.inform(ctx, dt, informer.Create)
return nil
}
func (ts *sTableSpec) InsertOrUpdate(ctx context.Context, dt interface{}) error {
if err := ts.STableSpec.InsertOrUpdate(dt); err != nil {
return err
}
ts.inform(ctx, dt, informer.Create)
return nil
}
func (ts *sTableSpec) Update(ctx context.Context, dt interface{}, doUpdate func() error) (sqlchemy.UpdateDiffs, error) {
oldObj := jsonutils.Marshal(dt)
diffs, err := ts.STableSpec.Update(dt, doUpdate)
if err != nil {
return nil, err
}
if diffs == nil {
// no data to update
return nil, nil
}
isDeleted, err := ts.isMarkDeleted(dt)
if err != nil {
return nil, errors.Wrap(err, "check is mark deleted")
}
if isDeleted {
ts.inform(ctx, dt, informer.Delete)
} else {
ts.informUpdate(ctx, dt, oldObj.(*jsonutils.JSONDict))
}
return diffs, nil
}
func (ts *sTableSpec) inform(ctx context.Context, dt interface{}, f func(ctx context.Context, obj *informer.ModelObject) error) {
nf := func() {
obj, err := ts.newInformerModel(dt)
if err != nil {
log.Warningf("newInformerModel error: %v", err)
return
}
if err := f(ctx, obj); err != nil {
if errors.Cause(err) == informer.ErrBackendNotInit {
log.V(4).Warningf("informer backend not init")
} else {
log.Errorf("call informer func error: %v", err)
}
}
}
nopanic.Run(nf)
}
func (ts *sTableSpec) informUpdate(ctx context.Context, dt interface{}, oldObj *jsonutils.JSONDict) {
nf := func() {
obj, err := ts.newInformerModel(dt)
if err != nil {
log.Warningf("newInformerModel error: %v", err)
return
}
if err := informer.Update(ctx, obj, oldObj); err != nil {
if errors.Cause(err) == informer.ErrBackendNotInit {
log.V(4).Warningf("informer backend not init")
} else {
log.Errorf("call informer update func error: %v", err)
}
}
}
nopanic.Run(nf)
}