Joedb 10.4.6
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 (
12 ):
13 Generator(".", "Readable.h", options)
14 {
15 }
16
17 ////////////////////////////////////////////////////////////////////////////
18 void Readable_h::write(std::ostream &out)
19 ////////////////////////////////////////////////////////////////////////////
20 {
21 const Database_Schema &db = options.get_db();
22 auto tables = db.get_tables();
23
24 namespace_include_guard_open(out, "Readable", options.get_name_space());
25
26 out << R"RRR(
27#include "Database_Writable.h"
28
29#include "joedb/interpreted/Database_Schema.h"
30#include "joedb/journal/Readonly_Memory_File.h"
31
32)RRR";
33
34 namespace_open(out, options.get_name_space());
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::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 if (table_has_field_type(tid, type_id))
92 {
93 out << "\n if (table_id == Table_Id{" << to_underlying(tid) << "})\n";
94 out << " {\n";
95
96 for (const auto &[fid, fname]: db.get_fields(tid))
97 {
98 const Type &type = db.get_field_type(tid, fid);
99
100 if (type.get_type_id() == type_id)
101 {
102 out << " if (field_id == Field_Id(" << fid << "))\n";
103 out << " {\n";
104 out << " return ";
105
106 if (type_id == joedb::Type::Type_Id::reference)
107 out << "*reinterpret_cast<const joedb::Record_Id *>(&";
108
109 out << "db.storage_of_" << tname << ".field_value_of_" << fname << "[to_underlying(record_id)]";
110
111 if (type_id == joedb::Type::Type_Id::reference)
112 out << ")";
113
114 out << ";\n";
115 out << " }\n";
116 }
117 }
118
119 out << " }\n";
120 }
121 }
122
123 out << R"RRR(
124 throw joedb::Exception("unknown field");
125 }
126)RRR";
127 }
128
129 out << " };\n\n";
130
131 for (const auto &[tid, tname]: tables)
132 {
133 out << " namespace " << tname << "_table\n";
134 out << " {\n";
135
136 out << " constexpr static Table_Id id{" << int(tid) << "};\n\n";
137
138 for (const auto &[fid, fname]: db.get_fields(tid))
139 {
140 out << " namespace " << fname << "_field\n";
141 out << " {\n";
142 out << " constexpr static Field_Id id{" << int(fid) << "};\n";
143 out << " }\n";
144 }
145
146 out << " }\n";
147 }
148
149 namespace_close(out, options.get_name_space());
151 }
152}
Type_Id get_type_id() const
Definition Type.h:41
@ type_ids
Definition Type.h:25
static const char * get_storage_type_string(Type type)
static const char * get_type_string(Type type)
const Compiler_Options & options
Definition Generator.h:16
bool table_has_field_type(Table_Id table_id, Type::Type_Id type_id) const
Definition Generator.cpp:23
Generator(std::string dir_name, std::string file_name, const Compiler_Options &options)
Readable_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.
Definition Client_h.cpp:5
constexpr index_t to_underlying(Record_Id id)
Definition index_types.h:59