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.close;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.db.protocol.postgresql.exception.PostgreSQLProtocolException;
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.identifier.PostgreSQLIdentifierTag;
26  import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
27  
28  /**
29   * Command close packet for PostgreSQL.
30   */
31  @Getter
32  public final class PostgreSQLComClosePacket extends PostgreSQLCommandPacket {
33      
34      private final Type type;
35      
36      private final String name;
37      
38      public PostgreSQLComClosePacket(final PostgreSQLPacketPayload payload) {
39          payload.readInt4();
40          type = Type.valueOf((char) payload.readInt1());
41          name = payload.readStringNul();
42      }
43      
44      @Override
45      protected void write(final PostgreSQLPacketPayload payload) {
46      }
47      
48      @Override
49      public PostgreSQLIdentifierTag getIdentifier() {
50          return PostgreSQLCommandPacketType.CLOSE_COMMAND;
51      }
52      
53      @RequiredArgsConstructor
54      public enum Type {
55          
56          PREPARED_STATEMENT('S'),
57          
58          PORTAL('P');
59          
60          private final char value;
61          
62          /**
63           * Value of type.
64           *
65           * @param type type char
66           * @return type
67           * @throws PostgreSQLProtocolException PostgreSQL protocol exception
68           */
69          public static Type valueOf(final char type) {
70              for (Type each : values()) {
71                  if (type == each.value) {
72                      return each;
73                  }
74              }
75              throw new PostgreSQLProtocolException("Close type must be 'S' or 'P'. Got '%c'.", type);
76          }
77      }
78  }