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.command.query;
19  
20  import com.google.common.base.Preconditions;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLBinaryColumnType;
23  import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
24  import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
25  
26  /**
27   * Column definition above MySQL 4.1 packet protocol.
28   *
29   * @see <a href="https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset_column_definition.html">ColumnDefinition41</a>
30   * @see <a href="https://mariadb.com/kb/en/library/resultset/#column-definition-packet">Column definition packet</a>
31   */
32  @RequiredArgsConstructor
33  public final class MySQLColumnDefinition41Packet extends MySQLPacket {
34      
35      private static final String CATALOG = "def";
36      
37      private static final int NEXT_LENGTH = 0x0c;
38      
39      private final int characterSet;
40      
41      private final int flags;
42      
43      private final String schema;
44      
45      private final String table;
46      
47      private final String orgTable;
48      
49      private final String name;
50      
51      private final String orgName;
52      
53      private final int columnLength;
54      
55      private final MySQLBinaryColumnType columnType;
56      
57      private final int decimals;
58      
59      private final boolean containDefaultValues;
60      
61      /*
62       * Field description of column definition Packet.
63       *
64       * @see <a href="https://github.com/apache/shardingsphere/issues/4358"></a>
65       */
66      public MySQLColumnDefinition41Packet(final int characterSet, final String schema, final String table, final String orgTable,
67                                           final String name, final String orgName, final int columnLength, final MySQLBinaryColumnType columnType,
68                                           final int decimals, final boolean containDefaultValues) {
69          this(characterSet, 0, schema, table, orgTable, name, orgName, columnLength, columnType, decimals, containDefaultValues);
70      }
71      
72      public MySQLColumnDefinition41Packet(final MySQLPacketPayload payload) {
73          Preconditions.checkArgument(CATALOG.equals(payload.readStringLenenc()));
74          schema = payload.readStringLenenc();
75          table = payload.readStringLenenc();
76          orgTable = payload.readStringLenenc();
77          name = payload.readStringLenenc();
78          orgName = payload.readStringLenenc();
79          Preconditions.checkArgument(NEXT_LENGTH == payload.readIntLenenc());
80          characterSet = payload.readInt2();
81          columnLength = payload.readInt4();
82          columnType = MySQLBinaryColumnType.valueOf(payload.readInt1());
83          flags = payload.readInt2();
84          decimals = payload.readInt1();
85          payload.skipReserved(2);
86          containDefaultValues = false;
87      }
88      
89      @Override
90      protected void write(final MySQLPacketPayload payload) {
91          payload.writeStringLenenc(CATALOG);
92          payload.writeStringLenenc(schema);
93          payload.writeStringLenenc(table);
94          payload.writeStringLenenc(orgTable);
95          payload.writeStringLenenc(name);
96          payload.writeStringLenenc(orgName);
97          payload.writeIntLenenc(NEXT_LENGTH);
98          payload.writeInt2(characterSet);
99          payload.writeInt4(columnLength);
100         payload.writeInt1(columnType.getValue());
101         payload.writeInt2(flags);
102         payload.writeInt1(decimals);
103         payload.writeReserved(2);
104         if (containDefaultValues) {
105             payload.writeIntLenenc(0);
106             payload.writeStringLenenc("");
107         }
108     }
109 }