Joedb 10.5.0
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 (
13 ):
14 Generator(".", "Writable_Database.cpp", options)
15 {
16 }
17
18 ////////////////////////////////////////////////////////////////////////////
19 void Writable_Database_cpp::write(std::ostream &out)
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/journal/Readonly_Memory_File.h\"\n";
27 out << '\n';
28 out << "#include <ctime>\n";
29 out << "#include <limits>\n\n";
30
31 namespace_open(out, options.get_name_space());
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 = journal.get_checkpoint();
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.skip_directly_to(int64_t(file_schema_size));
87 schema_journal.raw_play_until(journal, detail::schema_string_size);
88
89 schema_journal.skip_directly_to(int64_t(file_schema_size));
90 upgrading_schema = true;
91 schema_journal.raw_play_until(*this, detail::schema_string_size);
92 upgrading_schema = false;
93
94 journal.valid_data();
95 journal.soft_checkpoint();
96 }
97 }
98
99 ////////////////////////////////////////////////////////////////////////////
100 void Writable_Database::initialize()
101 ////////////////////////////////////////////////////////////////////////////
102 {
103 play_journal();
104 check_schema();
105 auto_upgrade();
106 check_single_row();
107 }
108
109 ////////////////////////////////////////////////////////////////////////////
110 Writable_Database::Writable_Database
111 ////////////////////////////////////////////////////////////////////////////
112 (
113 joedb::Abstract_File &file,
114 joedb::Recovery recovery,
115 bool perform_initialization
116 ):
117 journal(joedb::Journal_Construction_Lock(file, recovery))
118 {
119 journal.rewind();
120
121 if (perform_initialization)
122 initialize();
123 }
124
125 ////////////////////////////////////////////////////////////////////////////
126 Writable_Database::Writable_Database(joedb::Abstract_File &file):
127 ////////////////////////////////////////////////////////////////////////////
128 Writable_Database(file, joedb::Recovery::none, true)
129 {
130 }
131
132 ////////////////////////////////////////////////////////////////////////////
133 Writable_Database::Writable_Database
134 ////////////////////////////////////////////////////////////////////////////
135 (
136 joedb::Abstract_File &file,
137 joedb::Recovery recovery
138 ):
139 Writable_Database(file, recovery, true)
140 {
141 }
142
143 ////////////////////////////////////////////////////////////////////////////
144 void Writable_Database::check_single_row()
145 ////////////////////////////////////////////////////////////////////////////
146 {
147)RRR";
148
149 for (const auto &[tid, tname]: tables)
150 {
151 if (options.get_table_options(tid).single_row)
152 {
153 out << " {\n";
154 out << " const auto table = get_" << tname << "_table();\n";
155 out << " if (table.first() != the_" <<tname << "() || table.last() != the_" << tname << "())\n";
156 out << " throw joedb::Exception(\"Single-row constraint failure for table " << tname << "\");\n";
157 out << " }\n";
158 }
159 }
160 out << " }\n";
161
162 if (options.has_single_row())
163 {
164 out << R"RRR(
165 ////////////////////////////////////////////////////////////////////////////
166 void Writable_Database::create_table(const std::string &name)
167 ////////////////////////////////////////////////////////////////////////////
168 {
169 Database_Writable::create_table(name);
170
171 if (upgrading_schema)
172 {
173 )RRR";
174
175 for (const auto &[tid, tname]: tables)
176 {
177 if (options.get_table_options(tid).single_row)
178 {
179 out << " if (current_table_id == Table_Id{" << tid << "})\n";
180 out << " new_" << tname << "();\n";
181 }
182 }
183
184 out << " }\n }\n";
185 }
186
187 if (db_has_values())
188 {
189 out << R"RRR(
190 ////////////////////////////////////////////////////////////////////////////
191 void Writable_Database::add_field
192 ////////////////////////////////////////////////////////////////////////////
193 (
194 Table_Id table_id,
195 const std::string &name,
196 joedb::Type type
197 )
198 {
199 Database_Writable::add_field(table_id, name, type);
200 )RRR";
201
202 for (const auto &[tid, tname]: tables)
203 {
204 if (db.get_freedom(tid).get_used_count() > Record_Id{0})
205 {
206 const Record_Id record_id{db.get_freedom(tid).get_first_used()};
207
208 out << "\n if (table_id == Table_Id{" << tid << "})\n";
209 out << " {\n";
210 out << " const auto field_id = ++storage_of_" << tname << ".current_field_id;\n";
211 out << " if (upgrading_schema)\n";
212 out << " {\n";
213
214 for (const auto &[fid, fname]: db.get_fields(tid))
215 {
216 out << " if (field_id == Field_Id{" << fid << "})\n";
217 out << " {\n";
218 out << " for (const auto record: get_" << tname << "_table())\n";
219 out << " set_" << fname << "(record, ";
220
221 joedb::write_cpp_value(out, db, tid, record_id, fid);
222
223 out <<");\n";
224 out << " }\n";
225 }
226
227 out << " }\n";
228 out << " }\n";
229 }
230 }
231 out << " }\n";
232 }
233
234 for (const auto &[tid, tname]: tables)
235 {
236 const bool single_row = options.get_table_options(tid).single_row;
237
238 if (!single_row)
239 {
240 out << '\n';
241 out << " /////////////////////////////////////////////////////////////////////////////\n";
242 out << " void Writable_Database::clear_" << tname << "_table()\n";
243 out << " /////////////////////////////////////////////////////////////////////////////\n";
244 out << " {\n";
245 out << " while (!get_" << tname << "_table().is_empty())\n";
246 out << " delete_" << tname << "(get_" << tname << "_table().last());\n";
247 out << " }\n";
248 }
249 }
250
251 namespace_close(out, options.get_name_space());
252 }
253}
const Compiler_Options & options
Definition Generator.h:24
Generator(std::string dir_name, std::string file_name, const Compiler_Options &options)
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)
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5
void write_cpp_value(std::ostream &out, const Readable &readable, Table_Id table_id, Record_Id record_id, Field_Id field_id)