Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Readable_h.cpp
Go to the documentation of this file.
3#include "joedb/ui/type_io.h"
4
5namespace joedb::generator
6{
7 ////////////////////////////////////////////////////////////////////////////
9 ////////////////////////////////////////////////////////////////////////////
10 (
11 const Compiler_Options &options
12 ):
13 Generator(".", "Readable.h", options)
14 {
15 }
16
17 ////////////////////////////////////////////////////////////////////////////
19 ////////////////////////////////////////////////////////////////////////////
20 {
21 const Database_Schema &db = options.get_db();
22 auto tables = db.get_tables();
23
25
26 out << R"RRR(
27#include "Database.h"
28
29#include "joedb/interpreted/Database_Schema.h"
30#include "joedb/journal/Readonly_Memory_File.h"
31
32)RRR";
33
35
36 out << R"RRR(
37 /// Implement the @ref joedb::Readable interface for a compiled database
38 ///
39 /// This allows using a compiled database with tools such as the
40 /// command interpreter.
41 class Readable: public joedb::Database_Schema
42 {
43 private:
44 const Database &db;
45
46 public:
47 Readable(const Database &db): db(db)
48 {
49 joedb::Readonly_Memory_File file(detail::schema_string, detail::schema_string_size);
50 joedb::Readonly_Journal journal(file);
51 journal.replay_log(*this);
52 }
53
54 const joedb::Compact_Freedom_Keeper &get_freedom
55 (
56 Table_Id table_id
57 ) const override
58 {
59)RRR";
60
61 for (const auto &[tid, tname]: tables)
62 {
63 out << " if (table_id == Table_Id{" << to_underlying(tid) << "})\n";
64 out << " return db.storage_of_" << tname << ".freedom_keeper;\n";
65 }
66
67 out << R"RRR(
68 throw joedb::Exception("unknown table_id");
69 }
70)RRR";
71
72
73 for (int type_index = 1; type_index < int(Type::type_ids); type_index++)
74 {
75 const auto type_id = Type::Type_Id(type_index);
76
77 out << "\n ";
78 out << "const " << get_storage_type_string(type_id) << '&';
79 out << " get_" << get_type_string(type_id);
80
81 out << R"RRR(
82 (
83 Table_Id table_id,
84 Record_Id record_id,
85 Field_Id field_id
86 ) const override
87 {)RRR";
88
89 for (const auto &[tid, tname]: tables)
90 {
91 bool has_typed_field = false;
92
93 for (const auto &[fid, fname]: db.get_fields(tid))
94 {
95 const Type &type = db.get_field_type(tid, fid);
96 if (type.get_type_id() == type_id)
97 {
98 has_typed_field = true;
99 break;
100 }
101 }
102
103 if (has_typed_field)
104 {
105 out << "\n if (table_id == Table_Id{" << to_underlying(tid) << "})\n";
106 out << " {\n";
107
108 for (const auto &[fid, fname]: db.get_fields(tid))
109 {
110 const Type &type = db.get_field_type(tid, fid);
111
112 if (type.get_type_id() == type_id)
113 {
114 out << " if (field_id == Field_Id(" << fid << "))\n";
115 out << " {\n";
116 out << " return ";
117
118 if (type_id == joedb::Type::Type_Id::reference)
119 out << "*reinterpret_cast<const joedb::Record_Id *>(&";
120
121 out << "db.storage_of_" << tname << ".field_value_of_" << fname << "[size_t(record_id) - 1]";
122
123 if (type_id == joedb::Type::Type_Id::reference)
124 out << ")";
125
126 out << ";\n";
127 out << " }\n";
128 }
129 }
130
131 out << " }\n";
132 }
133 }
134
135 out << R"RRR(
136 throw joedb::Exception("unknown field");
137 }
138)RRR";
139 }
140
141 out << " };\n";
142
143 for (const auto &[tid, tname]: tables)
144 {
145 out << "\n namespace interpreted_" << tname << '\n';
146 out << " {\n";
147 out << " constexpr static Table_Id table_id = Table_Id{" << int(tid) << "};\n";
148 for (const auto &[fid, fname]: db.get_fields(tid))
149 out << " constexpr static Field_Id " << fname << "_field_id = Field_Id{" << int(fid) << "};\n";
150 out << " }\n";
151 }
152
154 out << "\n#endif\n";
155 }
156}
const std::vector< std::string > & get_name_space() const
const Database & get_db() const
const std::map< Table_Id, std::string > & get_tables() const override
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
Type_Id get_type_id() const
Definition Type.h:40
@ type_ids
Definition Type.h:24
static const char * get_storage_type_string(Type type)
static const char * get_type_string(Type type)
const Compiler_Options & options
Definition Generator.h:14
Readable_h(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 namespace_include_guard(std::ostream &out, const char *name, const std::vector< std::string > &n)
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5
constexpr std::underlying_type< Table_Id >::type to_underlying(Table_Id id)
Definition index_types.h:15