1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibSQL+SQLServer+SQLStudio+sql: Allocate per-statement-execution IDs

In order to execute a prepared statement multiple times, and track each
execution's results, clients will need to be provided an execution ID.
This will create a monotonically increasing ID each time a prepared
statement is executed for this purpose.
This commit is contained in:
Timothy Flynn 2022-12-02 17:14:56 -05:00 committed by Andreas Kling
parent e2f71d2808
commit aec75d749a
10 changed files with 66 additions and 51 deletions

View file

@ -71,17 +71,19 @@ Messages::SQLServer::PrepareStatementResponse ConnectionFromClient::prepare_stat
return { result.value() };
}
void ConnectionFromClient::execute_statement(u64 statement_id, Vector<SQL::Value> const& placeholder_values)
Messages::SQLServer::ExecuteStatementResponse ConnectionFromClient::execute_statement(u64 statement_id, Vector<SQL::Value> const& placeholder_values)
{
dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::execute_query_statement(statement_id: {})", statement_id);
auto statement = SQLStatement::statement_for(statement_id);
if (statement && statement->connection()->client_id() == client_id()) {
// FIXME: Support taking parameters from IPC requests.
statement->execute(move(const_cast<Vector<SQL::Value>&>(placeholder_values)));
} else {
dbgln_if(SQLSERVER_DEBUG, "Statement has disappeared");
async_execution_error(statement_id, SQL::SQLErrorCode::StatementUnavailable, DeprecatedString::formatted("{}", statement_id));
return statement->execute(move(const_cast<Vector<SQL::Value>&>(placeholder_values)));
}
dbgln_if(SQLSERVER_DEBUG, "Statement has disappeared");
async_execution_error(statement_id, -1, SQL::SQLErrorCode::StatementUnavailable, DeprecatedString::formatted("{}", statement_id));
return { {} };
}
}

View file

@ -30,7 +30,7 @@ private:
virtual Messages::SQLServer::ConnectResponse connect(DeprecatedString const&) override;
virtual Messages::SQLServer::PrepareStatementResponse prepare_statement(u64, DeprecatedString const&) override;
virtual void execute_statement(u64, Vector<SQL::Value> const& placeholder_values) override;
virtual Messages::SQLServer::ExecuteStatementResponse execute_statement(u64, Vector<SQL::Value> const& placeholder_values) override;
virtual void disconnect(u64) override;
};

View file

@ -4,9 +4,9 @@ endpoint SQLClient
{
connected(u64 connection_id, DeprecatedString connected_to_database) =|
connection_error(u64 connection_id, SQL::SQLErrorCode code, DeprecatedString message) =|
execution_success(u64 statement_id, bool has_results, size_t created, size_t updated, size_t deleted) =|
next_result(u64 statement_id, Vector<DeprecatedString> row) =|
results_exhausted(u64 statement_id, size_t total_rows) =|
execution_error(u64 statement_id, SQL::SQLErrorCode code, DeprecatedString message) =|
execution_success(u64 statement_id, u64 execution_id, bool has_results, size_t created, size_t updated, size_t deleted) =|
next_result(u64 statement_id, u64 execution_id, Vector<DeprecatedString> row) =|
results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows) =|
execution_error(u64 statement_id, u64 execution_id, SQL::SQLErrorCode code, DeprecatedString message) =|
disconnected(u64 connection_id) =|
}

View file

@ -4,6 +4,6 @@ endpoint SQLServer
{
connect(DeprecatedString name) => (u64 connection_id)
prepare_statement(u64 connection_id, DeprecatedString statement) => (Optional<u64> statement_id)
execute_statement(u64 statement_id, Vector<SQL::Value> placeholder_values) =|
execute_statement(u64 statement_id, Vector<SQL::Value> placeholder_values) => (Optional<u64> execution_id)
disconnect(u64 connection_id) =|
}

View file

