Repository navigation
Expand file tree
/
Copy pathexp.cpp
More file actions
1647 lines (1337 loc) · 39.1 KB
/
Copy pathexp.cpp
File metadata and controls
1647 lines (1337 loc) · 39.1 KB
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: C preprocessor
// MODULE: exp.cpp
// DESCRIPTION: Expression 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): ______________________________________.
// TMN (Mike Nordell) 11.APR.2001 - Reduce compiler warnings
//
//
//____________________________________________________________
//
//
#include "firebird.h"
#include <stdlib.h>
#include <string.h>
#include "ibase.h"
#include "../gpre/gpre.h"
#include "../jrd/intl.h"
#include "../gpre/cmp_proto.h"
#include "../gpre/exp_proto.h"
#include "../gpre/gpre_proto.h"
#include "../gpre/hsh_proto.h"
#include "../gpre/gpre_meta.h"
#include "../gpre/msc_proto.h"
#include "../gpre/par_proto.h"
#include "../gpre/sqe_proto.h"
#include "../gpre/sql_proto.h"
constexpr int ZERO_BASED = 0;
constexpr int ONE_BASED = 1;
static bool check_relation();
static gpre_nod* lookup_field(gpre_ctx*);
static gpre_nod* make_and(gpre_nod*, gpre_nod*);
static gpre_nod* make_list(gpre_lls*);
static gpre_nod* normalize_index(dim*, gpre_nod*, USHORT);
static gpre_nod* par_and(gpre_req*);
static gpre_nod* par_array(gpre_req*, gpre_fld*, bool, bool);
static gpre_nod* par_boolean(gpre_req*);
static gpre_nod* par_field(gpre_req*);
static gpre_nod* par_multiply(gpre_req*, gpre_fld*);
static gpre_nod* par_native_value(gpre_req*, gpre_fld*);
static gpre_nod* par_not(gpre_req*);
static gpre_nod* par_over(gpre_ctx*);
static gpre_nod* par_primitive_value(gpre_req*, gpre_fld*);
static gpre_nod* par_relational(gpre_req*);
static gpre_nod* par_udf(gpre_req*, USHORT, gpre_fld*);
static gpre_nod* par_value(gpre_req*, gpre_fld*);
static gpre_fld* global_count_field;
static gpre_fld* global_subscript_field;
struct rel_ops
{
nod_t rel_op;
kwwords_t rel_kw;
SSHORT rel_args;
};
static const rel_ops relops[] =
{
{ nod_eq, KW_EQ, 2 },
{ nod_eq, KW_EQUALS, 2 },
{ nod_equiv, KW_EQUIV, 2 },
{ nod_ne, KW_NE, 2 },
{ nod_gt, KW_GT, 2 },
{ nod_ge, KW_GE, 2 },
{ nod_le, KW_LE, 2 },
{ nod_lt, KW_LT, 2 },
{ nod_containing, KW_CONTAINING, 2 },
{ nod_matches, KW_MATCHES, 2 },
{ nod_like, KW_LIKE, 2 },
{ nod_starting, KW_STARTING, 2 },
{ nod_missing, KW_MISSING, 1 },
{ nod_between, KW_BETWEEN, 3},
{ nod_any, KW_none, 0}
};
struct dtypes
{
kwwords_t dtype_keyword;
USHORT dtype_dtype;
};
static const dtypes data_types[] =
{
{ KW_CHAR, dtype_text },
{ KW_VARYING, dtype_varying },
{ KW_STRING, dtype_cstring },
{ KW_SHORT, dtype_short },
{ KW_LONG, dtype_long },
{ KW_QUAD, dtype_quad },
{ KW_FLOAT, dtype_real },
{ KW_DOUBLE, dtype_double },
{ KW_DATE, dtype_date },
{ KW_none, 0}
};
//____________________________________________________________
//
// Parse array subscript.
//
gpre_nod* EXP_array(gpre_req* request, gpre_fld* field, bool subscript_flag, bool sql_flag)
{
return par_array(request, field, subscript_flag, sql_flag);
}
//____________________________________________________________
//
// Parse a datatype cast (sans leading period).
//
gpre_fld* EXP_cast(gpre_fld* field)
{
const dtypes* dtype = data_types;
while (true)
{
if (dtype->dtype_keyword == KW_none)
return NULL;
if (MSC_match(dtype->dtype_keyword))
break;
++dtype;
}
gpre_fld* cast = (gpre_fld*) MSC_alloc(FLD_LEN);
cast->fld_symbol = field->fld_symbol;
switch (cast->fld_dtype = dtype->dtype_dtype)
{
case dtype_varying:
cast->fld_length++;
[[fallthrough]];
case dtype_cstring:
cast->fld_length++;
[[fallthrough]];
case dtype_text:
if (gpreGlob.sw_cstring && !(cast->fld_dtype == dtype_cstring))
{
cast->fld_length++;
cast->fld_dtype = dtype_cstring;
}
if (!MSC_match(KW_L_BRCKET) && !MSC_match(KW_LT))
CPR_s_error("left bracket or <");
cast->fld_length += EXP_pos_USHORT_ordinal(true);
if (!MSC_match(KW_R_BRCKET) && !MSC_match(KW_GT))
CPR_s_error("right bracket or >");
break;
case dtype_quad:
cast->fld_length = 4;
break;
// Begin date/time/timestamp
case dtype_sql_date:
cast->fld_length = sizeof(ISC_DATE);
break;
case dtype_sql_time:
cast->fld_length = sizeof(ISC_TIME);
break;
case dtype_timestamp:
cast->fld_length = sizeof(ISC_TIMESTAMP);
break;
// End date/time/timestamp
case dtype_int64:
cast->fld_length = sizeof(ISC_INT64);
break;
case dtype_long:
cast->fld_length = sizeof(SLONG);
if (MSC_match(KW_SCALE))
cast->fld_scale = EXP_SSHORT_ordinal(true);
break;
case dtype_short:
cast->fld_length = sizeof(SSHORT);
if (MSC_match(KW_SCALE))
cast->fld_scale = EXP_SSHORT_ordinal(true);
break;
case dtype_real:
cast->fld_length = 4;
break;
case dtype_double:
cast->fld_length = 8;
break;
}
return cast;
}
//____________________________________________________________
//
// Parse a clause of the form "<context> IN <relation>".
// If the error flag is true, and the parse fails, quietly
// return NULL. Otherwise issue error messages where appropriate,
// and return a CONTEXT block as value.
//
gpre_ctx* EXP_context(gpre_req* request, gpre_sym* initial_symbol)
{
// Use the token (context name) to make up a symbol
// block. Then check for the keyword IN. If it's
// missing, either complain or punt, depending on the
// error flag. In either case, be sure to get rid of
// the symbol. If things look kosher, continue.
gpre_sym* symbol = initial_symbol;
if (!symbol)
{
symbol = PAR_symbol(SYM_context);
if (!MSC_match(KW_IN))
{
MSC_free(symbol);
CPR_s_error("IN");
}
}
symbol->sym_type = SYM_context;
gpre_rel* relation = EXP_relation();
gpre_ctx* context = MSC_context(request);
context->ctx_symbol = symbol;
context->ctx_relation = relation;
symbol->sym_object = context;
return context;
}
//____________________________________________________________
//
// Parse a qualified field clause. If recognized,
// return both the field block (as value) and the
// context block (by reference).
//
gpre_fld* EXP_field(gpre_ctx** rcontext)
{
gpre_sym* symbol;
for (symbol = gpreGlob.token_global.tok_symbol; symbol; symbol = symbol->sym_homonym)
{
if (symbol->sym_type == SYM_context)
break;
}
if (!symbol)
CPR_s_error("context variable");
gpre_ctx* context = symbol->sym_object;
gpre_rel* relation = context->ctx_relation;
PAR_get_token();
if (!MSC_match(KW_DOT))
CPR_s_error("dot after context variable");
SQL_resolve_identifier("<Field Name>", NULL, NAME_SIZE);
gpre_fld* field = MET_field(relation, gpreGlob.token_global.tok_string);
if (!field)
{
TEXT s[ERROR_LENGTH];
fb_utils::snprintf(s, sizeof(s), "field \"%s\" is not defined in relation %s",
gpreGlob.token_global.tok_string, relation->rel_symbol->sym_string);
PAR_error(s);
}
PAR_get_token();
*rcontext = context;
return field;
}
//____________________________________________________________
//
// Eat a left parenthesis, complain if not there.
//
void EXP_left_paren(const TEXT* string)
{
if (!MSC_match(KW_LEFT_PAREN))
CPR_s_error(string ? string : "left parenthesis");
}
//____________________________________________________________
//
// Parse a native literal constant value.
//
gpre_nod* EXP_literal()
{
switch (gpreGlob.sw_sql_dialect)
{
case 1:
if (!(gpreGlob.token_global.tok_type == tok_number || isQuoted(gpreGlob.token_global.tok_type)))
return NULL;
break;
default:
if (!(gpreGlob.token_global.tok_type == tok_number || gpreGlob.token_global.tok_type == tok_sglquoted))
return NULL;
}
ref* reference = (ref*) MSC_alloc(REF_LEN);
gpre_nod* node = MSC_unary(nod_literal, (gpre_nod*) reference);
if (isQuoted(gpreGlob.token_global.tok_type))
{
TEXT* string = (TEXT *) MSC_alloc(gpreGlob.token_global.tok_length + 3);
reference->ref_value = string;
strcat(string, "\'");
MSC_copy(gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length, string + 1);
strcat((string + gpreGlob.token_global.tok_length + 1), "\'");
// What kind of hack is this? The token has not been enlarged nor modified.
gpreGlob.token_global.tok_length += 2;
}
else
{
TEXT* string = (TEXT *) MSC_alloc(gpreGlob.token_global.tok_length + 1);
reference->ref_value = string;
MSC_copy(gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length, string);
}
// Begin date/time/timestamp
switch (gpreGlob.token_global.tok_keyword)
{
case KW_DATE:
reference->ref_flags |= REF_sql_date;
break;
case KW_TIME:
reference->ref_flags |= REF_sql_time;
break;
case KW_TIMESTAMP:
reference->ref_flags |= REF_timestamp;
break;
default:
// not a datetime
break;
}
// End date/time/timestamp
if ((gpreGlob.token_global.tok_type == tok_sglquoted && gpreGlob.token_global.tok_charset) ||
((isQuoted(gpreGlob.token_global.tok_type) && gpreGlob.sw_sql_dialect == 1) &&
gpreGlob.token_global.tok_charset))
{
reference->ref_flags |= REF_ttype;
gpre_sym* symbol = gpreGlob.token_global.tok_charset;
reference->ref_ttype = ((intlsym*) (symbol->sym_object))->intlsym_ttype;
}
else if (gpreGlob.sw_language == lang_internal)
{
// literals referenced in an Internal request are always correct charset
reference->ref_flags |= REF_ttype;
reference->ref_ttype = ttype_metadata;
}
PAR_get_token();
return node;
}
//____________________________________________________________
//
// Parse and convert to binary a numeric token.
// Restrict to LONG range.
//
SINT64 EXP_SINT64_ordinal(bool advance_flag)
{
const bool negate = (MSC_match(KW_MINUS));
if (gpreGlob.token_global.tok_type != tok_number)
CPR_s_error("<number>");
const char format[8] = "%" SQUADFORMAT;
SINT64 n;
sscanf(gpreGlob.token_global.tok_string, format, &n);
char buffer[64];
snprintf(buffer, sizeof(buffer), format, n);
if (strcmp(buffer, gpreGlob.token_global.tok_string) != 0)
PAR_error("Numeric value out of range");
if (advance_flag)
PAR_get_token();
return negate ? -n : n;
}
//____________________________________________________________
//
// Parse and convert to binary a numeric token.
// Restrict to LONG range.
//
SLONG EXP_SLONG_ordinal(bool advance_flag)
{
const bool negate = (MSC_match(KW_MINUS));
if (gpreGlob.token_global.tok_type != tok_number)
CPR_s_error("<number>");
const SLONG n = atoi(gpreGlob.token_global.tok_string);
char buffer[32];
snprintf(buffer, sizeof(buffer), "%" SLONGFORMAT, n);
if (strcmp(buffer, gpreGlob.token_global.tok_string) != 0)
PAR_error("Numeric value out of range");
if (advance_flag)
PAR_get_token();
return negate ? -n : n;
}
//____________________________________________________________
//
// Parse and convert to binary a numeric token.
// A SSHORT is desired.
//
SSHORT EXP_SSHORT_ordinal(bool advance_flag)
{
const bool negate = (MSC_match(KW_MINUS));
if (gpreGlob.token_global.tok_type != tok_number)
CPR_s_error("<number>");
const SLONG n = atoi(gpreGlob.token_global.tok_string);
if (negate && n > -1L * MIN_SSHORT)
PAR_error("Numeric value out of range");
else if (!negate && n > (SLONG) MAX_SSHORT)
PAR_error("Numeric value out of range");
if (advance_flag)
PAR_get_token();
return (SSHORT) (negate ? -n : n);
}
//____________________________________________________________
//
// Parse and convert to binary a numeric token.
// Restrict to LONG range.
//
ULONG EXP_ULONG_ordinal(bool advance_flag)
{
if (gpreGlob.token_global.tok_type != tok_number)
CPR_s_error("<unsigned number>");
const ULONG n = atoi(gpreGlob.token_global.tok_string);
char buffer[32];
snprintf(buffer, sizeof(buffer), "%" ULONGFORMAT, n);
if (strcmp(buffer, gpreGlob.token_global.tok_string) != 0)
PAR_error("Numeric value out of range");
if (advance_flag)
PAR_get_token();
return n;
}
//____________________________________________________________
//
// Parse and convert to binary a numeric token.
//
USHORT EXP_USHORT_ordinal(bool advance_flag)
{
if (gpreGlob.token_global.tok_type != tok_number)
CPR_s_error("<unsigned number>");
const ULONG n = atoi(gpreGlob.token_global.tok_string);
if (n > MAX_USHORT)
PAR_error("Numeric value out of range");
if (advance_flag)
PAR_get_token();
return (USHORT) n;
}
//____________________________________________________________
//
// Parse and convert to binary a numeric token.
// Which must be non-zero.
//
USHORT EXP_pos_USHORT_ordinal(bool advance_flag)
{
const USHORT n = EXP_USHORT_ordinal(advance_flag);
if (n == 0)
PAR_error("Expected positive value");
return n;
}
//____________________________________________________________
//
// We have a free reference to array. Make sure the whole damn thing
// gets sucked up.
//
void EXP_post_array( ref* reference)
{
gpre_fld* field = reference->ref_field;
if (!field->fld_array_info)
return;
reference->ref_flags |= REF_fetch_array;
gpre_ctx* context = reference->ref_context;
gpre_req* request = context->ctx_request;
ref* array_reference = MSC_reference(&request->req_array_references);
array_reference->ref_context = context;
array_reference->ref_field = field;
array_reference->ref_level = request->req_level;
field->fld_array_info->ary_ident = CMP_next_ident();
blb* blob = (blb*) MSC_alloc(BLB_LEN);
blob->blb_symbol = field->fld_symbol;
blob->blb_reference = reference;
if (!(blob->blb_seg_length = field->fld_seg_length))
blob->blb_seg_length = 512;
blob->blb_request = request;
}
//____________________________________________________________
//
// Post a field reference to a request. This
// can be called from either par_variable (free
// standing field reference) or EXP\par_value
// (cross request field reference).
//
ref* EXP_post_field(gpre_fld* field, gpre_ctx* context, bool null_flag)
{
gpre_req* request = context->ctx_request;
// If the reference is already posted, return the reference
ref* reference;
for (reference = request->req_references; reference; reference = reference->ref_next)
{
if (reference->ref_context == context)
{
gpre_fld* ref_field = reference->ref_field;
if (ref_field == field ||
(ref_field->fld_symbol == field->fld_symbol && ref_field->fld_array == field->fld_array))
{
if (!null_flag &&
(ref_field->fld_dtype != field->fld_dtype ||
ref_field->fld_length != field->fld_length ||
ref_field->fld_scale != field->fld_scale))
{
if (reference->ref_flags & REF_null)
reference->ref_field = field;
else
{
TEXT s[BUFFER_MEDIUM];
snprintf(s, sizeof(s), "field %s is inconsistently cast",
field->fld_symbol->sym_string);
PAR_error(s);
}
}
if (request->req_level > reference->ref_level)
reference->ref_level = request->req_level;
if (!null_flag)
reference->ref_flags &= ~REF_null;
return reference;
}
}
}
// This is first occurrence of field, make a new reference
reference = MSC_reference(&request->req_references);
reference->ref_context = context;
reference->ref_field = field;
reference->ref_level = request->req_level;
if (null_flag)
reference->ref_flags |= REF_null;
return reference;
}
//____________________________________________________________
//
// Match a trailing parenthesis. If isn't one, generate an error
// and return false.
//
bool EXP_match_paren()
{
if (MSC_match(KW_RIGHT_PAREN))
return true;
CPR_s_error("right parenthesis");
return false; // silence compiler warning
}
//____________________________________________________________
//
// Parse and look up a qualified relation name.
//
gpre_rel* EXP_relation()
{
if (!gpreGlob.isc_databases)
PAR_error("no database for operation");
// The current token is (i.e. should be) either a relation
// name or a database name. If it's a database name, search
// it for the relation name. If it's an unqualified relation
// name, search all databases for the name
gpre_rel* relation = NULL;
SQL_resolve_identifier("<identifier>", NULL, NAME_SIZE);
gpre_sym* symbol = MSC_find_symbol(gpreGlob.token_global.tok_symbol, SYM_database);
if (symbol)
{
gpre_dbb* db = (gpre_dbb*) symbol->sym_object;
PAR_get_token();
if (!MSC_match(KW_DOT))
CPR_s_error("period after database name");
SQL_resolve_identifier("<Table name>", NULL, NAME_SIZE);
relation = MET_get_relation(db, gpreGlob.token_global.tok_string, "");
}
else
{
for (gpre_dbb* db = gpreGlob.isc_databases; db; db = db->dbb_next)
{
gpre_rel* temp = MET_get_relation(db, gpreGlob.token_global.tok_string, "");
if (temp)
{
if (!relation)
relation = temp;
else
{
TEXT s[ERROR_LENGTH];
fb_utils::snprintf(s, sizeof(s), "relation %s is ambiguous", gpreGlob.token_global.tok_string);
PAR_get_token();
PAR_error(s);
}
}
}
}
if (!relation)
CPR_s_error("relation name");
PAR_get_token();
return relation;
}
//____________________________________________________________
//
// Parse a record selection expression. If there is an
// error, return NULL. This is slightly complicated by
// the fact that PASCAL and FORTRAN have a native FOR
// statement, and ADA has a FOR <variable> IN statement.
//
// If an initial symbol is given, the caller has already
// parsed the <contect> IN part of the expression.
//
gpre_rse* EXP_rse(gpre_req* request, gpre_sym* initial_symbol)
{
// parse FIRST n clause, if present
gpre_nod* first = NULL;
if (MSC_match(KW_FIRST))
{
if (!global_count_field)
global_count_field = MET_make_field("jrd_count", dtype_long, 4, false);
first = par_value(request, global_count_field);
}
// parse first context clause
if (initial_symbol && gpreGlob.sw_language == lang_ada && !check_relation())
return NULL;
gpre_ctx* context = EXP_context(request, initial_symbol);
SSHORT count = 1;
// parse subsequent context clauses if this is a join
gpre_nod* boolean = NULL;
while (MSC_match(KW_CROSS))
{
context = EXP_context(request, 0);
count++;
if (MSC_match(KW_OVER))
boolean = make_and(boolean, par_over(context));
}
// bug_3380 - could have an "over" clause without a "cross" clause
if (MSC_match(KW_OVER))
boolean = make_and(boolean, par_over(context));
// build rse node
gpre_rse* rec_expr = (gpre_rse*) MSC_alloc(RSE_LEN(count));
rec_expr->rse_count = count;
rec_expr->rse_first = first;
rec_expr->rse_boolean = boolean;
while (count)
{
rec_expr->rse_context[--count] = context;
HSH_insert(context->ctx_symbol);
context = context->ctx_next;
}
// parse boolean, if any. If there is an error, ignore the
// boolean, but keep the rse
if (MSC_match(KW_WITH))
boolean = make_and(boolean, par_boolean(request));
rec_expr->rse_boolean = boolean;
// Parse SORT clause, if any.
// CVC: It's not clear whether this var should be initialized at the same
// level than "direction".
bool insensitive = false;
while (true)
{
if (MSC_match(KW_SORTED))
{
MSC_match(KW_BY);
gpre_lls* items = NULL;
gpre_lls* directions = NULL;
bool direction = false;
count = 0;
while (true)
{
if (MSC_match(KW_ASCENDING))
{
direction = false;
continue;
}
if (MSC_match(KW_DESCENDING))
{
direction = true;
continue;
}
if (MSC_match(KW_EXACTCASE))
{
insensitive = false;
continue;
}
if (MSC_match(KW_ANYCASE))
{
insensitive = true;
continue;
}
gpre_nod* item = par_value(request, 0);
count++;
MSC_push((gpre_nod*) (IPTR) (direction ? 1 : 0), &directions);
if (insensitive)
{
gpre_nod* upcase = MSC_node(nod_upcase, 1);
upcase->nod_arg[0] = item;
MSC_push(upcase, &items);
}
else
MSC_push(item, &items);
if (!MSC_match(KW_COMMA))
break;
}
gpre_nod* sort = MSC_node(nod_sort, (SSHORT) (count * 2));
rec_expr->rse_sort = sort;
sort->nod_count = count;
gpre_nod** ptr = sort->nod_arg + count * 2;
while (--count >= 0)
{
*--ptr = (gpre_nod*) MSC_pop(&items);
*--ptr = (gpre_nod*) MSC_pop(&directions);
}
}
else if (MSC_match(KW_REDUCED))
{
// Parse REDUCED clause, if any.
MSC_match(KW_TO);
gpre_lls* items = NULL;
count = 0;
while (true)
{
gpre_nod* item = par_value(request, 0);
count++;
MSC_push(item, &items);
if (!MSC_match(KW_COMMA))
break;
}
gpre_nod* sort = MSC_node(nod_projection, count);
rec_expr->rse_reduced = sort;
sort->nod_count = count;
gpre_nod** ptr = sort->nod_arg + count;
while (--count >= 0)
*--ptr = (gpre_nod*) MSC_pop(&items);
}
else
break;
}
return rec_expr;
}
//____________________________________________________________
//
// Remove any context variables from hash table for a record
// selection expression.
//
void EXP_rse_cleanup( gpre_rse* rs)
{
// Clean up simple context variables
const gpre_ctx* const* context = rs->rse_context;
const gpre_ctx* const* const end = context + rs->rse_count;
for (; context < end; context++)
{
if ((*context)->ctx_symbol)
HSH_remove((*context)->ctx_symbol);
}
// If this is an aggregate, clean up the underlying rse
if (rs->rse_aggregate)
EXP_rse_cleanup(rs->rse_aggregate);
// If this is a union, clean up each of the primitive rse's
gpre_nod* node = rs->rse_union;
if (node)
{
for (int i = 0; i < node->nod_count; i++)
EXP_rse_cleanup((gpre_rse*) node->nod_arg[i]);
}
}
//____________________________________________________________
//
// Parse a subscript value. This is called by PAR\par_slice.
//
gpre_nod* EXP_subscript(gpre_req* request)
{
ref* reference = (ref*) MSC_alloc(REF_LEN);
gpre_nod* node = MSC_unary(nod_value, (gpre_nod*) reference);
// Special case literals
if (gpreGlob.token_global.tok_type == tok_number)
{
node->nod_type = nod_literal;
TEXT* string = (TEXT *) MSC_alloc(gpreGlob.token_global.tok_length + 1);
reference->ref_value = string;
MSC_copy(gpreGlob.token_global.tok_string, gpreGlob.token_global.tok_length, string);
PAR_get_token();
return node;
}
reference->ref_value = PAR_native_value(true, false);
if (request)
{
reference->ref_next = request->req_values;
request->req_values = reference;
}
return node;
}
//____________________________________________________________
//
// Check current token for either a relation or database name.
//
static bool check_relation()
{
// The current token is (i.e. should be) either a relation
// name or a database name. If it's a database name, search
// it for the relation name. If it's an unqualified relation
// name, search all databases for the name
gpre_sym* symbol = gpreGlob.token_global.tok_symbol;
if (symbol && symbol->sym_type == SYM_database)
return true;
for (gpre_dbb* db = gpreGlob.isc_databases; db; db = db->dbb_next)
{
if (MET_get_relation(db, gpreGlob.token_global.tok_string, ""))
return true;
}
return false;
}
//____________________________________________________________
//
// Check to see if the current token is a field name corresponding
// to a given context. If so, return a field block (with reference
// block) for field.
//
static gpre_nod* lookup_field(gpre_ctx* context)
{
SQL_resolve_identifier("<Field Name>", NULL, NAME_SIZE);
gpre_fld* field = MET_field(context->ctx_relation, gpreGlob.token_global.tok_string);
if (!field)
return NULL;
ref* reference = (ref*) MSC_alloc(REF_LEN);
reference->ref_field = field;
reference->ref_context = context;
return MSC_unary(nod_field, (gpre_nod*) reference);
}
//____________________________________________________________
//
// Combine two (potention) conjuncts into a single, valid
// boolean. Either or both on the conjunctions may be NULL.
// If both are null, return null.
//
static gpre_nod* make_and( gpre_nod* node1, gpre_nod* node2)
{
if (!node1)
return node2;
if (!node2)
return NULL;
return MSC_binary(nod_and, node1, node2);
}
//____________________________________________________________
//
// Make a generic variable length node from a stack.
//
static gpre_nod* make_list( gpre_lls* stack)
{
USHORT count = 0;