Joedb 10.0.1
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::end_writing(int64_t position)
74 ////////////////////////////////////////////////////////////////////////////
75 {
76 out << "COMMIT; --checkpoint = " << position << '\n';
77 out.flush();
78 }
79
80 ////////////////////////////////////////////////////////////////////////////
81 void SQL_Writable::create_table(const std::string &name)
82 ////////////////////////////////////////////////////////////////////////////
83 {
84 out << "CREATE TABLE \"" << name << "\"(" << id_field_name <<
85 ' ' << key_type << " PRIMARY KEY);\n";
86 }
87
88 ////////////////////////////////////////////////////////////////////////////
90 ////////////////////////////////////////////////////////////////////////////
91 {
92 out << "DROP TABLE \"" << schema.get_table_name(table_id) << "\";\n";
93 }
94
95 ////////////////////////////////////////////////////////////////////////////
97 ////////////////////////////////////////////////////////////////////////////
98 (
99 Table_Id table_id,
100 const std::string &name
101 )
102 {
103 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
104 out << "\" RENAME TO \"" << name << "\";\n";
105 }
106
107 ////////////////////////////////////////////////////////////////////////////
109 ////////////////////////////////////////////////////////////////////////////
110 (
111 Table_Id table_id,
112 const std::string &name,
113 Type type
114 )
115 {
116 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
117 out << "\" ADD \"" << name << "\" ";
118 write_type(type);
119 out << ";\n";
120 }
121
122 ////////////////////////////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////////////////////
125 {
126 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
127 out << "\" DROP COLUMN \"" << schema.get_field_name(table_id, field_id) << "\";\n";
128 }
129
130 ////////////////////////////////////////////////////////////////////////////
132 ////////////////////////////////////////////////////////////////////////////
133 (
134 Table_Id table_id,
135 Field_Id field_id,
136 const std::string &name
137 )
138 {
139 out << "ALTER TABLE \"" << schema.get_table_name(table_id) << "\" RENAME COLUMN \"";
140 out << schema.get_field_name(table_id, field_id) << "\" TO \"" << name << "\";\n";
141 }
142
143 ////////////////////////////////////////////////////////////////////////////
144 void SQL_Writable::custom(const std::string &name)
145 ////////////////////////////////////////////////////////////////////////////
146 {
147 out << "-- custom: " << name << '\n';
148 }
149
150 ////////////////////////////////////////////////////////////////////////////
151 void SQL_Writable::comment(const std::string &comment)
152 ////////////////////////////////////////////////////////////////////////////
153 {
154 out << "-- " << comment << '\n';
155 }
156
157 ////////////////////////////////////////////////////////////////////////////
158 void SQL_Writable::timestamp(int64_t timestamp)
159 ////////////////////////////////////////////////////////////////////////////
160 {
161 out << "-- " << get_time_string(timestamp) << '\n';
162 }
163
164 ////////////////////////////////////////////////////////////////////////////
166 ////////////////////////////////////////////////////////////////////////////
167 {
168 out << "-- valid data\n";
169 }
170
171 ////////////////////////////////////////////////////////////////////////////
173 ////////////////////////////////////////////////////////////////////////////
174 {
175 out << "INSERT INTO \"" << schema.get_table_name(table_id);
176 out << "\"(" << id_field_name << ") VALUES(" << record_id << ");\n";
177 }
178
179 ////////////////////////////////////////////////////////////////////////////
181 ////////////////////////////////////////////////////////////////////////////
182 {
183 out << "DELETE FROM \"" << schema.get_table_name(table_id) << '"';
184 write_where(record_id);
185 }
186
187 ////////////////////////////////////////////////////////////////////////////
188 void SQL_Writable::write_update(Table_Id table_id, Field_Id field_id)
189 ////////////////////////////////////////////////////////////////////////////
190 {
191 out << "UPDATE \"" << schema.get_table_name(table_id);
192 out << "\" SET \"" << schema.get_field_name(table_id, field_id) << "\" = ";
193 }
194
195 ////////////////////////////////////////////////////////////////////////////
196 void SQL_Writable::write_where(Record_Id record_id)
197 ////////////////////////////////////////////////////////////////////////////
198 {
199 out << " WHERE " << id_field_name << " = " << record_id << ";\n";
200 }
201
202 #define TYPE_MACRO(type, return_type, type_id, R, W)\
203 void SQL_Writable::update_##type_id\
204 (\
205 Table_Id table_id,\
206 Record_Id record_id,\
207 Field_Id field_id,\
208 return_type value\
209 )\
210 {\
211 write_update(table_id, field_id);\
212 write_##type_id(out, value);\
213 write_where(record_id);\
214 }
215 #define TYPE_MACRO_NO_STRING
216 #define TYPE_MACRO_NO_BLOB
217 #define TYPE_MACRO_NO_REFERENCE
218 #define TYPE_MACRO_NO_BOOL
219 #include "joedb/TYPE_MACRO.h"
220
221 ////////////////////////////////////////////////////////////////////////////
222 void SQL_Writable::update_boolean
223 ////////////////////////////////////////////////////////////////////////////
224 (
225 Table_Id table_id,
226 Record_Id record_id,
227 Field_Id field_id,
228 const bool value)
229 {
230 write_update(table_id, field_id);
231 out << value;
232 write_where(record_id);
233 }
234
235 ////////////////////////////////////////////////////////////////////////////
236 void SQL_Writable::update_string
237 ////////////////////////////////////////////////////////////////////////////
238 (
239 Table_Id table_id,
240 Record_Id record_id,
241 Field_Id field_id,
242 const std::string &value)
243 {
244 write_update(table_id, field_id);
245 write_sql_string(out, value);
246 write_where(record_id);
247 }
248
249 ////////////////////////////////////////////////////////////////////////////
250 void SQL_Writable::update_blob
251 ////////////////////////////////////////////////////////////////////////////
252 (
253 Table_Id table_id,
254 Record_Id record_id,
255 Field_Id field_id,
256 Blob value)
257 {
258 if (!blob_reader)
259 out << "-- ";
260
261 write_update(table_id, field_id);
262
263 if (blob_reader)
264 write_sql_string(out, blob_reader->read_blob(value));
265 else
266 out << "\"BLOB\"";
267
268 write_where(record_id);
269 }
270
271 ////////////////////////////////////////////////////////////////////////////
272 void SQL_Writable::update_reference
273 ////////////////////////////////////////////////////////////////////////////
274 (
275 Table_Id table_id,
276 Record_Id record_id,
277 Field_Id field_id,
278 Record_Id value
279 )
280 {
281 write_update(table_id, field_id);
282
283 if (value.is_null())
284 out << "NULL";
285 else
286 out << value;
287
288 write_where(record_id);
289 }
290
291 ////////////////////////////////////////////////////////////////////////////
292 SQL_Writable::~SQL_Writable() = default;
293 ////////////////////////////////////////////////////////////////////////////
294}
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 rename_field(Table_Id table_id, Field_Id field_id, const std::string &name) override
void add_field(Table_Id table_id, const std::string &name, Type type) override
void delete_from(Table_Id table_id, Record_Id record_id) override
void timestamp(int64_t timestamp) override
void custom(const std::string &name) override
void rename_table(Table_Id table_id, const std::string &name) override
void drop_field(Table_Id table_id, Field_Id field_id) override
void comment(const std::string &comment) override
void end_writing(int64_t position) override
void drop_table(Table_Id table_id) override
void insert_into(Table_Id table_id, Record_Id record_id) override
void create_table(const std::string &name) override
void start_writing(int64_t position) override
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