Search in sources :

Example 21 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generatePreparePayloadRequest.

public static byte[] generatePreparePayloadRequest(byte[] sql) {
    try (MySQLPayloadWriter byteArrayOutput = new MySQLPayloadWriter(1 + sql.length)) {
        byteArrayOutput.writeByte(0x16);
        byteArrayOutput.write(sql);
        byte[] bytes = byteArrayOutput.toByteArray();
        return bytes;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 22 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generateComQuery.

public static final byte[] generateComQuery(String sql) {
    try (MySQLPayloadWriter writer = new MySQLPayloadWriter(sql.length() + 5)) {
        writer.write(0x3);
        writer.writeEOFString(sql);
        return writer.toByteArray();
    }
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter)

Example 23 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generateMySQLPacket.

public static byte[] generateMySQLPacket(int packetId, byte[] packet) {
    try {
        if (packet.length < PacketSplitterImpl.MAX_PACKET_SIZE) {
            int wholePacketSize = MySQLPacketSplitter.caculWholePacketSize(packet.length);
            try (MySQLPayloadWriter byteArray = new MySQLPayloadWriter(wholePacketSize)) {
                int offset = 0;
                int len = packet.length;
                byteArray.writeFixInt(3, len);
                byteArray.write(packetId);
                byteArray.write(packet, offset, len);
                ++packetId;
                return byteArray.toByteArray();
            }
        } else {
            PacketSplitterImpl packetSplitter = new PacketSplitterImpl();
            int wholePacketSize = MySQLPacketSplitter.caculWholePacketSize(packet.length);
            try (MySQLPayloadWriter byteArray = new MySQLPayloadWriter(wholePacketSize)) {
                packetSplitter.init(packet.length);
                while (packetSplitter.nextPacketInPacketSplitter()) {
                    int offset = packetSplitter.getOffsetInPacketSplitter();
                    int len = packetSplitter.getPacketLenInPacketSplitter();
                    byteArray.writeFixInt(3, len);
                    byteArray.write(packetId);
                    byteArray.write(packet, offset, len);
                    ++packetId;
                }
                return byteArray.toByteArray();
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 24 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generateSSLRequest.

public static final byte[] generateSSLRequest(int clientCapacities, int maxPacketSize, int characterCollation, String reserved, int extendedClientCapabilitles, String reserved2) {
    try (MySQLPayloadWriter writer = new MySQLPayloadWriter(512)) {
        writer.writeFixInt(4, clientCapacities);
        writer.writeFixInt(4, maxPacketSize);
        writer.writeFixInt(1, characterCollation);
        writer.write(reserved.getBytes());
        if (MySQLServerCapabilityFlags.isLongPassword(clientCapacities)) {
            writer.writeFixInt(4, extendedClientCapabilitles);
        } else {
            writer.write(reserved2.getBytes());
        }
        return writer.toByteArray();
    }
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter)

Example 25 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generateAllColumnDefPayload.

// public static final byte[] generateColumnDefPayload(ResultSetMetaData metaData, int columnIndex) {
// try (MySQLPayloadWriter writer = new MySQLPayloadWriter(128)) {
// ColumnDefPacketImpl columnDefPacket = new ColumnDefPacketImpl(metaData, columnIndex);
// columnDefPacket.writePayload(writer);
// return writer.toByteArray();
// }
// }
public static final Iterable<byte[]> generateAllColumnDefPayload(MycatRowMetaData metaData) {
    List<byte[]> list = new ArrayList<>(metaData.getColumnCount());
    final int count = metaData.getColumnCount();
    if (metaData instanceof MycatMySQLRowMetaData) {
        List<ColumnDefPacketImpl> columnDefPackets = (List) ((MycatMySQLRowMetaData) metaData).getColumnDefPackets();
        for (int index = 0; index < count; index++) {
            try (MySQLPayloadWriter writer = new MySQLPayloadWriter(128)) {
                columnDefPackets.get(index).writePayload(writer);
                list.add(writer.toByteArray());
            }
        }
    } else {
        for (int index = 0; index < count; index++) {
            list.add(MySQLPacketUtil.generateColumnDefPayload(metaData, index));
        }
    }
    return list;
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) MycatMySQLRowMetaData(io.mycat.beans.mycat.MycatMySQLRowMetaData) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

MySQLPayloadWriter (io.mycat.beans.mysql.MySQLPayloadWriter)31 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 MycatMySQLRowMetaData (io.mycat.beans.mycat.MycatMySQLRowMetaData)2 AuthPacket (io.mycat.beans.mysql.packet.AuthPacket)2 AuthSwitchRequestPacket (io.mycat.beans.mysql.packet.AuthSwitchRequestPacket)2 Authenticator (io.mycat.Authenticator)1 MycatUser (io.mycat.MycatUser)1 HandshakePacket (io.mycat.beans.mysql.packet.HandshakePacket)1 MySQLPacket (io.mycat.beans.mysql.packet.MySQLPacket)1 MySQLServerCapabilityFlags (io.mycat.config.MySQLServerCapabilityFlags)1 UserConfig (io.mycat.config.UserConfig)1 Buffer (io.vertx.core.buffer.Buffer)1 SocketAddress (java.net.SocketAddress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1