- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathStatementMetadata.cpp
527 lines (422 loc) · 12.3 KB
/
StatementMetadata.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* The contents of this file are subject to the Initial
* Developer's 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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* 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 Adriano dos Santos Fernandes
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2011 Adriano dos Santos Fernandes <adrianosf at gmail.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
* Alex Peshkoff
*
*/
#include"firebird.h"
#include"../common/StatementMetadata.h"
#include"memory_routines.h"
#include"../common/StatusHolder.h"
#include"firebird/impl/inf_pub.h"
#include"../yvalve/gds_proto.h"
#include"../common/utils_proto.h"
#include"firebird/impl/sqlda_pub.h"
namespaceFirebird {
staticconst UCHAR DESCRIBE_VARS[] =
{
isc_info_sql_describe_vars,
isc_info_sql_sqlda_seq,
isc_info_sql_type,
isc_info_sql_sub_type,
isc_info_sql_scale,
isc_info_sql_length,
isc_info_sql_field,
isc_info_sql_relation,
isc_info_sql_owner,
isc_info_sql_alias,
isc_info_sql_describe_end
};
staticconstunsigned INFO_BUFFER_SIZE = MemoryPool::MAX_MEDIUM_BLOCK_SIZE;
static USHORT getLen(const UCHAR** ptr, const UCHAR* bufferEnd);
staticintgetNumericInfo(const UCHAR** ptr, const UCHAR* bufferEnd);
staticvoidgetStringInfo(const UCHAR** ptr, const UCHAR* bufferEnd, string* str);
// Build a list of info codes based on a prepare flags bitmask.
// Return "estimated" necessary size for the result buffer.
unsignedStatementMetadata::buildInfoItems(Array<UCHAR>& items, unsigned flags)
{
items.clear();
if (flags & IStatement::PREPARE_PREFETCH_TYPE)
items.add(isc_info_sql_stmt_type);
if (flags & IStatement::PREPARE_PREFETCH_FLAGS)
items.add(isc_info_sql_stmt_flags);
if (flags & IStatement::PREPARE_PREFETCH_INPUT_PARAMETERS)
{
items.add(isc_info_sql_bind);
items.push(DESCRIBE_VARS, sizeof(DESCRIBE_VARS));
}
if (flags & IStatement::PREPARE_PREFETCH_OUTPUT_PARAMETERS)
{
items.add(isc_info_sql_select);
items.push(DESCRIBE_VARS, sizeof(DESCRIBE_VARS));
}
if (flags & IStatement::PREPARE_PREFETCH_LEGACY_PLAN)
items.add(isc_info_sql_get_plan);
if (flags & IStatement::PREPARE_PREFETCH_DETAILED_PLAN)
items.add(isc_info_sql_explain_plan);
return INFO_BUFFER_SIZE;
}
// Build a prepare flags bitmask based on a list of info codes.
unsignedStatementMetadata::buildInfoFlags(unsigned itemsLength, const UCHAR* items)
{
unsigned flags = 0;
const UCHAR* end = items + itemsLength;
UCHAR c;
while (items < end && (c = *items++) != isc_info_end)
{
switch (c)
{
case isc_info_sql_stmt_type:
flags |= IStatement::PREPARE_PREFETCH_TYPE;
break;
case isc_info_sql_stmt_flags:
flags |= IStatement::PREPARE_PREFETCH_FLAGS;
break;
case isc_info_sql_get_plan:
flags |= IStatement::PREPARE_PREFETCH_LEGACY_PLAN;
break;
case isc_info_sql_explain_plan:
flags |= IStatement::PREPARE_PREFETCH_DETAILED_PLAN;
break;
case isc_info_sql_select:
flags |= IStatement::PREPARE_PREFETCH_OUTPUT_PARAMETERS;
break;
case isc_info_sql_bind:
flags |= IStatement::PREPARE_PREFETCH_INPUT_PARAMETERS;
break;
}
}
return flags;
}
// Get statement type.
unsignedStatementMetadata::getType()
{
if (!type.specified)
{
UCHAR info[] = {isc_info_sql_stmt_type};
UCHAR result[16];
getAndParse(sizeof(info), info, sizeof(result), result);
fb_assert(type.specified);
}
return type.value;
}
unsignedStatementMetadata::getFlags()
{
if (!flags.specified)
{
UCHAR info[] = {isc_info_sql_stmt_flags};
UCHAR result[16];
getAndParse(sizeof(info), info, sizeof(result), result);
fb_assert(flags.specified);
}
return flags.value;
}
// Get statement plan.
constchar* StatementMetadata::getPlan(bool detailed)
{
string* plan = detailed ? &detailedPlan : &legacyPlan;
if (plan->isEmpty())
{
UCHAR info[] = {UCHAR(detailed ? isc_info_sql_explain_plan : isc_info_sql_get_plan)};
UCHAR result[INFO_BUFFER_SIZE];
getAndParse(sizeof(info), info, sizeof(result), result);
}
return plan->nullStr();
}
// Get statement input parameters.
IMessageMetadata* StatementMetadata::getInputMetadata()
{
if (!inputParameters->fetched)
fetchParameters(isc_info_sql_bind, inputParameters);
inputParameters->addRef();
return inputParameters;
}
// Get statement output parameters.
IMessageMetadata* StatementMetadata::getOutputMetadata()
{
if (!outputParameters->fetched)
fetchParameters(isc_info_sql_select, outputParameters);
outputParameters->addRef();
return outputParameters;
}
// Get number of records affected by the statement execution.
ISC_UINT64 StatementMetadata::getAffectedRecords()
{
UCHAR info[] = {isc_info_sql_records};
UCHAR result[33];
getAndParse(sizeof(info), info, sizeof(result), result);
ISC_UINT64 count = 0;
if (result[0] == isc_info_sql_records)
{
const UCHAR* p = result + 3;
while (*p != isc_info_end)
{
UCHAR counter = *p++;
const SSHORT len = gds__vax_integer(p, 2);
p += 2;
if (counter != isc_info_req_select_count)
count += gds__vax_integer(p, len);
p += len;
}
}
return count;
}
// Reset the object to its initial state.
voidStatementMetadata::clear()
{
type.specified = false;
legacyPlan = detailedPlan = "";
inputParameters->items.clear();
outputParameters->items.clear();
inputParameters->fetched = outputParameters->fetched = false;
}
// Parse an info response buffer.
voidStatementMetadata::parse(unsigned bufferLength, const UCHAR* buffer)
{
const UCHAR* bufferEnd = buffer + bufferLength;
Parameters* parameters = NULL;
bool finish = false;
UCHAR c;
while (!finish && buffer < bufferEnd && (c = *buffer++) != isc_info_end)
{
switch (c)
{
case isc_info_sql_stmt_type:
type = getNumericInfo(&buffer, bufferEnd);
break;
case isc_info_sql_stmt_flags:
flags = getNumericInfo(&buffer, bufferEnd);
break;
case isc_info_sql_get_plan:
case isc_info_sql_explain_plan:
{
string* plan = (c == isc_info_sql_explain_plan ? &detailedPlan : &legacyPlan);
getStringInfo(&buffer, bufferEnd, plan);
break;
}
case isc_info_sql_select:
parameters = outputParameters;
break;
case isc_info_sql_bind:
parameters = inputParameters;
break;
case isc_info_sql_num_variables:
case isc_info_sql_describe_vars:
{
if (!parameters)
{
finish = true;
break;
}
getNumericInfo(&buffer, bufferEnd); // skip the message index
if (c == isc_info_sql_num_variables)
continue;
Parameters::Item temp(*getDefaultMemoryPool());
Parameters::Item* param = &temp;
bool finishDescribe = false;
// Loop over the variables being described.
while (!finishDescribe)
{
fb_assert(buffer < bufferEnd);
if (buffer >= bufferEnd)
break;
switch ((c = *buffer++))
{
case isc_info_sql_describe_end:
param->finished = true;
break;
case isc_info_sql_sqlda_seq:
if (!parameters->fetched)
{
unsigned num = getNumericInfo(&buffer, bufferEnd);
while (parameters->items.getCount() < num)
parameters->items.add();
param = ¶meters->items[num - 1];
}
break;
case isc_info_sql_type:
param->type = getNumericInfo(&buffer, bufferEnd);
param->nullable = (param->type & 1) != 0;
param->type &= ~1;
break;
case isc_info_sql_sub_type:
param->subType = getNumericInfo(&buffer, bufferEnd);
break;
case isc_info_sql_length:
param->length = getNumericInfo(&buffer, bufferEnd);
break;
case isc_info_sql_scale:
param->scale = getNumericInfo(&buffer, bufferEnd);
break;
case isc_info_sql_field:
getStringInfo(&buffer, bufferEnd, ¶m->field);
break;
case isc_info_sql_relation:
getStringInfo(&buffer, bufferEnd, ¶m->relation);
break;
case isc_info_sql_owner:
getStringInfo(&buffer, bufferEnd, ¶m->owner);
break;
case isc_info_sql_alias:
getStringInfo(&buffer, bufferEnd, ¶m->alias);
break;
case isc_info_truncated:
--buffer;
finishDescribe = true;
break;
default:
--buffer;
finishDescribe = true;
if (parameters->fetched)
break;
parameters->fetched = true;
for (unsigned n = 0; n < parameters->items.getCount(); ++n)
{
Parameters::Item* param = ¶meters->items[n];
if (!param->finished)
{
parameters->fetched = false;
break;
}
}
if (parameters->fetched && parameters->makeOffsets() != ~0u)
parameters->fetched = false;
if (parameters->fetched)
{
for (unsigned n = 0; n < parameters->items.getCount(); ++n)
{
Parameters::Item* param = ¶meters->items[n];
switch (param->type)
{
case SQL_VARYING:
case SQL_TEXT:
param->charSet = param->subType;
param->subType = 0;
break;
case SQL_BLOB:
param->charSet = param->scale;
param->scale = 0;
break;
}
}
}
break;
}
}
break;
}
default:
finish = true;
break;
}
}
}
// Get a info buffer and parse it.
voidStatementMetadata::getAndParse(unsigned itemsLength, const UCHAR* items,
unsigned bufferLength, UCHAR* buffer)
{
LocalStatus ls;
CheckStatusWrapper status(&ls);
statement->getInfo(&status, itemsLength, items, bufferLength, buffer);
ls.check();
parse(bufferLength, buffer);
}
// Fill an output buffer from the cached data. Return true if succeeded.
boolStatementMetadata::fillFromCache(unsigned itemsLength, const UCHAR* items,
unsigned bufferLength, UCHAR* buffer)
{
//// TODO: Respond more things locally. isc_dsql_prepare_m will need.
if (((itemsLength == 1 && items[0] == isc_info_sql_stmt_type) ||
(itemsLength == 2 && items[0] == isc_info_sql_stmt_type &&
(items[1] == isc_info_end || items[1] == 0))) &&
type.specified)
{
if (bufferLength >= 8)
{
*buffer++ = isc_info_sql_stmt_type;
put_vax_short(buffer, 4);
buffer += 2;
put_vax_long(buffer, type.value);
buffer += 4;
*buffer = isc_info_end;
}
else
*buffer = isc_info_truncated;
returntrue;
}
returnfalse;
}
// Fetch input or output parameter list.
voidStatementMetadata::fetchParameters(UCHAR code, Parameters* parameters)
{
while (!parameters->fetched)
{
unsigned startIndex = 1;
for (ObjectsArray<Parameters::Item>::iterator i = parameters->items.begin();
i != parameters->items.end();
++i)
{
if (!i->finished)
break;
++startIndex;
}
UCHAR items[5 + sizeof(DESCRIBE_VARS)] =
{
isc_info_sql_sqlda_start,
2,
UCHAR(startIndex & 0xFF),
UCHAR((startIndex >> 8) & 0xFF),
code
};
memcpy(items + 5, DESCRIBE_VARS, sizeof(DESCRIBE_VARS));
UCHAR buffer[INFO_BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
getAndParse(sizeof(items), items, sizeof(buffer), buffer);
}
}
//--------------------------------------
static USHORT getLen(const UCHAR** ptr, const UCHAR* bufferEnd)
{
if (bufferEnd - *ptr < 2)
fatal_exception::raise("Invalid info structure - no space for clumplet length");
const USHORT len = static_cast<USHORT>(gds__vax_integer(*ptr, 2));
*ptr += 2;
fb_assert(bufferEnd - *ptr >= len);
if (bufferEnd - *ptr < len)
fatal_exception::raiseFmt("Invalid info structure - no space for clumplet data: need %d, actual %d",
len, bufferEnd - *ptr);
return len;
}
// Pick up a VAX format numeric info item with a 2 byte length.
staticintgetNumericInfo(const UCHAR** ptr, const UCHAR* bufferEnd)
{
const USHORT len = getLen(ptr, bufferEnd);
int item = gds__vax_integer(*ptr, len);
*ptr += len;
return item;
}
// Pick up a string valued info item.
staticvoidgetStringInfo(const UCHAR** ptr, const UCHAR* bufferEnd, string* str)
{
const USHORT len = getLen(ptr, bufferEnd);
str->assign(*ptr, len);
*ptr += len;
}
} // namespace Firebird