-
Notifications
You must be signed in to change notification settings - Fork 268
/
image.go
284 lines (250 loc) · 6.71 KB
/
image.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 types
import (
"encoding/base64"
"encoding/json"
"reflect"
"time"
)
// RoundRecordImage Front and rear mirror data
type RoundRecordImage struct {
bIndex int32
before RecordImages
aIndex int32
after RecordImages
}
// AppendBeofreImages
func (r *RoundRecordImage) AppendBeofreImages(images []*RecordImage) {
for _, image := range images {
r.AppendBeofreImage(image)
}
}
// AppendBeofreImage
func (r *RoundRecordImage) AppendBeofreImage(image *RecordImage) {
r.bIndex++
image.index = r.bIndex
r.before = append(r.before, image)
}
// AppendAfterImages
func (r *RoundRecordImage) AppendAfterImages(images []*RecordImage) {
for _, image := range images {
r.AppendAfterImage(image)
}
}
// AppendAfterImage
func (r *RoundRecordImage) AppendAfterImage(image *RecordImage) {
r.aIndex++
image.index = r.aIndex
r.after = append(r.after, image)
}
func (r *RoundRecordImage) BeofreImages() RecordImages {
return r.before
}
func (r *RoundRecordImage) AfterImages() RecordImages {
return r.after
}
func (r *RoundRecordImage) IsEmpty() bool {
return len(r.before) == 0 && len(r.after) == 0
}
func (r *RoundRecordImage) IsBeforeAfterSizeEq() bool {
return len(r.before) == len(r.after)
}
type RecordImages []*RecordImage
// Reserve The order of reverse mirrors, when executing undo, needs to be executed in reverse
func (rs RecordImages) Reserve() {
l := 0
r := len(rs) - 1
for l <= r {
rs[l], rs[r] = rs[r], rs[l]
l++
r--
}
}
func (rs RecordImages) IsEmptyImage() bool {
if len(rs) == 0 {
return true
}
for _, r := range rs {
if r == nil || len(r.Rows) == 0 {
continue
}
return false
}
return true
}
// RecordImage
type RecordImage struct {
// index
index int32 `json:"-"`
// TableName table name
TableName string `json:"tableName"`
// SQLType sql type
SQLType SQLType `json:"sqlType"`
// Rows data row
Rows []RowImage `json:"rows"`
// TableMeta table information schema
TableMeta *TableMeta `json:"-"`
}
func NewEmptyRecordImage(tableMeta *TableMeta, sqlType SQLType) *RecordImage {
return &RecordImage{
TableName: tableMeta.TableName,
TableMeta: tableMeta,
SQLType: sqlType,
}
}
// RowImage Mirror data information information
type RowImage struct {
// Columns All columns of image data
Columns []ColumnImage `json:"fields"`
}
func (r *RowImage) GetColumnMap() map[string]*ColumnImage {
m := make(map[string]*ColumnImage, 0)
for _, column := range r.Columns {
tmpColumn := column
m[column.ColumnName] = &tmpColumn
}
return m
}
// PrimaryKeys Primary keys list.
func (r *RowImage) PrimaryKeys(cols []ColumnImage) []ColumnImage {
var pkFields []ColumnImage
for key, _ := range cols {
if cols[key].KeyType == PrimaryKey.Number() {
pkFields = append(pkFields, cols[key])
}
}
return pkFields
}
// NonPrimaryKeys get non-primary keys
func (r *RowImage) NonPrimaryKeys(cols []ColumnImage) []ColumnImage {
var nonPkFields []ColumnImage
for key, _ := range cols {
if cols[key].KeyType != PrimaryKey.Number() {
nonPkFields = append(nonPkFields, cols[key])
}
}
return nonPkFields
}
var _ json.Unmarshaler = (*ColumnImage)(nil)
var _ json.Marshaler = (*ColumnImage)(nil)
type CommonValue struct {
Value interface{}
}
// ColumnImage The mirror data information of the column
type ColumnImage struct {
// KeyType index type
KeyType IndexType `json:"keyType"`
// ColumnName column name
ColumnName string `json:"name"`
// ColumnType column type
ColumnType JDBCType `json:"type"`
// Value column value
Value interface{} `json:"value"`
}
type columnImageAlias ColumnImage
func (c *ColumnImage) MarshalJSON() ([]byte, error) {
if c == nil || c.Value == nil {
return json.Marshal(*c)
}
value := c.Value
if t, ok := c.Value.(time.Time); ok {
value = t.Format(time.RFC3339Nano)
}
return json.Marshal(&columnImageAlias{
KeyType: c.KeyType,
ColumnName: c.ColumnName,
ColumnType: c.ColumnType,
Value: value,
})
}
func (c *ColumnImage) UnmarshalJSON(data []byte) error {
var err error
tmpImage := make(map[string]interface{})
if err := json.Unmarshal(data, &tmpImage); err != nil {
return err
}
var (
keyType string
columnType int16
columnName string
value interface{}
actualValue interface{}
)
keyType = tmpImage["keyType"].(string)
columnType = int16(int64(tmpImage["type"].(float64)))
columnName = tmpImage["name"].(string)
value = tmpImage["value"]
if value != nil {
switch JDBCType(columnType) {
case JDBCTypeReal: // 4 Bytes
actualValue = value.(float32)
case JDBCTypeDecimal, JDBCTypeDouble: // 8 Bytes
actualValue = value.(float64)
case JDBCTypeTinyInt: // 1 Bytes
actualValue = int8(value.(float64))
case JDBCTypeSmallInt: // 2 Bytes
actualValue = int16(value.(float64))
case JDBCTypeInteger: // 4 Bytes
actualValue = int32(value.(float64))
case JDBCTypeBigInt: // 8Bytes
actualValue = int64(value.(float64))
case JDBCTypeTimestamp: // 4 Bytes
actualValue, err = time.Parse(time.RFC3339Nano, value.(string))
if err != nil {
return err
}
case JDBCTypeDate: // 3Bytes
actualValue, err = time.Parse(time.RFC3339Nano, value.(string))
if err != nil {
return err
}
case JDBCTypeTime: // 3Bytes
actualValue, err = time.Parse(time.RFC3339Nano, value.(string))
if err != nil {
return err
}
case JDBCTypeChar, JDBCTypeVarchar:
var val []byte
if val, err = base64.StdEncoding.DecodeString(value.(string)); err != nil {
val = []byte(value.(string))
}
actualValue = string(val)
case JDBCTypeBinary, JDBCTypeVarBinary, JDBCTypeLongVarBinary, JDBCTypeBit:
actualValue = value
}
}
*c = ColumnImage{
KeyType: ParseIndexType(keyType),
ColumnName: columnName,
ColumnType: JDBCType(columnType),
Value: actualValue,
}
return nil
}
func (c *ColumnImage) GetActualValue() interface{} {
if c.Value == nil {
return nil
}
value := reflect.ValueOf(c.Value)
kind := reflect.TypeOf(c.Value).Kind()
switch kind {
case reflect.Ptr:
return value.Elem().Interface()
}
return c.Value
}