use of io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket 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.mysql.packet.generic.EofPacket 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;
}
use of io.shardingjdbc.proxy.transport.mysql.packet.generic.EofPacket 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