Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibSQL/Meta.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/ByteString.h>
10
#include <AK/NonnullRefPtr.h>
11
#include <AK/RefCounted.h>
12
#include <AK/Result.h>
13
#include <AK/Vector.h>
14
#include <LibCore/EventReceiver.h>
15
#include <LibSQL/Forward.h>
16
#include <LibSQL/Heap.h>
17
#include <LibSQL/Type.h>
18
#include <LibSQL/Value.h>
19
20
namespace SQL {
21
22
/**
23
 * This file declares objects describing tables, indexes, and columns.
24
 * It remains to be seen if this will survive in it's current form.
25
 */
26
27
class Relation : public RefCounted<Relation> {
28
public:
29
0
    virtual ~Relation() = default;
30
31
0
    ByteString const& name() const { return m_name; }
32
0
    Relation const* parent() const { return m_parent; }
33
34
    u32 hash() const;
35
0
    Block::Index block_index() const { return m_block_index; }
36
0
    void set_block_index(Block::Index block_index) { m_block_index = block_index; }
37
    virtual Key key() const = 0;
38
39
protected:
40
    Relation(ByteString name, Block::Index block_index, Relation* parent = nullptr)
41
0
        : m_name(move(name))
42
0
        , m_block_index(block_index)
43
0
        , m_parent(parent)
44
0
    {
45
0
    }
46
47
    explicit Relation(ByteString name, Relation* parent = nullptr)
48
0
        : Relation(move(name), 0, parent)
49
0
    {
50
0
    }
51
52
private:
53
    ByteString m_name;
54
    Block::Index m_block_index { 0 };
55
    Relation const* m_parent { nullptr };
56
};
57
58
class SchemaDef : public Relation {
59
public:
60
    static ErrorOr<NonnullRefPtr<SchemaDef>> create(ByteString name);
61
    static ErrorOr<NonnullRefPtr<SchemaDef>> create(Key const&);
62
63
    Key key() const override;
64
    static NonnullRefPtr<IndexDef> index_def();
65
    static Key make_key();
66
67
private:
68
    explicit SchemaDef(ByteString);
69
};
70
71
class ColumnDef : public Relation {
72
public:
73
    static ErrorOr<NonnullRefPtr<ColumnDef>> create(Relation*, size_t, ByteString, SQLType);
74
75
    Key key() const override;
76
0
    SQLType type() const { return m_type; }
77
0
    size_t column_number() const { return m_index; }
78
0
    void set_not_null(bool can_not_be_null) { m_not_null = can_not_be_null; }
79
0
    bool not_null() const { return m_not_null; }
80
    void set_default_value(Value const& default_value);
81
0
    Value const& default_value() const { return m_default; }
82
83
    static NonnullRefPtr<IndexDef> index_def();
84
    static Key make_key(TableDef const&);
85
86
protected:
87
    ColumnDef(Relation*, size_t, ByteString, SQLType);
88
89
private:
90
    size_t m_index;
91
    SQLType m_type { SQLType::Text };
92
    bool m_not_null { false };
93
    Value m_default;
94
};
95
96
class KeyPartDef : public ColumnDef {
97
public:
98
    static ErrorOr<NonnullRefPtr<KeyPartDef>> create(IndexDef*, ByteString, SQLType, Order = Order::Ascending);
99
100
0
    Order sort_order() const { return m_sort_order; }
101
102
private:
103
    KeyPartDef(IndexDef*, ByteString, SQLType, Order);
104
105
    Order m_sort_order { Order::Ascending };
106
};
107
108
class IndexDef : public Relation {
109
public:
110
    static ErrorOr<NonnullRefPtr<IndexDef>> create(TableDef*, ByteString, bool unique = true, u32 pointer = 0);
111
    static ErrorOr<NonnullRefPtr<IndexDef>> create(ByteString, bool unique = true, u32 pointer = 0);
112
113
0
    Vector<NonnullRefPtr<KeyPartDef>> const& key_definition() const { return m_key_definition; }
114
0
    bool unique() const { return m_unique; }
115
0
    [[nodiscard]] size_t size() const { return m_key_definition.size(); }
116
    void append_column(ByteString, SQLType, Order = Order::Ascending);
117
    Key key() const override;
118
    [[nodiscard]] NonnullRefPtr<TupleDescriptor> to_tuple_descriptor() const;
119
    static NonnullRefPtr<IndexDef> index_def();
120
    static Key make_key(TableDef const& table_def);
121
122
private:
123
    IndexDef(TableDef*, ByteString, bool unique, u32 pointer);
124
125
    Vector<NonnullRefPtr<KeyPartDef>> m_key_definition;
126
    bool m_unique { false };
127
128
    friend TableDef;
129
};
130
131
class TableDef : public Relation {
132
public:
133
    static ErrorOr<NonnullRefPtr<TableDef>> create(SchemaDef*, ByteString);
134
135
    Key key() const override;
136
    void append_column(ByteString, SQLType);
137
    void append_column(Key const&);
138
0
    size_t num_columns() { return m_columns.size(); }
139
0
    size_t num_indexes() { return m_indexes.size(); }
140
0
    Vector<NonnullRefPtr<ColumnDef>> const& columns() const { return m_columns; }
141
0
    Vector<NonnullRefPtr<IndexDef>> const& indexes() const { return m_indexes; }
142
    [[nodiscard]] NonnullRefPtr<TupleDescriptor> to_tuple_descriptor() const;
143
144
    static NonnullRefPtr<IndexDef> index_def();
145
    static Key make_key(SchemaDef const& schema_def);
146
    static Key make_key(Key const& schema_key);
147
148
private:
149
    explicit TableDef(SchemaDef*, ByteString);
150
151
    Vector<NonnullRefPtr<ColumnDef>> m_columns;
152
    Vector<NonnullRefPtr<IndexDef>> m_indexes;
153
};
154
155
}