-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
cmp.cpp
6773 lines (5717 loc) · 184 KB
/
cmp.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROGRAM: JRD Access Method
* MODULE: cmp.cpp
* DESCRIPTION: Request compiler
*
* 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): ______________________________________.
*
* 2001.07.28: John Bellardo: Added code to handle rse_skip.
* 2001.07.17 Claudio Valderrama: Stop crash when parsing user-supplied SQL plan.
* 2001.10.04 Claudio Valderrama: Fix annoying & invalid server complaint about
* triggers not having REFERENCES privilege over their owner table.
* 2002.02.24 Claudio Valderrama: substring() should signal output as string even
* if source is blob and should check implementation limits on field lengths.
* 2002.02.25 Claudio Valderrama: concatenate() should be a civilized function.
* This closes the heart of SF Bug #518282.
* 2002.09.28 Dmitry Yemanov: Reworked internal_info stuff, enhanced
* exception handling in SPs/triggers,
* implemented ROWS_AFFECTED system variable
* 2002.10.21 Nickolay Samofatov: Added support for explicit pessimistic locks
* 2002.10.29 Nickolay Samofatov: Added support for savepoints
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
* 2002.10.30 Sean Leyne - Removed support for obsolete "PC_PLATFORM" define
* 2003.10.05 Dmitry Yemanov: Added support for explicit cursors in PSQL
* Adriano dos Santos Fernandes
*/
#include "firebird.h"
#include <string.h>
#include <stdlib.h> // abort
#include "../jrd/common.h"
#include "../jrd/ibase.h"
#include "../jrd/jrd.h"
#include "../jrd/req.h"
#include "../jrd/val.h"
#include "../jrd/align.h"
#include "../jrd/lls.h"
#include "../jrd/exe.h"
#include "../jrd/rse.h"
#include "../jrd/scl.h"
#include "../jrd/tra.h"
#include "../jrd/lck.h"
#include "../jrd/irq.h"
#include "../jrd/drq.h"
#include "../jrd/intl.h"
#include "../jrd/btr.h"
#include "../jrd/sort.h"
#include "../jrd/acl.h"
#include "../jrd/gdsassert.h"
#include "../jrd/cmp_proto.h"
#include "../jrd/dsc_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/exe_proto.h"
#include "../jrd/fun_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/idx_proto.h"
#include "../jrd/intl_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/opt_proto.h"
#include "../jrd/par_proto.h"
#include "../jrd/scl_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/dsc_proto.h"
#include "../jrd/dbg_proto.h" // DBG_supervisor
#include "../jrd/sort_proto.h"
#include "../jrd/execute_statement.h"
#include "../jrd/Optimizer.h"
#include "../jrd/DataTypeUtil.h"
#include "../jrd/SysFunction.h"
// Pick up relation ids
#include "../jrd/ini.h"
#include "../common/classes/auto.h"
#include "../common/utils_proto.h"
#include "../dsql/Nodes.h"
#include "../jrd/ValuesImpl.h"
#include "../jrd/recsrc/RecordSource.h"
#include "../jrd/recsrc/Cursor.h"
#include "../jrd/Function.h"
#include "../dsql/ExprNodes.h"
#include "../dsql/StmtNodes.h"
using Firebird::AutoSetRestore;
// Firebird provides transparent conversion from string to date in
// contexts where it makes sense. This macro checks a descriptor to
// see if it is something that *could* represent a date value
inline bool COULD_BE_DATE(const dsc desc)
{
return ((DTYPE_IS_DATE(desc.dsc_dtype)) || (desc.dsc_dtype <= dtype_any_text));
}
// One of d1,d2 is time, the other is date
inline bool IS_DATE_AND_TIME(const dsc d1, const dsc d2)
{
return (((d1.dsc_dtype == dtype_sql_time) && (d2.dsc_dtype == dtype_sql_date)) ||
((d2.dsc_dtype == dtype_sql_time) && (d1.dsc_dtype == dtype_sql_date)));
}
// size of req_rpb[0]
const size_t REQ_TAIL = sizeof(Jrd::jrd_req::blk_repeat_type);
const int MAP_LENGTH = 256;
// RITTER - changed HP10 to HPUX
#if defined (HPUX) && defined (SUPERSERVER)
const int MAX_RECURSION = 96;
#else
const int MAX_RECURSION = 128;
#endif
const int MAX_REQUEST_SIZE = 10485760; // 10 MB - just to be safe
using namespace Jrd;
using namespace Firebird;
namespace
{
// Node copier for views.
class ViewNodeCopier : public NodeCopier
{
public:
ViewNodeCopier(CompilerScratch* aCsb, UCHAR* aRemap)
: NodeCopier(aCsb, aRemap)
{
}
protected:
virtual bool remapArgument()
{
return true;
}
virtual USHORT remapField(USHORT stream, USHORT fldId)
{
jrd_rel* relation = csb->csb_rpt[stream].csb_relation;
jrd_fld* field = MET_get_field(relation, fldId);
if (field->fld_source)
fldId = (USHORT)(IPTR) field->fld_source->nod_arg[e_fld_id];
return fldId;
}
};
// Node copier that remaps the field id 0 of stream 0 to a given field id.
class RemapFieldNodeCopier : public NodeCopier
{
public:
RemapFieldNodeCopier(CompilerScratch* aCsb, UCHAR* aRemap, USHORT aFldId)
: NodeCopier(aCsb, aRemap),
fldId(aFldId)
{
}
protected:
virtual USHORT getFieldId(jrd_nod* input)
{
if ((input->nod_flags & nod_id) && !input->nod_arg[e_fld_id] &&
!input->nod_arg[e_fld_stream])
{
return fldId;
}
return NodeCopier::getFieldId(input);
}
private:
USHORT fldId;
};
// Copy sub expressions (including subqueries).
class SubExprNodeCopier : public NodeCopier
{
public:
SubExprNodeCopier(CompilerScratch* aCsb)
: NodeCopier(aCsb, localMap)
{
// Initialize the map so all streams initially resolve to the original number. As soon
// copy creates new streams, the map are being overwritten.
for (unsigned i = 0; i < MAP_LENGTH; ++i)
localMap[i] = i;
}
static jrd_nod* copy(thread_db* tdbb, CompilerScratch* csb, jrd_nod* input)
{
SubExprNodeCopier obj(csb);
return static_cast<NodeCopier&>(obj).copy(tdbb, input);
}
private:
UCHAR localMap[MAP_LENGTH];
};
} // namespace
static UCHAR* alloc_map(thread_db*, CompilerScratch*, USHORT);
static jrd_nod* catenate_nodes(thread_db*, NodeStack&);
static jrd_nod* convertNeqAllToNotAny(thread_db* tdbb, CompilerScratch* csb, jrd_nod* node);
static void expand_view_nodes(thread_db*, CompilerScratch*, USHORT, NodeStack&, nod_t, bool);
static void ignore_dbkey(thread_db*, CompilerScratch*, RecordSelExpr*, const jrd_rel*);
static jrd_nod* make_defaults(thread_db*, CompilerScratch*, USHORT, jrd_nod*);
static jrd_nod* make_validation(thread_db*, CompilerScratch*, USHORT);
static void mark_variant(thread_db* tdbb, CompilerScratch* csb, USHORT stream);
static void pass1_erase(thread_db*, CompilerScratch*, jrd_nod*);
static jrd_nod* pass1_expand_view(thread_db*, CompilerScratch*, USHORT, USHORT, bool);
static void pass1_modify(thread_db*, CompilerScratch*, jrd_nod*);
static RecordSelExpr* pass1_rse(thread_db*, CompilerScratch*, RecordSelExpr*);
static void pass1_source(thread_db*, CompilerScratch*, RecordSelExpr*, jrd_nod*, jrd_nod**, NodeStack&);
static bool pass1_store(thread_db*, CompilerScratch*, jrd_nod*);
static jrd_nod* pass1_update(thread_db*, CompilerScratch*, jrd_rel*, const trig_vec*, USHORT, USHORT,
SecurityClass::flags_t, jrd_rel*, USHORT);
static void pass2_rse(thread_db*, CompilerScratch*, RecordSelExpr*);
static jrd_nod* pass2_union(thread_db*, CompilerScratch*, jrd_nod*);
static jrd_nod* pass2_validation(thread_db*, CompilerScratch*, const Item&);
static void plan_check(const CompilerScratch*, const RecordSelExpr*);
static void plan_set(CompilerScratch*, RecordSelExpr*, jrd_nod*);
static void post_procedure_access(thread_db*, CompilerScratch*, jrd_prc*);
static Cursor* post_rse(thread_db*, CompilerScratch*, RecordSelExpr*);
static void post_trigger_access(CompilerScratch*, jrd_rel*, ExternalAccess::exa_act, jrd_rel*);
static void process_map(thread_db*, CompilerScratch*, jrd_nod*, Format**);
static SSHORT strcmp_space(const char*, const char*);
static bool stream_in_rse(const USHORT, const RecordSelExpr*);
static void build_external_access(thread_db* tdbb, ExternalAccessList& list, jrd_req* request);
static void verify_trigger_access(thread_db* tdbb, jrd_rel* owner_relation, trig_vec* triggers, jrd_rel* view);
#ifdef CMP_DEBUG
#include <stdarg.h>
IMPLEMENT_TRACE_ROUTINE(cmp_trace, "CMP")
#endif
bool CMP_clone_is_active(const jrd_req* request)
{
/**************************************
*
* C M P _ c l o n e _ i s _ a c t i v e
*
**************************************
*
* Functional description
* Determine if a request or any of its clones are active.
*
**************************************/
DEV_BLKCHK(request, type_req);
if (request->req_flags & req_in_use)
return true;
vec<jrd_req*>* vector = request->req_sub_requests;
if (vector)
{
for (vec<jrd_req*>::const_iterator sub_req = vector->begin(), end = vector->end();
sub_req < end; ++sub_req)
{
if (*sub_req && (*sub_req)->req_flags & req_in_use)
return true;
}
}
return false;
}
jrd_nod* CMP_clone_node(thread_db* tdbb, CompilerScratch* csb, jrd_nod* node)
{
/**************************************
*
* C M P _ c l o n e _ n o d e
*
**************************************
*
* Functional description
* Clone a node.
*
**************************************/
SET_TDBB(tdbb);
DEV_BLKCHK(csb, type_csb);
DEV_BLKCHK(node, type_nod);
return NodeCopier::copy(tdbb, csb, node, NULL);
}
jrd_nod* CMP_clone_node_opt(thread_db* tdbb, CompilerScratch* csb, jrd_nod* node)
{
/**************************************
*
* C M P _ c l o n e _ n o d e _ o p t
*
**************************************
*
* Functional description
* Clone a value node for the optimizer. Make a copy of the node
* (if necessary) and assign impure space.
*
**************************************/
SET_TDBB(tdbb);
DEV_BLKCHK(csb, type_csb);
DEV_BLKCHK(node, type_nod);
if (node->nod_type == nod_argument) {
return node;
}
jrd_nod* clone = NodeCopier::copy(tdbb, csb, node, NULL);
CMP_pass2(tdbb, csb, clone, 0);
return clone;
}
inline void triggers_external_access(thread_db* tdbb, ExternalAccessList& list, trig_vec* tvec)
/**************************************
*
* t r i g g e r s _ e x t e r n a l _ a c c e s s
*
**************************************
*
* Functional description
* Invoke build_external_access for triggers in vector
*
**************************************/
{
if (tvec)
{
for (size_t i = 0; i < tvec->getCount(); i++)
{
Trigger& t = (*tvec)[i];
t.compile(tdbb);
if (t.request)
{
build_external_access(tdbb, list, t.request);
}
}
}
}
static void build_external_access(thread_db* tdbb, ExternalAccessList& list, jrd_req* request)
{
/**************************************
*
* b u i l d _ e x t e r n a l _ a c c e s s
*
**************************************
*
* Functional description
* Recursively walk external dependencies (procedures, triggers) for request to assemble full
* list of requests it depends on
*
**************************************/
for (ExternalAccess *item = request->req_external.begin(); item < request->req_external.end(); item++)
{
size_t i;
if (list.find(*item, i))
continue;
list.insert(i, *item);
// Add externals recursively
if (item->exa_action == ExternalAccess::exa_procedure)
{
jrd_prc* const procedure = MET_lookup_procedure_id(tdbb, item->exa_prc_id, false, false, 0);
if (procedure && procedure->getRequest())
build_external_access(tdbb, list, procedure->getRequest());
}
else if (item->exa_action == ExternalAccess::exa_function)
{
Function* const function = Function::lookup(tdbb, item->exa_fun_id, false, false, 0);
if (function && function->getRequest())
build_external_access(tdbb, list, function->getRequest());
}
else
{
jrd_rel* relation = MET_lookup_relation_id(tdbb, item->exa_rel_id, false);
if (!relation)
continue;
trig_vec *vec1, *vec2;
switch (item->exa_action)
{
case ExternalAccess::exa_insert:
vec1 = relation->rel_pre_store;
vec2 = relation->rel_post_store;
break;
case ExternalAccess::exa_update:
vec1 = relation->rel_pre_modify;
vec2 = relation->rel_post_modify;
break;
case ExternalAccess::exa_delete:
vec1 = relation->rel_pre_erase;
vec2 = relation->rel_post_erase;
break;
default:
continue; // should never happen, silence the compiler
}
triggers_external_access(tdbb, list, vec1);
triggers_external_access(tdbb, list, vec2);
}
}
}
static void verify_trigger_access(thread_db* tdbb, jrd_rel* owner_relation, trig_vec* triggers,
jrd_rel* view)
{
/**************************************
*
* v e r i f y _ t r i g g e r _ a c c e s s
*
**************************************
*
* Functional description
* Check that we have enough rights to access all resources this list of triggers touches
*
**************************************/
if (!triggers) {
return;
}
SET_TDBB(tdbb);
for (size_t i = 0; i < triggers->getCount(); i++)
{
Trigger& t = (*triggers)[i];
t.compile(tdbb);
if (!t.request)
{
continue;
}
for (const AccessItem* access = t.request->req_access.begin();
access < t.request->req_access.end(); access++)
{
// If this is not a system relation, we don't post access check if:
//
// - The table being checked is the owner of the trigger that's accessing it.
// - The field being checked is owned by the same table than the trigger
// that's accessing the field.
// - Since the trigger name comes in the triggers vector of the table and each
// trigger can be owned by only one table for now, we know for sure that
// it's a trigger defined on our target table.
if (!(owner_relation->rel_flags & REL_system))
{
if (access->acc_type == SCL_object_table &&
(owner_relation->rel_name == access->acc_name))
{
continue;
}
if (access->acc_type == SCL_object_column &&
(MET_lookup_field(tdbb, owner_relation, access->acc_name,
&access->acc_security_name) >= 0 ||
MET_relation_default_class(tdbb, owner_relation->rel_name, access->acc_security_name)))
{
continue;
}
}
// a direct access to an object from this trigger
const SecurityClass* sec_class = SCL_get_class(tdbb, access->acc_security_name.c_str());
SCL_check_access(tdbb, sec_class,
(access->acc_view_id) ? access->acc_view_id : (view ? view->rel_id : 0),
id_trigger, t.request->req_trg_name, access->acc_mask,
access->acc_type, access->acc_name, access->acc_r_name);
}
}
}
void CMP_verify_access(thread_db* tdbb, jrd_req* request)
{
/**************************************
*
* C M P _ v e r i f y _ a c c e s s
*
**************************************
*
* Functional description
* Check that we have enough rights to access all resources this request touches including
* resources it used indirectecty via procedures or triggers
*
**************************************/
SET_TDBB(tdbb);
ExternalAccessList external;
build_external_access(tdbb, external, request);
for (ExternalAccess* item = external.begin(); item < external.end(); item++)
{
const Routine* routine = NULL;
int aclType;
if (item->exa_action == ExternalAccess::exa_procedure)
{
routine = MET_lookup_procedure_id(tdbb, item->exa_prc_id, false, false, 0);
aclType = id_procedure;
}
else if (item->exa_action == ExternalAccess::exa_function)
{
routine = Function::lookup(tdbb, item->exa_fun_id, false, false, 0);
aclType = id_function;
}
else
{
jrd_rel* relation = MET_lookup_relation_id(tdbb, item->exa_rel_id, false);
jrd_rel* view = NULL;
if (item->exa_view_id)
view = MET_lookup_relation_id(tdbb, item->exa_view_id, false);
if (!relation)
continue;
switch (item->exa_action)
{
case ExternalAccess::exa_insert:
verify_trigger_access(tdbb, relation, relation->rel_pre_store, view);
verify_trigger_access(tdbb, relation, relation->rel_post_store, view);
break;
case ExternalAccess::exa_update:
verify_trigger_access(tdbb, relation, relation->rel_pre_modify, view);
verify_trigger_access(tdbb, relation, relation->rel_post_modify, view);
break;
case ExternalAccess::exa_delete:
verify_trigger_access(tdbb, relation, relation->rel_pre_erase, view);
verify_trigger_access(tdbb, relation, relation->rel_post_erase, view);
break;
default:
fb_assert(false);
}
continue;
}
if (!routine->getRequest())
continue;
for (const AccessItem* access = routine->getRequest()->req_access.begin();
access < routine->getRequest()->req_access.end();
access++)
{
const SecurityClass* sec_class = SCL_get_class(tdbb, access->acc_security_name.c_str());
if (routine->getName().package.isEmpty())
{
SCL_check_access(tdbb, sec_class, access->acc_view_id, aclType,
routine->getName().identifier, access->acc_mask, access->acc_type,
access->acc_name, access->acc_r_name);
}
else
{
SCL_check_access(tdbb, sec_class, access->acc_view_id,
id_package, routine->getName().package,
access->acc_mask, access->acc_type,
access->acc_name, access->acc_r_name);
}
}
}
// Inherit privileges of caller stored procedure or trigger if and only if
// this request is called immediately by caller (check for empty req_caller).
// Currently (in v2.5) this rule will work for EXECUTE STATEMENT only, as
// tra_callback_count incremented only by it.
// In v3.0, this rule also works for external procedures and triggers.
jrd_tra* transaction = tdbb->getTransaction();
const bool useCallerPrivs = transaction && transaction->tra_callback_count &&
!request->req_caller;
for (const AccessItem* access = request->req_access.begin();
access < request->req_access.end();
access++)
{
const SecurityClass* sec_class = SCL_get_class(tdbb, access->acc_security_name.c_str());
Firebird::MetaName objName;
SLONG objType = 0;
if (useCallerPrivs)
{
switch (transaction->tra_caller_name.type)
{
case obj_trigger:
objType = id_trigger;
break;
case obj_procedure:
objType = id_procedure;
break;
case obj_udf:
objType = id_function;
break;
case obj_package_header:
objType = id_package;
break;
case obj_type_MAX: // CallerName() constructor
fb_assert(transaction->tra_caller_name.name.isEmpty());
break;
default:
fb_assert(false);
}
objName = transaction->tra_caller_name.name;
}
SCL_check_access(tdbb, sec_class, access->acc_view_id, objType, objName,
access->acc_mask, access->acc_type, access->acc_name, access->acc_r_name);
}
}
jrd_req* CMP_clone_request(thread_db* tdbb, jrd_req* request, USHORT level, bool validate)
{
/**************************************
*
* C M P _ c l o n e _ r e q u e s t
*
**************************************
*
* Functional description
* Get the incarnation of the request appropriate for a given level.
* If the incarnation doesn't exist, clone the request.
*
**************************************/
DEV_BLKCHK(request, type_req);
SET_TDBB(tdbb);
Database* const dbb = tdbb->getDatabase();
Jrd::Attachment* const attachment = tdbb->getAttachment();
fb_assert(dbb);
// find the request if we've got it
if (!level) {
return request;
}
jrd_req* clone;
vec<jrd_req*>* vector = request->req_sub_requests;
if (vector && level < vector->count() && (clone = (*vector)[level]))
{
return clone;
}
if (validate)
{
const Routine* routine = request->getRoutine();
if (routine)
{
const TEXT* secName = routine->getSecurityName().nullStr();
const SecurityClass* secClass = SCL_get_class(tdbb, secName);
if (routine->getName().package.isEmpty())
{
SCL_check_access(tdbb, secClass, 0, 0, NULL, SCL_execute, routine->getSclType(),
routine->getName().identifier);
}
else
{
SCL_check_access(tdbb, secClass, 0, 0, NULL, SCL_execute, SCL_object_package,
routine->getName().package);
}
}
CMP_verify_access(tdbb, request);
}
MemoryPool* const pool = request->req_pool;
// we need to clone the request - find someplace to put it
vector = request->req_sub_requests =
vec<jrd_req*>::newVector(*pool, request->req_sub_requests, level + 1);
// clone the request
const size_t n = (request->req_impure_size - REQ_SIZE + REQ_TAIL - 1) / REQ_TAIL;
clone = FB_NEW_RPT(*pool, n) jrd_req(pool, &dbb->dbb_memory_stats);
(*vector)[level] = clone;
clone->req_attachment = attachment;
clone->req_count = request->req_count;
clone->req_impure_size = request->req_impure_size;
clone->req_top_node = request->req_top_node;
clone->req_trg_name = request->req_trg_name;
clone->req_procedure = request->req_procedure;
clone->req_function = request->req_function;
clone->req_flags = request->req_flags & REQ_FLAGS_CLONE_MASK;
clone->req_last_xcp = request->req_last_xcp;
clone->req_charset = request->req_charset;
clone->req_id = fb_utils::genUniqueId();
// We are cloning full lists here, not assigning pointers
clone->req_invariants = request->req_invariants;
clone->req_fors = request->req_fors;
clone->req_exec_sta = request->req_exec_sta;
clone->req_map_field_info.assign(request->req_map_field_info);
record_param* rpb1 = clone->req_rpb;
const record_param* const end = rpb1 + clone->req_count;
for (const record_param* rpb2 = request->req_rpb; rpb1 < end; rpb1++, rpb2++)
{
if (rpb2->rpb_stream_flags & RPB_s_update) {
rpb1->rpb_stream_flags |= RPB_s_update;
}
rpb1->rpb_relation = rpb2->rpb_relation;
}
return clone;
}
jrd_req* CMP_compile2(thread_db* tdbb, const UCHAR* blr, ULONG blr_length, bool internal_flag,
USHORT dbginfo_length, const UCHAR* dbginfo)
{
/**************************************
*
* C M P _ c o m p i l e 2
*
**************************************
*
* Functional description
* Compile a BLR request.
*
**************************************/
jrd_req* request = NULL;
SET_TDBB(tdbb);
Database* const dbb = tdbb->getDatabase();
// 26.09.2002 Nickolay Samofatov: default memory pool will become statement pool
// and will be freed by CMP_release
MemoryPool* const new_pool = dbb->createPool();
try
{
Jrd::ContextPoolHolder context(tdbb, new_pool);
CompilerScratch* csb =
PAR_parse(tdbb, blr, blr_length, internal_flag, dbginfo_length, dbginfo);
request = CMP_make_request(tdbb, csb, internal_flag);
new_pool->setStatsGroup(request->req_memory_stats);
#ifdef CMP_DEBUG
if (csb->csb_dump.hasData())
{
csb->dump("streams:\n");
for (unsigned i = 0; i < csb->csb_n_stream; ++i)
{
const CompilerScratch::csb_repeat& s = csb->csb_rpt[i];
csb->dump(
"\t%2d - view_stream: %2d; alias: %s; relation: %s; procedure: %s; view: %s\n",
i, s.csb_view_stream,
(s.csb_alias ? s.csb_alias->c_str() : ""),
(s.csb_relation ? s.csb_relation->rel_name.c_str() : ""),
(s.csb_procedure ? s.csb_procedure->getName().toString().c_str() : ""),
(s.csb_view ? s.csb_view->rel_name.c_str() : ""));
}
cmp_trace("\n%s\n", csb->csb_dump.c_str());
}
#endif
if (internal_flag)
{
request->req_charset = CS_METADATA;
request->req_flags |= req_internal;
}
CMP_verify_access(tdbb, request);
delete csb;
}
catch (const Firebird::Exception& ex)
{
Firebird::stuff_exception(tdbb->tdbb_status_vector, ex);
if (request)
CMP_release(tdbb, request);
else
dbb->deletePool(new_pool);
ERR_punt();
}
return request;
}
CompilerScratch::csb_repeat* CMP_csb_element(CompilerScratch* csb, USHORT element)
{
/**************************************
*
* C M P _ c s b _ e l e m e n t
*
**************************************
*
* Functional description
* Find tail element of compiler scratch block. If the csb isn't big
* enough, extend it.
*
**************************************/
DEV_BLKCHK(csb, type_csb);
CompilerScratch::csb_repeat empty_item;
while (element >= csb->csb_rpt.getCount()) {
csb->csb_rpt.add(empty_item);
}
return &csb->csb_rpt[element];
}
jrd_req* CMP_find_request(thread_db* tdbb, USHORT id, USHORT which)
{
/**************************************
*
* C M P _ f i n d _ r e q u e s t
*
**************************************
*
* Functional description
* Find an inactive incarnation of a system request. If necessary,
* clone it.
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
// if the request hasn't been compiled or isn't active,
// there're nothing to do
Database::CheckoutLockGuard guard(dbb, dbb->dbb_cmp_clone_mutex);
jrd_req* request;
if ((which == IRQ_REQUESTS && !(request = REQUEST(id))) ||
(which == DYN_REQUESTS && !(request = DYN_REQUEST(id))) ||
!(request->req_flags & (req_active | req_reserved)))
{
if (request) {
request->req_flags |= req_reserved;
}
return request;
}
// Request exists and is in use. Look for clones until we find
// one that is available.
for (int n = 1; true; n++)
{
if (n > MAX_RECURSION)
{
ERR_post(Arg::Gds(isc_no_meta_update) <<
Arg::Gds(isc_req_depth_exceeded) << Arg::Num(MAX_RECURSION));
// Msg363 "request depth exceeded. (Recursive definition?)"
}
jrd_req* clone = CMP_clone_request(tdbb, request, n, false);
if (!(clone->req_flags & (req_active | req_reserved)))
{
clone->req_flags |= req_reserved;
return clone;
}
}
}
void CMP_fini(thread_db* tdbb)
{
/**************************************
*
* C M P _ f i n i
*
**************************************
*
* Functional description
* Get rid of resource locks during shutdown.
*
**************************************/
SET_TDBB(tdbb);
Database* const dbb = tdbb->getDatabase();
// shutdown shared database locks
CMP_shutdown_database(tdbb);
// unwind any active system requests
while (dbb->dbb_sys_trans->tra_requests)
{
EXE_unwind(tdbb, dbb->dbb_sys_trans->tra_requests);
}
// and release them
for (jrd_req** itr = dbb->dbb_internal.begin(); itr < dbb->dbb_internal.end(); itr++)
{
if (*itr)
{
CMP_release(tdbb, *itr);
}
}
for (jrd_req** itr = dbb->dbb_dyn_req.begin(); itr < dbb->dbb_dyn_req.end(); itr++)
{
if (*itr)
{
CMP_release(tdbb, *itr);
}
}
}
Format* CMP_format(thread_db* tdbb, CompilerScratch* csb, USHORT stream)
{
/**************************************
*
* C M P _ f o r m a t
*
**************************************
*
* Functional description
* Pick up a format for a stream.
*
**************************************/
SET_TDBB(tdbb);
DEV_BLKCHK(csb, type_csb);
CompilerScratch::csb_repeat* tail = &csb->csb_rpt[stream];
if (tail->csb_format) {
return tail->csb_format;
}
if (tail->csb_relation) {
return tail->csb_format = MET_current(tdbb, tail->csb_relation);
}
if (tail->csb_procedure) {
return tail->csb_format = tail->csb_procedure->prc_format;
}
IBERROR(222); // msg 222 bad blr - invalid stream
return NULL;
}
void CMP_get_desc(thread_db* tdbb, CompilerScratch* csb, jrd_nod* node, DSC* desc)
{
/**************************************
*
* C M P _ g e t _ d e s c
*
**************************************
*
* Functional description
* Compute descriptor for value expression.
*
**************************************/
USHORT dtype = dtype_unknown;
SET_TDBB(tdbb);
DEV_BLKCHK(csb, type_csb);
DEV_BLKCHK(node, type_nod);
switch (node->nod_type)
{
case nod_max:
case nod_min:
case nod_from:
CMP_get_desc(tdbb, csb, node->nod_arg[e_stat_value], desc);
return;
case nod_total:
if (node->nod_type == nod_total)
CMP_get_desc(tdbb, csb, node->nod_arg[e_stat_value], desc);
else
CMP_get_desc(tdbb, csb, node->nod_arg[0], desc);