Joedb 9.5.0
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
SQL_Dump_Writable.cpp
Go to the documentation of this file.
2#include "joedb/ui/type_io.h"
5
6#include <iostream>
7
8namespace joedb
9{
10 ////////////////////////////////////////////////////////////////////////////
11 void SQL_Writable::write_type(Type type)
12 ////////////////////////////////////////////////////////////////////////////
13 {
14 switch(type.get_type_id())
15 {
17 out << "NULL";
18 break;
19
20 case Type::Type_Id::string:
21 out << "TEXT";
22 break;
23
24 case Type::Type_Id::int32:
25 out << "INTEGER";
26 break;
27
28 case Type::Type_Id::int64:
29 out << "BIGINT";
30 break;
31
32 case Type::Type_Id::reference:
33 {
34 out << key_type << " REFERENCES ";
35 out << '\"' << schema.get_table_name(type.get_table_id()) << '\"';
36 }
37 break;
38
39 case Type::Type_Id::boolean:
40 out << "SMALLINT";
41 break;
42
43 case Type::Type_Id::float32:
44 out << "REAL";
45 break;
46
47 case Type::Type_Id::float64:
48 out << "REAL";
49 break;
50
51 case Type::Type_Id::int8:
52 out << "SMALLINT";
53 break;
54
55 case Type::Type_Id::int16:
56 out << "SMALLINT";
57 break;
58
59 case Type::Type_Id::blob:
60 out << "BLOB";
61 break;
62 }
63 }
64
65 ////////////////////////////////////////////////////////////////////////////
66 void SQL_Writable::start_writing(int64_t position)
67 ////////////////////////////////////////////////////////////////////////////
68 {
69 out << "BEGIN TRANSACTION; -- checkpoint = " << position << '\n';
70 }
71
72 ////////////////////////////////////////////////////////////////////////////
73 void SQL_Writable::soft_checkpoint_at(int64_t position)
74 ////////////////////////////////////////////////////////////////////////////
75 {
76 out << "COMMIT; --checkpoint = " << position << '\n';
77 }
78
79 ////////////////////////////////////////////////////////////////////////////
80 void SQL_Writable::create_table(const std::string &name)
81 ////////////////////////////////////////////////////////////////////////////
82 {
83 out << "CREATE TABLE \"" << name << "\"(" << id_field_name <<
84 ' ' << key_type << " PRIMARY KEY);\n";
85 }
86
87 ////////////////////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////////////////////
90 {
91 out << "DROP TABLE \"" << schema.get_table_name(table_id) << "\";\n";
92 }
93
94 ////////////////////////////////////////////////////////////////////////////
96 ////////////////////////////////////////////////////////////////////////////
97 (
98 Table_Id table_id,
99 const std::string &name
100 )
101 {
102 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
103 out << "\" RENAME TO \"" << name << "\";\n";
104 }
105
106 ////////////////////////////////////////////////////////////////////////////
108 ////////////////////////////////////////////////////////////////////////////
109 (
110 Table_Id table_id,
111 const std::string &name,
112 Type type
113 )
114 {
115 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
116 out << "\" ADD \"" << name << "\" ";
117 write_type(type);
118 out << ";\n";
119 }
120
121 ////////////////////////////////////////////////////////////////////////////
123 ////////////////////////////////////////////////////////////////////////////
124 {
125 if (!drop_column)
126 out << "-- ";
127 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
128 out << "\" DROP COLUMN \"" << schema.get_field_name(table_id, field_id) << "\";\n";
129 }
130
131 ////////////////////////////////////////////////////////////////////////////
133 ////////////////////////////////////////////////////////////////////////////
134 (
135 Table_Id table_id,
136 Field_Id field_id,
137 const std::string &name
138 )
139 {
140 out << "ALTER TABLE \"" << schema.get_table_name(table_id) << "\" RENAME COLUMN \"";
141 out << schema.get_field_name(table_id, field_id) << "\" TO \"" << name << "\";\n";
142 }
143
144 ////////////////////////////////////////////////////////////////////////////
145 void SQL_Writable::custom(const std::string &name)
146 ////////////////////////////////////////////////////////////////////////////
147 {
148 out << "-- custom: " << name << '\n';
149 }
150
151 ////////////////////////////////////////////////////////////////////////////
152 void SQL_Writable::comment(const std::string &comment)
153 ////////////////////////////////////////////////////////////////////////////
154 {
155 out << "-- " << comment << '\n';
156 }
157
158 ////////////////////////////////////////////////////////////////////////////
159 void SQL_Writable::timestamp(int64_t timestamp)
160 ////////////////////////////////////////////////////////////////////////////
161 {
162 out << "-- " << get_time_string(timestamp) << '\n';
163 }
164
165 ////////////////////////////////////////////////////////////////////////////
167 ////////////////////////////////////////////////////////////////////////////
168 {
169 out << "-- valid data\n";
170 }
171
172 ////////////////////////////////////////////////////////////////////////////
174 ////////////////////////////////////////////////////////////////////////////
175 {
176 out << "INSERT INTO \"" << schema.get_table_name(table_id);
177 out << "\"(" << id_field_name << ") VALUES(" << record_id << ");\n";
178 }
179
180 ////////////////////////////////////////////////////////////////////////////
182 ////////////////////////////////////////////////////////////////////////////
183 {
184 out << "DELETE FROM \"" << schema.get_table_name(table_id) << '"';
185 write_where(record_id);
186 }
187
188 ////////////////////////////////////////////////////////////////////////////
189 void SQL_Writable::write_update(Table_Id table_id, Field_Id field_id)
190 ////////////////////////////////////////////////////////////////////////////
191 {
192 out << "UPDATE \"" << schema.get_table_name(table_id);
193 out << "\" SET \"" << schema.get_field_name(table_id, field_id) << "\" = ";
194 }
195
196 ////////////////////////////////////////////////////////////////////////////
197 void SQL_Writable::write_where(Record_Id record_id)
198 ////////////////////////////////////////////////////////////////////////////
199 {
200 out << " WHERE " << id_field_name << " = " << record_id << ";\n";
201 }
202
203 #define TYPE_MACRO(type, return_type, type_id, R, W)\
204 void SQL_Writable::update_##type_id\
205 (\
206 Table_Id table_id,\
207 Record_Id record_id,\
208 Field_Id field_id,\
209 return_type value\
210 )\
211 {\
212 write_update(table_id, field_id);\
213 write_##type_id(out, value);\
214 write_where(record_id);\
215 }
216 #define TYPE_MACRO_NO_STRING
217 #define TYPE_MACRO_NO_BLOB
218 #define TYPE_MACRO_NO_REFERENCE
219 #define TYPE_MACRO_NO_BOOL
220 #include "joedb/TYPE_MACRO.h"
221
222 ////////////////////////////////////////////////////////////////////////////
223 void SQL_Writable::update_boolean
224 ////////////////////////////////////////////////////////////////////////////
225 (
226 Table_Id table_id,
227 Record_Id record_id,
228 Field_Id field_id,
229 const bool value)
230 {
231 write_update(table_id, field_id);
232 out << value;
233 write_where(record_id);
234 }
235
236 ////////////////////////////////////////////////////////////////////////////
237 void SQL_Writable::update_string
238 ////////////////////////////////////////////////////////////////////////////
239 (
240 Table_Id table_id,
241 Record_Id record_id,
242 Field_Id field_id,
243 const std::string &value)
244 {
245 write_update(table_id, field_id);
246 write_sql_string(out, value);
247 write_where(record_id);
248 }
249
250 ////////////////////////////////////////////////////////////////////////////
251 void SQL_Writable::update_blob
252 ////////////////////////////////////////////////////////////////////////////
253 (
254 Table_Id table_id,
255 Record_Id record_id,
256 Field_Id field_id,
257 Blob value)
258 {
259 if (!blob_reader)
260 out << "-- ";
261
262 write_update(table_id, field_id);
263
264 if (blob_reader)
265 write_sql_string(out, blob_reader->read_blob(value));
266 else
267 out << "\"BLOB\"";
268
269 write_where(record_id);
270 }
271
272 ////////////////////////////////////////////////////////////////////////////
273 void SQL_Writable::update_reference
274 ////////////////////////////////////////////////////////////////////////////
275 (
276 Table_Id table_id,
277 Record_Id record_id,
278 Field_Id field_id,
279 Record_Id value
280 )
281 {
282 write_update(table_id, field_id);
283
284 if (value == Record_Id(0))
285 out << "NULL";
286 else
287 out << value;
288
289 write_where(record_id);
290 }
291
292 ////////////////////////////////////////////////////////////////////////////
293 SQL_Writable::~SQL_Writable() = default;
294 ////////////////////////////////////////////////////////////////////////////
295}
const std::string & get_field_name(Table_Id table_id, Field_Id field_id) const
Definition Readable.cpp:54
const std::string & get_table_name(Table_Id table_id) const
Definition Readable.cpp:38
void drop_table(Table_Id table_id) final
void insert_into(Table_Id table_id, Record_Id record_id) final
void custom(const std::string &name) final
void rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) final
void timestamp(int64_t timestamp) final
void delete_from(Table_Id table_id, Record_Id record_id) final
void drop_field(Table_Id table_id, Field_Id field_id) final
void add_field(Table_Id table_id, const std::string &name, Type type) final
void soft_checkpoint_at(int64_t position)
void comment(const std::string &comment) final
void rename_table(Table_Id table_id, const std::string &name) final
void start_writing(int64_t position)
void create_table(const std::string &name) final
std::string get_time_string(int64_t timestamp)
void write_sql_string(std::ostream &out, const std::string &s)
Definition type_io.cpp:90
Definition Blob.h:7