mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:38:11 +00:00

Currently, when clients connect to SQL server, we inform them of any errors opening the database via an asynchronous IPC. But we already know about these errors before returning from the connect() IPC, so this roundabout propagation is a bit unnecessary. Now if we fail to open the database, we will simply not send back a valid connection ID. Disconnect has a similar story. Rather than disconnecting and invoking an asynchronous IPC to inform the client of the disconnect, make the disconnect() IPC synchronous (because all it does is remove the database from the map of open databases). Further, the only user of this command is the SQL REPL when it wants to connect to a different database, so it makes sense to block it. This did require moving a bit of logic around in the REPL to accommodate this change.
40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
/*
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
#include <LibCore/Object.h>
|
|
#include <LibSQL/Database.h>
|
|
#include <LibSQL/Result.h>
|
|
#include <SQLServer/Forward.h>
|
|
|
|
namespace SQLServer {
|
|
|
|
class DatabaseConnection final : public Core::Object {
|
|
C_OBJECT_ABSTRACT(DatabaseConnection)
|
|
|
|
public:
|
|
static ErrorOr<NonnullRefPtr<DatabaseConnection>> create(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; }
|
|
int client_id() const { return m_client_id; }
|
|
NonnullRefPtr<SQL::Database> database() { return m_database; }
|
|
void disconnect();
|
|
SQL::ResultOr<u64> 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 };
|
|
int m_client_id { 0 };
|
|
};
|
|
|
|
}
|