@ -43,7 +43,7 @@ SQLStatement::SQLStatement(DatabaseConnection& connection, NonnullRefPtr<SQL::AS
s_statements.set(m_statement_id, *this);
}
void SQLStatement::report_error(SQL::Result result)
void SQLStatement::report_error(SQL::Result result, u64 execution_id)
{
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), result.error_string());
@ -53,29 +53,34 @@ void SQLStatement::report_error(SQL::Result result)
remove_from_parent();
if (client_connection)
client_connection->async_execution_error(statement_id(), result.error(), result.error_string());
client_connection->async_execution_error(statement_id(), execution_id, result.error(), result.error_string());
else
warnln("Cannot return execution error. Client disconnected");
m_result = {};
}
void SQLStatement::execute(Vector<SQL::Value> placeholder_values)
Optional<u64> SQLStatement::execute(Vector<SQL::Value> placeholder_values)
{
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::execute(statement_id {}", statement_id());
auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
if (!client_connection) {
warnln("Cannot yield next result. Client disconnected");
return;
return {};
}
deferred_invoke([this, placeholder_values = move(placeholder_values)] {
auto execution_id = m_next_execution_id++;
m_ongoing_executions.set(execution_id);
deferred_invoke([this, placeholder_values = move(placeholder_values), execution_id] {
VERIFY(!connection()->database().is_null());
auto execution_result = m_statement->execute(connection()->database().release_nonnull(), placeholder_values);
m_ongoing_executions.remove(execution_id);
if (execution_result.is_error()) {
report_error(execution_result.release_error());
report_error(execution_result.release_error(), execution_id);
return;
}
@ -88,13 +93,15 @@ void SQLStatement::execute(Vector<SQL::Value> placeholder_values)
m_result = execution_result.release_value();
if (should_send_result_rows()) {
client_connection->async_execution_success(statement_id(), true, 0, 0, 0);
client_connection->async_execution_success(statement_id(), execution_id, true, 0, 0, 0);
m_index = 0;
next();
next(execution_id);
} else {
client_connection->async_execution_success(statement_id(), false, 0, m_result->size(), 0);
client_connection->async_execution_success(statement_id(), execution_id, false, 0, m_result->size(), 0);
}
});
return execution_id;
}
bool SQLStatement::should_send_result_rows() const
@ -113,22 +120,24 @@ bool SQLStatement::should_send_result_rows() const
}
}
void SQLStatement::next()
void SQLStatement::next(u64 execution_id)
{
VERIFY(!m_result->is_empty());
auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id());
if (!client_connection) {
warnln("Cannot yield next result. Client disconnected");
return;
}
if (m_index < m_result->size()) {
auto& tuple = m_result->at(m_index++).row;
client_connection->async_next_result(statement_id(), tuple.to_deprecated_string_vector());
deferred_invoke([this]() {
next();
client_connection->async_next_result(statement_id(), execution_id, tuple.to_deprecated_string_vector());
deferred_invoke([this, execution_id]() {
next(execution_id);
});
} else {
client_connection->async_results_exhausted(statement_id(), m_index);
client_connection->async_results_exhausted(statement_id(), execution_id, m_index);
}
}

View file

@ -28,17 +28,21 @@ public:
static RefPtr<SQLStatement> statement_for(u64 statement_id);
u64 statement_id() const { return m_statement_id; }
DatabaseConnection* connection() { return dynamic_cast<DatabaseConnection*>(parent()); }
void execute(Vector<SQL::Value> placeholder_values);
Optional<u64> execute(Vector<SQL::Value> placeholder_values);
private:
SQLStatement(DatabaseConnection&, NonnullRefPtr<SQL::AST::Statement> statement);
bool should_send_result_rows() const;
void next();
void report_error(SQL::Result);
void next(u64 execution_id);
void report_error(SQL::Result, u64 execution_id);
u64 m_statement_id { 0 };
size_t m_index { 0 };
HashTable<u64> m_ongoing_executions;
u64 m_next_execution_id { 0 };
NonnullRefPtr<SQL::AST::Statement> m_statement;
Optional<SQL::ResultSet> m_result {};
};