22 auto tables =
db.get_tables();
27 out <<
"\n#include \"joedb/Span.h\"\n";
30#include "Database_Writable.h"
49 class Memory_Database;
52 class Writable_Database: public Database_Writable
54 friend class detail::Client_Data;
56 friend class Multiplexer;
57 friend class Memory_Database;
60 joedb::Writable_Journal journal;
65 void check_single_row();
70 if (!
options.get_custom_names().empty())
73 void custom(const std::string &name) override
75 Database_Writable::custom(name);
80 for (
const auto &name:
options.get_custom_names())
82 out <<
" if (name == \"" << name <<
"\")\n";
83 out <<
" " << name <<
"(*this);\n";
90 for (
const auto &name:
options.get_custom_names())
91 out <<
" static void " << name <<
"(Writable_Database &db);\n";
101 const std::string &name,
109 out <<
"\n void create_table(const std::string &name) override;\n";
115 joedb::Abstract_File &file,
116 joedb::Recovery recovery,
117 bool perform_initialization
121 Writable_Database(joedb::Abstract_File &file);
125 joedb::Abstract_File &file,
126 joedb::Recovery recovery
129 const joedb::Readonly_Journal &get_journal() const {return journal;}
131 std::string read_blob(joedb::Blob blob) const
133 return journal.get_file().read_blob(blob);
136 joedb::Blob write_blob(std::string_view data) override
138 return journal.write_blob(data);
141 int64_t ahead_of_checkpoint() const
143 return journal.ahead_of_checkpoint();
146 void soft_checkpoint() override
148 journal.soft_checkpoint();
151 void hard_checkpoint() override
153 journal.hard_checkpoint();
156 void write_comment(const std::string &comment);
157 void write_timestamp();
158 void write_timestamp(int64_t timestamp);
159 void write_valid_data();
160 void flush() override {journal.flush();}
163 for (
const auto &[tid, tname]: tables)
166 const bool single_row =
options.get_table_options(tid).single_row;
173 out <<
" void clear_" << tname <<
"_table();\n";
178 out <<
" private:\n";
183 out <<
" id_of_" << tname <<
" new_" << tname <<
"()\n";
186 out <<
" id_of_" << tname <<
" result(Record_Id(storage_of_" << tname <<
".freedom_keeper.get_free_record()));\n";
187 out <<
" storage_of_" << tname <<
".resize(storage_of_" << tname <<
".freedom_keeper.size());\n";
188 out <<
" internal_insert_" << tname <<
"(result.get_record_id());\n\n";
189 out <<
" journal.insert_into(Table_Id(" << tid <<
"), result.get_record_id());\n";
190 out <<
" return result;\n";
193 out <<
" void insert_" << tname <<
"(id_of_" << tname <<
" id)\n";
195 out <<
" if (id.get_record_id() >= Record_Id(storage_of_" << tname <<
".size()))\n";
196 out <<
" storage_of_" << tname <<
".resize(id.get_id() + 1);\n";
197 out <<
" internal_insert_" << tname <<
"(id.get_record_id());\n";
198 out <<
" journal.insert_into(Table_Id(" << tid <<
"), id.get_record_id());\n";
206 out <<
" id_of_" << tname <<
" new_vector_of_" << tname <<
"(size_t size)\n";
208 out <<
" id_of_" << tname <<
" result(Record_Id(storage_of_" << tname;
209 out <<
".size()));\n";
210 out <<
" storage_of_" << tname <<
".resize(storage_of_";
211 out << tname <<
".size() + size);\n";
212 out <<
" internal_vector_insert_" << tname <<
"(result.get_record_id(), size);\n";
213 out <<
" journal.insert_vector(Table_Id(" << tid;
214 out <<
"), result.get_record_id(), size);\n";
215 out <<
" return result;\n";
223 if (!
db.get_fields(tid).empty())
225 out <<
" id_of_" << tname <<
" new_" << tname <<
'\n';
230 for (
const auto &[fid, fname]:
db.get_fields(tid))
237 const Type &type =
db.get_field_type(tid, fid);
239 out <<
" field_value_of_" << fname;
246 out <<
" auto result = new_" << tname <<
"();\n";
248 for (
const auto &[fid, fname]:
db.get_fields(tid))
251 out <<
" set_" << fname <<
"(result, ";
254 out <<
"field_value_of_" << fname;
260 out <<
" return result;\n";
272 out <<
" void delete_" << tname <<
"(id_of_" << tname <<
" record)\n";
274 out <<
" internal_delete_" << tname <<
"(record.get_record_id());\n";
275 out <<
" journal.delete_from(Table_Id(" << tid <<
"), record.get_record_id());\n";
278 out <<
" void delete_vector_of_" << tname <<
"(id_of_" << tname <<
" v, size_t size)\n";
280 out <<
" for (size_t i = size; i > 0;)\n";
281 out <<
" internal_delete_" << tname <<
"(v[--i].get_record_id());\n";
282 out <<
" journal.delete_vector(Table_Id(" << tid <<
"), v.get_record_id(), size);\n";
289 for (
const auto &[fid, fname]:
db.get_fields(tid))
291 const Type &type =
db.get_field_type(tid, fid);
296 out <<
" void set_" << fname;
297 out <<
"(id_of_" << tname <<
" record, ";
299 out <<
" field_value_of_" << fname <<
")\n";
301 out <<
" internal_update_" << tname <<
"__" << fname;
303 out <<
"(record.get_record_id(), ";
306 out <<
"field_value_of_" << fname;
310 out <<
" journal.update_";
312 out <<
"(Table_Id(" << tid <<
"), record.get_record_id(), Field_Id(" << fid <<
"), ";
314 out <<
"get_" << fname <<
"(record)";
316 out <<
"field_value_of_" << fname;
318 out <<
".get_record_id()";
322 if (single_row &&
options.is_unique_field_name(fname))
324 out <<
" void set_" << fname;
327 out <<
" field_value_of_" << fname <<
")\n";
329 out <<
" set_" << fname <<
"(the_" << tname;
333 out <<
"field_value_of_" << fname;
345 out <<
" template<typename F> void update_vector_of_" << fname;
346 out <<
"(id_of_" << tname <<
" record, size_t size, F f)\n";
348 out <<
" JOEDB_DEBUG_ASSERT(storage_of_" << tname <<
".freedom_keeper.is_used_vector(record.get_record_id(), size));\n";
349 out <<
" joedb::Span<";
351 out <<
"> span(&storage_of_" << tname;
352 out <<
".field_value_of_" << fname <<
".data()[record.get_id()], size);\n";
353 out <<
" f(span);\n";
354 out <<
" internal_update_vector_" << tname <<
"__" << fname <<
"(record.get_record_id(), size, span.begin());\n";
355 out <<
" journal.update_vector_" <<
get_type_string(type) <<
"(Table_Id(" << tid <<
"), record.get_record_id(), Field_Id(" << fid <<
"), size, ";
358 out <<
"reinterpret_cast<Record_Id *>";
360 out <<
"(span.begin()));\n";
Type_Id get_type_id() const
static const char * get_type_string(Type type)
const Compiler_Options & options
void write_type(std::ostream &out, Type type, Type_Usage usage)
bool db_has_values() const
Generator(std::string dir_name, std::string file_name, const Compiler_Options &options)
Writable_Database_h(const Compiler_Options &options)
void write(std::ostream &out) override
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 namespace_include_guard_open(std::ostream &out, const char *name, const std::vector< std::string > &n)
void namespace_include_guard_close(std::ostream &out)
One code generator for each of the file generated by joedbc.