Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Database_Schema.cpp
Go to the documentation of this file.
4
5namespace joedb
6{
7 ////////////////////////////////////////////////////////////////////////////
9 ////////////////////////////////////////////////////////////////////////////
10 (
11 const char *message,
12 const std::string &name
13 )
14 {
15 if (!is_identifier(name))
16 {
17 throw Exception(std::string(message) + ": invalid identifier: " + name);
18 }
19 }
20
21 ////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////
24 (
25 Table_Id table_id
26 ) const
27 {
28 return (const_cast<Database_Schema *>(this))->get_table(table_id);
29 }
30
31 ////////////////////////////////////////////////////////////////////////////
33 ////////////////////////////////////////////////////////////////////////////
34 (
35 Table_Id table_id
36 )
37 {
38 const auto it = tables.find(table_id);
39
40 if (it == tables.end())
41 {
42 throw Exception
43 (
44 "get_table: invalid table_id: " + std::to_string(to_underlying(table_id))
45 );
46 }
47
48 return it->second;
49 }
50
51 ////////////////////////////////////////////////////////////////////////////
52 const std::map<Field_Id, std::string> &Database_Schema::get_fields
53 ////////////////////////////////////////////////////////////////////////////
54 (
55 Table_Id table_id
56 ) const
57 {
58 return get_table(table_id).field_names;
59 }
60
61 ////////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////////
64 (
65 Table_Id table_id,
66 Field_Id field_id
67 ) const
68 {
69 static Type null_type;
70 const auto table_it = tables.find(table_id);
71 if (table_it == tables.end())
72 return null_type;
73 const auto &fields = table_it->second.get_fields();
74 const auto field_it = fields.find(field_id);
75 if (field_it == fields.end())
76 return null_type;
77 return field_it->second.get_type();
78 }
79
80 ////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////
83 (
84 Table_Id table_id
85 ) const
86 {
87 return get_table(table_id).freedom;
88 }
89
90 #define TYPE_MACRO(type, return_type, type_id, R, W)\
91 const type &Database_Schema::get_##type_id\
92 (\
93 Table_Id table_id,\
94 Record_Id record_id,\
95 Field_Id field_id\
96 ) const\
97 {\
98 return *get_table(table_id).get_own_##type_id##_storage(record_id, field_id);\
99 }
100 #include "joedb/TYPE_MACRO.h"
101
102 ////////////////////////////////////////////////////////////////////////////
103 void Database_Schema::create_table(const std::string &name)
104 ////////////////////////////////////////////////////////////////////////////
105 {
106 check_identifier("create_table", name);
107
108 if (find_table(name) != Table_Id(0))
109 throw Exception("create_table: name already used: " + name);
110
112 tables.insert(std::make_pair(current_table_id, Table()));
114 }
115
116 ////////////////////////////////////////////////////////////////////////////
118 ////////////////////////////////////////////////////////////////////////////
119 {
120 const auto it = tables.find(table_id);
121 if (it == tables.end())
122 throw Exception("drop_table: invalid table_id");
123 table_names.erase(table_id);
124 tables.erase(it);
125 }
126
127 ////////////////////////////////////////////////////////////////////////////
129 ////////////////////////////////////////////////////////////////////////////
130 (
131 Table_Id table_id,
132 const std::string &name
133 )
134 {
135 check_identifier("rename_table", name);
136
137 if (find_table(name) != Table_Id(0))
138 throw Exception("rename_table: name already used: " + name);
139
140 get_table(table_id); // make sure the table exists
141
142 table_names[table_id] = name;
143 }
144
145 ////////////////////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////////////////////
148 (
149 Table_Id table_id,
150 const std::string &name,
151 Type type
152 )
153 {
154 check_identifier("add_field", name);
155
156 get_table(table_id).add_field(name, type);
157 }
158
159 ////////////////////////////////////////////////////////////////////////////
161 ////////////////////////////////////////////////////////////////////////////
162 {
163 get_table(table_id).drop_field(field_id);
164 }
165
166 ////////////////////////////////////////////////////////////////////////////
168 ////////////////////////////////////////////////////////////////////////////
169 (
170 Table_Id table_id,
171 Field_Id field_id,
172 const std::string &name
173 )
174 {
175 check_identifier("rename_field", name);
176
177 Table &table = get_table(table_id);
178
179 auto &field_names = table.field_names;
180 const auto field_it = field_names.find(field_id);
181 if (field_it == field_names.end())
182 throw Exception("rename_field: invalid field_id");
183
184 if (table.find_field(name) != Field_Id(0))
185 throw Exception("rename_field: name already used: " + name);
186
187 field_it->second = name;
188 }
189
190 ////////////////////////////////////////////////////////////////////////////
192 ////////////////////////////////////////////////////////////////////////////
193}
std::map< Table_Id, std::string > table_names
void create_table(const std::string &name) override
const Table & get_table(Table_Id table_id) const
static void check_identifier(const char *message, const std::string &name)
virtual ~Database_Schema() override
void drop_table(Table_Id table_id) override
void add_field(Table_Id table_id, const std::string &name, Type type) override
const Compact_Freedom_Keeper & get_freedom(Table_Id table_id) const override
void rename_field(Table_Id, Field_Id, const std::string &name) override
void rename_table(Table_Id table_id, const std::string &name) override
void drop_field(Table_Id table_id, Field_Id field_id) override
std::map< Table_Id, Table > tables
const Type & get_field_type(Table_Id table_id, Field_Id field_id) const override
const std::map< Field_Id, std::string > & get_fields(Table_Id table_id) const override
Table_Id find_table(const std::string &name) const
Definition Readable.cpp:12
void drop_field(Field_Id field_id)
Definition Table.cpp:32
Field_Id find_field(const std::string &name) const
Definition Table.cpp:8
Definition Blob.h:7
constexpr std::underlying_type< Table_Id >::type to_underlying(Table_Id id)
Definition index_types.h:15
bool is_identifier(const std::string &s)