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(PostgreSQLCommandPacketType.PARSE_COMMAND,
59              PostgreSQLCommandPacketType.BIND_COMMAND, PostgreSQLCommandPacketType.DESCRIBE_COMMAND, PostgreSQLCommandPacketType.EXECUTE_COMMAND,
60              PostgreSQLCommandPacketType.SYNC_COMMAND, PostgreSQLCommandPacketType.CLOSE_COMMAND, PostgreSQLCommandPacketType.FLUSH_COMMAND);
61      
62      private final char value;
63      
64      /**
65       * Value of integer.
66       * 
67       * @param value integer value
68       * @return command packet type enum
69       * @throws PostgreSQLProtocolException PostgreSQL protocol exception
70       */
71      public static PostgreSQLCommandPacketType valueOf(final int value) {
72          for (PostgreSQLCommandPacketType each : values()) {
73              if (value == each.value) {
74                  return each;
75              }
76          }
77          throw new PostgreSQLProtocolException("Can not find `%s` in PostgreSQL command packet type.", value);
78      }
79      
80      /**
81       * Check if the packet type is extended protocol packet type.
82       *
83       * @param commandPacketType command packet type
84       * @return is extended protocol packet type
85       */
86      public static boolean isExtendedProtocolPacketType(final CommandPacketType commandPacketType) {
87          return EXTENDED_PROTOCOL_PACKET_TYPES.contains(commandPacketType);
88      }
89  }