diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-02-03 10:33:10 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-02-03 20:34:45 +0100 |
commit | d6dee8c0e838da8cf43bdac5223665668a2d9980 (patch) | |
tree | 7053a3c6d816aab28cff7522d20da9dd84700289 /Userland/Libraries | |
parent | f0441ee16ac0c168e6c3d1124c8b2ba5c75413c1 (diff) | |
download | serenity-d6dee8c0e838da8cf43bdac5223665668a2d9980.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibSQL/SQLClient.cpp | 77 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/SQLClient.h | 42 |
2 files changed, 90 insertions, 29 deletions
diff --git a/Userland/Libraries/LibSQL/SQLClient.cpp b/Userland/Libraries/LibSQL/SQLClient.cpp index bab488ef0f..1b0705ed16 100644 --- a/Userland/Libraries/LibSQL/SQLClient.cpp +++ b/Userland/Libraries/LibSQL/SQLClient.cpp @@ -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); + return; + } + + 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::next_result(u64 statement_id, u64 execution_id, Vector<SQL::Value> const& row) +void SQLClient::execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message) { - if (on_next_result) { - on_next_result(statement_id, execution_id, row); + if (!on_execution_error) { + warnln("Execution error for statement_id {}: {} ({})", statement_id, message, to_underlying(code)); return; } - bool first = true; - for (auto& column : row) { - if (!first) - out(", "); - out("\"{}\"", column); - first = false; + 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; } - outln(); + + 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)); } } diff --git a/Userland/Libraries/LibSQL/SQLClient.h b/Userland/Libraries/LibSQL/SQLClient.h index 76c4a94f7e..97c37a7ea8 100644 --- a/Userland/Libraries/LibSQL/SQLClient.h +++ b/Userland/Libraries/LibSQL/SQLClient.h @@ -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; }; } |