/
ddl.cpp
7060 lines (6017 loc) · 195 KB
/
ddl.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: Dynamic SQL runtime support
* MODULE: ddl.cpp
* DESCRIPTION: Utilities for generating ddl
*
* 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.5.20 Claudio Valderrama: Stop null pointer that leads to a crash,
* caused by incomplete yacc syntax that allows ALTER DOMAIN dom SET;
*
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
* December 2001 Mike Nordell - Attempt to make it C++
*
* 2001.5.20 Claudio Valderrama: Stop null pointer that leads to a crash,
* caused by incomplete yacc syntax that allows ALTER DOMAIN dom SET;
* 2001.5.29 Claudio Valderrama: Check for view v/s relation in DROP
* command will stop a user that uses DROP VIEW and drops a table by
* accident and vice-versa.
* 2001.5.30 Claudio Valderrama: alter column should use 1..N for the
* position argument since the call comes from SQL DDL.
* 2001.6.27 Claudio Valderrama: DDL_resolve_intl_type() was adding 2 to the
* length of varchars instead of just checking that len+2<=MAX_COLUMN_SIZE.
* It required a minor change to put_field() where it was decremented, too.
* 2001.6.27 Claudio Valderrama: Finally stop users from invoking the same option
* several times when altering a domain. Specially dangerous with text data types.
* Ex: alter domain d type char(5) type varchar(5) default 'x' default 'y';
* Bear in mind that if DYN functions are addressed directly, this protection
* becomes a moot point.
* 2001.6.30 Claudio Valderrama: revert changes from 2001.6.26 because the code
* is called from several places and there are more functions, even in metd.c,
* playing the same nonsense game with the field's length, so it needs more
* careful examination. For now, the new checks in DYN_MOD should catch most anomalies.
* 2001.7.3 Claudio Valderrama: fix Firebird Bug #223059 with mismatch between number
* of declared fields for a VIEW and effective fields in the SELECT statement.
* 2001.07.22 Claudio Valderrama: minor fixes and improvements.
* 2001.08.18 Claudio Valderrama: RECREATE PROCEDURE.
* 2001.10.01 Claudio Valderrama: modify_privilege() should recognize that a ROLE can
* now be made an explicit grantee.
* 2001.10.08 Claudio Valderrama: implement fb_sysflag enum values for autogenerated
* non-system triggers so DFW can recognize them easily.
* 2001.10.26 Claudio Valderrama: added a call to the new METD_drop_function()
* in DDL_execute() so the metadata cache for udfs can be refreshed.
* 2001.12.06 Claudio Valderrama: DDL_resolve_intl_type should calculate field length
* 2002.08.04 Claudio Valderrama: allow declaring and defining variables at the same time
* 2002.08.04 Dmitry Yemanov: ALTER VIEW
* 2002.08.31 Dmitry Yemanov: allowed user-defined index names for PK/FK/UK constraints
* 2002.09.01 Dmitry Yemanov: RECREATE VIEW
* 2002.09.12 Nickolay Samofatov: fixed cached metadata errors
* 2004.01.16 Vlad Horsun: added support for default parameters and
* EXECUTE BLOCK statement
* Adriano dos Santos Fernandes
*/
#include "firebird.h"
#include <stdio.h>
#include <string.h>
#include "../jrd/SysFunction.h"
#include "../common/classes/MetaName.h"
#include "../dsql/dsql.h"
#include "../jrd/ibase.h"
#include "../jrd/thd.h"
#include "../jrd/intl.h"
#include "../jrd/flags.h"
#include "../jrd/constants.h"
#include "../dsql/alld_proto.h"
#include "../dsql/errd_proto.h"
#include "../dsql/ddl_proto.h"
#include "../dsql/gen_proto.h"
#include "../dsql/make_proto.h"
#include "../dsql/metd_proto.h"
#include "../dsql/pass1_proto.h"
#include "../dsql/utld_proto.h"
#include "../jrd/sch_proto.h"
#include "../jrd/thread_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/why_proto.h"
#include "../common/utils_proto.h"
#ifdef DSQL_DEBUG
#include "../gpre/prett_proto.h"
#endif
const int BLOB_BUFFER_SIZE = 4096; // to read in blr blob for default values
static void assign_field_length(dsql_fld*, USHORT);
static bool is_array_or_blob(dsql_req*, const dsql_nod*);
static void check_constraint(dsql_req*, dsql_nod*, bool);
static void check_one_call(USHORT*, SSHORT, const TEXT*);
static void create_view_triggers(dsql_req*, dsql_nod*, dsql_nod*);
static void define_computed(dsql_req*, dsql_nod*, dsql_fld*, dsql_nod*);
static void define_constraint_trigger(dsql_req*, dsql_nod*);
static void define_database(dsql_req*);
static void define_del_cascade_trg(dsql_req*, const dsql_nod*, const dsql_nod*,
const dsql_nod*, const char*, const char*);
//static void define_del_default_trg(dsql_req*, dsql_nod*, dsql_nod*, dsql_nod*, TEXT*, TEXT*);
static void define_dimensions(dsql_req*, const dsql_fld*);
static void define_domain(dsql_req*);
static void define_exception(dsql_req*, NOD_TYPE);
static void define_field(dsql_req*, dsql_nod*, SSHORT, const dsql_str*, const dsql_nod* pkcols);
static void define_filter(dsql_req*);
static SSHORT getBlobFilterSubType(dsql_req* request, const dsql_nod* node);
static void define_collation(dsql_req*);
static void define_generator(dsql_req*);
static void define_role(dsql_req*);
static void define_index(dsql_req*);
#ifdef NOT_USED_OR_REPLACED
static dsql_nod* define_insert_action(dsql_req*);
#endif
static void define_procedure(dsql_req*, NOD_TYPE);
static void define_rel_constraint(dsql_req*, dsql_nod*);
static void define_relation(dsql_req*);
static void define_set_null_trg(dsql_req*, const dsql_nod*, const dsql_nod*,
const dsql_nod*, const char*, const char*, bool);
static void define_set_default_trg(dsql_req*, const dsql_nod*, const dsql_nod*,
const dsql_nod*, const char*, const char*, bool);
static void define_shadow(dsql_req*);
static void define_trigger(dsql_req*, NOD_TYPE);
static void define_udf(dsql_req*);
static void define_update_action(dsql_req*, dsql_nod**, dsql_nod**, dsql_nod*);
static void define_upd_cascade_trg(dsql_req*, const dsql_nod*, const dsql_nod*,
const dsql_nod*, const char*, const char*);
static void define_view(dsql_req*, NOD_TYPE);
static void define_view_trigger(dsql_req*, dsql_nod*, dsql_nod*, dsql_nod*);
static void delete_collation(dsql_req*);
static void delete_exception(dsql_req*, dsql_nod*, bool);
static void delete_procedure(dsql_req*, dsql_nod*, bool);
static void delete_relation_view(dsql_req*, dsql_nod*, bool);
static void delete_trigger(dsql_req*, dsql_nod*, bool);
static const dsql_nod* find_pk_columns(const dsql_nod* def_rel_elements);
static ULONG find_start_of_body(const dsql_str* string);
static void fix_default_source(dsql_str* string);
static void foreign_key(dsql_req*, dsql_nod*, const char* index_name);
static void generate_dyn(dsql_req*, dsql_nod*);
static void grant_revoke(dsql_req*);
static void make_comment(dsql_req*);
static void make_index(dsql_req*, const dsql_nod*, const dsql_nod*,
const dsql_nod*, const char*, const char*);
static void make_index_trg_ref_int(dsql_req*, dsql_nod*, dsql_nod*, dsql_nod*,
const char*, const char*);
static void modify_database(dsql_req*);
static void modify_domain(dsql_req*);
static void modify_field(dsql_req*, dsql_nod*, SSHORT, const dsql_str*);
static void modify_index(dsql_req*);
static void modify_privilege(dsql_req*, NOD_TYPE, SSHORT, const UCHAR*,
const dsql_nod*, const dsql_nod*, const dsql_str*);
static SCHAR modify_privileges(dsql_req*, NOD_TYPE, SSHORT, const dsql_nod*,
const dsql_nod*, const dsql_nod*);
static void modify_relation(dsql_req*);
static void modify_udf(dsql_req*);
static dsql_par* parameter_reverse_order(dsql_par* parameter, dsql_par* prev);
static void process_role_nm_list(dsql_req*, SSHORT, dsql_nod*, dsql_nod*, NOD_TYPE);
static void put_descriptor(dsql_req*, const dsc*);
static void put_dtype(dsql_req*, const dsql_fld*, bool);
static void put_field(dsql_req*, dsql_fld*, bool);
static void put_local_variable(dsql_req*, dsql_var*, dsql_nod*, const dsql_str*);
static void put_local_variables(dsql_req*, dsql_nod*, SSHORT);
static void put_msg_field(dsql_req*, dsql_fld*);
static dsql_nod* replace_field_names(dsql_nod*, dsql_nod*, dsql_nod*, bool, const char*);
static void reset_context_stack(dsql_req*);
static void save_field(dsql_req*, const SCHAR*);
static void save_relation(dsql_req*, const dsql_str*);
static void set_statistics(dsql_req*);
static void stuff_default_blr(dsql_req*, const TEXT*, USHORT);
static void stuff_matching_blr(dsql_req*, const dsql_nod*, const dsql_nod*);
static void stuff_trg_firing_cond(dsql_req*, const dsql_nod*);
static void set_nod_value_attributes(dsql_nod*, const dsql_fld*);
static void clearPermanentField (dsql_rel*, bool);
#ifdef BLKCHK
#undef BLKCHK
#endif
#ifdef DEV_BUILD
static inline void BLKCHK(const void* p, const USHORT type)
{
if (p && MemoryPool::blk_type(p) != type) {
ERRD_bugcheck("Invalid block type");
}
}
#else
static inline void BLKCHK(const void* p, const USHORT type)
{
}
#endif
enum trigger_type {
PRE_STORE_TRIGGER = 1,
POST_STORE_TRIGGER = 2,
PRE_MODIFY_TRIGGER = 3,
POST_MODIFY_TRIGGER = 4,
PRE_ERASE_TRIGGER = 5,
POST_ERASE_TRIGGER = 6
};
const int DEFAULT_BUFFER = 2048;
const int DEFAULT_BLOB_SEGMENT_SIZE = 80; // bytes
static const USHORT blr_dtypes[] = {
0,
blr_text, // dtype_text
blr_cstring, // dtype_cstring
blr_varying, // dtype_varying
0,
0,
0, // dtype_packed
0, // dtype_byte
blr_short, // dtype_short
blr_long, // dtype_long
blr_quad, // dtype_quad
blr_float, // dtype_real
blr_double, // dtype_double
blr_double, // dtype_d_float
blr_sql_date, // dtype_sql_date
blr_sql_time, // dtype_sql_time
blr_timestamp, // dtype_timestamp
blr_blob, // dtype_blob // ASF: CAST use blr_blob2 because blr_blob don't fit in UCHAR
blr_short, // dtype_array
blr_int64 // dtype_int64
};
static const UCHAR nonnull_validation_blr[] = {
blr_version5,
blr_not,
blr_missing,
blr_fid, 0, 0, 0,
blr_eoc
};
static inline bool hasOldContext(const int value)
{
const int val1 = ((value + 1) >> 1) & 3;
const int val2 = ((value + 1) >> 3) & 3;
const int val3 = ((value + 1) >> 5) & 3;
return (val1 && val1 != 1) || (val2 && val2 != 1) || (val3 && val3 != 1);
}
static inline bool hasNewContext(const int value)
{
const int val1 = ((value + 1) >> 1) & 3;
const int val2 = ((value + 1) >> 3) & 3;
const int val3 = ((value + 1) >> 5) & 3;
return (val1 && val1 != 3) || (val2 && val2 != 3) || (val3 && val3 != 3);
}
void DDL_execute(dsql_req* request)
{
/**************************************
*
* D D L _ e x e c u t e
*
**************************************
*
* Functional description
* Call access method layered service DYN
* to interpret dyn string and perform
* metadata updates.
*
**************************************/
tsql* tdsql = DSQL_get_thread_data();
#ifdef DSQL_DEBUG
if (DSQL_debug & 4) {
dsql_trace("Output DYN string for DDL:");
PRETTY_print_dyn(request->req_blr_data.begin(),
gds__trace_printer, NULL, 0);
}
#endif
const USHORT length = request->req_blr_data.getCount();
THREAD_EXIT();
ISC_STATUS s =
isc_ddl(tdsql->tsql_status, &request->req_dbb->dbb_database_handle,
&request->req_trans, length,
(const char*)(request->req_blr_data.begin()));
THREAD_ENTER();
// for delete & modify, get rid of the cached relation metadata
const dsql_str* string = NULL;
SYM_TYPE sym_type;
const dsql_nod* relation_node;
const NOD_TYPE type = request->req_ddl_node->nod_type;
switch (type)
{
case nod_mod_relation:
case nod_redef_relation:
relation_node = request->req_ddl_node->nod_arg[e_alt_name];
string = (dsql_str*) relation_node->nod_arg[e_rln_name];
// fall into
case nod_mod_view:
case nod_replace_view:
case nod_redef_view:
case nod_del_relation:
case nod_del_view:
if (!string)
string = (dsql_str*) request->req_ddl_node->nod_arg[e_alt_name];
sym_type = SYM_relation;
METD_drop_relation(request, string);
break;
case nod_mod_procedure:
case nod_del_procedure:
case nod_replace_procedure:
case nod_redef_procedure:
// for delete & modify, get rid of the cached procedure metadata
string = (dsql_str*) request->req_ddl_node->nod_arg[e_prc_name];
sym_type = SYM_procedure;
METD_drop_procedure(request, string);
break;
case nod_del_collation:
// for delete, get rid of the cached collation metadata
string = (dsql_str*) request->req_ddl_node->nod_arg[e_del_coll_name];
sym_type = SYM_intlsym_collation;
METD_drop_collation(request, string);
break;
case nod_del_udf:
case nod_mod_udf:
// Signal UDF for obsolescence
string = (dsql_str*) request->req_ddl_node->nod_arg[e_udf_name];
sym_type = SYM_udf;
METD_drop_function (request, string);
break;
}
if (s)
Firebird::status_exception::raise(tdsql->tsql_status);
#ifndef SUPERSERVER
if (string)
{
THREAD_EXIT();
s = gds__dsql_cache(tdsql->tsql_status,
&request->req_dbb->dbb_database_handle, DSQL_CACHE_RELEASE, sym_type, string->str_data, NULL);
THREAD_ENTER();
if (s)
Firebird::status_exception::raise(tdsql->tsql_status);
}
#endif // SUPERSERVER
}
void DDL_generate(dsql_req* request, dsql_nod* node)
{
/**************************************
*
* D D L _ g e n e r a t e
*
**************************************
*
* Functional description
* Generate the DYN string for a
* metadata update. Done during the
* prepare phase.
*
**************************************/
if (request->req_dbb->dbb_flags & DBB_read_only) {
ERRD_post(isc_read_only_database, 0);
return;
}
request->append_uchar(isc_dyn_version_1);
generate_dyn(request, node);
request->append_uchar(isc_dyn_eoc);
}
//
// Determine whether ids or names should be referenced
// when generating blr for fields and relations.
//
bool DDL_ids(const dsql_req* request)
{
return !request->req_ddl_node;
/*
const dsql_nod* ddl_node = request->req_ddl_node;
if (!ddl_node) {
return true;
}
switch (ddl_node->nod_type)
{
case nod_def_constraint:
case nod_def_computed:
case nod_def_view:
case nod_redef_view:
case nod_mod_view:
case nod_replace_view:
case nod_def_trigger:
case nod_redef_trigger:
case nod_mod_trigger:
case nod_replace_trigger:
case nod_def_procedure:
case nod_redef_procedure:
case nod_mod_procedure:
case nod_replace_procedure:
return false;
default:
return true;
}
*/
}
//
// Emit blr that describes a descriptor.
// Note that this depends on the same stuff variant
// as used in gen.cpp
//
void DDL_put_field_dtype(dsql_req* request, const dsql_fld* field, bool use_subtype)
{
put_dtype(request, field, use_subtype);
}
//
// See the next function for description. This is only a
// wrapper that sets the last parameter to false to indicate
// we are creating a field, not modifying one.
//
void DDL_resolve_intl_type(dsql_req* request, dsql_fld* field, const dsql_str* collation_name)
{
DDL_resolve_intl_type2 (request, field, collation_name, false);
}
void DDL_resolve_intl_type2(dsql_req* request,
dsql_fld* field,
const dsql_str* collation_name,
bool modifying)
{
/**************************************
*
* D D L _ r e s o l v e _ i n t l _ t y p e 2
*
**************************************
*
* Function
* If the field is defined with a character set or collation,
* resolve the names to a subtype now.
*
* Also resolve the field length & whatnot.
*
* If the field is being created, it will pick the db-wide charset
* and collation if not specified. If the field is being modified,
* since we don't allow changes to those attributes, we'll go and
* calculate the correct old lenth from the field itself so DYN
* can validate the change properly.
*
* For International text fields, this is a good time to calculate
* their actual size - when declared they were declared in
* lengths of CHARACTERs, not BYTES.
*
**************************************/
if (field->fld_type_of_name)
{
if (ENCODE_ODS(request->req_dbb->dbb_ods_version, request->req_dbb->dbb_minor_version) < ODS_11_1)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -804,
isc_arg_gds, isc_dsql_feature_not_supported_ods,
isc_arg_number, 11,
isc_arg_number, 1,
// Feature not supported on ODS version older than %d.%d
0);
}
if (!METD_get_domain(request, field, field->fld_type_of_name))
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -607,
isc_arg_gds, isc_dsql_command_err,
isc_arg_gds, isc_dsql_domain_not_found,
isc_arg_string, field->fld_type_of_name,
// Specified domain or source field does not exist
0);
}
}
if ((field->fld_dtype > dtype_any_text) && field->fld_dtype != dtype_blob)
{
if (field->fld_character_set || collation_name ||
field->fld_flags & FLD_national)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204,
isc_arg_gds, isc_dsql_datatype_err,
isc_arg_gds, isc_collation_requires_text, 0);
}
return;
}
if (field->fld_dtype == dtype_blob)
{
if (field->fld_sub_type_name)
{
SSHORT blob_sub_type;
if (!METD_get_type(request,
reinterpret_cast<const dsql_str*>(field->fld_sub_type_name),
"RDB$FIELD_SUB_TYPE",
&blob_sub_type))
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204, isc_arg_gds,
isc_dsql_datatype_err, isc_arg_gds,
isc_dsql_blob_type_unknown, isc_arg_string,
((dsql_str*) field->fld_sub_type_name)->str_data, 0);
}
field->fld_sub_type = blob_sub_type;
}
if (field->fld_sub_type > isc_blob_text)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204, isc_arg_gds,
isc_dsql_datatype_err, isc_arg_gds,
isc_subtype_for_internal_use, 0);
}
if (field->fld_character_set && (field->fld_sub_type == isc_blob_untyped))
{
field->fld_sub_type = isc_blob_text;
}
if (field->fld_character_set && (field->fld_sub_type != isc_blob_text))
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204, isc_arg_gds,
isc_dsql_datatype_err, isc_arg_gds,
isc_collation_requires_text, 0);
}
if (collation_name && (field->fld_sub_type != isc_blob_text))
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204, isc_arg_gds,
isc_dsql_datatype_err, isc_arg_gds,
isc_collation_requires_text, 0);
}
if (field->fld_sub_type != isc_blob_text) {
return;
}
}
if (field->fld_character_set_id != 0 && !collation_name) {
// This field has already been resolved once, and the collation
// hasn't changed. Therefore, no need to do it again.
return;
}
if (modifying)
{
const dsql_rel* relation = request->req_relation;
const dsql_fld* afield = field->fld_next;
USHORT bpc = 0;
while (afield) {
// The first test is redundant.
if (afield != field && afield->fld_relation
&& !strcmp (afield->fld_name, field->fld_name))
{
fb_assert(afield->fld_relation == relation || !relation);
break;
}
afield = afield->fld_next;
}
if (afield) {
field->fld_character_set_id = afield->fld_character_set_id;
bpc = METD_get_charset_bpc (request, field->fld_character_set_id);
field->fld_collation_id = afield->fld_collation_id;
field->fld_ttype = afield->fld_ttype;
if (afield->fld_flags & FLD_national) {
field->fld_flags |= FLD_national;
}
else {
field->fld_flags &= ~FLD_national;
}
assign_field_length (field, bpc);
return;
}
}
if (!(field->fld_character_set || field->fld_character_set_id || // set if a domain
(field->fld_flags & FLD_national)))
{
// Attach the database default character set, if not otherwise specified
const dsql_str* dfl_charset = METD_get_default_charset(request);
if (dfl_charset)
{
field->fld_character_set = (dsql_nod*) dfl_charset;
}
else
{
/* If field is not specified with NATIONAL, or CHARACTER SET
* treat it as a single-byte-per-character field of character set NONE.
*/
assign_field_length (field, 1);
field->fld_ttype = 0;
if (!collation_name) {
return;
}
}
}
const char* charset_name = 0;
if (field->fld_flags & FLD_national) {
charset_name = NATIONAL_CHARACTER_SET;
}
else if (field->fld_character_set) {
charset_name = ((dsql_str*) field->fld_character_set)->str_data;
}
// Find an intlsym for any specified character set name & collation name
const dsql_intlsym* resolved_type = NULL;
if (charset_name)
{
const dsql_intlsym* resolved_charset =
METD_get_charset(request,
(USHORT) strlen(charset_name),
charset_name);
// Error code -204 (IBM's DB2 manual) is close enough
if (!resolved_charset)
{
// specified character set not found
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204,
isc_arg_gds, isc_dsql_datatype_err,
isc_arg_gds, isc_charset_not_found, isc_arg_string,
charset_name, 0);
}
field->fld_character_set_id = resolved_charset->intlsym_charset_id;
resolved_type = resolved_charset;
}
if (collation_name)
{
const dsql_intlsym* resolved_collation =
METD_get_collation(request, collation_name, field->fld_character_set_id);
if (!resolved_collation)
{
Firebird::MetaName charSetName;
if (charset_name)
charSetName = charset_name;
else
charSetName = METD_get_charset_name(request, field->fld_character_set_id);
// Specified collation not found
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204,
isc_arg_gds, isc_dsql_datatype_err,
isc_arg_gds, isc_collation_not_found,
isc_arg_string, collation_name->str_data,
isc_arg_string, charSetName.c_str(), 0);
}
// If both specified, must be for same character set
// A "literal constant" must be handled (charset as ttype_dynamic)
resolved_type = resolved_collation;
if ((field->fld_character_set_id != resolved_type->intlsym_charset_id)
&& (field->fld_character_set_id != ttype_dynamic))
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204, isc_arg_gds,
isc_dsql_datatype_err, isc_arg_gds,
isc_collation_not_for_charset, isc_arg_string,
collation_name->str_data, 0);
}
field->fld_explicit_collation = true;
}
assign_field_length (field, resolved_type->intlsym_bytes_per_char);
field->fld_ttype = resolved_type->intlsym_ttype;
field->fld_character_set_id = resolved_type->intlsym_charset_id;
field->fld_collation_id = resolved_type->intlsym_collate_id;
}
static void assign_field_length (
dsql_fld* field,
USHORT bytes_per_char)
{
/**************************************
*
* a s s i g n _ f i e l d _ l e n g t h
*
**************************************
*
* Function
* We'll see if the field's length fits in the maximum
* allowed field, including charset and space for varchars.
* Either we raise an error or assign the field's length.
* If the charlen comes as zero, we do nothing, although we
* know that DYN, MET and DFW will blindly set field length
* to zero if they don't catch charlen or another condition.
*
**************************************/
if (field->fld_character_length)
{
ULONG field_length = (ULONG) bytes_per_char *
field->fld_character_length;
if (field->fld_dtype == dtype_varying) {
field_length += sizeof(USHORT);
}
if (field_length > MAX_COLUMN_SIZE)
{
ERRD_post(isc_sqlerr, isc_arg_number, (SLONG) -204,
isc_arg_gds, isc_dsql_datatype_err,
isc_arg_gds, isc_imp_exc,
isc_arg_gds, isc_field_name, isc_arg_string,
field->fld_name, 0);
}
field->fld_length = (USHORT) field_length;
}
}
static bool is_array_or_blob(dsql_req* request, const dsql_nod* node)
{
/**************************************
*
* i s _ a r r a y _ o r _ b l o b
*
**************************************
*
* Functional description
* Return true if there is an array or blob in expression, else false.
* Array and blob expressions have limited usefullness in a computed
* expression - so we detect it here to report a syntax error at
* definition time, rather than a runtime error at execution.
*
**************************************/
BLKCHK(node, dsql_type_nod);
switch (node->nod_type) {
case nod_agg_count:
//case nod_count:
case nod_gen_id:
case nod_gen_id2:
case nod_dbkey:
case nod_current_date:
case nod_current_time:
case nod_current_timestamp:
case nod_constant:
case nod_strlen:
case nod_null:
case nod_substr:
case nod_internal_info:
return false;
case nod_via:
return is_array_or_blob(request, node->nod_arg[e_via_value_1]);
case nod_map:
{
const dsql_map* map = (dsql_map*) node->nod_arg[e_map_map];
return is_array_or_blob(request, map->map_node);
}
case nod_agg_max:
case nod_agg_min:
case nod_agg_average:
case nod_agg_total:
case nod_agg_average2:
case nod_agg_total2:
case nod_agg_list:
case nod_upcase:
case nod_lowcase:
case nod_negate:
return is_array_or_blob(request, node->nod_arg[0]);
case nod_cast:
{
const dsql_fld* fld = (dsql_fld*) node->nod_arg[e_cast_target];
if (fld->fld_dtype == dtype_blob || fld->fld_dtype == dtype_array)
return true;
}
return is_array_or_blob(request, node->nod_arg[e_cast_source]);
case nod_add:
case nod_subtract:
case nod_concatenate:
case nod_multiply:
case nod_divide:
case nod_add2:
case nod_subtract2:
case nod_multiply2:
case nod_divide2:
if (is_array_or_blob(request, node->nod_arg[0])) {
return true;
}
return is_array_or_blob(request, node->nod_arg[1]);
case nod_alias:
return is_array_or_blob(request, node->nod_arg[e_alias_value]);
case nod_udf:
{
const dsql_udf* userFunc = (dsql_udf*) node->nod_arg[0];
if (userFunc->udf_dtype == dtype_blob || userFunc->udf_dtype == dtype_array)
return true;
}
// parameters to UDF don't need checking, a blob or array can be passed
return false;
case nod_sys_function:
{
const dsql_str* name = (dsql_str*) node->nod_arg[e_sysfunc_name];
dsql_nod* nodeArgs = node->nod_arg[e_sysfunc_args];
Firebird::Array<const dsc*> args;
if (nodeArgs)
{
fb_assert(nodeArgs->nod_type == nod_list);
for (dsql_nod** p = nodeArgs->nod_arg;
p < nodeArgs->nod_arg + nodeArgs->nod_count; ++p)
{
MAKE_desc(request, &(*p)->nod_desc, *p, NULL);
args.add(&(*p)->nod_desc);
}
}
dsc desc;
DSqlDataTypeUtil(request).makeSysFunction(&desc, name->str_data, args.getCount(), args.begin());
return DTYPE_IS_BLOB_OR_QUAD(desc.dsc_dtype);
}
case nod_extract:
case nod_list:
{
const dsql_nod* const* const end = node->nod_arg + node->nod_count;
for (const dsql_nod* const* ptr = node->nod_arg; ptr < end; ++ptr)
{
if (is_array_or_blob(request, *ptr)) {
return true;
}
}
}
return false;
case nod_field:
case nod_coalesce:
case nod_simple_case:
case nod_searched_case:
if (node->nod_desc.dsc_dtype == dtype_blob ||
node->nod_desc.dsc_dtype == dtype_array)
{
return true;
}
return false;
case nod_trim:
return is_array_or_blob(request, node->nod_arg[e_trim_value]);
case nod_derived_field:
return is_array_or_blob(request, node->nod_arg[e_derived_field_value]);
default:
fb_assert(false);
return false;
}
}
static void check_constraint( dsql_req* request,
dsql_nod* element,
bool delete_trigger_required)
{
/**************************************
*
* c h e c k _ c o n s t r a i n t
*
**************************************
*
* Function
* Generate triggers to implement the CHECK
* clause, either at the field or table level.
*
**************************************/
dsql_nod* ddl_node = request->req_ddl_node;
if (!(element->nod_arg[e_cnstr_table])) {
element->nod_arg[e_cnstr_table] = ddl_node->nod_arg[e_drl_name];
}
// specify that the trigger should abort if the condition is not met
dsql_nod* list_node = MAKE_node(nod_list, 1);
element->nod_arg[e_cnstr_actions] = list_node;
list_node->nod_arg[0] = MAKE_node(nod_gdscode, 1);
dsql_nod** errorcode_node = &list_node->nod_arg[0]->nod_arg[0];
*errorcode_node = (dsql_nod*) MAKE_cstring("check_constraint");
// create the INSERT trigger
element->nod_arg[e_cnstr_type] =
MAKE_const_slong(PRE_STORE_TRIGGER);
define_constraint_trigger(request, element);
// create the UPDATE trigger
element->nod_arg[e_cnstr_type] =
MAKE_const_slong(PRE_MODIFY_TRIGGER);
define_constraint_trigger(request, element);
// create the DELETE trigger, if required
if (delete_trigger_required)
{
element->nod_arg[e_cnstr_type] =
MAKE_const_slong(PRE_ERASE_TRIGGER);
define_constraint_trigger(request, element);
}
request->append_uchar(isc_dyn_end); // For CHECK constraint definition
}
static void check_one_call (USHORT* repetition_count,
SSHORT pos,
const TEXT* error_msg)
{
/**************************************
*
* c h e c k _ o n e _ c a l l
*
**************************************
*
* Function
* Ensure that each option in modify_domain() is called only once.
* This restriction cannot be enforced by the DSQL parser.
*
**************************************/
if (++repetition_count[pos] > 1)
{
ERRD_post (isc_sqlerr, isc_arg_number, (SLONG) -637,
isc_arg_gds, isc_dsql_duplicate_spec,
isc_arg_string, error_msg,
0);
}
}
static void create_view_triggers(dsql_req* request, dsql_nod* element,
dsql_nod* items)
{ // Fields in the VIEW actually
/**************************************
*
* c r e a t e _ v i e w _ t r i g g e r s
*
**************************************
*
* Function
* Generate triggers to implement the WITH CHECK OPTION
* clause for a VIEW
*
**************************************/