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.postgresql.packet.command.query.extended.parse;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import org.apache.shardingsphere.db.protocol.packet.sql.SQLReceivedPacket;
23  import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
24  import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
25  import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
26  import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
27  import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
28  import org.apache.shardingsphere.infra.hint.HintValueContext;
29  import org.apache.shardingsphere.infra.hint.SQLHintUtils;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * Command parse packet for PostgreSQL.
36   */
37  @Getter
38  public final class PostgreSQLComParsePacket extends PostgreSQLCommandPacket implements SQLReceivedPacket {
39      
40      private final PostgreSQLPacketPayload payload;
41      
42      private final String statementId;
43      
44      @Getter(AccessLevel.NONE)
45      private final String sql;
46      
47      private final HintValueContext hintValueContext;
48      
49      public PostgreSQLComParsePacket(final PostgreSQLPacketPayload payload) {
50          this.payload = payload;
51          payload.readInt4();
52          statementId = payload.readStringNul();
53          String originSQL = payload.readStringNul();
54          hintValueContext = SQLHintUtils.extractHint(originSQL);
55          sql = SQLHintUtils.removeHint(originSQL);
56      }
57      
58      /**
59       * Read parameter types from Parse message.
60       * 
61       * @return types of parameters
62       */
63      public List<PostgreSQLColumnType> readParameterTypes() {
64          int parameterCount = payload.readInt2();
65          List<PostgreSQLColumnType> result = new ArrayList<>(parameterCount);
66          for (int i = 0; i < parameterCount; i++) {
67              result.add(PostgreSQLColumnType.valueOf(payload.readInt4()));
68          }
69          return result;
70      }
71      
72      @Override
73      protected void write(final PostgreSQLPacketPayload payload) {
74      }
75      
76      @Override
77      public String getSQL() {
78          return sql;
79      }
80      
81      @Override
82      public PostgreSQLIdentifierTag getIdentifier() {
83          return PostgreSQLCommandPacketType.PARSE_COMMAND;
84      }
85  }