Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Writable_Database_cpp.cpp
Go to the documentation of this file.
3#include "joedb/ui/type_io.h"
5
6namespace joedb::generator
7{
8 ////////////////////////////////////////////////////////////////////////////
10 ////////////////////////////////////////////////////////////////////////////
11 (
12 const Compiler_Options &options
13 ):
14 Generator(".", "Writable_Database.cpp", options)
15 {
16 }
17
18 ////////////////////////////////////////////////////////////////////////////
20 ////////////////////////////////////////////////////////////////////////////
21 {
22 const auto &db = options.get_db();
23 const auto &tables = db.get_tables();
24
25 out << "#include \"Writable_Database.h\"\n";
26 out << "#include \"joedb/Writable.h\"\n";
27 out << "#include \"joedb/journal/Readonly_Memory_File.h\"\n";
28 out << '\n';
29 out << "#include <ctime>\n\n";
30
32
33 out << R"RRR(
34 ////////////////////////////////////////////////////////////////////////////
35 void Writable_Database::write_comment(const std::string &comment)
36 ////////////////////////////////////////////////////////////////////////////
37 {
38 journal.comment(comment);
39 }
40
41 ////////////////////////////////////////////////////////////////////////////
42 void Writable_Database::write_timestamp()
43 ////////////////////////////////////////////////////////////////////////////
44 {
45 journal.timestamp(std::time(nullptr));
46 }
47
48 ////////////////////////////////////////////////////////////////////////////
49 void Writable_Database::write_timestamp(int64_t timestamp)
50 ////////////////////////////////////////////////////////////////////////////
51 {
52 journal.timestamp(timestamp);
53 }
54
55 ////////////////////////////////////////////////////////////////////////////
56 void Writable_Database::write_valid_data()
57 ////////////////////////////////////////////////////////////////////////////
58 {
59 journal.valid_data();
60 }
61
62 ////////////////////////////////////////////////////////////////////////////
63 void Writable_Database::play_journal()
64 ////////////////////////////////////////////////////////////////////////////
65 {
66 max_record_id = size_t(journal.get_checkpoint_position());
67 ready_to_write = false;
68 journal.play_until_checkpoint(*this);
69 ready_to_write = true;
70 max_record_id = 0;
71 }
72
73 ////////////////////////////////////////////////////////////////////////////
74 void Writable_Database::auto_upgrade()
75 ////////////////////////////////////////////////////////////////////////////
76 {
77 const size_t file_schema_size = size_t(schema_file.get_size());
78
79 if (file_schema_size < detail::schema_string_size)
80 {
81 journal.comment("Automatic schema upgrade");
82
83 joedb::Readonly_Memory_File schema_file(detail::schema_string, detail::schema_string_size);
84 joedb::Readonly_Journal schema_journal(schema_file);
85
86 schema_journal.set_position(int64_t(file_schema_size));
87 schema_journal.play_until(journal, detail::schema_string_size);
88
89 schema_journal.set_position(int64_t(file_schema_size));
90 upgrading_schema = true;
91 schema_journal.play_until(*this, detail::schema_string_size);
92 upgrading_schema = false;
93
94 journal.valid_data();
95 }
96 }
97
98 ////////////////////////////////////////////////////////////////////////////
99 Writable_Database::Writable_Database
100 ////////////////////////////////////////////////////////////////////////////
101 (
102 joedb::Buffered_File &file,
103 bool perform_initialization,
104 joedb::Readonly_Journal::Check check,
105 joedb::Commit_Level commit_level
106 ):
107 journal(file, check, commit_level)
108 {
109 journal.rewind();
110
111 if (perform_initialization)
112 initialize();
113 }
114
115 ////////////////////////////////////////////////////////////////////////////
116 Writable_Database::Writable_Database
117 ////////////////////////////////////////////////////////////////////////////
118 (
119 joedb::Buffered_File &file,
120 joedb::Readonly_Journal::Check check,
121 joedb::Commit_Level commit_level
122 ):
123 Writable_Database(file, true, check, commit_level)
124 {
125 }
126
127 ////////////////////////////////////////////////////////////////////////////
128 void Writable_Database::check_single_row()
129 ////////////////////////////////////////////////////////////////////////////
130 {
131 )RRR";
132
133 for (const auto &[tid, tname]: tables)
134 {
136 {
137 out << " {\n";
138 out << " const auto table = get_" << tname << "_table();\n";
139 out << " if (table.first() != the_" <<tname << "() || table.last() != the_" << tname << "())\n";
140 out << " throw joedb::Exception(\"Single-row constraint failure for table " << tname << "\");\n";
141 out << " }\n";
142 }
143 }
144 out << " }\n";
145
147 {
148 out << R"RRR(
149 ////////////////////////////////////////////////////////////////////////////
150 void Writable_Database::create_table(const std::string &name)
151 ////////////////////////////////////////////////////////////////////////////
152 {
153 Database::create_table(name);
154
155 if (upgrading_schema)
156 {
157 )RRR";
158
159 for (const auto &[tid, tname]: tables)
160 {
162 {
163 out << " if (current_table_id == Table_Id{" << tid << "})\n";
164 out << " new_" << tname << "();\n";
165 }
166 }
167
168 out << " }\n }\n";
169 }
170
171 if (db_has_values())
172 {
173 out << R"RRR(
174 ////////////////////////////////////////////////////////////////////////////
175 void Writable_Database::add_field
176 ////////////////////////////////////////////////////////////////////////////
177 (
178 Table_Id table_id,
179 const std::string &name,
180 joedb::Type type
181 )
182 {
183 Database::add_field(table_id, name, type);
184 )RRR";
185
186 for (const auto &[tid, tname]: tables)
187 {
188 if (db.get_freedom(tid).size() > 0)
189 {
190 const Record_Id record_id{db.get_freedom(tid).get_first_used() - 1};
191
192 out << "\n if (table_id == Table_Id{" << tid << "})\n";
193 out << " {\n";
194 out << " const auto field_id = ++storage_of_" << tname << ".current_field_id;\n";
195 out << " if (upgrading_schema)\n";
196 out << " {\n";
197
198 for (const auto &[fid, fname]: db.get_fields(tid))
199 {
200 out << " if (field_id == Field_Id{" << fid << "})\n";
201 out << " {\n";
202 out << " for (const auto record: get_" << tname << "_table())\n";
203 out << " set_" << fname << "(record, ";
204
205 const auto &type = db.get_field_type(tid, fid);
206 const bool reference = type.get_type_id() == joedb::Type::Type_Id::reference;
207
208 if (reference)
209 {
210 const auto it = tables.find(type.get_table_id());
211 if (it != tables.end())
212 out << "id_of_" << it->second;
213 out << "(";
214 }
215
216 joedb::write_value(out, db, tid, record_id, fid);
217
218 if (reference)
219 out << ")";
220
221 out <<");\n";
222 out << " }\n";
223 }
224
225 out << " }\n";
226 out << " }\n";
227 }
228 }
229 out << " }\n";
230 }
231
232 for (const auto &[tid, tname]: tables)
233 {
234 const bool single_row = options.get_table_options(tid).single_row;
235
236 if (!single_row)
237 {
238 out << '\n';
239 out << " /////////////////////////////////////////////////////////////////////////////\n";
240 out << " void Writable_Database::clear_" << tname << "_table()\n";
241 out << " /////////////////////////////////////////////////////////////////////////////\n";
242 out << " {\n";
243 out << " while (!get_" << tname << "_table().is_empty())\n";
244 out << " delete_" << tname << "(get_" << tname << "_table().last());\n";
245 out << " }\n";
246 }
247 }
248
250 }
251}
const std::vector< std::string > & get_name_space() const
const Table_Options & get_table_options(Table_Id table_id) const
const Database & get_db() const
const std::map< Table_Id, std::string > & get_tables() const override
const Compiler_Options & options
Definition Generator.h:14
Writable_Database_cpp(const Compiler_Options &options)
void namespace_open(std::ostream &out, const std::vector< std::string > &n)
void namespace_close(std::ostream &out, const std::vector< std::string > &n)
void write_value(std::ostream &out, const Readable &readable, Table_Id table_id, Record_Id record_id, Field_Id field_id)
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5