mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 14:45:07 +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.
63 lines
2.3 KiB
C++
63 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/LexicalPath.h>
|
|
#include <SQLServer/DatabaseConnection.h>
|
|
#include <SQLServer/SQLStatement.h>
|
|
|
|
namespace SQLServer {
|
|
|
|
static HashMap<u64, NonnullRefPtr<DatabaseConnection>> s_connections;
|
|
static u64 s_next_connection_id = 0;
|
|
|
|
RefPtr<DatabaseConnection> DatabaseConnection::connection_for(u64 connection_id)
|
|
{
|
|
if (s_connections.contains(connection_id))
|
|
return *s_connections.get(connection_id).value();
|
|
dbgln_if(SQLSERVER_DEBUG, "Invalid connection_id {}", connection_id);
|
|
return nullptr;
|
|
}
|
|
|
|
ErrorOr<NonnullRefPtr<DatabaseConnection>> DatabaseConnection::create(DeprecatedString database_name, int client_id)
|
|
{
|
|
if (LexicalPath path(database_name); (path.title() != database_name) || (path.dirname() != "."))
|
|
return Error::from_string_view("Invalid database name"sv);
|
|
|
|
auto database = SQL::Database::construct(DeprecatedString::formatted("/home/anon/sql/{}.db", database_name));
|
|
if (auto result = database->open(); result.is_error()) {
|
|
warnln("Could not open database: {}", result.error().error_string());
|
|
return Error::from_string_view("Could not open database"sv);
|
|
}
|
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) DatabaseConnection(move(database), move(database_name), client_id));
|
|
}
|
|
|
|
DatabaseConnection::DatabaseConnection(NonnullRefPtr<SQL::Database> database, DeprecatedString database_name, int client_id)
|
|
: Object()
|
|
, m_database(move(database))
|
|
, m_database_name(move(database_name))
|
|
, m_connection_id(s_next_connection_id++)
|
|
, m_client_id(client_id)
|
|
{
|
|
dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection {} initiatedconnection with database '{}'", connection_id(), m_database_name);
|
|
s_connections.set(m_connection_id, *this);
|
|
}
|
|
|
|
void DatabaseConnection::disconnect()
|
|
{
|
|
dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::disconnect(connection_id {}, database '{}'", connection_id(), m_database_name);
|
|
s_connections.remove(connection_id());
|
|
}
|
|
|
|
SQL::ResultOr<u64> DatabaseConnection::prepare_statement(StringView sql)
|
|
{
|
|
dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::prepare_statement(connection_id {}, database '{}', sql '{}'", connection_id(), m_database_name, sql);
|
|
|
|
auto statement = TRY(SQLStatement::create(*this, sql));
|
|
return statement->statement_id();
|
|
}
|
|
|
|
}
|