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.text;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
23  import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
24  import org.apache.shardingsphere.infra.util.datetime.DateTimeFormatterFactory;
25  
26  import java.math.BigDecimal;
27  import java.sql.Timestamp;
28  import java.time.LocalDateTime;
29  import java.util.ArrayList;
30  import java.util.Collection;
31  
32  /**
33   * Text result set row packet for MySQL.
34   *
35   * @see <a href="https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset_row.html">Text Resultset Row</a>
36   */
37  @RequiredArgsConstructor
38  @Getter
39  public final class MySQLTextResultSetRowPacket extends MySQLPacket {
40      
41      private static final int NULL = 0xfb;
42      
43      private final Collection<Object> data;
44      
45      public MySQLTextResultSetRowPacket(final MySQLPacketPayload payload, final int columnCount) {
46          data = new ArrayList<>(columnCount);
47          for (int i = 0; i < columnCount; i++) {
48              data.add(payload.readStringLenenc());
49          }
50      }
51      
52      @Override
53      protected void write(final MySQLPacketPayload payload) {
54          for (Object each : data) {
55              if (null == each) {
56                  payload.writeInt1(NULL);
57                  continue;
58              }
59              writeDataIntoPayload(payload, each);
60          }
61      }
62      
63      private void writeDataIntoPayload(final MySQLPacketPayload payload, final Object data) {
64          if (data instanceof byte[]) {
65              payload.writeBytesLenenc((byte[]) data);
66          } else if (data instanceof Timestamp && 0 == ((Timestamp) data).getNanos()) {
67              payload.writeStringLenenc(data.toString().split("\\.")[0]);
68          } else if (data instanceof BigDecimal) {
69              payload.writeStringLenenc(((BigDecimal) data).toPlainString());
70          } else if (data instanceof Boolean) {
71              payload.writeBytesLenenc((boolean) data ? new byte[]{1} : new byte[]{0});
72          } else if (data instanceof LocalDateTime) {
73              payload.writeStringLenenc(DateTimeFormatterFactory.getStandardFormatter().format((LocalDateTime) data));
74          } else {
75              payload.writeStringLenenc(data.toString());
76          }
77      }
78  }