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#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/NonnullOwnPtr.h>
11#include <AK/NonnullRefPtr.h>
12#include <AK/Result.h>
13#include <AK/Vector.h>
14#include <LibCore/Object.h>
15#include <LibSQL/Forward.h>
16#include <LibSQL/Type.h>
17#include <LibSQL/Value.h>
18
19namespace SQL {
20
21/**
22 * This file declares objects describing tables, indexes, and columns.
23 * It remains to be seen if this will survive in it's current form.
24 */
25
26class Relation : public Core::Object {
27 C_OBJECT_ABSTRACT(Relation);
28
29public:
30 u32 hash() const;
31 u32 pointer() const { return m_pointer; }
32 void set_pointer(u32 pointer) { m_pointer = pointer; }
33 ~Relation() override = default;
34 virtual Key key() const = 0;
35 Relation const* parent_relation() const { return dynamic_cast<Relation const*>(parent()); }
36
37protected:
38 Relation(DeprecatedString name, u32 pointer, Relation* parent = nullptr)
39 : Core::Object(parent)
40 , m_pointer(pointer)
41 {
42 set_name(move(name));
43 }
44
45 explicit Relation(DeprecatedString name, Relation* parent = nullptr)
46 : Core::Object(parent)
47 , m_pointer(0)
48 {
49 set_name(move(name));
50 }
51
52private:
53 u32 m_pointer { 0 };
54};
55
56class SchemaDef : public Relation {
57 C_OBJECT(SchemaDef);
58
59public:
60 Key key() const override;
61 static NonnullRefPtr<IndexDef> index_def();
62 static Key make_key();
63
64private:
65 explicit SchemaDef(DeprecatedString);
66 explicit SchemaDef(Key const&);
67};
68
69class ColumnDef : public Relation {
70 C_OBJECT(ColumnDef);
71
72public:
73 Key key() const override;
74 SQLType type() const { return m_type; }
75 size_t column_number() const { return m_index; }
76 void set_not_null(bool can_not_be_null) { m_not_null = can_not_be_null; }
77 bool not_null() const { return m_not_null; }
78 void set_default_value(Value const& default_value);
79 Value const& default_value() const { return m_default; }
80
81 static NonnullRefPtr<IndexDef> index_def();
82 static Key make_key(TableDef const&);
83
84protected:
85 ColumnDef(Relation*, size_t, DeprecatedString, SQLType);
86
87private:
88 size_t m_index;
89 SQLType m_type { SQLType::Text };
90 bool m_not_null { false };
91 Value m_default;
92};
93
94class KeyPartDef : public ColumnDef {
95 C_OBJECT(KeyPartDef);
96
97public:
98 Order sort_order() const { return m_sort_order; }
99
100private:
101 KeyPartDef(IndexDef*, DeprecatedString, SQLType, Order = Order::Ascending);
102
103 Order m_sort_order { Order::Ascending };
104};
105
106class IndexDef : public Relation {
107 C_OBJECT(IndexDef);
108
109public:
110 ~IndexDef() override = default;
111
112 Vector<NonnullRefPtr<KeyPartDef>> const& key_definition() const { return m_key_definition; }
113 bool unique() const { return m_unique; }
114 [[nodiscard]] size_t size() const { return m_key_definition.size(); }
115 void append_column(DeprecatedString, SQLType, Order = Order::Ascending);
116 Key key() const override;
117 [[nodiscard]] NonnullRefPtr<TupleDescriptor> to_tuple_descriptor() const;
118 static NonnullRefPtr<IndexDef> index_def();
119 static Key make_key(TableDef const& table_def);
120
121private:
122 IndexDef(TableDef*, DeprecatedString, bool unique = true, u32 pointer = 0);
123 explicit IndexDef(DeprecatedString, bool unique = true, u32 pointer = 0);
124
125 Vector<NonnullRefPtr<KeyPartDef>> m_key_definition;
126 bool m_unique { false };
127
128 friend TableDef;
129};
130
131class TableDef : public Relation {
132 C_OBJECT(TableDef);
133
134public:
135 Key key() const override;
136 void append_column(DeprecatedString, SQLType);
137 void append_column(Key const&);
138 size_t num_columns() { return m_columns.size(); }
139 size_t num_indexes() { return m_indexes.size(); }
140 Vector<NonnullRefPtr<ColumnDef>> const& columns() const { return m_columns; }
141 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
148private:
149 explicit TableDef(SchemaDef*, DeprecatedString);
150
151 Vector<NonnullRefPtr<ColumnDef>> m_columns;
152 Vector<NonnullRefPtr<IndexDef>> m_indexes;
153};
154
155}