View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.db.protocol.mysql.packet.binlog.row;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinaryColumnType;
22  import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.AbstractMySQLBinlogEventPacket;
23  import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.MySQLBinlogEventHeader;
24  import org.apache.shardingsphere.db.protocol.mysql.packet.binlog.row.column.MySQLBinlogColumnDef;
25  import org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLNullBitmap;
26  import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
27  
28  import java.util.LinkedList;
29  import java.util.List;
30  
31  /**
32   * MySQL binlog table map event packet.
33   *
34   * @see <a href="https://dev.mysql.com/doc/dev/mysql-server/latest/classbinary__log_1_1Table__map__event.html">TABLE_MAP_EVENT</a>
35   */
36  @Getter
37  public final class MySQLBinlogTableMapEventPacket extends AbstractMySQLBinlogEventPacket {
38      
39      private final long tableId;
40      
41      private final int flags;
42      
43      private final String schemaName;
44      
45      private final String tableName;
46      
47      private final int columnCount;
48      
49      private final List<MySQLBinlogColumnDef> columnDefs;
50      
51      private final MySQLNullBitmap nullBitMap;
52      
53      public MySQLBinlogTableMapEventPacket(final MySQLBinlogEventHeader binlogEventHeader, final MySQLPacketPayload payload) {
54          super(binlogEventHeader);
55          tableId = payload.readInt6();
56          flags = payload.readInt2();
57          schemaName = payload.readStringFix(payload.readInt1());
58          payload.skipReserved(1);
59          tableName = payload.readStringFix(payload.readInt1());
60          payload.skipReserved(1);
61          columnCount = (int) payload.readIntLenenc();
62          columnDefs = new LinkedList<>();
63          readColumnDefs(payload);
64          readColumnMetaDefs(payload);
65          nullBitMap = new MySQLNullBitmap(columnCount, payload);
66          // mysql 8 binlog table map event include column type def
67          // for support lower than 8, read column type def by sql query
68          // so skip readable bytes here
69          int remainBytesLength = getRemainBytesLength(payload);
70          if (remainBytesLength > 0) {
71              payload.skipReserved(remainBytesLength);
72          }
73      }
74      
75      private void readColumnDefs(final MySQLPacketPayload payload) {
76          for (int i = 0; i < columnCount; i++) {
77              columnDefs.add(new MySQLBinlogColumnDef(MySQLBinaryColumnType.valueOf(payload.readInt1())));
78          }
79      }
80      
81      private void readColumnMetaDefs(final MySQLPacketPayload payload) {
82          payload.readIntLenenc();
83          for (MySQLBinlogColumnDef each : columnDefs) {
84              each.setColumnMeta(readColumnMetaDef(each.getColumnType(), payload));
85          }
86      }
87      
88      private int readColumnMetaDef(final MySQLBinaryColumnType columnType, final MySQLPacketPayload payload) {
89          switch (columnType) {
90              case STRING:
91              case DECIMAL:
92              case NEWDECIMAL:
93                  return payload.getByteBuf().readUnsignedShort();
94              case BIT:
95              case VAR_STRING:
96              case VARCHAR:
97              case ENUM:
98                  return payload.readInt2();
99              case BLOB:
100             case TINY_BLOB:
101             case DOUBLE:
102             case FLOAT:
103             case TIME2:
104             case TIMESTAMP2:
105             case DATETIME2:
106             case JSON:
107                 return payload.readInt1();
108             default:
109                 return 0;
110         }
111     }
112     
113     @Override
114     protected void writeEvent(final MySQLPacketPayload payload) {
115         // TODO
116     }
117 }