Search in sources :

Example 1 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

the class FrontendConnection method query.

public void query(byte[] data) {
    // 取得语句
    String sql = null;
    try {
        MySQLMessage mm = new MySQLMessage(data);
        mm.position(5);
        sql = mm.readString(charset);
    } catch (UnsupportedEncodingException e) {
        writeErrMessage(ErrorCode.ER_UNKNOWN_CHARACTER_SET, "Unknown charset '" + charset + "'");
        return;
    }
    this.query(sql);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 2 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

the class FrontendConnection method initDB.

public void initDB(byte[] data) {
    MySQLMessage mm = new MySQLMessage(data);
    mm.position(5);
    String db = mm.readString();
    // 检查schema的有效性
    if (db == null || !privileges.schemaExists(db)) {
        writeErrMessage(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + db + "'");
        return;
    }
    if (!privileges.userExists(user, host)) {
        writeErrMessage(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + user + "'");
        return;
    }
    Set<String> schemas = privileges.getUserSchemas(user);
    if (schemas == null || schemas.size() == 0 || schemas.contains(db)) {
        this.schema = db;
        write(writeToBuffer(OkPacket.OK, allocate()));
    } else {
        String s = "Access denied for user '" + user + "' to database '" + db + "'";
        writeErrMessage(ErrorCode.ER_DBACCESS_DENIED_ERROR, s);
    }
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 3 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

the class FrontendCommandHandler method handle.

@Override
public void handle(byte[] data) {
    if (source.getLoadDataInfileHandler() != null && source.getLoadDataInfileHandler().isStartLoadData()) {
        MySQLMessage mm = new MySQLMessage(data);
        int packetLength = mm.readUB3();
        if (packetLength + 4 == data.length) {
            source.loadDataInfileData(data);
        }
        return;
    }
    switch(data[4]) {
        case MySQLPacket.COM_INIT_DB:
            commands.doInitDB();
            source.initDB(data);
            break;
        case MySQLPacket.COM_QUERY:
            commands.doQuery();
            source.query(data);
            break;
        case MySQLPacket.COM_PING:
            commands.doPing();
            source.ping();
            break;
        case MySQLPacket.COM_QUIT:
            commands.doQuit();
            source.close("quit cmd");
            break;
        case MySQLPacket.COM_PROCESS_KILL:
            commands.doKill();
            source.kill(data);
            break;
        case MySQLPacket.COM_STMT_PREPARE:
            commands.doStmtPrepare();
            source.stmtPrepare(data);
            break;
        case MySQLPacket.COM_STMT_SEND_LONG_DATA:
            commands.doStmtSendLongData();
            source.stmtSendLongData(data);
            break;
        case MySQLPacket.COM_STMT_RESET:
            commands.doStmtReset();
            source.stmtReset(data);
            break;
        case MySQLPacket.COM_STMT_EXECUTE:
            commands.doStmtExecute();
            source.stmtExecute(data);
            break;
        case MySQLPacket.COM_STMT_CLOSE:
            commands.doStmtClose();
            source.stmtClose(data);
            break;
        case MySQLPacket.COM_HEARTBEAT:
            commands.doHeartbeat();
            source.heartbeat(data);
            break;
        default:
            commands.doOther();
            source.writeErrMessage(ErrorCode.ER_UNKNOWN_COM_ERROR, "Unknown command");
    }
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

Example 4 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

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 5 with MySQLMessage

use of io.mycat.backend.mysql.MySQLMessage in project Mycat_plus by coderczp.

the class HeartbeatPacket method read.

public void read(byte[] data) {
    MySQLMessage mm = new MySQLMessage(data);
    packetLength = mm.readUB3();
    packetId = mm.read();
    command = mm.read();
    id = mm.readLength();
}
Also used : MySQLMessage(io.mycat.backend.mysql.MySQLMessage)

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