-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsqlite3-pp.cpp
457 lines (419 loc) · 10.5 KB
/
sqlite3-pp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <string.h>
#include <assert.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
static inline bool asc_isspace(unsigned char c)
{
return c==' ' || c=='\n' || c=='\r' || c=='\t' || c=='\f' || c=='\v';
}
static inline bool asc_isdigit(unsigned char c)
{
return '0' <= c && c <= '9';
}
static inline bool asc_isalpha(unsigned char c)
{
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
}
struct SubStr {
char * begin;
char * end;
size_t size() const {return end-begin;}
explicit operator bool() const {return begin != end;}
};
ostream & operator << (ostream & o, SubStr str) {
o << string(str.begin,str.end);
return o;
}
static inline bool operator==(SubStr x, const char * y) {
return strncmp(x.begin,y,x.size()) == 0 && y[x.size()] == '\0';
}
static inline bool eq_icase(const string & x, const char * y) {
return strcasecmp(x.c_str(), y) == 0;
}
struct Main {
int main() {
char * data;
char * end;
{
// FIXME: Use a better method that doesn't involve unnecessary copying
std::stringstream buffer;
buffer << cin.rdbuf();
auto file = buffer.str();
// for convince make sure the string has _two_ null characters
file += '\0';
file += '\0';
data = (char *)malloc(file.size());
memcpy(data, file.data(), file.size());
end = data + file.size();
}
char * p = data;
while (p < end) {
p = skip_whitespace(p);
if (*p == '"') {
p = get_quote(p, '"').end;
} else if (*p == '\'') {
p = get_quote(p, '\'').end;
} else if (asc_isalpha(*p)) {
auto id = get_id(p);
if (id == "SQL") {
p = handle_sql(p, "SQL");
} else if (id == "EXEC") {
p = handle_sql(p, "EXEC");
} else if (id == "SELECT") {
p = handle_sql(p, "SELECT");
} else {
p = id.end;
}
} else {
++p;
}
}
ofstream("syncfs-gen.cpp")
<< "#line 1 \"syncfs.cpp\"\n"
<< data;
return 0;
}
char * skip_whitespace(char * p) {
again:
if (asc_isspace(*p)) {++p; goto again;}
if (p[0] == '/' && p[1] == '/') {p += 2; while (*p && *p != '\n') ++p; goto again;}
if (p[0] == '/' && p[1] == '*') {p += 2; while (*p && !(p[0] == '*' && p[1] == '/')) ++p; goto again;}
if (p[0] == '#') {p += 1; while (*p && *p != '\n') ++p; goto again;}
return p;
}
SubStr get_quote(char * p, char q) {
char * begin = p;
++p;
while (*p && p[0] != q) {
if (p[0] == '\\') p += 2;
else p += 1;
}
expect(p, q);
++p;
return {begin, p};
}
SubStr get_id(char * p) {
char * begin = p;
while (asc_isalpha(*p) || asc_isdigit(*p)) ++p;
return {begin, p};
}
char * handle_sql(char * p, const char * what) {
vector<SubStr> parts;
char * begin = p;
p += strlen(what);
p = skip_whitespace(p);
expect(p, '(');
++p;
p = skip_whitespace(p);
size_t query_len = 0;
while (*p && *p != ')') {
expect(p, '"');
auto res = get_quote(p, '"');
p = res.end;
res.begin++;
res.end--;
query_len += res.size();
parts.push_back(res);
p = skip_whitespace(p);
}
expect(p, ')');
++p;
char * end = p;
proc_sql({begin, end}, std::move(parts), query_len, what);
return p;
}
void expect(char * p, char c) {
// FIXME: Proper error message
assert(*p == c);
}
void proc_sql(SubStr, vector<SubStr>&&, size_t, const char * what);
};
int sql_num = 0;
struct ProcSql {
SubStr orig_text;
vector<SubStr> parts;
vector<SubStr>::iterator itr;
char * p;
string query;
int splice_count;
struct Column {
string name;
string type;
bool last;
};
vector<Column> columns;
const char * what;
ProcSql(SubStr orig, vector<SubStr> && pts, size_t sz, const char * w)
: orig_text(orig), parts(std::move(pts)), splice_count(0), what(w)
{
itr = parts.begin();
p = itr->begin;
query.reserve(sz);
}
void main() {
clear_orig();
string id;
try_id(id);
bool what_eq_select = strcmp(what, "SELECT")==0;
bool id_eq_select = eq_icase(id, "select");
if (what_eq_select || id_eq_select) {
if (id_eq_select) get_id(id);
else query += "select ";
proc_select();
proc_rest();
} else {
get_id(id);
proc_rest();
}
cout << what << " ";
if (splice_count > 0)
cout << "$=" << splice_count << ' ';
cout << query << "\n";
cout << "@ ";
for (auto & col : columns) {
if (col.name.empty())
cout << "-";
else
cout << col.name;
if (!col.type.empty())
cout << '`' << col.type;
cout << ' ';
}
cout << "\n";
p = orig_text.begin;
char buf[16];
auto res = snprintf(buf, 16, "SqL%d(", sql_num++);
//int newlines = 0
for (int i = 0; i < res; ++i) {
assert(asc_isspace(p[i]));
//if (p[i] == '\n') newlines++;
p[i] = buf[i];
}
orig_text.end[-1] = ')';
}
struct Token {
enum Type {OTHER, ID, AS, TYPE, COMMA, FROM} type;
string id;
};
Token get_token() {
Token ret {Token::OTHER};
auto & id = ret.id;
if (asc_isalpha(*p) || *p == '*') {
get_id(id);
if (eq_icase(id, "as")) {
if (!asc_isalpha(*p)) return ret;
get_id(id);
ret.type = Token::AS;
} else if (eq_icase(id, "from")) {
ret.type = Token::FROM;
} else {
// skip over table names
while (p != NULL && *p == '.') {
adv(); skipspace();
if (!(asc_isalpha(*p) || *p == '*')) return ret;
get_id(id);
}
ret.type = Token::ID;
}
} else if (*p == ',') {
adv(); skipspace();
ret.type = Token::COMMA;
} else if (*p == '`') {
adv(false); skipspace(false);
if (!asc_isalpha(*p)) return ret;
get_id(ret.id, false);
ret.type = Token::TYPE;
} else if (proc_special()) {
/* nothing to do */
} else {
adv(); skipspace();
}
return ret;
}
Column get_column() {
string id;
string type;
while (p != NULL) {
auto tok = get_token();
switch (tok.type) {
case Token::OTHER:
id.clear();
type.clear();
break;
case Token::ID:
case Token::AS:
id = tok.id;
break;
case Token::TYPE:
type = tok.id;
break;
case Token::COMMA:
return {id, type, false};
case Token::FROM:
return {id, type, true};
}
}
return {id, type, true};
}
void proc_select() {
while (p != NULL) {
auto res = get_column();
columns.push_back(res);
if (res.last) break;
}
}
void proc_rest() {
while (p != NULL) {
if (proc_special()) {
/* nothing to do */
} else {
adv();
}
}
}
bool proc_special() {
if (*p == '(') proc_group(')');
else if (*p == '\'') proc_single_quote();
else if (*p == '$') proc_dollar();
else return false;
return true;
}
void skipspace(bool output = true) {
again:
if (p == NULL) return;
if (asc_isspace(*p)) {adv(output); goto again;}
if (p[0] == '-' && p[1] == '-') {adv(output); adv(output); while (p != NULL && *p != '\n') adv(output); goto again;}
if (p[0] == '/' && p[1] == '*') {adv(output); adv(output); while (p != NULL && !(p[0] == '*' && p[1] == '/')) adv(output); goto again;}
}
void proc_group(char close) {
adv();
while (p != NULL && *p != close) {
skipspace();
bool res = proc_special();
if (!res) adv();
}
skipspace();
}
void proc_single_quote() {
adv();
while (p != NULL && *p != '\'')
adv();
adv();
skipspace();
}
void clear_orig() {
blank(orig_text.begin, parts[0].begin);
for (unsigned i = 0; i + 1 < parts.size(); ++i)
blank(parts[i].end, parts[i+1].begin);
blank(parts.back().end, orig_text.end);
}
void blank(char * b, char * e) {
assert(b <= e);
while (b < e) {
if (!asc_isspace(*b)) *b = ' ';
++b;
}
}
void try_id(string & res) {
char * begin = this->p;
char * p = this->p;
while (p && (asc_isalpha(*p) || asc_isdigit(*p))) ++p;
res.assign(begin,p);
}
void get_id(string & res, bool output = true) {
char * begin = p;
if (*p == '*') {
++p;
} else {
while (asc_isalpha(*p) || asc_isdigit(*p)) ++p;
}
if (output) query.append(begin, p);
res.assign(begin,p);
blank(begin, p);
adv0();
skipspace();
}
void adv0() {
if (p == itr->end) {
itr++;
if (itr != parts.end()) {
query.append("\\n");
p = itr->begin;
} else {
p = NULL;
}
}
}
void adv(bool output = true) {
if (output) query += *p;
*p = ' ';
++p;
adv0();
}
void proc_dollar() {
query += '?';
if (splice_count > 0)
*p = ',';
else
*p = ' ';
splice_count++;
++p;
for (;;) {
if (asc_isalpha(*p) || asc_isdigit(*p)) ++p;
else if (*p == '.') ++p;
else if (p[0] == ':' && p[1] == ':') p += 2;
else if (p[0] == '-' && p[1] == '>') p += 2;
else break;
}
adv0();
}
};
void Main::proc_sql(SubStr orig, vector<SubStr>&& p, size_t sz, const char * what) {
ProcSql proc_sql(orig,std::move(p), sz, what);
proc_sql.main();
}
// const char * unescape_char(const char * s, const char * end, string & out) {
// ++s;
// if (s == end) throw error(si, s, "Unexpected EOS when parsing escape sequence.");
// switch (*s) {
// case 'a': out += '\a'; return s + 1;
// case 'b': out += '\b'; return s + 1;
// case 'f': out += '\f'; return s + 1;
// case 'n': out += '\n'; return s + 1;
// case 'r': out += '\r'; return s + 1;
// case 't': out += '\t'; return s + 1;
// case 'v': out += '\v'; return s + 1;
// case 'x': {
// // hex
// ++s;
// char * e = (char *)s;
// unsigned val = strtol(s, &e, 16);
// if (s == e) throw error(SourceStr(si, s-2, s), "Invalid hex escape sequence.");
// if (val > 255) throw error(SourceStr(si, s, e), "Value %u is not between 0-255 in hex escape sequence.", val);
// s = e;
// out += (char)val;
// return s;
// }
// case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
// // oct
// const char * b = s;
// unsigned val = *s - '0'; ++s;
// if (s != end && '0' <= *s && *s <= '7') {val *= 8; val += *s - '0'; ++s;}
// if (s != end && '0' <= *s && *s <= '7') {val *= 8; val += *s - '0'; ++s;}
// if (val > 255) throw error(SourceStr(si, b, s), "Value %u is not between 0-255 in octal escape sequence.", val);
// out += (char)val;
// return s;
// }
// default:
// out += *s;
// return s + 1;
// }
// }
int main() {
Main main_c;
return main_c.main();
};