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());
}
}
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));
}
}
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);
}
}
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());
}
}
}
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);
}
Aggregations