Joedb 9.1.4
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::create_table(const std::string &name)
67 ////////////////////////////////////////////////////////////////////////////
68 {
69 out << "CREATE TABLE \"" << name << "\"(" << id_field_name <<
70 ' ' << key_type << " PRIMARY KEY);\n";
71 }
72
73 ////////////////////////////////////////////////////////////////////////////
75 ////////////////////////////////////////////////////////////////////////////
76 {
77 out << "DROP TABLE \"" << schema.get_table_name(table_id) << "\";\n";
78 }
79
80 ////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////
83 (
84 Table_Id table_id,
85 const std::string &name
86 )
87 {
88 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
89 out << "\" RENAME TO \"" << name << "\";\n";
90 }
91
92 ////////////////////////////////////////////////////////////////////////////
94 ////////////////////////////////////////////////////////////////////////////
95 (
96 Table_Id table_id,
97 const std::string &name,
98 Type type
99 )
100 {
101 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
102 out << "\" ADD \"" << name << "\" ";
103 write_type(type);
104 out << ";\n";
105 }
106
107 ////////////////////////////////////////////////////////////////////////////
109 ////////////////////////////////////////////////////////////////////////////
110 {
111 if (!drop_column)
112 out << "-- ";
113 out << "ALTER TABLE \"" << schema.get_table_name(table_id);
114 out << "\" DROP COLUMN \"" << schema.get_field_name(table_id, field_id) << "\";\n";
115 }
116
117 ////////////////////////////////////////////////////////////////////////////
119 ////////////////////////////////////////////////////////////////////////////
120 (
121 Table_Id table_id,
122 Field_Id field_id,
123 const std::string &name
124 )
125 {
126 out << "ALTER TABLE \"" << schema.get_table_name(table_id) << "\" RENAME COLUMN \"";
127 out << schema.get_field_name(table_id, field_id) << "\" TO \"" << name << "\";\n";
128 }
129
130 ////////////////////////////////////////////////////////////////////////////
131 void SQL_Writable::custom(const std::string &name)
132 ////////////////////////////////////////////////////////////////////////////
133 {
134 out << "-- custom: " << name << '\n';
135 }
136
137 ////////////////////////////////////////////////////////////////////////////
138 void SQL_Writable::comment(const std::string &comment)
139 ////////////////////////////////////////////////////////////////////////////
140 {
141 out << "-- " << comment << '\n';
142 }
143
144 ////////////////////////////////////////////////////////////////////////////
145 void SQL_Writable::timestamp(int64_t timestamp)
146 ////////////////////////////////////////////////////////////////////////////
147 {
148 out << "-- " << get_time_string(timestamp) << '\n';
149 }
150
151 ////////////////////////////////////////////////////////////////////////////
153 ////////////////////////////////////////////////////////////////////////////
154 {
155 out << "-- valid data\n";
156 }
157
158 ////////////////////////////////////////////////////////////////////////////
160 ////////////////////////////////////////////////////////////////////////////
161 {
162 out << "INSERT INTO \"" << schema.get_table_name(table_id);
163 out << "\"(" << id_field_name << ") VALUES(" << record_id << ");\n";
164 }
165
166 ////////////////////////////////////////////////////////////////////////////
168 ////////////////////////////////////////////////////////////////////////////
169 (
170 Table_Id table_id,
171 Record_Id record_id,
172 size_t size
173 )
174 {
175 for (size_t i = 0; i < size; i++)
176 insert_into(table_id, record_id + i);
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_data(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 == Record_Id(0))
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 drop_table(Table_Id table_id) final
void insert_into(Table_Id table_id, Record_Id record_id) final
void insert_vector(Table_Id table_id, Record_Id record_id, size_t size) 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 comment(const std::string &comment) final
void rename_table(Table_Id table_id, const std::string &name) final
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