// Path: src/main/java/com/github/pgasync/Oid.java
// public enum Oid {
// 
//     UNSPECIFIED(0),
//     INT2(21),
//     INT2_ARRAY(1005),
//     INT4(23),
//     INT4_ARRAY(1007),
//     INT8(20),
//     INT8_ARRAY(1016),
//     TEXT(25),
//     TEXT_ARRAY(1009),
//     NUMERIC(1700),
//     NUMERIC_ARRAY(1231),
//     FLOAT4(700),
//     FLOAT4_ARRAY(1021),
//     FLOAT8(701),
//     FLOAT8_ARRAY(1022),
//     BOOL(16),
//     BOOL_ARRAY(1000),
//     DATE(1082),
//     DATE_ARRAY(1182),
//     TIME(1083),
//     TIME_ARRAY(1183),
//     TIMETZ(1266),
//     TIMETZ_ARRAY(1270),
//     TIMESTAMP(1114),
//     TIMESTAMP_ARRAY(1115),
//     TIMESTAMPTZ(1184),
//     TIMESTAMPTZ_ARRAY(1185),
//     BYTEA(17),
//     BYTEA_ARRAY(1001),
//     VARCHAR(1043),
//     VARCHAR_ARRAY(1015),
//     OID(26),
//     OID_ARRAY(1028),
//     BPCHAR(1042),
//     BPCHAR_ARRAY(1014),
//     MONEY(790),
//     MONEY_ARRAY(791),
//     NAME(19),
//     NAME_ARRAY(1003),
//     BIT(1560),
//     BIT_ARRAY(1561),
//     VOID(2278),
//     INTERVAL(1186),
//     INTERVAL_ARRAY(1187),
//     CHAR(18),
//     CHAR_ARRAY(1002),
//     VARBIT(1562),
//     VARBIT_ARRAY(1563),
//     UUID(2950),
//     UUID_ARRAY(2951),
//     XML(142),
//     XML_ARRAY(143),
//     POINT(600),
//     BOX(603),
//     // Added
//     JSON(114),
//     JSONB(3802),
//     HSTORE(33670)
//     ;
// 
//     final int id;
// 
//     Oid(int id) {
//         this.id = id;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
//     public static Oid valueOfId(int id) {
//         for (Oid oid : values()) {
//             if (oid.id == id) {
//                 return oid;
//             }
//         }
//         return Oid.UNSPECIFIED;
//     }
// }
// 
// Path: src/main/java/com/github/pgasync/message/frontend/Parse.java
// public class Parse implements ExtendedQueryMessage {
// 
//     private final String sql;
//     private final String sname;
//     private final Oid[] types;
// 
//     public Parse(String sql, String sname, Oid[] types) {
//         this.sql = sql;
//         this.sname = sname;
//         this.types = types;
//     }
// 
//     public String getQuery() {
//         return sql;
//     }
// 
//     public String getSname() {
//         return sname;
//     }
// 
//     public Oid[] getTypes() {
//         return types;
//     }
// }
// 
// Path: src/main/java/com/github/pgasync/io/IO.java
// public class IO {
// 
//     public static String getCString(ByteBuffer buffer, Charset charset) {
//         ByteArrayOutputStream readBuffer = new ByteArrayOutputStream(255);
//         for (int c = buffer.get(); c != 0; c = buffer.get()) {
//             readBuffer.write(c);
//         }
//         return new String(readBuffer.toByteArray(), charset);
//     }
// 
//     public static void putCString(ByteBuffer buffer, String value, Charset charset) {
//         if (!value.isEmpty()) {
//             buffer.put(value.getBytes(charset));
//         }
//         buffer.put((byte) 0);
//     }
// 
// }


import com.github.pgasync.Oid;
import com.github.pgasync.message.frontend.Parse;
import com.github.pgasync.io.IO;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.pgasync.io.frontend;



/**
 * See <a href="www.postgresql.org/docs/9.3/static/protocol-message-formats.html">Postgres message formats</a>
 *
 * <pre>
 * Parse (F)
 *  Byte1('P')
 *      Identifies the message as a Parse command.
 *  Int32
 *      Length of message contents in bytes, including self.
 *  String
 *      The name of the destination prepared statement (an empty string selects the unnamed prepared statement).
 *  String
 *      The query string to be parsed.
 *  Int16
 *      The number of parameter data types specified (can be zero). Note that this is not an indication of the number of parameters that might appear in the query string, only the number that the frontend wants to prespecify types for.
 *  Then, for each parameter, there is the following:
 *  Int32
 *      Specifies the object ID of the parameter data type. Placing a zero here is equivalent to leaving the type unspecified.
 * </pre>
 *
 * @author Antti Laisi
 */
public class ParseEncoder extends ExtendedQueryEncoder<Parse> {

    @Override
    public Class<Parse> getMessageType() {
        return Parse.class;
    }

    @Override
    protected byte getMessageId() {
        return (byte) 'P';
    }

