summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/Meta.cpp
blob: ce8dc12bd29106cd7995c35ea72cc49d1c32d00e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibSQL/Key.h>
#include <LibSQL/Meta.h>
#include <LibSQL/Type.h>

namespace SQL {

SchemaDef::SchemaDef(String name)
    : Relation(move(name))
{
}

SchemaDef::SchemaDef(Key const& key)
    : Relation(key["schema_name"].to_string().value())
{
}

Key SchemaDef::key() const
{
    auto key = Key(index_def()->to_tuple_descriptor());
    key["schema_name"] = name();
    key.set_pointer(pointer());
    return key;
}

Key SchemaDef::make_key()
{
    return Key(index_def());
}

NonnullRefPtr<IndexDef> SchemaDef::index_def()
{
    NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$schema", true, 0);
    if (!s_index_def->size()) {
        s_index_def->append_column("schema_name", SQLType::Text, AST::Order::Ascending);
    }
    return s_index_def;
}

ColumnDef::ColumnDef(Relation* parent, size_t column_number, String name, SQLType sql_type)
    : Relation(move(name), parent)
    , m_index(column_number)
    , m_type(sql_type)
{
}

Key ColumnDef::key() const
{
    auto key = Key(index_def());
    key["table_hash"] = parent_relation()->hash();
    key["column_number"] = (int)column_number();
    key["column_name"] = name();
    key["column_type"] = (int)type();
    return key;
}

Key ColumnDef::make_key(TableDef const& table_def)
{
    Key key(index_def());
    key["table_hash"] = table_def.key().hash();
    return key;
}

NonnullRefPtr<IndexDef> ColumnDef::index_def()
{
    NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$column", true, 0);
    if (!s_index_def->size()) {
        s_index_def->append_column("table_hash", SQLType::Integer, AST::Order::Ascending);
        s_index_def->append_column("column_number", SQLType::Integer, AST::Order::Ascending);
        s_index_def->append_column("column_name", SQLType::Text, AST::Order::Ascending);
        s_index_def->append_column("column_type", SQLType::Integer, AST::Order::Ascending);
    }
    return s_index_def;
}

KeyPartDef::KeyPartDef(IndexDef* index, String name, SQLType sql_type, AST::Order sort_order)
    : ColumnDef(index, index->size(), move(name), sql_type)
    , m_sort_order(sort_order)
{
}

IndexDef::IndexDef(TableDef* table, String name, bool unique, u32 pointer)
    : Relation(move(name), pointer, table)
    , m_key_definition()
    , m_unique(unique)
{
}

IndexDef::IndexDef(String name, bool unique, u32 pointer)
    : IndexDef(nullptr, move(name), unique, pointer)
{
}

void IndexDef::append_column(String name, SQLType sql_type, AST::Order sort_order)
{
    auto part = KeyPartDef::construct(this, move(name), sql_type, sort_order);
    m_key_definition.append(part);
}

TupleDescriptor IndexDef::to_tuple_descriptor() const
{
    TupleDescriptor ret;
    for (auto& part : m_key_definition) {
        ret.append({ part.name(), part.type(), part.sort_order() });
    }
    return ret;
}

Key IndexDef::key() const
{
    auto key = Key(index_def()->to_tuple_descriptor());
    key["table_hash"] = parent_relation()->key().hash();
    key["index_name"] = name();
    key["unique"] = unique() ? 1 : 0;
    return key;
}

Key IndexDef::make_key(TableDef const& table_def)
{
    Key key(index_def());
    key["table_hash"] = table_def.key().hash();
    return key;
}

NonnullRefPtr<IndexDef> IndexDef::index_def()
{
    NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$index", true, 0);
    if (!s_index_def->size()) {
        s_index_def->append_column("table_hash", SQLType::Integer, AST::Order::Ascending);
        s_index_def->append_column("index_name", SQLType::Text, AST::Order::Ascending);
        s_index_def->append_column("unique", SQLType::Integer, AST::Order::Ascending);
    }
    return s_index_def;
}

TableDef::TableDef(SchemaDef* schema, String name)
    : Relation(move(name), schema)
    , m_columns()
    , m_indexes()
{
}

TupleDescriptor TableDef::to_tuple_descriptor() const
{
    TupleDescriptor ret;
    for (auto& part : m_columns) {
        ret.append({ part.name(), part.type(), AST::Order::Ascending });
    }
    return ret;
}

Key TableDef::key() const
{
    auto key = Key(index_def()->to_tuple_descriptor());
    key["schema_hash"] = parent_relation()->key().hash();
    key["table_name"] = name();
    key.set_pointer(pointer());
    return key;
}

void TableDef::append_column(String name, SQLType sql_type)
{
    auto column = ColumnDef::construct(this, num_columns(), move(name), sql_type);
    m_columns.append(column);
}

void TableDef::append_column(Key const& column)
{
    append_column(
        (String)column["column_name"],
        (SQLType)((int)column["column_type"]));
}

Key TableDef::make_key(SchemaDef const& schema_def)
{
    return TableDef::make_key(schema_def.key());
}

Key TableDef::make_key(Key const& schema_key)
{
    Key key(index_def());
    key["schema_hash"] = schema_key.hash();
    return key;
}

NonnullRefPtr<IndexDef> TableDef::index_def()
{
    NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$table", true, 0);
    if (!s_index_def->size()) {
        s_index_def->append_column("schema_hash", SQLType::Integer, AST::Order::Ascending);
        s_index_def->append_column("table_name", SQLType::Text, AST::Order::Ascending);
    }
    return s_index_def;
}

}