Search in sources :

Example 1 with PreparedStatement

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

the class ServerPrepareHandler method close.

@Override
public void close(byte[] data) {
    // 获取prepare stmt id
    long pstmtId = ByteUtil.readUB4(data, 5);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("close prepare stmt, stmtId = " + pstmtId);
    }
    PreparedStatement pstmt = pstmtForId.remove(pstmtId);
    if (pstmt != null) {
        pstmtForSql.remove(pstmt.getStatement());
    }
}
Also used : PreparedStatement(io.mycat.backend.mysql.PreparedStatement)

Example 2 with PreparedStatement

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

the class ServerPrepareHandler method reset.

@Override
public void reset(byte[] data) {
    ResetPacket packet = new ResetPacket();
    packet.read(data);
    long pstmtId = packet.getPstmtId();
    PreparedStatement pstmt = pstmtForId.get(pstmtId);
    if (pstmt != null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("reset prepare sql : " + pstmtForId.get(pstmtId));
        }
        pstmt.resetLongData();
        source.write(OkPacket.OK);
    } else {
        source.writeErrMessage(ErrorCode.ERR_FOUND_EXCEPION, "can not reset prepare statement : " + pstmtForId.get(pstmtId));
    }
}
Also used : PreparedStatement(io.mycat.backend.mysql.PreparedStatement) ResetPacket(io.mycat.net.mysql.ResetPacket)

Example 3 with PreparedStatement

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

the class ServerPrepareHandler method execute.

@Override
public void execute(byte[] data) {
    long pstmtId = ByteUtil.readUB4(data, 5);
    PreparedStatement pstmt = null;
    if ((pstmt = pstmtForId.get(pstmtId)) == null) {
        source.writeErrMessage(ErrorCode.ER_ERROR_WHEN_EXECUTING_COMMAND, "Unknown pstmtId when executing.");
    } else {
        ExecutePacket packet = new ExecutePacket(pstmt);
        try {
            packet.read(data, source.getCharset());
        } catch (UnsupportedEncodingException e) {
            source.writeErrMessage(ErrorCode.ER_ERROR_WHEN_EXECUTING_COMMAND, e.getMessage());
            return;
        }
        BindValue[] bindValues = packet.values;
        // 还原sql中的动态参数为实际参数值
        String sql = prepareStmtBindValue(pstmt, bindValues);
        // 执行sql
        source.getSession2().setPrepared(true);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("execute prepare sql: " + sql);
        }
        source.query(sql);
    }
}
Also used : ExecutePacket(io.mycat.net.mysql.ExecutePacket) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(io.mycat.backend.mysql.PreparedStatement) BindValue(io.mycat.backend.mysql.BindValue)

Example 4 with PreparedStatement

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

the class ServerPrepareHandler method sendLongData.

@Override
public void sendLongData(byte[] data) {
    LongDataPacket packet = new LongDataPacket();
    packet.read(data);
    long pstmtId = packet.getPstmtId();
    PreparedStatement pstmt = pstmtForId.get(pstmtId);
    if (pstmt != null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("send long data to prepare sql : " + pstmtForId.get(pstmtId));
        }
        long paramId = packet.getParamId();
        try {
            pstmt.appendLongData(paramId, packet.getLongData());
        } catch (IOException e) {
            source.writeErrMessage(ErrorCode.ERR_FOUND_EXCEPION, e.getMessage());
        }
    }
}
Also used : LongDataPacket(io.mycat.net.mysql.LongDataPacket) PreparedStatement(io.mycat.backend.mysql.PreparedStatement) IOException(java.io.IOException)

Example 5 with PreparedStatement

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

the class ServerPrepareHandler method prepare.

@Override
public void prepare(String sql) {
    LOGGER.debug("use server prepare, sql: " + sql);
    PreparedStatement pstmt = null;
    if ((pstmt = pstmtForSql.get(sql)) == null) {
        // 解析获取字段个数和参数个数
        int columnCount = getColumnCount(sql);
        int paramCount = getParamCount(sql);
        pstmt = new PreparedStatement(++pstmtId, sql, columnCount, paramCount);
        pstmtForSql.put(pstmt.getStatement(), pstmt);
        pstmtForId.put(pstmt.getId(), pstmt);
    }
    PreparedStmtResponse.response(pstmt, source);
}
Also used : PreparedStatement(io.mycat.backend.mysql.PreparedStatement)

Aggregations

PreparedStatement (io.mycat.backend.mysql.PreparedStatement)5 BindValue (io.mycat.backend.mysql.BindValue)1 ExecutePacket (io.mycat.net.mysql.ExecutePacket)1 LongDataPacket (io.mycat.net.mysql.LongDataPacket)1 ResetPacket (io.mycat.net.mysql.ResetPacket)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1