use of io.shardingjdbc.proxy.transport.mysql.packet.generic.ErrPacket 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.mysql.packet.generic.ErrPacket 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.ErrPacket in project sharding-jdbc by shardingjdbc.
the class SQLExecuteBackendHandler method buildPackets.
private List<DatabaseProtocolPacket> buildPackets(final List<List<DatabaseProtocolPacket>> packets, final MergedResult mergedResult) {
List<DatabaseProtocolPacket> result = new LinkedList<>();
Iterator<DatabaseProtocolPacket> databaseProtocolPacketsSampling = packets.iterator().next().iterator();
FieldCountPacket fieldCountPacketSampling = (FieldCountPacket) databaseProtocolPacketsSampling.next();
result.add(fieldCountPacketSampling);
int columnCount = fieldCountPacketSampling.getColumnCount();
for (int i = 0; i < columnCount; i++) {
result.add(databaseProtocolPacketsSampling.next());
}
result.add(databaseProtocolPacketsSampling.next());
int currentSequenceId = result.size();
try {
while (mergedResult.next()) {
List<Object> data = new ArrayList<>(columnCount);
for (int i = 1; i <= columnCount; i++) {
data.add(mergedResult.getValue(i, Object.class));
}
result.add(new TextResultSetRowPacket(++currentSequenceId, data));
}
} catch (final SQLException ex) {
return Collections.<DatabaseProtocolPacket>singletonList(new ErrPacket(1, ex.getErrorCode(), "", ex.getSQLState(), ex.getMessage()));
}
result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
return result;
}
Aggregations