Serenity Operating System
1/*
2 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
3 * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#pragma once
9
10#include <AK/DeprecatedString.h>
11#include <AK/RefPtr.h>
12#include <LibCore/Object.h>
13#include <LibSQL/Forward.h>
14#include <LibSQL/Heap.h>
15#include <LibSQL/Meta.h>
16#include <LibSQL/Result.h>
17#include <LibSQL/Serializer.h>
18
19namespace SQL {
20
21/**
22 * A Database object logically connects a Heap with the SQL data we want
23 * to store in it. It has BTree pointers for B-Trees holding the definitions
24 * of tables, columns, indexes, and other SQL objects.
25 */
26class Database : public Core::Object {
27 C_OBJECT(Database);
28
29public:
30 ~Database() override;
31
32 ResultOr<void> open();
33 bool is_open() const { return m_open; }
34 ErrorOr<void> commit();
35
36 ResultOr<void> add_schema(SchemaDef const&);
37 static Key get_schema_key(DeprecatedString const&);
38 ResultOr<NonnullRefPtr<SchemaDef>> get_schema(DeprecatedString const&);
39
40 ResultOr<void> add_table(TableDef& table);
41 static Key get_table_key(DeprecatedString const&, DeprecatedString const&);
42 ResultOr<NonnullRefPtr<TableDef>> get_table(DeprecatedString const&, DeprecatedString const&);
43
44 ErrorOr<Vector<Row>> select_all(TableDef&);
45 ErrorOr<Vector<Row>> match(TableDef&, Key const&);
46 ErrorOr<void> insert(Row&);
47 ErrorOr<void> remove(Row&);
48 ErrorOr<void> update(Row&);
49
50private:
51 explicit Database(DeprecatedString);
52
53 bool m_open { false };
54 NonnullRefPtr<Heap> m_heap;
55 Serializer m_serializer;
56 RefPtr<BTree> m_schemas;
57 RefPtr<BTree> m_tables;
58 RefPtr<BTree> m_table_columns;
59
60 HashMap<u32, NonnullRefPtr<SchemaDef>> m_schema_cache;
61 HashMap<u32, NonnullRefPtr<TableDef>> m_table_cache;
62};
63
64}