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

LibSQL+Userland: Pass SQL IPC results to clients in a structure

SQLClient exists as a wrapper around SQL IPC to provide a bit friendlier
interface for clients to deal with. Though right now, it mostly forwards
values as-is from IPC to the clients. This makes it a bit verbose to add
values to IPC responses, as we then have to add it to the callbacks used
by all clients. It's also a bit confusing seeing a sea of "auto" as the
parameter types for these callbacks.

This patch moves these response values to named structures instead. This
will allow adding values without needing to simultaneously update all
clients. We can then separately handle the new values in interested
clients only.
This commit is contained in:
Timothy Flynn 2023-02-03 10:33:10 -05:00 committed by Andreas Kling
parent f0441ee16a
commit d6dee8c0e8
6 changed files with 127 additions and 62 deletions

View file

@ -154,45 +154,74 @@ ErrorOr<NonnullRefPtr<SQLClient>> SQLClient::launch_server_and_create_client(Vec
#endif
void SQLClient::execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message)
{
if (on_execution_error)
on_execution_error(statement_id, execution_id, code, message);
else
warnln("Execution error for statement_id {}: {} ({})", statement_id, message, to_underlying(code));
}
void SQLClient::execution_success(u64 statement_id, u64 execution_id, bool has_results, size_t created, size_t updated, size_t deleted)
{
if (on_execution_success)
on_execution_success(statement_id, execution_id, has_results, created, updated, deleted);
else
if (!on_execution_success) {
outln("{} row(s) created, {} updated, {} deleted", created, updated, deleted);
}
void SQLClient::next_result(u64 statement_id, u64 execution_id, Vector<SQL::Value> const& row)
{
if (on_next_result) {
on_next_result(statement_id, execution_id, row);
return;
}
bool first = true;
for (auto& column : row) {
if (!first)
out(", ");
out("\"{}\"", column);
first = false;
ExecutionSuccess success {
.statement_id = statement_id,
.execution_id = execution_id,
.has_results = has_results,
.rows_created = created,
.rows_updated = updated,
.rows_deleted = deleted,
};
on_execution_success(move(success));
}
void SQLClient::execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message)
{
if (!on_execution_error) {
warnln("Execution error for statement_id {}: {} ({})", statement_id, message, to_underlying(code));
return;
}
outln();
ExecutionError error {
.statement_id = statement_id,
.execution_id = execution_id,
.error_code = code,
.error_message = move(const_cast<DeprecatedString&>(message)),
};
on_execution_error(move(error));
}
void SQLClient::next_result(u64 statement_id, u64 execution_id, Vector<Value> const& row)
{
if (!on_next_result) {
StringBuilder builder;
builder.join(", "sv, row, "\"{}\""sv);
outln("{}", builder.string_view());
return;
}
ExecutionResult result {
.statement_id = statement_id,
.execution_id = execution_id,
.values = move(const_cast<Vector<Value>&>(row)),
};
on_next_result(move(result));
}
void SQLClient::results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows)
{
if (on_results_exhausted)
on_results_exhausted(statement_id, execution_id, total_rows);
else
if (!on_results_exhausted) {
outln("{} total row(s)", total_rows);
return;
}
ExecutionComplete success {
.statement_id = statement_id,
.execution_id = execution_id,
.total_rows = total_rows,
};
on_results_exhausted(move(success));
}
}

View file

@ -15,6 +15,38 @@
namespace SQL {
struct ExecutionSuccess {
u64 statement_id { 0 };
u64 execution_id { 0 };
bool has_results { false };
size_t rows_created { 0 };
size_t rows_updated { 0 };
size_t rows_deleted { 0 };
};
struct ExecutionError {
u64 statement_id { 0 };
u64 execution_id { 0 };
SQLErrorCode error_code;
DeprecatedString error_message;
};
struct ExecutionResult {
u64 statement_id { 0 };
u64 execution_id { 0 };
Vector<Value> values;
};
struct ExecutionComplete {
u64 statement_id { 0 };
u64 execution_id { 0 };
size_t total_rows { 0 };
};
class SQLClient
: public IPC::ConnectionToServer<SQLClientEndpoint, SQLServerEndpoint>
, public SQLClientEndpoint {
@ -27,10 +59,10 @@ public:
virtual ~SQLClient() = default;
Function<void(u64, u64, SQLErrorCode, DeprecatedString const&)> on_execution_error;
Function<void(u64, u64, bool, size_t, size_t, size_t)> on_execution_success;
Function<void(u64, u64, Span<SQL::Value const>)> on_next_result;
Function<void(u64, u64, size_t)> on_results_exhausted;
Function<void(ExecutionSuccess)> on_execution_success;
Function<void(ExecutionError)> on_execution_error;
Function<void(ExecutionResult)> on_next_result;
Function<void(ExecutionComplete)> on_results_exhausted;
private:
explicit SQLClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
@ -39,9 +71,9 @@ private:
}
virtual void execution_success(u64 statement_id, u64 execution_id, bool has_results, size_t created, size_t updated, size_t deleted) override;
virtual void execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message) override;
virtual void next_result(u64 statement_id, u64 execution_id, Vector<SQL::Value> const&) override;
virtual void results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows) override;
virtual void execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message) override;
};
}