- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathRoutine.cpp
387 lines (305 loc) · 9.11 KB
/
Routine.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
/*
* 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): ______________________________________.
*
* Adriano dos Santos Fernandes
*/
#include"firebird.h"
#include"../jrd/Routine.h"
#include"../jrd/Statement.h"
#include"../jrd/Function.h"
#include"../jrd/jrd.h"
#include"../jrd/exe.h"
#include"../common/StatusHolder.h"
#include"../jrd/lck_proto.h"
#include"../jrd/par_proto.h"
usingnamespaceFirebird;
namespaceJrd {
// Create a MsgMetadata from a parameters array.
MsgMetadata* Routine::createMetadata(const Array<NestConst<Parameter> >& parameters, bool isExtern)
{
RefPtr<MsgMetadata> metadata(FB_NEW MsgMetadata);
for (Array<NestConst<Parameter> >::const_iterator i = parameters.begin();
i != parameters.end();
++i)
{
dsc d((*i)->prm_desc);
metadata->addItem((*i)->prm_name, (*i)->prm_nullable, d);
}
metadata->makeOffsets();
metadata->addRef();
return metadata;
}
// Create a Format based on an IMessageMetadata.
Format* Routine::createFormat(MemoryPool& pool, IMessageMetadata* params, bool addEof)
{
FbLocalStatus status;
unsigned count = params->getCount(&status);
status.check();
Format* format = Format::newFormat(pool, count * 2 + (addEof ? 1 : 0));
unsigned runOffset = 0;
dsc* desc = format->fmt_desc.begin();
for (unsigned i = 0; i < count; ++i)
{
unsigned descOffset, nullOffset, descDtype, descLength;
unsigned type = params->getType(&status, i);
status.check();
unsigned len = params->getLength(&status, i);
status.check();
runOffset = fb_utils::sqlTypeToDsc(runOffset, type, len, &descDtype, &descLength,
&descOffset, &nullOffset);
desc->clear();
desc->dsc_dtype = descDtype;
desc->dsc_length = descLength;
desc->dsc_scale = params->getScale(&status, i);
status.check();
desc->dsc_sub_type = params->getSubType(&status, i);
status.check();
desc->setTextType(params->getCharSet(&status, i));
status.check();
desc->dsc_address = (UCHAR*)(IPTR) descOffset;
desc->dsc_flags = (params->isNullable(&status, i) ? DSC_nullable : 0);
status.check();
++desc;
desc->makeShort(0, (SSHORT*)(IPTR) nullOffset);
++desc;
}
if (addEof)
{
// Next item is aligned on USHORT, so as the previous one.
desc->makeShort(0, (SSHORT*)(IPTR) runOffset);
runOffset += sizeof(USHORT);
}
format->fmt_length = runOffset;
return format;
}
voidRoutine::setStatement(Statement* value)
{
statement = value;
if (statement)
{
switch (getObjectType())
{
case obj_procedure:
statement->procedure = static_cast<jrd_prc*>(this);
break;
case obj_udf:
statement->function = static_cast<Function*>(this);
break;
default:
fb_assert(false);
break;
}
}
}
voidRoutine::checkReload(thread_db* tdbb)
{
if (!(flags & FLAG_RELOAD))
return;
if (!reload(tdbb))
{
string err;
err.printf("Recompile of %s \"%s\" failed",
getObjectType() == obj_udf ? "FUNCTION" : "PROCEDURE",
getName().toString().c_str());
(Arg::Gds(isc_random) << Arg::Str(err)).raise();
}
}
// Parse routine BLR and debug info.
voidRoutine::parseBlr(thread_db* tdbb, CompilerScratch* csb, bid* blob_id, bid* blobDbg)
{
Jrd::Attachment* attachment = tdbb->getAttachment();
if (blobDbg)
DBG_parse_debug_info(tdbb, blobDbg, *csb->csb_dbg_info);
UCharBuffer tmp;
if (blob_id)
{
blb* blob = blb::open(tdbb, attachment->getSysTransaction(), blob_id);
ULONG length = blob->blb_length + 10;
UCHAR* temp = tmp.getBuffer(length);
length = blob->BLB_get_data(tdbb, temp, length);
tmp.resize(length);
}
parseMessages(tdbb, csb, BlrReader(tmp.begin(), (unsigned) tmp.getCount()));
flags &= ~Routine::FLAG_RELOAD;
Statement* statement = getStatement();
PAR_blr(tdbb, NULL, tmp.begin(), (ULONG) tmp.getCount(), NULL, &csb, &statement, false, 0);
setStatement(statement);
if (csb->csb_g_flags & csb_reload)
flags |= FLAG_RELOAD;
if (!blob_id)
setImplemented(false);
}
// Parse the messages of a blr request. For specified message, allocate a format (Format) block.
voidRoutine::parseMessages(thread_db* tdbb, CompilerScratch* csb, BlrReader blrReader)
{
if (blrReader.getLength() < 2)
status_exception::raise(Arg::Gds(isc_metadata_corrupt));
csb->csb_blr_reader = blrReader;
const SSHORT version = csb->csb_blr_reader.getByte();
switch (version)
{
case blr_version4:
case blr_version5:
//case blr_version6:
break;
default:
status_exception::raise(
Arg::Gds(isc_metadata_corrupt) <<
Arg::Gds(isc_wroblrver2) << Arg::Num(blr_version4) << Arg::Num(blr_version5/*6*/) <<
Arg::Num(version));
}
if (csb->csb_blr_reader.getByte() != blr_begin)
status_exception::raise(Arg::Gds(isc_metadata_corrupt));
while (csb->csb_blr_reader.getByte() == blr_message)
{
const USHORT msgNumber = csb->csb_blr_reader.getByte();
USHORT count = csb->csb_blr_reader.getWord();
Format* format = Format::newFormat(*tdbb->getDefaultPool(), count);
USHORT padField;
constbool shouldPad = csb->csb_message_pad.get(msgNumber, padField);
USHORT maxAlignment = 0;
ULONG offset = 0;
USHORT i = 0;
for (Format::fmt_desc_iterator desc = format->fmt_desc.begin(); i < count; ++i, ++desc)
{
const USHORT align = PAR_desc(tdbb, csb, &*desc);
if (align)
offset = FB_ALIGN(offset, align);
desc->dsc_address = (UCHAR*)(IPTR) offset;
offset += desc->dsc_length;
maxAlignment = MAX(maxAlignment, align);
if (maxAlignment && shouldPad && i + 1 == padField)
offset = FB_ALIGN(offset, maxAlignment);
}
format->fmt_length = offset;
switch (msgNumber)
{
case0:
setInputFormat(format);
break;
case1:
setOutputFormat(format);
break;
default:
delete format;
}
}
}
// Decrement the routine's use count.
voidRoutine::release(thread_db* tdbb)
{
// Actually, it's possible for routines to have intermixed dependencies, so
// this routine can be called for the routine which is being freed itself.
// Hence we should just silently ignore such a situation.
if (!useCount)
return;
if (intUseCount > 0)
intUseCount--;
--useCount;
#ifdef DEBUG_PROCS
{
string buffer;
buffer.printf(
"Called from CMP_decrement():\n\t Decrementing use count of %s\n",
getName().toString().c_str());
JRD_print_procedure_info(tdbb, buffer.c_str());
}
#endif
// Call recursively if and only if the use count is zero AND the routine
// in the cache is different than this routine.
// The routine will be different than in the cache only if it is a
// floating copy, i.e. an old copy or a deleted routine.
if (useCount == 0 && !checkCache(tdbb))
{
if (getStatement())
releaseStatement(tdbb);
flags &= ~Routine::FLAG_BEING_ALTERED;
remove(tdbb);
}
}
voidRoutine::releaseStatement(thread_db* tdbb)
{
if (getStatement())
{
getStatement()->release(tdbb);
setStatement(NULL);
}
setInputFormat(NULL);
setOutputFormat(NULL);
flags &= ~FLAG_SCANNED;
}
// Remove a routine from cache.
voidRoutine::remove(thread_db* tdbb)
{
SET_TDBB(tdbb);
// MET_procedure locks it. Lets unlock it now to avoid troubles later
if (existenceLock)
LCK_release(tdbb, existenceLock);
// Routine that is being altered may have references
// to it by other routines via pointer to current meta
// data structure, so don't lose the structure or the pointer.
if (checkCache(tdbb) && !(flags & Routine::FLAG_BEING_ALTERED))
clearCache(tdbb);
// deallocate all structure which were allocated. The routine
// blr is originally read into a new pool from which all request
// are allocated. That will not be freed up.
if (existenceLock)
{
delete existenceLock;
existenceLock = NULL;
}
// deallocate input param structures
for (Array<NestConst<Parameter> >::iterator i = getInputFields().begin();
i != getInputFields().end(); ++i)
{
if (*i)
delete i->getObject();
}
getInputFields().clear();
// deallocate output param structures
for (Array<NestConst<Parameter> >::iterator i = getOutputFields().begin();
i != getOutputFields().end(); ++i)
{
if (*i)
delete i->getObject();
}
getOutputFields().clear();
if (!useCount)
releaseFormat();
if (!(flags & Routine::FLAG_BEING_ALTERED) && useCount == 0)
deletethis;
else
{
// Fully clear routine block. Some pieces of code check for empty
// routine name, this is why we do it.
setName(QualifiedName());
setSecurityName("");
setDefaultCount(0);
releaseExternal();
flags |= FLAG_CLEARED;
}
}
booljrd_prc::checkCache(thread_db* tdbb) const
{
return tdbb->getAttachment()->att_procedures[getId()] == this;
}
voidjrd_prc::clearCache(thread_db* tdbb)
{
tdbb->getAttachment()->att_procedures[getId()] = NULL;
}
} // namespace Jrd