Serenity Operating System
1/*
2 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/LexicalPath.h>
8#include <SQLServer/DatabaseConnection.h>
9#include <SQLServer/SQLStatement.h>
10
11namespace SQLServer {
12
13static HashMap<SQL::ConnectionID, NonnullRefPtr<DatabaseConnection>> s_connections;
14static SQL::ConnectionID s_next_connection_id = 0;
15
16static ErrorOr<NonnullRefPtr<SQL::Database>> find_or_create_database(StringView database_path, StringView database_name)
17{
18 for (auto const& connection : s_connections) {
19 if (connection.value->database_name() == database_name)
20 return connection.value->database();
21 }
22
23 auto database_file = DeprecatedString::formatted("{}/{}.db", database_path, database_name);
24 return SQL::Database::try_create(move(database_file));
25}
26
27RefPtr<DatabaseConnection> DatabaseConnection::connection_for(SQL::ConnectionID connection_id)
28{
29 if (s_connections.contains(connection_id))
30 return *s_connections.get(connection_id).value();
31 dbgln_if(SQLSERVER_DEBUG, "Invalid connection_id {}", connection_id);
32 return nullptr;
33}
34
35ErrorOr<NonnullRefPtr<DatabaseConnection>> DatabaseConnection::create(StringView database_path, DeprecatedString database_name, int client_id)
36{
37 if (LexicalPath path(database_name); (path.title() != database_name) || (path.dirname() != "."))
38 return Error::from_string_view("Invalid database name"sv);
39
40 auto database = TRY(find_or_create_database(database_path, database_name));
41
42 if (auto result = database->open(); result.is_error()) {
43 warnln("Could not open database: {}", result.error().error_string());
44 return Error::from_string_view("Could not open database"sv);
45 }
46
47 return adopt_nonnull_ref_or_enomem(new (nothrow) DatabaseConnection(move(database), move(database_name), client_id));
48}
49
50DatabaseConnection::DatabaseConnection(NonnullRefPtr<SQL::Database> database, DeprecatedString database_name, int client_id)
51 : Object()
52 , m_database(move(database))
53 , m_database_name(move(database_name))
54 , m_connection_id(s_next_connection_id++)
55 , m_client_id(client_id)
56{
57 dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection {} initiatedconnection with database '{}'", connection_id(), m_database_name);
58 s_connections.set(m_connection_id, *this);
59}
60
61void DatabaseConnection::disconnect()
62{
63 dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::disconnect(connection_id {}, database '{}'", connection_id(), m_database_name);
64 s_connections.remove(connection_id());
65}
66
67SQL::ResultOr<SQL::StatementID> DatabaseConnection::prepare_statement(StringView sql)
68{
69 dbgln_if(SQLSERVER_DEBUG, "DatabaseConnection::prepare_statement(connection_id {}, database '{}', sql '{}'", connection_id(), m_database_name, sql);
70
71 auto statement = TRY(SQLStatement::create(*this, sql));
72 return statement->statement_id();
73}
74
75}