use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.
the class SQLExecuteBackendHandler method execute.
@Override
public List<DatabaseProtocolPacket> execute() {
SQLRouteResult routeResult = routingEngine.route(sql);
if (routeResult.getExecutionUnits().isEmpty()) {
return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
}
List<List<DatabaseProtocolPacket>> result = new LinkedList<>();
for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
// TODO multiple threads
result.add(execute(routeResult.getSqlStatement(), each));
}
return merge(routeResult.getSqlStatement(), result);
}
use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.
the class SQLExecuteBackendHandler method mergeDML.
private List<DatabaseProtocolPacket> mergeDML(final List<DatabaseProtocolPacket> firstPackets) {
int affectedRows = 0;
for (DatabaseProtocolPacket each : firstPackets) {
if (each instanceof OKPacket) {
OKPacket okPacket = (OKPacket) each;
affectedRows += okPacket.getAffectedRows();
}
}
return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, affectedRows, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
}
use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.
the class MySQLFrontendHandler method auth.
@Override
protected void auth(final ChannelHandlerContext context, final ByteBuf message) {
MySQLPacketPayload mysqlPacketPayload = new MySQLPacketPayload(message);
// TODO use authPluginData to auth
HandshakeResponse41Packet response41 = new HandshakeResponse41Packet(mysqlPacketPayload);
context.writeAndFlush(new OKPacket(response41.getSequenceId() + 1, 0L, 0L, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
}
use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.
the class SQLExecuteBackendHandler method executeUpdate.
private List<DatabaseProtocolPacket> executeUpdate(final DataSource dataSource, final String sql, final SQLStatement sqlStatement) {
try (Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement()) {
int affectedRows;
long lastInsertId = 0;
if (sqlStatement instanceof InsertStatement) {
affectedRows = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
lastInsertId = getGeneratedKey(statement);
} else {
affectedRows = statement.executeUpdate(sql);
}
return Collections.<DatabaseProtocolPacket>singletonList(new OKPacket(1, affectedRows, lastInsertId, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
} catch (final SQLException ex) {
return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
}
}
use of io.shardingjdbc.proxy.transport.mysql.packet.generic.OKPacket in project sharding-jdbc by shardingjdbc.
the class SQLExecuteBackendHandler method getDatabaseProtocolPackets.
private List<DatabaseProtocolPacket> getDatabaseProtocolPackets(final ResultSet resultSet) throws SQLException {
List<DatabaseProtocolPacket> result = new LinkedList<>();
int currentSequenceId = 0;
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
if (0 == columnCount) {
result.add(new OKPacket(++currentSequenceId, 0, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue(), 0, ""));
return result;
}
result.add(new FieldCountPacket(++currentSequenceId, columnCount));
for (int i = 1; i <= columnCount; i++) {
result.add(new ColumnDefinition41Packet(++currentSequenceId, resultSetMetaData.getSchemaName(i), resultSetMetaData.getTableName(i), resultSetMetaData.getTableName(i), resultSetMetaData.getColumnLabel(i), resultSetMetaData.getColumnName(i), resultSetMetaData.getColumnDisplaySize(i), ColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(i)), 0));
}
result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
while (resultSet.next()) {
List<Object> data = new ArrayList<>(columnCount);
for (int i = 1; i <= columnCount; i++) {
data.add(resultSet.getObject(i));
}
result.add(new TextResultSetRowPacket(++currentSequenceId, data));
}
result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
return result;
}
Aggregations