Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibSQL/Database.h
Line
Count
Source (jump to first uncovered line)
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/ByteString.h>
11
#include <AK/NonnullRefPtr.h>
12
#include <AK/RefPtr.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
19
namespace 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
 */
26
class Database : public RefCounted<Database> {
27
public:
28
    static ErrorOr<NonnullRefPtr<Database>> create(ByteString);
29
    ~Database();
30
31
    ResultOr<void> open();
32
0
    bool is_open() const { return m_open; }
33
    ErrorOr<void> commit();
34
0
    ErrorOr<size_t> file_size_in_bytes() const { return m_heap->file_size_in_bytes(); }
35
36
    ResultOr<void> add_schema(SchemaDef const&);
37
    static Key get_schema_key(ByteString const&);
38
    ResultOr<NonnullRefPtr<SchemaDef>> get_schema(ByteString const&);
39
40
    ResultOr<void> add_table(TableDef& table);
41
    static Key get_table_key(ByteString const&, ByteString const&);
42
    ResultOr<NonnullRefPtr<TableDef>> get_table(ByteString const&, ByteString 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
50
private:
51
    explicit Database(NonnullRefPtr<Heap>);
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
}