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.
This commit is contained in:
Timothy Flynn 2022-12-07 13:01:55 -05:00 committed by Andreas Kling
parent 44ff3a374f
commit c372012842
Notes: sideshowbarker 2024-07-17 23:02:37 +09:00
9 changed files with 37 additions and 30 deletions

View File

@ -65,7 +65,7 @@ private:
Optional<DeprecatedString> 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 };
};
}

View File

@ -85,4 +85,8 @@ enum class Nulls {
Last,
};
using ConnectionID = u64;
using StatementID = u64;
using ExecutionID = u64;
}

View File

@ -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<SQL::Value> const& placeholder_values)
Messages::SQLServer::ExecuteStatementResponse ConnectionFromClient::execute_statement(SQL::StatementID statement_id, Vector<SQL::Value> const& placeholder_values)
{
dbgln_if(SQLSERVER_DEBUG, "ConnectionFromClient::execute_query_statement(statement_id: {})", statement_id);

View File

@ -10,6 +10,7 @@
#include <AK/HashMap.h>
#include <AK/Vector.h>
#include <LibIPC/ConnectionFromClient.h>
#include <LibSQL/Type.h>
#include <SQLServer/SQLClientEndpoint.h>
#include <SQLServer/SQLServerEndpoint.h>
@ -32,9 +33,9 @@ private:
explicit ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket>, 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<SQL::Value> 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<SQL::Value> const& placeholder_values) override;
virtual void disconnect(SQL::ConnectionID) override;
DeprecatedString m_database_path;
};

View File

@ -10,10 +10,10 @@
namespace SQLServer {
static HashMap<u64, NonnullRefPtr<DatabaseConnection>> s_connections;
static u64 s_next_connection_id = 0;
static HashMap<SQL::ConnectionID, NonnullRefPtr<DatabaseConnection>> s_connections;
static SQL::ConnectionID s_next_connection_id = 0;
RefPtr<DatabaseConnection> DatabaseConnection::connection_for(u64 connection_id)
RefPtr<DatabaseConnection> 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<u64> DatabaseConnection::prepare_statement(StringView sql)
SQL::ResultOr<SQL::StatementID> DatabaseConnection::prepare_statement(StringView sql)
{
dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::prepare_statement(connection_id {}, database '{}', sql '{}'", connection_id(), m_database_name, sql);

View File

@ -10,6 +10,7 @@
#include <LibCore/Object.h>
#include <LibSQL/Database.h>
#include <LibSQL/Result.h>
#include <LibSQL/Type.h>
#include <SQLServer/Forward.h>
namespace SQLServer {
@ -21,19 +22,19 @@ public:
static ErrorOr<NonnullRefPtr<DatabaseConnection>> create(StringView database_path, DeprecatedString database_name, int client_id);
~DatabaseConnection() override = default;
static RefPtr<DatabaseConnection> connection_for(u64 connection_id);
u64 connection_id() const { return m_connection_id; }
static RefPtr<DatabaseConnection> connection_for(SQL::ConnectionID connection_id);
SQL::ConnectionID connection_id() const { return m_connection_id; }
int client_id() const { return m_client_id; }
NonnullRefPtr<SQL::Database> database() { return m_database; }
void disconnect();
SQL::ResultOr<u64> prepare_statement(StringView sql);
SQL::ResultOr<SQL::StatementID> prepare_statement(StringView sql);
private:
DatabaseConnection(NonnullRefPtr<SQL::Database> database, DeprecatedString database_name, int client_id);
NonnullRefPtr<SQL::Database> m_database;
DeprecatedString m_database_name;
u64 m_connection_id { 0 };
SQL::ConnectionID m_connection_id { 0 };
int m_client_id { 0 };
};

View File

@ -12,10 +12,10 @@
namespace SQLServer {
static HashMap<u64, NonnullRefPtr<SQLStatement>> s_statements;
static u64 s_next_statement_id = 0;
static HashMap<SQL::StatementID, NonnullRefPtr<SQLStatement>> s_statements;
static SQL::StatementID s_next_statement_id = 0;
RefPtr<SQLStatement> SQLStatement::statement_for(u64 statement_id)
RefPtr<SQLStatement> 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<SQL::AS
s_statements.set(m_statement_id, *this);
}
void SQLStatement::report_error(SQL::Result result, u64 execution_id)
void SQLStatement::report_error(SQL::Result result, SQL::ExecutionID execution_id)
{
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), result.error_string());
@ -58,7 +58,7 @@ void SQLStatement::report_error(SQL::Result result, u64 execution_id)
warnln("Cannot return execution error. Client disconnected");
}
Optional<u64> SQLStatement::execute(Vector<SQL::Value> placeholder_values)
Optional<SQL::ExecutionID> SQLStatement::execute(Vector<SQL::Value> 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) {

View File

@ -13,6 +13,7 @@
#include <LibSQL/AST/AST.h>
#include <LibSQL/Result.h>
#include <LibSQL/ResultSet.h>
#include <LibSQL/Type.h>
#include <SQLServer/DatabaseConnection.h>
#include <SQLServer/Forward.h>
@ -25,22 +26,22 @@ public:
static SQL::ResultOr<NonnullRefPtr<SQLStatement>> create(DatabaseConnection&, StringView sql);
~SQLStatement() override = default;
static RefPtr<SQLStatement> statement_for(u64 statement_id);
u64 statement_id() const { return m_statement_id; }
static RefPtr<SQLStatement> statement_for(SQL::StatementID statement_id);
SQL::StatementID statement_id() const { return m_statement_id; }
DatabaseConnection* connection() { return dynamic_cast<DatabaseConnection*>(parent()); }
Optional<u64> execute(Vector<SQL::Value> placeholder_values);
Optional<SQL::ExecutionID> execute(Vector<SQL::Value> placeholder_values);
private:
SQLStatement(DatabaseConnection&, NonnullRefPtr<SQL::AST::Statement> 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<u64> m_ongoing_executions;
u64 m_next_execution_id { 0 };
HashTable<SQL::ExecutionID> m_ongoing_executions;
SQL::ExecutionID m_next_execution_id { 0 };
NonnullRefPtr<SQL::AST::Statement> m_statement;
};

View File

@ -152,7 +152,7 @@ private:
bool m_keep_running { true };
DeprecatedString m_database_name {};
AK::RefPtr<SQL::SQLClient> m_sql_client { nullptr };
u64 m_connection_id { 0 };
SQL::ConnectionID m_connection_id { 0 };
Core::EventLoop m_loop;
OwnPtr<Core::Stream::BufferedFile> m_input_file { nullptr };
bool m_quit_when_files_read { false };