From b9d8c25b0bfcf902bfceffb0ff7576c8d4256157 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 3 Dec 2022 11:34:30 -0500 Subject: [PATCH] LibSQL+SQLServer+SQLStudio+sql: Send result rows over IPC as SQL::Value We've been sending the values converted to a string, but now that the Value type is transferrable over IPC, send the values themselves. Any client that wants the value as a string may do so easily, whereas this will allow less trivial clients to avoid string parsing. --- Userland/DevTools/SQLStudio/MainWidget.cpp | 9 +++++++-- Userland/Libraries/LibSQL/SQLClient.cpp | 3 ++- Userland/Libraries/LibSQL/SQLClient.h | 4 ++-- Userland/Libraries/LibSQL/Tuple.cpp | 9 --------- Userland/Libraries/LibSQL/Tuple.h | 3 ++- Userland/Services/SQLServer/SQLClient.ipc | 3 ++- Userland/Services/SQLServer/SQLStatement.cpp | 2 +- Userland/Utilities/sql.cpp | 2 +- 8 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Userland/DevTools/SQLStudio/MainWidget.cpp b/Userland/DevTools/SQLStudio/MainWidget.cpp index f47b5a8e9e..31d31d6f26 100644 --- a/Userland/DevTools/SQLStudio/MainWidget.cpp +++ b/Userland/DevTools/SQLStudio/MainWidget.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "MainWidget.h" #include "ScriptEditor.h" @@ -224,8 +225,12 @@ MainWidget::MainWidget() m_sql_client->on_execution_success = [this](auto, auto, auto, auto, auto, auto) { read_next_sql_statement_of_editor(); }; - m_sql_client->on_next_result = [this](auto, auto, auto const& row) { - m_results.append(row); + m_sql_client->on_next_result = [this](auto, auto, auto row) { + m_results.append({}); + m_results.last().ensure_capacity(row.size()); + + for (auto const& value : row) + m_results.last().unchecked_append(value.to_deprecated_string()); }; m_sql_client->on_results_exhausted = [this](auto, auto, auto) { if (m_results.size() == 0) diff --git a/Userland/Libraries/LibSQL/SQLClient.cpp b/Userland/Libraries/LibSQL/SQLClient.cpp index 4a5a2693ec..94514ccf32 100644 --- a/Userland/Libraries/LibSQL/SQLClient.cpp +++ b/Userland/Libraries/LibSQL/SQLClient.cpp @@ -25,12 +25,13 @@ void SQLClient::execution_success(u64 statement_id, u64 execution_id, bool has_r outln("{} row(s) created, {} updated, {} deleted", created, updated, deleted); } -void SQLClient::next_result(u64 statement_id, u64 execution_id, Vector const& row) +void SQLClient::next_result(u64 statement_id, u64 execution_id, Vector const& row) { if (on_next_result) { on_next_result(statement_id, execution_id, row); return; } + bool first = true; for (auto& column : row) { if (!first) diff --git a/Userland/Libraries/LibSQL/SQLClient.h b/Userland/Libraries/LibSQL/SQLClient.h index ae45d852de..b86da1d82b 100644 --- a/Userland/Libraries/LibSQL/SQLClient.h +++ b/Userland/Libraries/LibSQL/SQLClient.h @@ -22,7 +22,7 @@ class SQLClient Function on_execution_error; Function on_execution_success; - Function const&)> on_next_result; + Function)> on_next_result; Function on_results_exhausted; private: @@ -32,7 +32,7 @@ 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 next_result(u64 statement_id, u64 execution_id, Vector const&) override; + virtual void next_result(u64 statement_id, u64 execution_id, Vector 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; }; diff --git a/Userland/Libraries/LibSQL/Tuple.cpp b/Userland/Libraries/LibSQL/Tuple.cpp index 093bca530d..5fcc5fbdfb 100644 --- a/Userland/Libraries/LibSQL/Tuple.cpp +++ b/Userland/Libraries/LibSQL/Tuple.cpp @@ -176,15 +176,6 @@ DeprecatedString Tuple::to_deprecated_string() const return builder.build(); } -Vector Tuple::to_deprecated_string_vector() const -{ - Vector ret; - for (auto& value : m_data) { - ret.append(value.to_deprecated_string()); - } - return ret; -} - void Tuple::copy_from(Tuple const& other) { if (*m_descriptor != *other.m_descriptor) { diff --git a/Userland/Libraries/LibSQL/Tuple.h b/Userland/Libraries/LibSQL/Tuple.h index d4a1e6a5f5..1b10f4e60f 100644 --- a/Userland/Libraries/LibSQL/Tuple.h +++ b/Userland/Libraries/LibSQL/Tuple.h @@ -37,7 +37,6 @@ public: [[nodiscard]] DeprecatedString to_deprecated_string() const; explicit operator DeprecatedString() const { return to_deprecated_string(); } - [[nodiscard]] Vector to_deprecated_string_vector() const; bool operator<(Tuple const& other) const { return compare(other) < 0; } bool operator<=(Tuple const& other) const { return compare(other) <= 0; } @@ -69,6 +68,8 @@ public: [[nodiscard]] int match(Tuple const&) const; [[nodiscard]] u32 hash() const; + [[nodiscard]] Vector take_data() { return move(m_data); } + protected: [[nodiscard]] Optional index_of(StringView) const; void copy_from(Tuple const&); diff --git a/Userland/Services/SQLServer/SQLClient.ipc b/Userland/Services/SQLServer/SQLClient.ipc index 15d3533333..90fddd5880 100644 --- a/Userland/Services/SQLServer/SQLClient.ipc +++ b/Userland/Services/SQLServer/SQLClient.ipc @@ -1,9 +1,10 @@ #include +#include endpoint SQLClient { 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 row) =| + next_result(u64 statement_id, u64 execution_id, Vector 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) =| } diff --git a/Userland/Services/SQLServer/SQLStatement.cpp b/Userland/Services/SQLServer/SQLStatement.cpp index 72e12e1050..1b89b4e395 100644 --- a/Userland/Services/SQLServer/SQLStatement.cpp +++ b/Userland/Services/SQLServer/SQLStatement.cpp @@ -125,7 +125,7 @@ void SQLStatement::next(u64 execution_id, SQL::ResultSet result, size_t result_s if (!result.is_empty()) { auto result_row = result.take_first(); - client_connection->async_next_result(statement_id(), execution_id, result_row.row.to_deprecated_string_vector()); + client_connection->async_next_result(statement_id(), execution_id, result_row.row.take_data()); deferred_invoke([this, execution_id, result = move(result), result_size]() { next(execution_id, move(result), result_size); diff --git a/Userland/Utilities/sql.cpp b/Userland/Utilities/sql.cpp index 6020c3f01a..864a3918ef 100644 --- a/Userland/Utilities/sql.cpp +++ b/Userland/Utilities/sql.cpp @@ -85,7 +85,7 @@ public: } }; - m_sql_client->on_next_result = [](auto, auto, auto const& row) { + m_sql_client->on_next_result = [](auto, auto, auto row) { StringBuilder builder; builder.join(", "sv, row); outln("{}", builder.build());