@@ -30,6 +30,7 @@ func normalizeRowData(
30
30
data []interface {},
31
31
meta * tableMeta ,
32
32
) {
33
+
33
34
for i , val := range data {
34
35
35
36
// No need to handle nil.
@@ -39,17 +40,16 @@ func normalizeRowData(
39
40
40
41
// NOTE: go-mysql stores int as signed values since before MySQL-8, no signedness
41
42
// information is presents in binlog. So we need to convert here if it is unsigned.
42
- if isNumericColumn ( meta .Table , i ) {
43
+ if meta .IsNumericColumn ( i ) {
43
44
if v , ok := val .(decimal.Decimal ); ok {
44
45
data [i ] = v .String ()
45
46
continue
46
47
}
47
48
48
- if ! meta .UnsignedMap [i ] {
49
+ if ! meta .UnsignedMap () [i ] {
49
50
continue
50
51
}
51
52
52
- typ := realType (meta .Table , i )
53
53
// Copy from go-mysql/canal/rows.go
54
54
switch v := val .(type ) {
55
55
case int8 :
@@ -59,7 +59,7 @@ func normalizeRowData(
59
59
data [i ] = uint16 (v )
60
60
61
61
case int32 :
62
- if v < 0 && typ == MYSQL_TYPE_INT24 {
62
+ if v < 0 && meta . RealType ( i ) == MYSQL_TYPE_INT24 {
63
63
// 16777215 is the maximum value of mediumint
64
64
data [i ] = uint32 (16777215 + v + 1 )
65
65
} else {
@@ -78,21 +78,21 @@ func normalizeRowData(
78
78
continue
79
79
}
80
80
81
- if isEnumColumn ( meta .Table , i ) {
81
+ if meta .IsEnumColumn ( i ) {
82
82
v , ok := val .(int64 )
83
83
if ! ok {
84
84
panic (fmt .Errorf ("Expect int64 for enum (MYSQL_TYPE_ENUM) field but got %T %#v" , val , val ))
85
85
}
86
- data [i ] = meta .EnumStrValueMap [i ][int (v )- 1 ]
86
+ data [i ] = meta .EnumStrValueMap () [i ][int (v )- 1 ]
87
87
continue
88
88
}
89
89
90
- if isSetColumn ( meta .Table , i ) {
90
+ if meta .IsSetColumn ( i ) {
91
91
v , ok := val .(int64 )
92
92
if ! ok {
93
93
panic (fmt .Errorf ("Expect int64 for set (MYSQL_TYPE_SET) field but got %T %#v" , val , val ))
94
94
}
95
- setStrValue := meta .SetStrValueMap [i ]
95
+ setStrValue := meta .SetStrValueMap () [i ]
96
96
vals := []string {}
97
97
for j := 0 ; j < 64 ; j ++ {
98
98
if (v & (1 << uint (j ))) != 0 {
@@ -103,7 +103,7 @@ func normalizeRowData(
103
103
continue
104
104
}
105
105
106
- if realType ( meta .Table , i ) == MYSQL_TYPE_YEAR {
106
+ if meta .RealType ( i ) == MYSQL_TYPE_YEAR {
107
107
v , ok := val .(int )
108
108
if ! ok {
109
109
panic (fmt .Errorf ("Expect int for year (MYSQL_TYPE_YEAR) field but got %T %#v" , val , val ))
@@ -113,7 +113,7 @@ func normalizeRowData(
113
113
continue
114
114
}
115
115
116
- if realType ( meta .Table , i ) == MYSQL_TYPE_NEWDATE {
116
+ if meta .RealType ( i ) == MYSQL_TYPE_NEWDATE {
117
117
v , ok := val .(string )
118
118
if ! ok {
119
119
panic (fmt .Errorf ("Expect string for date (MYSQL_TYPE_NEWDATE) field but got %T %#v" , val , val ))
@@ -136,113 +136,3 @@ func normalizeRowData(
136
136
}
137
137
}
138
138
}
139
-
140
- /*
141
- TODO:
142
- My PR has not merged yet: https://github.com/siddontang/go-mysql/pull/482
143
- So copy here.
144
- */
145
-
146
- type tableMeta struct {
147
- Table * replication.TableMapEvent
148
- UnsignedMap map [int ]bool
149
- EnumStrValueMap map [int ][]string
150
- SetStrValueMap map [int ][]string
151
- }
152
-
153
- func newTableMeta (e * replication.TableMapEvent ) * tableMeta {
154
- return & tableMeta {
155
- Table : e ,
156
- UnsignedMap : unsignedMap (e ),
157
- EnumStrValueMap : enumStrValueMap (e ),
158
- SetStrValueMap : setStrValueMap (e ),
159
- }
160
- }
161
-
162
- func enumStrValueMap (e * replication.TableMapEvent ) map [int ][]string {
163
- return strValueMap (e , isEnumColumn , e .EnumStrValueString ())
164
- }
165
-
166
- func setStrValueMap (e * replication.TableMapEvent ) map [int ][]string {
167
- return strValueMap (e , isSetColumn , e .SetStrValueString ())
168
- }
169
-
170
- func strValueMap (
171
- e * replication.TableMapEvent ,
172
- includeType func (* replication.TableMapEvent , int ) bool ,
173
- strValue [][]string ,
174
- ) map [int ][]string {
175
-
176
- if len (strValue ) == 0 {
177
- return nil
178
- }
179
- p := 0
180
- ret := make (map [int ][]string )
181
- for i := 0 ; i < int (e .ColumnCount ); i ++ {
182
- if ! includeType (e , i ) {
183
- continue
184
- }
185
- ret [i ] = strValue [p ]
186
- p ++
187
- }
188
- return ret
189
- }
190
-
191
- func unsignedMap (e * replication.TableMapEvent ) map [int ]bool {
192
- if len (e .SignednessBitmap ) == 0 {
193
- return nil
194
- }
195
- p := 0
196
- ret := make (map [int ]bool )
197
- for i := 0 ; i < int (e .ColumnCount ); i ++ {
198
- if ! isNumericColumn (e , i ) {
199
- continue
200
- }
201
- ret [i ] = e .SignednessBitmap [p / 8 ]& (1 << uint (7 - p % 8 )) != 0
202
- p ++
203
- }
204
- return ret
205
- }
206
-
207
- func isNumericColumn (e * replication.TableMapEvent , i int ) bool {
208
- switch realType (e , i ) {
209
- case MYSQL_TYPE_TINY ,
210
- MYSQL_TYPE_SHORT ,
211
- MYSQL_TYPE_INT24 ,
212
- MYSQL_TYPE_LONG ,
213
- MYSQL_TYPE_LONGLONG ,
214
- MYSQL_TYPE_NEWDECIMAL ,
215
- MYSQL_TYPE_FLOAT ,
216
- MYSQL_TYPE_DOUBLE :
217
- return true
218
-
219
- default :
220
- return false
221
- }
222
- }
223
-
224
- func isEnumColumn (e * replication.TableMapEvent , i int ) bool {
225
- return realType (e , i ) == MYSQL_TYPE_ENUM
226
- }
227
-
228
- func isSetColumn (e * replication.TableMapEvent , i int ) bool {
229
- return realType (e , i ) == MYSQL_TYPE_SET
230
- }
231
-
232
- func realType (e * replication.TableMapEvent , i int ) byte {
233
- typ := e .ColumnType [i ]
234
- meta := e .ColumnMeta [i ]
235
-
236
- switch typ {
237
- case MYSQL_TYPE_STRING :
238
- rtyp := byte (meta >> 8 )
239
- if rtyp == MYSQL_TYPE_ENUM || rtyp == MYSQL_TYPE_SET {
240
- return rtyp
241
- }
242
-
243
- case MYSQL_TYPE_DATE :
244
- return MYSQL_TYPE_NEWDATE
245
- }
246
-
247
- return typ
248
- }
0 commit comments