- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathpreparse.cpp
390 lines (320 loc) · 8.32 KB
/
preparse.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
/*
* PROGRAM: Dynamic SQL runtime support
* MODULE: preparse.cpp
* DESCRIPTION: Dynamic SQL pre parser / parser on client side.
* This module will probably change to a YACC parser.
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include"firebird.h"
#include<stdlib.h>
#include<string.h>
#include"../yvalve/prepa_proto.h"
#include"../yvalve/gds_proto.h"
#include"../yvalve/YObjects.h"
#include"../common/classes/ClumpletWriter.h"
#include"../common/StatusArg.h"
#include"../common/Tokens.h"
#include<firebird/Interface.h>
enum pp_vals {
PP_CREATE = 0,
PP_DATABASE = 1,
PP_SCHEMA = 2,
PP_PAGE_SIZE = 3,
PP_USER = 4,
PP_PASSWORD = 5,
PP_PAGESIZE = 6,
PP_LENGTH = 7,
PP_PAGES = 8,
PP_PAGE = 9,
PP_SET = 10,
PP_NAMES = 11,
PP_ROLE = 12
};
staticvoidgenerate_error(const Firebird::NoCaseString&, SSHORT, char = 0);
structpp_table
{
SCHAR symbol[10];
SSHORT code;
};
// This should be kept in sync with the rule db_initial_desc of parse.y for CREATE DATABASE.
// Should delete SCHEMA in the future.
staticconst pp_table pp_symbols[] =
{
{"CREATE", PP_CREATE},
{"DATABASE", PP_DATABASE},
{"SCHEMA", PP_SCHEMA},
{"PAGE_SIZE", PP_PAGE_SIZE},
{"USER", PP_USER},
{"PASSWORD", PP_PASSWORD},
{"PAGESIZE", PP_PAGESIZE},
{"LENGTH", PP_LENGTH},
{"PAGES", PP_PAGES},
{"PAGE", PP_PAGE},
{"SET", PP_SET},
{"NAMES", PP_NAMES},
{"ROLE", PP_ROLE},
{"", 0}
};
// define the tokens
enum token_vals {
NO_MORE_TOKENS = -1,
TOKEN_TOO_LONG = -2,
UNEXPECTED_END_OF_COMMAND = -3,
UNEXPECTED_TOKEN = -4,
STRING = 257,
NUMERIC = 258,
SYMBOL = 259
};
staticconstchar* quotes = "\"\'";
usingnamespaceFirebird;
static NoCaseString getToken(unsigned& pos, const Tokens& toks, int symbol = SYMBOL)
{
if (pos >= toks.getCount())
generate_error("", UNEXPECTED_END_OF_COMMAND);
NoCaseString curTok(NoCaseString(toks[pos].text, toks[pos].length));
switch(symbol)
{
case SYMBOL:
break;
case STRING:
if (!strchr(quotes, toks[pos].text[0]))
generate_error(curTok, UNEXPECTED_TOKEN);
return toks[pos++].stripped().ToNoCaseString();
case NUMERIC:
{
constchar* const end = &toks[pos].text[toks[pos].length];
for (constchar* ptr = toks[pos].text; ptr < end; ++ptr)
{
if (*ptr < '0' || *ptr > '9')
generate_error(curTok, UNEXPECTED_TOKEN);
}
}
break;
default:
if (symbol > 0 && symbol <= 127) // good ascii symbol
{
if (toks[pos].length != 1 || toks[pos].text[0] != symbol)
generate_error(curTok, UNEXPECTED_TOKEN);
}
fb_assert(false);
break;
}
++pos;
return curTok;
}
/**
PREPARSE_execute
@brief
@param status
@param ptrAtt
@param stmt_length
@param stmt
@param stmt_eaten
@param dialect
**/
boolPREPARSE_execute(CheckStatusWrapper* status, Why::YAttachment** ptrAtt,
string& stmt, bool* stmt_eaten, USHORT dialect)
{
// no use creating separate pool for a couple of strings
ContextPoolHolder context(getDefaultMemoryPool());
try
{
if (stmt.isEmpty())
{
returnfalse; // let others care
}
bool hasUser = true;
status->init();
for (int qStrip = 0; qStrip < 2; ++qStrip)
{
hasUser = false;
Tokens tks;
tks.quotes(quotes);
tks.parse(stmt.length(), stmt.c_str());
for (int tokenPos = tks.getCount() - 1; tokenPos >= 0; --tokenPos)
{
const Tokens::Tok& token = tks[tokenPos];
if (token.length > 0 && token.text[0] == '"')
{
string newToken = "'";
for (unsigned i = 1; i < token.length - 1; ++i)
{
switch (token.text[i])
{
case'\'':
newToken += "''";
break;
case'"':
++i;
newToken += '"';
break;
default:
newToken += token.text[i];
}
}
newToken += "'";
stmt.replace(token.origin, token.length, newToken);
}
}
unsigned pos = 0;
if (getToken(pos, tks) != pp_symbols[PP_CREATE].symbol)
{
returnfalse;
}
NoCaseString token(getToken(pos, tks));
if (token != pp_symbols[PP_DATABASE].symbol && token != pp_symbols[PP_SCHEMA].symbol)
{
returnfalse;
}
PathName file_name(getToken(pos, tks, STRING).ToPathName());
*stmt_eaten = false;
ClumpletWriter dpb(ClumpletReader::dpbList, MAX_DPB_SIZE);
dpb.insertByte(isc_dpb_overwrite, 0);
dpb.insertInt(isc_dpb_sql_dialect, dialect);
SLONG page_size = 0;
bool matched;
do
{
try
{
token = getToken(pos, tks);
}
catch (const Exception&)
{
*stmt_eaten = true;
break;
}
matched = false;
for (int i = 3; pp_symbols[i].symbol[0] && !matched; i++)
{
if (token == pp_symbols[i].symbol)
{
// CVC: What's strange, this routine doesn't check token.length()
// but it proceeds blindly, trying to exhaust the token itself.
switch (pp_symbols[i].code)
{
case PP_PAGE_SIZE:
case PP_PAGESIZE:
token = getToken(pos, tks);
if (token == "=")
token = getToken(pos, tks, NUMERIC);
page_size = token.length() > 8 ? 100000000 : atol(token.c_str());
dpb.insertInt(isc_dpb_page_size, page_size);
matched = true;
break;
case PP_USER:
token = getToken(pos, tks, qStrip ? STRING : SYMBOL);
dpb.insertString(isc_dpb_user_name, token.ToString());
matched = true;
hasUser = true;
break;
case PP_PASSWORD:
token = getToken(pos, tks, STRING);
dpb.insertString(isc_dpb_password, token.ToString());
matched = true;
break;
case PP_ROLE:
token = getToken(pos, tks);
dpb.insertString(isc_dpb_sql_role_name, token.ToString());
matched = true;
break;
case PP_SET:
token = getToken(pos, tks);
if (token != pp_symbols[PP_NAMES].symbol)
generate_error(token, UNEXPECTED_TOKEN);
token = getToken(pos, tks, STRING);
dpb.insertString(isc_dpb_lc_ctype, token.ToString());
matched = true;
break;
case PP_LENGTH:
token = getToken(pos, tks);
if (token == "=")
token = getToken(pos, tks, NUMERIC);
// Skip a token for value
matched = true;
break;
case PP_PAGE:
case PP_PAGES:
matched = true;
break;
} // switch
} // if
} // for
} while (matched);
RefPtr<Why::Dispatcher> dispatcher(FB_NEW Why::Dispatcher);
*ptrAtt = dispatcher->createDatabase(status, file_name.c_str(),
dpb.getBufferLength(), dpb.getBuffer());
if ((!hasUser) || ((status->getState() & IStatus::STATE_ERRORS) == 0) ||
(status->getErrors()[1] != isc_login))
{
break;
}
}
}
catch (const Exception& ex)
{
if (!(status->getState() & IStatus::STATE_ERRORS))
ex.stuffException(status);
returntrue;
}
returntrue;
}
/**
generate_error
@brief
@param status
@param token
@param error
@param result
**/
staticvoidgenerate_error(const NoCaseString& token, SSHORT error, char result)
{
string err_string;
ISC_STATUS_ARRAY temp_status;
temp_status[0] = isc_arg_gds;
temp_status[1] = isc_sqlerr;
temp_status[2] = isc_arg_number;
temp_status[3] = -104;
temp_status[4] = isc_arg_gds;
switch (error)
{
case UNEXPECTED_END_OF_COMMAND:
temp_status[5] = isc_command_end_err;
temp_status[6] = isc_arg_end;
break;
case UNEXPECTED_TOKEN:
case TOKEN_TOO_LONG:
if (result)
{
err_string = result;
err_string += token.ToString();
err_string += result;
}
else
err_string = token.ToString();
temp_status[5] = isc_token_err;
temp_status[6] = isc_arg_gds;
temp_status[7] = isc_random;
temp_status[8] = isc_arg_string;
temp_status[9] = (ISC_STATUS)(err_string.c_str());
temp_status[10] = isc_arg_end;
break;
}
Arg::StatusVector(temp_status).raise();
}