use of io.mycat.vertx.ReadView in project Mycat2 by MyCATApache.
the class ByteArrayDecoder method convert.
@Override
public byte[][] convert(Buffer payload) {
final int NULL = 0xFB;
byte[][] row = new byte[columnCount][];
ReadView readView = new ReadView(payload);
// TEXT row decoding
for (int c = 0; c < columnCount; c++) {
if ((readView.getByte() & 0xff) == NULL) {
readView.skipInReading(1);
} else {
row[c] = readView.readLenencBytes();
}
}
return row;
}
use of io.mycat.vertx.ReadView in project Mycat2 by MyCATApache.
the class StringArrayDecoder method convert.
@Override
public Object[] convert(Buffer payload) {
final int NULL = 0xFB;
Object[] row = new Object[columnCount][];
ReadView readView = new ReadView(payload);
// TEXT row decoding
for (int c = 0; c < columnCount; c++) {
if ((readView.getByte() & 0xff) == NULL) {
readView.skipInReading(1);
} else {
ColumnDefPacket columnDefPacket = columnDefPackets[c];
int columnFlags = columnDefPacket.getColumnFlags();
int columnType = columnDefPacket.getColumnType();
if (columnDefPacket.getColumnCharsetSet() == 63) {
row[c] = readView.readLenencBytes();
} else {
row[c] = readView.readLenencString();
}
}
}
return row;
}
use of io.mycat.vertx.ReadView in project Mycat2 by MyCATApache.
the class Decoder method convertException.
default Throwable convertException(Buffer payload) {
ReadView readView = new ReadView(payload);
readView.skipInReading(1);
int errorCode = (int) readView.readFixInt(2);
readView.skipInReading(1);
String sqlState = readView.readFixString(5);
String errorMessage = readView.readEOFString();
return (new MySQLException(errorMessage, errorCode, sqlState));
}
use of io.mycat.vertx.ReadView in project Mycat2 by MyCATApache.
the class ObjectArrayDecoder method decodeColumnDefinitionPacketPayload.
ColumnDefPacketImpl decodeColumnDefinitionPacketPayload(Buffer payload) {
ReadView readView = new ReadView(payload);
byte[] catalog = readView.readLenencStringBytes();
byte[] schema = readView.readLenencStringBytes();
byte[] table = readView.readLenencStringBytes();
byte[] orgTable = readView.readLenencStringBytes();
byte[] name = readView.readLenencStringBytes();
byte[] orgName = readView.readLenencStringBytes();
long lengthOfFixedLengthFields = readView.readLenencInt();
int characterSet = (int) readView.readFixInt(2);
long columnLength = (int) readView.readFixInt(4);
int type = readView.readByte() & 0xff;
int flags = (int) readView.readFixInt(2);
byte decimals = readView.readByte();
ColumnDefPacketImpl columnDefPacket = new ColumnDefPacketImpl();
columnDefPacket.setColumnSchema(schema);
columnDefPacket.setColumnTable(table);
columnDefPacket.setColumnOrgTable(orgTable);
columnDefPacket.setColumnName(name);
columnDefPacket.setColumnOrgName(orgName);
columnDefPacket.setColumnCharsetSet(characterSet);
columnDefPacket.setColumnLength((int) columnLength);
columnDefPacket.setColumnType(type);
columnDefPacket.setColumnFlags(flags);
columnDefPacket.setColumnDecimals(decimals);
return columnDefPacket;
}
use of io.mycat.vertx.ReadView in project Mycat2 by MyCATApache.
the class MycatVertxMySQLHandler method handle0.
public Future<Void> handle0(int packetId, Buffer event) {
Process process = Process.getCurrentProcess();
session.setPacketId(packetId);
ReadView readView = new ReadView(event);
Future<Void> promise;
try {
byte command = readView.readByte();
process.setCommand(command);
process.setContext(mycatDataContext);
switch(command) {
case MySQLCommandType.COM_SLEEP:
{
promise = handleSleep(this.session);
break;
}
case MySQLCommandType.COM_QUIT:
{
promise = handleQuit(this.session);
break;
}
case MySQLCommandType.COM_QUERY:
{
String sql = new String(readView.readEOFStringBytes(), StandardCharsets.UTF_8);
process.setQuery(sql);
process.setState(Process.State.INIT);
IOExecutor vertx = MetaClusterCurrent.wrapper(IOExecutor.class);
promise = vertx.executeBlocking((Handler<Promise<Void>>) event1 -> handleQuery(sql, session).onComplete(event1));
break;
}
case MySQLCommandType.COM_INIT_DB:
{
String schema = readView.readEOFString();
promise = handleInitDb(schema, this.session);
break;
}
case MySQLCommandType.COM_PING:
{
promise = handlePing(this.session);
break;
}
case MySQLCommandType.COM_FIELD_LIST:
{
String table = readView.readNULString();
String field = readView.readEOFString();
promise = handleFieldList(table, field, this.session);
break;
}
case MySQLCommandType.COM_SET_OPTION:
{
boolean option = readView.readFixInt(2) == 1;
promise = handleSetOption(option, this.session);
break;
}
case MySQLCommandType.COM_STMT_PREPARE:
{
byte[] bytes = readView.readEOFStringBytes();
IOExecutor ioExecutor = MetaClusterCurrent.wrapper(IOExecutor.class);
promise = ioExecutor.executeBlocking(voidPromise -> {
try {
handlePrepareStatement(bytes, session).onComplete(voidPromise);
} catch (Throwable throwable) {
voidPromise.fail(throwable);
}
});
break;
}
case MySQLCommandType.COM_STMT_SEND_LONG_DATA:
{
long statementId = readView.readFixInt(4);
int paramId = (int) readView.readFixInt(2);
byte[] data = readView.readEOFStringBytes();
promise = handlePrepareStatementLongdata(statementId, paramId, data, this.session);
break;
}
case MySQLCommandType.COM_STMT_EXECUTE:
{
MycatDataContext dataContext = this.session.getDataContext();
Map<Long, PreparedStatement> prepareInfo = dataContext.getPrepareInfo();
long statementId = readView.readFixInt(4);
byte flags = readView.readByte();
long iteration = readView.readFixInt(4);
assert iteration == 1;
int numParams = getNumParamsByStatementId(statementId, this.session);
byte[] nullMap = null;
if (numParams > 0) {
nullMap = readView.readBytes((numParams + 7) / 8);
}
int[] params = prepareInfo.get(statementId).getParametersType();
BindValue[] values = new BindValue[numParams];
boolean newParameterBoundFlag = !readView.readFinished() && readView.readByte() == 1;
if (newParameterBoundFlag) {
for (int i = 0; i < numParams; i++) {
params[i] = (int) readView.readFixInt(2);
}
}
for (int i = 0; i < numParams; i++) {
BindValue bv = new BindValue();
bv.type = params[i];
if ((nullMap[i / 8] & (1 << (i & 7))) != 0) {
bv.isNull = true;
} else {
byte[] longData = getLongData(statementId, i, this.session);
if (longData == null) {
ServerConfig serverConfig = MetaClusterCurrent.wrapper(ServerConfig.class);
BindValueUtil.read(readView, bv, StandardCharsets.UTF_8, !serverConfig.isPstmtStringVal());
bv.isLongData = false;
} else {
bv.value = longData;
bv.isLongData = true;
}
}
values[i] = bv;
}
saveBindValue(statementId, values, this.session);
promise = handlePrepareStatementExecute(statementId, flags, params, values, this.session);
break;
}
case MySQLCommandType.COM_STMT_CLOSE:
{
long statementId = readView.readFixInt(4);
promise = handlePrepareStatementClose(statementId, this.session);
break;
}
case MySQLCommandType.COM_STMT_FETCH:
{
long statementId = readView.readFixInt(4);
long row = readView.readFixInt(4);
promise = handlePrepareStatementFetch(statementId, row, this.session);
break;
}
case MySQLCommandType.COM_STMT_RESET:
{
long statementId = readView.readFixInt(4);
promise = handlePrepareStatementReset(statementId, this.session);
break;
}
case MySQLCommandType.COM_CREATE_DB:
{
String schema = readView.readEOFString();
promise = handleCreateDb(schema, this.session);
break;
}
case MySQLCommandType.COM_DROP_DB:
{
String schema = readView.readEOFString();
promise = handleDropDb(schema, this.session);
break;
}
case MySQLCommandType.COM_REFRESH:
{
byte subCommand = readView.readByte();
promise = handleRefresh(subCommand, this.session);
break;
}
case MySQLCommandType.COM_SHUTDOWN:
{
try {
if (!readView.readFinished()) {
byte shutdownType = readView.readByte();
promise = handleShutdown(shutdownType, this.session);
} else {
promise = handleShutdown(0, this.session);
}
} finally {
}
break;
}
case MySQLCommandType.COM_STATISTICS:
{
promise = handleStatistics(this.session);
break;
}
case MySQLCommandType.COM_PROCESS_INFO:
{
promise = handleProcessInfo(this.session);
break;
}
case MySQLCommandType.COM_CONNECT:
{
promise = handleConnect(this.session);
break;
}
case MySQLCommandType.COM_PROCESS_KILL:
{
long connectionId = readView.readFixInt(4);
promise = handleProcessKill(connectionId, this.session);
break;
}
case MySQLCommandType.COM_DEBUG:
{
promise = handleDebug(this.session);
break;
}
case MySQLCommandType.COM_TIME:
{
promise = handleTime(this.session);
break;
}
case MySQLCommandType.COM_DELAYED_INSERT:
{
promise = handleDelayedInsert(this.session);
break;
}
case MySQLCommandType.COM_CHANGE_USER:
{
String userName = readView.readNULString();
String authResponse = null;
String schemaName = null;
Integer characterSet = null;
String authPluginName = null;
HashMap<String, String> clientConnectAttrs = new HashMap<>();
int capabilities = this.session.getCapabilities();
if (MySQLServerCapabilityFlags.isCanDo41Anthentication(capabilities)) {
byte len = readView.readByte();
authResponse = readView.readFixString(len);
} else {
authResponse = readView.readNULString();
}
schemaName = readView.readNULString();
if (!readView.readFinished()) {
characterSet = (int) readView.readFixInt(2);
if (MySQLServerCapabilityFlags.isPluginAuth(capabilities)) {
authPluginName = readView.readNULString();
}
if (MySQLServerCapabilityFlags.isConnectAttrs(capabilities)) {
long kvAllLength = readView.readLenencInt();
if (kvAllLength != 0) {
clientConnectAttrs = new HashMap<>();
}
int count = 0;
while (count < kvAllLength) {
String k = readView.readLenencString();
String v = readView.readLenencString();
count += k.length();
count += v.length();
count += calcLenencLength(k.length());
count += calcLenencLength(v.length());
clientConnectAttrs.put(k, v);
}
}
}
promise = handleChangeUser(userName, authResponse, schemaName, characterSet, authPluginName, clientConnectAttrs, this.session);
break;
}
case MySQLCommandType.COM_RESET_CONNECTION:
{
promise = handleResetConnection(this.session);
break;
}
case MySQLCommandType.COM_DAEMON:
{
promise = handleDaemon(this.session);
break;
}
default:
{
promise = VertxUtil.newFailPromise(new MycatException(MycatErrorCode.ERR_NOT_SUPPORT, "无法识别的MYSQL数据包"));
assert false;
}
}
return promise.onSuccess(event12 -> mycatDataContext.setLastMessage((String) null)).recover(cause -> {
int errorCode = 0;
String message;
String sqlState;
if (cause instanceof SQLException) {
errorCode = ((SQLException) cause).getErrorCode();
message = ((SQLException) cause).getMessage();
sqlState = ((SQLException) cause).getSQLState();
} else if (cause instanceof MycatException) {
errorCode = ((MycatException) cause).getErrorCode();
message = ((MycatException) cause).getMessage();
sqlState = "";
} else {
message = cause.toString();
}
mycatDataContext.setLastMessage(message);
return this.session.writeErrorEndPacketBySyncInProcessError(errorCode);
});
} catch (Throwable throwable) {
mycatDataContext.setLastMessage(throwable);
return this.session.writeErrorEndPacketBySyncInProcessError(0);
}
}
Aggregations