Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Generator.cpp
Go to the documentation of this file.
2#include "joedb/get_version.h"
4
5#include <filesystem>
6
7namespace joedb::generator
8{
9 ////////////////////////////////////////////////////////////////////////////
11 ////////////////////////////////////////////////////////////////////////////
12 {
13 for (const auto &[tid, tname]: options.db.get_tables())
14 {
16 return true;
17 }
18 return false;
19 }
20
21 ////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////
24 {
25 out << "/////////////////////////////////////////////////////////////////////////////\n";
26 out << "//\n";
27 out << "// This code was automatically generated by the joedb compiler\n";
28 out << "// https://www.joedb.org/\n";
29 out << "//\n";
30 out << "// Path to compiler: " << options.exe_path << '\n';
31 out << "// Version: " << joedb::get_version() << '\n';
32 out << "// joedbc compilation time: " << __DATE__ << ' ' << __TIME__ << '\n';
33 out << "// Generation of this file: " << get_time_string_of_now() << '\n';
34 out << "//\n";
35 out << "/////////////////////////////////////////////////////////////////////////////\n";
36 }
37
38 ////////////////////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////////////////////
41 (
42 Type type,
43 bool return_type,
44 bool setter_type
45 )
46 {
47 switch (type.get_type_id())
48 {
50 out << "void";
51 break;
52
53 case Type::Type_Id::reference:
54 out << "id_of_" << options.db.get_table_name(type.get_table_id());
55 break;
56
57 #define TYPE_MACRO(storage_tt, return_tt, type_id, read, write)\
58 case Type::Type_Id::type_id:\
59 if (return_type || setter_type)\
60 out << #return_tt;\
61 else\
62 out << #storage_tt;\
63 break;
64 #define TYPE_MACRO_NO_REFERENCE
65 #include "joedb/TYPE_MACRO.h"
66 }
67 }
68
69 ////////////////////////////////////////////////////////////////////////////
70 void Generator::write_tuple_type(const Compiler_Options::Index &index, bool reference)
71 ////////////////////////////////////////////////////////////////////////////
72 {
73 if (index.field_ids.size() > 1)
74 {
75 out << "std::tuple<";
76
77 for (size_t i = 0; i < index.field_ids.size(); i++)
78 {
79 if (i > 0)
80 out << ", ";
81
82 const Type &type = options.db.get_field_type
83 (
84 index.table_id,
85 index.field_ids[i]
86 );
87
88 write_type(type, false, false);
89 }
90
91 out << ">";
92 }
93 else
94 {
95 if (!reference)
96 {
98 (
100 false,
101 false
102 );
103 }
104 }
105 }
106
107 ////////////////////////////////////////////////////////////////////////////
109 ////////////////////////////////////////////////////////////////////////////
110 {
111 out << "std::";
112 if (index.unique)
113 out << "map";
114 else
115 out << "multimap";
116 out << '<';
117
118 write_tuple_type(index, false);
119
120 out << ", id_of_" << options.db.get_table_name(index.table_id) << ">";
121 }
122
123 ////////////////////////////////////////////////////////////////////////////
124 // type arrays
125 ////////////////////////////////////////////////////////////////////////////
126 #define STRINGIFY(X) #X
127 #define EXPAND_AND_STRINGIFY(X) STRINGIFY(X)
128
129 ////////////////////////////////////////////////////////////////////////////
131 ////////////////////////////////////////////////////////////////////////////
132 {
133 static constexpr char const * const types[] =
134 {
135 nullptr,
136 #define TYPE_MACRO(a, b, type_id, d, e) EXPAND_AND_STRINGIFY(type_id),
137 #include "joedb/TYPE_MACRO.h"
138 };
139
140 return types[int(type.get_type_id())];
141 }
142
143 ////////////////////////////////////////////////////////////////////////////
145 ////////////////////////////////////////////////////////////////////////////
146 {
147 static char const * const cpp_types[] =
148 {
149 nullptr,
150 #define TYPE_MACRO(a, type, c, d, e) EXPAND_AND_STRINGIFY(type),
151 #include "joedb/TYPE_MACRO.h"
152 };
153
154 return cpp_types[int(type.get_type_id())];
155 }
156
157 ////////////////////////////////////////////////////////////////////////////
159 ////////////////////////////////////////////////////////////////////////////
160 {
161 static char const * const storage_types[] =
162 {
163 nullptr,
164 #define TYPE_MACRO(storage, b, c, d, e) EXPAND_AND_STRINGIFY(storage),
165 #include "joedb/TYPE_MACRO.h"
166 };
167
168 return storage_types[int(type.get_type_id())];
169 }
170
171 #undef EXPAND_AND_STRINGIFY
172 #undef STRINGIFY
173
174 ////////////////////////////////////////////////////////////////////////////
176 ////////////////////////////////////////////////////////////////////////////
177 (
178 const char *dir_name,
179 const char *file_name
180 )
181 {
182 const std::string dir_string =
183 options.output_path + "/" +
184 options.get_name_space_back() + "/" +
185 std::string(dir_name);
186
187 std::filesystem::create_directories(dir_string);
188
189 return dir_string + "/" + std::string(file_name);
190 }
191
192 ////////////////////////////////////////////////////////////////////////////
194 ////////////////////////////////////////////////////////////////////////////
195 (
196 const char *dir_name,
197 const char *file_name,
198 const Compiler_Options &options
199 ):
200 options(options),
201 out(get_file_string(dir_name, file_name), Open_Mode::truncate)
202 {
204 }
205
206 ////////////////////////////////////////////////////////////////////////////
207 Generator::~Generator() = default;
208 ////////////////////////////////////////////////////////////////////////////
209}
const Freedom_Keeper & get_freedom(Table_Id table_id) const override
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
Record_Id get_used_count() const
const std::string & get_table_name(Table_Id table_id) const
Definition Readable.cpp:38
Type_Id get_type_id() const
Definition Type.h:41
void write_index_type(const Compiler_Options::Index &index)
static const char * get_storage_type_string(Type type)
static const char * get_type_string(Type type)
std::string get_file_string(const char *dir_name, const char *file_name)
void write_type(Type type, bool return_type, bool setter_type)
Definition Generator.cpp:41
static const char * get_cpp_type_string(Type type)
const Compiler_Options & options
Definition Generator.h:13
void write_tuple_type(const Compiler_Options::Index &index, bool reference)
Definition Generator.cpp:70
Generator(const char *dir_name, const char *file_name, const Compiler_Options &options)
constexpr const char * get_version()
Definition get_version.h:7
Open_Mode
Definition Open_Mode.h:8
@ truncate
create new file, or truncate existing file, and locks the file
std::string get_time_string_of_now()
One code generator for each of the file generated by joedbc.
Definition Client_h.cpp:5
std::vector< Field_Id > field_ids