use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket 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.common.packet.DatabaseProtocolPacket 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.common.packet.DatabaseProtocolPacket in project sharding-jdbc by shardingjdbc.
the class SQLExecuteBackendHandler method mergeDQLorDAL.
private List<DatabaseProtocolPacket> mergeDQLorDAL(final SQLStatement sqlStatement, final List<List<DatabaseProtocolPacket>> packets) {
List<QueryResult> queryResults = new ArrayList<>(packets.size());
for (List<DatabaseProtocolPacket> each : packets) {
// TODO replace to a common PacketQueryResult
queryResults.add(new MySQLPacketQueryResult(each));
}
MergedResult mergedResult;
try {
mergedResult = MergeEngineFactory.newInstance(ShardingRuleRegistry.getInstance().getShardingRule(), queryResults, sqlStatement).merge();
} catch (final SQLException ex) {
return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
}
return buildPackets(packets, mergedResult);
}
use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket in project sharding-jdbc by shardingjdbc.
the class ComStmtPreparePacket method execute.
@Override
public List<DatabaseProtocolPacket> execute() {
log.debug("COM_STMT_PREPARE received for Sharding-Proxy: {}", sql);
List<DatabaseProtocolPacket> result = new LinkedList<>();
int currentSequenceId = 0;
SQLStatement sqlStatement = new SQLParsingEngine(DatabaseType.MySQL, sql, ShardingRuleRegistry.getInstance().getShardingRule()).parse();
result.add(new ComStmtPrepareOKPacket(++currentSequenceId, PreparedStatementRegistry.getInstance().register(sql), getNumColumns(sqlStatement), sqlStatement.getParametersIndex(), 0));
for (int i = 0; i < sqlStatement.getParametersIndex(); i++) {
// TODO add column name
result.add(new ColumnDefinition41Packet(++currentSequenceId, ShardingConstant.LOGIC_SCHEMA_NAME, sqlStatement.getTables().isSingleTable() ? sqlStatement.getTables().getSingleTableName() : "", "", "", "", 100, ColumnType.MYSQL_TYPE_VARCHAR, 0));
}
if (sqlStatement.getParametersIndex() > 0) {
result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
}
// TODO add If numColumns > 0
return result;
}
use of io.shardingjdbc.proxy.transport.common.packet.DatabaseProtocolPacket 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()));
}
}
Aggregations