Search in sources :

Example 21 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class ExecutePacket method read.

public void read(byte[] data, String charset) throws UnsupportedEncodingException {
    MySQLMessage mm = new MySQLMessage(data);
    packetLength = mm.readUB3();
    packetId = mm.read();
    code = mm.read();
    statementId = mm.readUB4();
    flags = mm.read();
    iterationCount = mm.readUB4();
    // 读取NULL指示器数据
    int parameterCount = values.length;
    if (parameterCount > 0) {
        nullBitMap = new byte[(parameterCount + 7) / 8];
        for (int i = 0; i < nullBitMap.length; i++) {
            nullBitMap[i] = mm.read();
        }
        // 当newParameterBoundFlag==1时,更新参数类型。
        newParameterBoundFlag = mm.read();
    }
    if (newParameterBoundFlag == (byte) 1) {
        for (int i = 0; i < parameterCount; i++) {
            pstmt.getParametersType()[i] = mm.readUB2();
        }
    }
    // 设置参数类型和读取参数值
    byte[] nullBitMap = this.nullBitMap;
    for (int i = 0; i < parameterCount; i++) {
        BindValue bv = new BindValue();
        bv.type = pstmt.getParametersType()[i];
        if ((nullBitMap[i / 8] & (1 << (i & 7))) != 0) {
            bv.isNull = true;
        } else {
            BindValueUtil.read(mm, bv, charset);
            if (bv.isLongData) {
                bv.value = pstmt.getLongData(i);
            }
        }
        values[i] = bv;
    }
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage) BindValue(io.mycat.backend.mysql.BindValue)

Example 22 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class HandshakePacket method read.

public void read(BinaryPacket bin) {
    packetLength = bin.packetLength;
    packetId = bin.packetId;
    MySQLMessage mm = new MySQLMessage(bin.data);
    protocolVersion = mm.read();
    serverVersion = mm.readBytesWithNull();
    threadId = mm.readUB4();
    seed = mm.readBytesWithNull();
    serverCapabilities = mm.readUB2();
    serverCharsetIndex = mm.read();
    serverStatus = mm.readUB2();
    mm.move(13);
    restOfScrambleBuff = mm.readBytesWithNull();
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 23 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class FrontendConnection method stmtPrepare.

public void stmtPrepare(byte[] data) {
    if (prepareHandler != null) {
        // 取得语句
        MySQLMessage mm = new MySQLMessage(data);
        mm.position(5);
        String sql = null;
        try {
            sql = mm.readString(charset);
        } catch (UnsupportedEncodingException e) {
            writeErrMessage(ErrorCode.ER_UNKNOWN_CHARACTER_SET, "Unknown charset '" + charset + "'");
            return;
        }
        if (sql == null || sql.length() == 0) {
            writeErrMessage(ErrorCode.ER_NOT_ALLOWED_COMMAND, "Empty SQL");
            return;
        }
        // 记录SQL
        this.setExecuteSql(sql);
        // 执行预处理
        prepareHandler.prepare(sql);
    } else {
        writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Prepare unsupported!");
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 24 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class MySQLMessageTest method testReadBytesWithNull.

@Test
public void testReadBytesWithNull() {
    byte[] bytes = new byte[] { 1, 2, 3, 0, 5 };
    MySQLMessage message = new MySQLMessage(bytes);
    byte[] ab = message.readBytesWithNull();
    Assert.assertEquals(3, ab.length);
    Assert.assertEquals(4, message.position());
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage) Test(org.junit.Test)

Example 25 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat-Server by MyCATApache.

the class MySQLMessageTest method testReadBytesWithNull3.

@Test
public void testReadBytesWithNull3() {
    byte[] bytes = new byte[] {};
    MySQLMessage message = new MySQLMessage(bytes);
    byte[] ab = message.readBytesWithNull();
    Assert.assertEquals(0, ab.length);
    Assert.assertEquals(0, message.position());
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage) Test(org.junit.Test)

Aggregations

MySQLMessage (io.mycat.backend.mysql.MySQLMessage)56 Test (org.junit.Test)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ByteBuffer (java.nio.ByteBuffer)4 BindValue (io.mycat.backend.mysql.BindValue)2 BufferHolder (io.mycat.memory.unsafe.row.BufferHolder)2 UnsafeRow (io.mycat.memory.unsafe.row.UnsafeRow)2 UnsafeRowWriter (io.mycat.memory.unsafe.row.UnsafeRowWriter)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 ServerConnection (io.mycat.server.ServerConnection)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2