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;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.db.protocol.packet.command.CommandPacketType;
23  import org.apache.shardingsphere.db.protocol.postgresql.exception.PostgreSQLProtocolException;
24  import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
25  
26  import java.util.EnumSet;
27  import java.util.Set;
28  
29  /**
30   * Command packet type for PostgreSQL.
31   * 
32   * @see <a href="https://www.postgresql.org/docs/13/protocol-message-formats.html">Message Formats</a>
33   */
34  @RequiredArgsConstructor
35  @Getter
36  public enum PostgreSQLCommandPacketType implements CommandPacketType, PostgreSQLIdentifierTag {
37      
38      PASSWORD('p'),
39      
40      SIMPLE_QUERY('Q'),
41      
42      PARSE_COMMAND('P'),
43      
44      BIND_COMMAND('B'),
45      
46      DESCRIBE_COMMAND('D'),
47      
48      EXECUTE_COMMAND('E'),
49      
50      SYNC_COMMAND('S'),
51      
52      CLOSE_COMMAND('C'),
53      
54      FLUSH_COMMAND('H'),
55      
56      TERMINATE('X');
57      
58      private static final Set<PostgreSQLCommandPacketType> EXTENDED_PROTOCOL_PACKET_TYPES = EnumSet.of(
59              PARSE_COMMAND, BIND_COMMAND, DESCRIBE_COMMAND, EXECUTE_COMMAND, SYNC_COMMAND, CLOSE_COMMAND, FLUSH_COMMAND);
60      
61      private final char value;
62      
63      /**
64       * Value of integer.
65       *
66       * @param value integer value
67       * @return command packet type enum
68       * @throws PostgreSQLProtocolException PostgreSQL protocol exception
69       */
70      public static PostgreSQLCommandPacketType valueOf(final int value) {
71          for (PostgreSQLCommandPacketType each : values()) {
72              if (value == each.value) {
73                  return each;
74              }
75          }
76          throw new PostgreSQLProtocolException("Can not find `%s` in PostgreSQL command packet type.", value);
77      }
78      
79      /**
80       * Check if the packet type is extended protocol packet type.
81       *
82       * @param commandPacketType command packet type
83       * @return is extended protocol packet type
84       */
85      public static boolean isExtendedProtocolPacketType(final CommandPacketType commandPacketType) {
86          return EXTENDED_PROTOCOL_PACKET_TYPES.contains(commandPacketType);
87      }
88  }