    @Override
    public void writeBody(Parse msg, ByteBuffer buffer, Charset encoding) {
        IO.putCString(buffer, msg.getSname(), encoding); // prepared statement
        IO.putCString(buffer, msg.getQuery(), encoding);
        buffer.putShort((short) msg.getTypes().length); // parameter types count
// Path: src/main/java/com/github/pgasync/Oid.java
// public enum Oid {
// 
//     UNSPECIFIED(0),
//     INT2(21),
//     INT2_ARRAY(1005),
//     INT4(23),
//     INT4_ARRAY(1007),
//     INT8(20),
//     INT8_ARRAY(1016),
//     TEXT(25),
//     TEXT_ARRAY(1009),
//     NUMERIC(1700),
//     NUMERIC_ARRAY(1231),
//     FLOAT4(700),
//     FLOAT4_ARRAY(1021),
//     FLOAT8(701),
//     FLOAT8_ARRAY(1022),
//     BOOL(16),
//     BOOL_ARRAY(1000),
//     DATE(1082),
//     DATE_ARRAY(1182),
//     TIME(1083),
//     TIME_ARRAY(1183),
//     TIMETZ(1266),
//     TIMETZ_ARRAY(1270),
//     TIMESTAMP(1114),
//     TIMESTAMP_ARRAY(1115),
//     TIMESTAMPTZ(1184),
//     TIMESTAMPTZ_ARRAY(1185),
//     BYTEA(17),
//     BYTEA_ARRAY(1001),
//     VARCHAR(1043),
//     VARCHAR_ARRAY(1015),
//     OID(26),
//     OID_ARRAY(1028),
//     BPCHAR(1042),
//     BPCHAR_ARRAY(1014),
//     MONEY(790),
//     MONEY_ARRAY(791),
//     NAME(19),
//     NAME_ARRAY(1003),
//     BIT(1560),
//     BIT_ARRAY(1561),
//     VOID(2278),
//     INTERVAL(1186),
//     INTERVAL_ARRAY(1187),
//     CHAR(18),
//     CHAR_ARRAY(1002),
//     VARBIT(1562),
//     VARBIT_ARRAY(1563),
//     UUID(2950),
//     UUID_ARRAY(2951),
//     XML(142),
//     XML_ARRAY(143),
//     POINT(600),
//     BOX(603),
//     // Added
//     JSON(114),
//     JSONB(3802),
//     HSTORE(33670)
//     ;
// 
//     final int id;
// 
//     Oid(int id) {
//         this.id = id;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
//     public static Oid valueOfId(int id) {
//         for (Oid oid : values()) {
//             if (oid.id == id) {
//                 return oid;
//             }
//         }
//         return Oid.UNSPECIFIED;
//     }
// }
// 
// Path: src/main/java/com/github/pgasync/message/frontend/Parse.java
// public class Parse implements ExtendedQueryMessage {
// 
//     private final String sql;
//     private final String sname;
//     private final Oid[] types;
// 
//     public Parse(String sql, String sname, Oid[] types) {
//         this.sql = sql;
//         this.sname = sname;
//         this.types = types;
//     }
// 
//     public String getQuery() {
//         return sql;
//     }
// 
//     public String getSname() {
//         return sname;
//     }
// 
//     public Oid[] getTypes() {
//         return types;
//     }
// }
// 
// Path: src/main/java/com/github/pgasync/io/IO.java
// public class IO {
// 
//     public static String getCString(ByteBuffer buffer, Charset charset) {
//         ByteArrayOutputStream readBuffer = new ByteArrayOutputStream(255);
//         for (int c = buffer.get(); c != 0; c = buffer.get()) {
//             readBuffer.write(c);
//         }
//         return new String(readBuffer.toByteArray(), charset);
//     }
// 
//     public static void putCString(ByteBuffer buffer, String value, Charset charset) {
//         if (!value.isEmpty()) {
//             buffer.put(value.getBytes(charset));
//         }
//         buffer.put((byte) 0);
//     }
// 
// }


// Path: src/main/java/com/github/pgasync/io/frontend/ParseEncoder.java
import com.github.pgasync.Oid;
import com.github.pgasync.message.frontend.Parse;
import com.github.pgasync.io.IO;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.pgasync.io.frontend;



/**
 * See <a href="www.postgresql.org/docs/9.3/static/protocol-message-formats.html">Postgres message formats</a>
 *
 * <pre>
 * Parse (F)
 *  Byte1('P')
 *      Identifies the message as a Parse command.
 *  Int32
 *      Length of message contents in bytes, including self.
 *  String
 *      The name of the destination prepared statement (an empty string selects the unnamed prepared statement).
 *  String
 *      The query string to be parsed.
 *  Int16
 *      The number of parameter data types specified (can be zero). Note that this is not an indication of the number of parameters that might appear in the query string, only the number that the frontend wants to prespecify types for.
 *  Then, for each parameter, there is the following:
 *  Int32
 *      Specifies the object ID of the parameter data type. Placing a zero here is equivalent to leaving the type unspecified.
 * </pre>
 *
 * @author Antti Laisi
 */
public class ParseEncoder extends ExtendedQueryEncoder<Parse> {

    @Override
    public Class<Parse> getMessageType() {
        return Parse.class;
    }

    @Override
    protected byte getMessageId() {
        return (byte) 'P';
    }

    @Override
    public void writeBody(Parse msg, ByteBuffer buffer, Charset encoding) {
        IO.putCString(buffer, msg.getSname(), encoding); // prepared statement
        IO.putCString(buffer, msg.getQuery(), encoding);
        buffer.putShort((short) msg.getTypes().length); // parameter types count
        for (Oid type : msg.getTypes()) {