From c3720128420556789d2c78cb592e4919bac129de Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 7 Dec 2022 13:01:55 -0500 Subject: [PATCH] LibSQL+SQLServer+SQLStudio+sql: Give ID types a distinct name Makes it clearer what is being stored, especially in future clients that will store a bunch of statement IDs. --- Userland/DevTools/SQLStudio/MainWidget.h | 2 +- Userland/Libraries/LibSQL/Type.h | 4 ++++ .../Services/SQLServer/ConnectionFromClient.cpp | 6 +++--- .../Services/SQLServer/ConnectionFromClient.h | 7 ++++--- .../Services/SQLServer/DatabaseConnection.cpp | 8 ++++---- .../Services/SQLServer/DatabaseConnection.h | 9 +++++---- Userland/Services/SQLServer/SQLStatement.cpp | 12 ++++++------ Userland/Services/SQLServer/SQLStatement.h | 17 +++++++++-------- Userland/Utilities/sql.cpp | 2 +- 9 files changed, 37 insertions(+), 30 deletions(-) diff --git a/Userland/DevTools/SQLStudio/MainWidget.h b/Userland/DevTools/SQLStudio/MainWidget.h index 3c025d5a19..f3c1f032cd 100644 --- a/Userland/DevTools/SQLStudio/MainWidget.h +++ b/Userland/DevTools/SQLStudio/MainWidget.h @@ -65,7 +65,7 @@ private: Optional read_next_line_of_editor(); size_t m_current_line_for_parsing { 0 }; int m_editor_line_level { 0 }; - u64 m_connection_id { 0 }; + SQL::ConnectionID m_connection_id { 0 }; }; } diff --git a/Userland/Libraries/LibSQL/Type.h b/Userland/Libraries/LibSQL/Type.h index b132115095..a05fc6a260 100644 --- a/Userland/Libraries/LibSQL/Type.h +++ b/Userland/Libraries/LibSQL/Type.h @@ -85,4 +85,8 @@ enum class Nulls { Last, }; +using ConnectionID = u64; +using StatementID = u64; +using ExecutionID = u64; + } diff --git a/Userland/Services/SQLServer/ConnectionFromClient.cpp b/Userland/Services/SQLServer/ConnectionFromClient.cpp index 0445244aaa..6e1156f946 100644 --- a/Userland/Services/SQLServer/ConnectionFromClient.cpp +++ b/Userland/Services/SQLServer/ConnectionFromClient.cpp @@ -49,7 +49,7 @@ Messages::SQLServer::ConnectResponse ConnectionFromClient::connect(DeprecatedStr return { {} }; } -void ConnectionFromClient::disconnect(u64 connection_id) +void ConnectionFromClient::disconnect(SQL::ConnectionID connection_id) { dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::disconnect(connection_id: {})", connection_id); auto database_connection = DatabaseConnection::connection_for(connection_id); @@ -59,7 +59,7 @@ void ConnectionFromClient::disconnect(u64 connection_id) dbgln("Database connection has disappeared"); } -Messages::SQLServer::PrepareStatementResponse ConnectionFromClient::prepare_statement(u64 connection_id, DeprecatedString const& sql) +Messages::SQLServer::PrepareStatementResponse ConnectionFromClient::prepare_statement(SQL::ConnectionID connection_id, DeprecatedString const& sql) { dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::prepare_statement(connection_id: {}, sql: '{}')", connection_id, sql); @@ -79,7 +79,7 @@ Messages::SQLServer::PrepareStatementResponse ConnectionFromClient::prepare_stat return { result.value() }; } -Messages::SQLServer::ExecuteStatementResponse ConnectionFromClient::execute_statement(u64 statement_id, Vector const& placeholder_values) +Messages::SQLServer::ExecuteStatementResponse ConnectionFromClient::execute_statement(SQL::StatementID statement_id, Vector const& placeholder_values) { dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::execute_query_statement(statement_id: {})", statement_id); diff --git a/Userland/Services/SQLServer/ConnectionFromClient.h b/Userland/Services/SQLServer/ConnectionFromClient.h index 783ab55883..e28947e8e5 100644 --- a/Userland/Services/SQLServer/ConnectionFromClient.h +++ b/Userland/Services/SQLServer/ConnectionFromClient.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -32,9 +33,9 @@ private: explicit ConnectionFromClient(NonnullOwnPtr, int client_id); virtual Messages::SQLServer::ConnectResponse connect(DeprecatedString const&) override; - virtual Messages::SQLServer::PrepareStatementResponse prepare_statement(u64, DeprecatedString const&) override; - virtual Messages::SQLServer::ExecuteStatementResponse execute_statement(u64, Vector const& placeholder_values) override; - virtual void disconnect(u64) override; + virtual Messages::SQLServer::PrepareStatementResponse prepare_statement(SQL::ConnectionID, DeprecatedString const&) override; + virtual Messages::SQLServer::ExecuteStatementResponse execute_statement(SQL::StatementID, Vector const& placeholder_values) override; + virtual void disconnect(SQL::ConnectionID) override; DeprecatedString m_database_path; }; diff --git a/Userland/Services/SQLServer/DatabaseConnection.cpp b/Userland/Services/SQLServer/DatabaseConnection.cpp index 9ba9cf2b73..0373962cc0 100644 --- a/Userland/Services/SQLServer/DatabaseConnection.cpp +++ b/Userland/Services/SQLServer/DatabaseConnection.cpp @@ -10,10 +10,10 @@ namespace SQLServer { -static HashMap> s_connections; -static u64 s_next_connection_id = 0; +static HashMap> s_connections; +static SQL::ConnectionID s_next_connection_id = 0; -RefPtr DatabaseConnection::connection_for(u64 connection_id) +RefPtr DatabaseConnection::connection_for(SQL::ConnectionID connection_id) { if (s_connections.contains(connection_id)) return *s_connections.get(connection_id).value(); @@ -54,7 +54,7 @@ void DatabaseConnection::disconnect() s_connections.remove(connection_id()); } -SQL::ResultOr DatabaseConnection::prepare_statement(StringView sql) +SQL::ResultOr DatabaseConnection::prepare_statement(StringView sql) { dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::prepare_statement(connection_id {}, database '{}', sql '{}'", connection_id(), m_database_name, sql); diff --git a/Userland/Services/SQLServer/DatabaseConnection.h b/Userland/Services/SQLServer/DatabaseConnection.h index 60f852bc8e..bab3ff87f3 100644 --- a/Userland/Services/SQLServer/DatabaseConnection.h +++ b/Userland/Services/SQLServer/DatabaseConnection.h @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace SQLServer { @@ -21,19 +22,19 @@ public: static ErrorOr> create(StringView database_path, DeprecatedString database_name, int client_id); ~DatabaseConnection() override = default; - static RefPtr connection_for(u64 connection_id); - u64 connection_id() const { return m_connection_id; } + static RefPtr connection_for(SQL::ConnectionID connection_id); + SQL::ConnectionID connection_id() const { return m_connection_id; } int client_id() const { return m_client_id; } NonnullRefPtr database() { return m_database; } void disconnect(); - SQL::ResultOr prepare_statement(StringView sql); + SQL::ResultOr prepare_statement(StringView sql); private: DatabaseConnection(NonnullRefPtr database, DeprecatedString database_name, int client_id); NonnullRefPtr m_database; DeprecatedString m_database_name; - u64 m_connection_id { 0 }; + SQL::ConnectionID m_connection_id { 0 }; int m_client_id { 0 }; }; diff --git a/Userland/Services/SQLServer/SQLStatement.cpp b/Userland/Services/SQLServer/SQLStatement.cpp index 0b30c013d1..74b9139778 100644 --- a/Userland/Services/SQLServer/SQLStatement.cpp +++ b/Userland/Services/SQLServer/SQLStatement.cpp @@ -12,10 +12,10 @@ namespace SQLServer { -static HashMap> s_statements; -static u64 s_next_statement_id = 0; +static HashMap> s_statements; +static SQL::StatementID s_next_statement_id = 0; -RefPtr SQLStatement::statement_for(u64 statement_id) +RefPtr SQLStatement::statement_for(SQL::StatementID statement_id) { if (s_statements.contains(statement_id)) return *s_statements.get(statement_id).value(); @@ -43,7 +43,7 @@ SQLStatement::SQLStatement(DatabaseConnection& connection, NonnullRefPtr SQLStatement::execute(Vector placeholder_values) +Optional SQLStatement::execute(Vector placeholder_values) { dbgln_if(SQLSERVER_DEBUG, "SQLStatement::execute(statement_id {}", statement_id()); @@ -122,7 +122,7 @@ bool SQLStatement::should_send_result_rows(SQL::ResultSet const& result) const } } -void SQLStatement::next(u64 execution_id, SQL::ResultSet result, size_t result_size) +void SQLStatement::next(SQL::ExecutionID execution_id, SQL::ResultSet result, size_t result_size) { auto client_connection = ConnectionFromClient::client_connection_for(connection()->client_id()); if (!client_connection) { diff --git a/Userland/Services/SQLServer/SQLStatement.h b/Userland/Services/SQLServer/SQLStatement.h index 4efa60e8ff..2f23e86c99 100644 --- a/Userland/Services/SQLServer/SQLStatement.h +++ b/Userland/Services/SQLServer/SQLStatement.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -25,22 +26,22 @@ public: static SQL::ResultOr> create(DatabaseConnection&, StringView sql); ~SQLStatement() override = default; - static RefPtr statement_for(u64 statement_id); - u64 statement_id() const { return m_statement_id; } + static RefPtr statement_for(SQL::StatementID statement_id); + SQL::StatementID statement_id() const { return m_statement_id; } DatabaseConnection* connection() { return dynamic_cast(parent()); } - Optional execute(Vector placeholder_values); + Optional execute(Vector placeholder_values); private: SQLStatement(DatabaseConnection&, NonnullRefPtr statement); bool should_send_result_rows(SQL::ResultSet const& result) const; - void next(u64 execution_id, SQL::ResultSet result, size_t result_size); - void report_error(SQL::Result, u64 execution_id); + void next(SQL::ExecutionID execution_id, SQL::ResultSet result, size_t result_size); + void report_error(SQL::Result, SQL::ExecutionID execution_id); - u64 m_statement_id { 0 }; + SQL::StatementID m_statement_id { 0 }; - HashTable m_ongoing_executions; - u64 m_next_execution_id { 0 }; + HashTable m_ongoing_executions; + SQL::ExecutionID m_next_execution_id { 0 }; NonnullRefPtr m_statement; }; diff --git a/Userland/Utilities/sql.cpp b/Userland/Utilities/sql.cpp index 1869edbd2b..8008c82adc 100644 --- a/Userland/Utilities/sql.cpp +++ b/Userland/Utilities/sql.cpp @@ -152,7 +152,7 @@ private: bool m_keep_running { true }; DeprecatedString m_database_name {}; AK::RefPtr m_sql_client { nullptr }; - u64 m_connection_id { 0 }; + SQL::ConnectionID m_connection_id { 0 }; Core::EventLoop m_loop; OwnPtr m_input_file { nullptr }; bool m_quit_when_files_read { false };