1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

SQLServer: Do not store statement execution results at the class level

If a statement is executed multiple times in quick succession, we may
overwrite the results of a previous execution. Instead of storing the
result, pass it around as it is sent to the client.
This commit is contained in:
Timothy Flynn 2022-12-03 11:22:39 -05:00 committed by Andreas Kling
parent f9d23e1d2f
commit 27ce88864f
2 changed files with 19 additions and 25 deletions

View file

@ -56,8 +56,6 @@ void SQLStatement::report_error(SQL::Result result, u64 execution_id)
client_connection->async_execution_error(statement_id(), execution_id, result.error(), result.error_string()); client_connection->async_execution_error(statement_id(), execution_id, result.error(), result.error_string());
else else
warnln("Cannot return execution error. Client disconnected"); warnln("Cannot return execution error. Client disconnected");
m_result = {};
} }
Optional<u64> SQLStatement::execute(Vector<SQL::Value> placeholder_values) Optional<u64> SQLStatement::execute(Vector<SQL::Value> placeholder_values)
@ -88,28 +86,27 @@ Optional<u64> SQLStatement::execute(Vector<SQL::Value> placeholder_values)
return; return;
} }
m_result = execution_result.release_value(); auto result = execution_result.release_value();
if (should_send_result_rows()) { if (should_send_result_rows(result)) {
client_connection->async_execution_success(statement_id(), execution_id, true, 0, 0, 0); client_connection->async_execution_success(statement_id(), execution_id, true, 0, 0, 0);
m_index = 0;
next(execution_id); auto result_size = result.size();
next(execution_id, move(result), result_size);
} else { } else {
client_connection->async_execution_success(statement_id(), execution_id, false, 0, m_result->size(), 0); client_connection->async_execution_success(statement_id(), execution_id, false, 0, result.size(), 0);
} }
}); });
return execution_id; return execution_id;
} }
bool SQLStatement::should_send_result_rows() const bool SQLStatement::should_send_result_rows(SQL::ResultSet const& result) const
{ {
VERIFY(m_result.has_value()); if (result.is_empty())
if (m_result->is_empty())
return false; return false;
switch (m_result->command()) { switch (result.command()) {
case SQL::SQLCommand::Describe: case SQL::SQLCommand::Describe:
case SQL::SQLCommand::Select: case SQL::SQLCommand::Select:
return true; return true;
@ -118,24 +115,23 @@ bool SQLStatement::should_send_result_rows() const
} }
} }
void SQLStatement::next(u64 execution_id) void SQLStatement::next(u64 execution_id, SQL::ResultSet result, size_t result_size)
{ {
VERIFY(!m_result->is_empty());
auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id()); auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
if (!client_connection) { if (!client_connection) {
warnln("Cannot yield next result. Client disconnected"); warnln("Cannot yield next result. Client disconnected");
return; return;
} }
if (m_index < m_result->size()) { if (!result.is_empty()) {
auto& tuple = m_result->at(m_index++).row; auto result_row = result.take_first();
client_connection->async_next_result(statement_id(), execution_id, tuple.to_deprecated_string_vector()); client_connection->async_next_result(statement_id(), execution_id, result_row.row.to_deprecated_string_vector());
deferred_invoke([this, execution_id]() {
next(execution_id); deferred_invoke([this, execution_id, result = move(result), result_size]() {
next(execution_id, move(result), result_size);
}); });
} else { } else {
client_connection->async_results_exhausted(statement_id(), execution_id, m_index); client_connection->async_results_exhausted(statement_id(), execution_id, result_size);
} }
} }

View file

@ -33,18 +33,16 @@ public:
private: private:
SQLStatement(DatabaseConnection&, NonnullRefPtr<SQL::AST::Statement> statement); SQLStatement(DatabaseConnection&, NonnullRefPtr<SQL::AST::Statement> statement);
bool should_send_result_rows() const; bool should_send_result_rows(SQL::ResultSet const& result) const;
void next(u64 execution_id); void next(u64 execution_id, SQL::ResultSet result, size_t result_size);
void report_error(SQL::Result, u64 execution_id); void report_error(SQL::Result, u64 execution_id);
u64 m_statement_id { 0 }; u64 m_statement_id { 0 };
size_t m_index { 0 };
HashTable<u64> m_ongoing_executions; HashTable<u64> m_ongoing_executions;
u64 m_next_execution_id { 0 }; u64 m_next_execution_id { 0 };
NonnullRefPtr<SQL::AST::Statement> m_statement; NonnullRefPtr<SQL::AST::Statement> m_statement;
Optional<SQL::ResultSet> m_result {};
}; };
} }