-
Notifications
You must be signed in to change notification settings - Fork 1
/
D2_99_PGRERA.TPW
6238 lines (6128 loc) · 265 KB
/
D2_99_PGRERA.TPW
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
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#! File Name: D2PGRERA.tpw
#! Purpose: DCT to POSTGRESQL creation script
#! Author: Copyright © 1999-2999 by Roberto Artigas Jr
#! All rights reserved world wide.
#! Changes:
#! 2011.04.09 Roberto Artigas
#! * If there is no primary key, DO NOT output a sort order. (Ex. Temporary Work Files).
#! Added for the VIEWS sort order. For the tables remove EMULATEAUTONUMBER on the options.
#! 2010.09.28 Roberto Artigas
#! * There is a mismatched count condition that happens when a parent file with one key links
#! to a child file with two keys (Duke Dictionary). For example (invoice -> invoice+lineitem).
#! This causes mismatched keys items which are a problem with postgreSQL in the RELATIONS.
#! Solved using the lesser of the count for matching keys. For the above example I used only
#! one key relating (invoice -> invoice) which works correctly (for the purposes intended).
#! * The one relation problem that is ---NOT--- solved yet, is the one for the relation of the
#! lookup key. One of the sides has no key. This creates a count of 0 which produces ---NO---
#! relation. The correct solution is to make both keys equal and this is ---NOT--- done yet.
#! I will ponder the coding approach to this. At this time, I need a break from this template.
#! * SELF NOTE: I do not have this problem on my dictionary because of the design decisions I made.
#! I assigned unique keys to all the relations so I could track "PK" and "FK" constraints easily.
#! I also avoid file validation lookups via the relations. This area might need enhancement.
#! * The Duke Dictionary is one that will really test any template that does DCT to SQL conversions.
#! 2010.09.25 Roberto Artigas
#! * Enhanced "SchemaRelat" to deal with EXTERNAL file, key, and field names.
#! NOTE: There are some "Duke" dictionary conditions that cause errors (on the RELATION side).
#! * Enhanced "SchemaTrigr" to deal with EXTERNAL file, key, and field names.
#! * Enhanced "SchemaGrant" to deal with EXTERNAL file, key, and field names.
#! * Enhanced "SchemaValid" to deal with EXTERNAL file, key, and field names.
#! 2010.09.17 Roberto Artigas
#! * TO DO: Other "Schema*" need to be changed to deal with the EXTERNAL name for tables, keys, and columns.
#! * Duke DCT moved to PostgreSQL with the TABLES, KEYS and FIELDS.
#! * Enhanced "SchemaIndex" to deal with EXTERNAL file, key, and field names.
#! * Enhanced "SchemaViews" to deal with EXTERNAL file, key, and field names.
#! 2010.09.13 Roberto Artigas
#! * Enhanced template for EXTERNAL table, key, and field names.
#! * Added messages to show field that are ARRAYS that are not being processed processed.
#! * Decision to leave array key showing to that the DDL errors out unless RASQL(NO).
#! 2010.09.10 Roberto Artigas
#! * Changed the location of the 'databases_and_schemas.txt' file to the dictionary directory.
#! 2009.04.11 Roberto Artigas
#! * Adjusted logic for numeric processing to handle the picture '@N-_13.8' and
#! get the correct number of digits and decimal places.
#! * Now uses the dictionary options or the 'databases_and_schemas.txt' file
#! for the database names and schema names.
#! 2009.03.28 Roberto Artigas
#! * Adjusted the search_path to include public. Additional custom procedures and aggregates
#! where added to public and are used in views, so I need to be able to find them.
#! 2009.03.09 Roberto Artigas
#! * Adjusted the logic for data type REAL and DOUBLE PRECISION so
#! it would generate correctly for the backend.
#! 2009.02.25 Roberto Artigas
#! * Group all schema processing blocks for all sections.
#! * Implemented option 3 for generating single database-schema.
#! 2009.02.21 Roberto Artigas
#! * Change sequence from FILE NAME to FILE PREFIX for smaller size.
#! 2009.02.19 Roberto Artigas
#! * Continue adding schema logic for option 2 of the case statement. VALIDATION, TRIGGER, VIEW.
#! 2009.02.18 Roberto Artigas
#! * Continue adding schema logic for option 2 of the case statement. INDEX, RELATION.
#! 2009.01.27 Roberto Artigas
#! * Begin adding schema for option 2 of the case statement. TABLE, GRANT.
#! 2009.01.20 Roberto Artigas
#! * Table creation option now generating schema only when there is a table.
#! The schema drop still generates for everything just in case of errors.
#! * Put the following options in the correct place in the case statement:
#! grant, index, relat, valid, trigr, views.
#! 2009.01.01 Roberto Artigas
#! * Create new variant to add multi-database, multi-schema features.
#! * Got case statement working correctly for create option.
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#GROUP(%MITLicenseD2PGRERA)
#TAB('D2PGRERA'),PROP(PROP:FontStyle,700)
#BOXED('MIT License'),AT(,,278),PROP(PROP:FontStyle,700)
#DISPLAY('')
#DISPLAY('Copyright © 1999 by Roberto Artigas y Soler.'),PROP(PROP:FontStyle,700)
#DISPLAY('')
#DISPLAY('Permission is hereby granted, free of charge, to any person ')
#DISPLAY('obtaining a copy of this software and associated documentation ')
#DISPLAY('files (the "Software"), to deal in the software without ')
#DISPLAY('restriction, including without limitation the rights to use, ')
#DISPLAY('copy, modify, merge, publish, distribute, sublicense, and/or sell ')
#DISPLAY('copies of the Software, and to permit persons to whom the Software ')
#DISPLAY('is furnished to do so, subject to the following conditions: ')
#DISPLAY('')
#DISPLAY('The above copyright notice and this permission notice shall be '),PROP(PROP:FontStyle,700)
#DISPLAY('included in all copies or substantial portions of the Software.'),PROP(PROP:FontStyle,700)
#DISPLAY('')
#DISPLAY('THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ')
#DISPLAY('EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ')
#DISPLAY('MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ')
#DISPLAY('NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT ')
#DISPLAY('HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, ')
#DISPLAY('WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ')
#DISPLAY('OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ')
#DISPLAY('DEALINGS IN THE SOFTWARE.')
#ENDBOXED
#ENDTAB
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#UTILITY(Dct2PostgreSQL_RobertArtigas, '2016.02.27: Dictionary to POSTGRESQL script (SCHEMA Version)')
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#BOXED('Hidden Variables'),HIDE
#PROMPT('DictName: ' ,@S40 ),%DictName
#PROMPT('DictPath: ' ,@S80 ),%DictPath
#PROMPT('nLoc1: ' ,@N4 ),%nLoc1
#PROMPT('nLoc1a: ' ,@N4 ),%nLoc1a #! RA.2009.04.11
#PROMPT('nLoc2: ' ,@N4 ),%nLoc2
#PROMPT('aFileUser: ' ,@s40 ),%aFileUser #! RA.2008.09.17
#PROMPT('aFileOpt1: ' ,@s40 ),%aFileOpt1 #! RA.2008.09.17
#PROMPT('aFileOpt2: ' ,@s40 ),%aFileOpt2 #! RA.2008.09.17
#PROMPT('aFileOpt3: ' ,@s40 ),%aFileOpt3 #! RA.2008.09.17
#PROMPT('aFileOpt4: ' ,@s40 ),%aFileOpt4 #! RA.2008.09.17
#PROMPT('DCTSCH: ' ,@S120),%dctsch #! RA.2010.09.10
#PROMPT('OutTableName: ',@S120),%OutTableName
#PROMPT('OutGrantName: ',@S120),%OutGrantName
#PROMPT('OutIndexName: ',@S120),%OutIndexName
#PROMPT('OutRelatName: ',@S120),%OutRelatName
#PROMPT('OutValidName: ',@S120),%OutValidName
#PROMPT('OutTrigrName: ',@S120),%OutTrigrName
#PROMPT('OutViewsName: ',@S120),%OutViewsName
#ENDBOXED
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#PREPARE
#!----------------------------------------------------------------!
#! RA.2010.09.10 - Copied this code to the front of the template. !
#!----------------------------------------------------------------!
#SET(%nLoc1,INSTRING('\',%DictionaryFile,1,1))
#LOOP,WHILE(%nLoc1)
#SET(%nLoc2,%nLoc1)
#SET(%nLoc1,INSTRING('\',%DictionaryFile,1,%nLoc1+1))
#ENDLOOP
#SET(%DictName,UPPER(SUB(%DictionaryFile,%nLoc2+1,LEN(%DictionaryFile)-%nLoc2-4)))
#SET(%DictPath,UPPER(SUB(%DictionaryFile,1,%nLoc2)))
#!
#SET(%SQLDirectory,%DictPath)
#SET(%SQLTableName,%DictName & '_01_PG.SQL')
#SET(%SQLGrantName,%DictName & '_02_PG.SQL')
#SET(%SQLIndexName,%DictName & '_03_PG.SQL')
#SET(%SQLRelatName,%DictName & '_04_PG.SQL')
#SET(%SQLValidName,%DictName & '_05_PG.SQL')
#SET(%SQLTrigrName,%DictName & '_06_PG.SQL')
#SET(%SQLViewsName,%DictName & '_07_PG.SQL')
#!
#!--------------------------------------------------------!
#! RA.2009.01.01 - Created this in the front of template. !
#!--------------------------------------------------------!
#DECLARE(%dctUserOptions)
#DECLARE(%dctDatabaseID)
#DECLARE(%dctDatabase)
#DECLARE(%dctSchemaID)
#DECLARE(%dctSchema)
#!
#DECLARE(%dctOptions)
#DECLARE(%dctOpt01)
#DECLARE(%dctOpt02)
#DECLARE(%dctOpt03)
#DECLARE(%dctOpt04)
#!
#! #EQUATE(%dctsch,'databases_and_schemas.txt') #! RA.2009.04.11
#EQUATE(%dctschRec,'') #! RA.2009.04.11
#DECLARE(%A1,LONG) #! RA.2009.04.11
#DECLARE(%A2,LONG) #! RA.2009.04.11
#DECLARE(%A3,LONG) #! RA.2009.04.11
#SET(%dctsch, %DictPath & 'databases_and_schemas.txt') #! RA.2010.09.10
#!
#DECLARE(%dctDB),MULTI,UNIQUE
#DECLARE(%DBid,%dctDB)
#DECLARE(%DBname,%dctDB)
#DECLARE(%DBtext,%dctDB)
#!
#DECLARE(%dctSC),MULTI,UNIQUE
#DECLARE(%SCid,%dctSC)
#DECLARE(%SCname,%dctSC)
#DECLARE(%SCtext,%dctSC)
#!
#DECLARE(%P,LONG)
#DECLARE(%L,LONG)
#SET(%dctUserOptions,%DictionaryUserOptions)
#!-----------------------------------------------------------------------------------!
#DECLARE(%multiOpt) #! RA.2009.01.01
#SET(%multiOpt,1) #! RA.2009.01.01
#! DATABASE: ALL, SCHEMA: ALL : %multiOpt = 1
#! DATABASE: <*>, SCHEMA: ALL : %multiOpt = 2
#! DATABASE: <*>, SCHEMA: <*> : %multiOpt = 3
#! DATABASE: ALL, SCHEMA: <*> : %multiOpt = 4
#!-----------------------------------------------------------------------------------!
#! RA.2009.04.11 - See the procedure that loads the options from a file. !
#!-----------------------------------------------------------------------------------!
#! RA.2009.03.29 - There is a limit to the amount of data the dictionary options !
#! will hold (size of record) and I have reached it. I might need in the future to !
#! to load these DATABASE_ and SCHEMA_ options from external files. !
#!-----------------------------------------------------------------------------------!
#SET(%dctDatabaseID, 0)
#ADD(%dctDB, 'ALL')
#SET(%DBid, %dctDatabaseID)
#SET(%DBname, 'ALL')
#SET(%DBtext, 'Create ALL databases')
#!--------------------------------------------------------------------
#SET(%dctDatabaseID, 1)
#SET(%dctDatabase, 'DATABASE_' & FORMAT(%dctDatabaseID,@N02))
#LOOP
#SET(%dctOptions, EXTRACT(%dctUserOptions, %dctDatabase))
#IF(NOT %dctOptions)
#BREAK
#ENDIF
#SET(%dctOpt01, EXTRACT(%dctUserOptions, %dctDatabase, 1))
#SET(%dctOpt02, EXTRACT(%dctUserOptions, %dctDatabase, 2))
#SET(%dctOpt03, EXTRACT(%dctUserOptions, %dctDatabase, 3))
#IF(UPPER(SUB(%dctOpt01,1,1)) <> 'N')
#ADD(%dctDB, %dctOpt02)
#SET(%DBid, %dctDatabaseID)
#SET(%DBname, %dctOpt02)
#SET(%DBtext, %dctOpt03)
#ENDIF
#SET(%dctDatabaseID, %dctDatabaseID + 1)
#SET(%dctDatabase, 'DATABASE_' & FORMAT(%dctDatabaseID,@N02))
#ENDLOOP
#!--------------------------------------------------------------------
#SET(%dctSchemaID, 0)
#ADD(%dctSC, 'ALL')
#SET(%SCid, %dctSchemaID)
#SET(%SCname, 'ALL')
#SET(%SCtext, 'Create all schemas')
#!--------------------------------------------------------------------
#SET(%dctSchemaID, 1)
#SET(%dctSchema, 'SCHEMA_' & FORMAT(%dctSchemaID,@N03))
#LOOP
#SET(%dctOptions, EXTRACT(%dctUserOptions, %dctSchema))
#IF(NOT %dctOptions)
#BREAK
#ENDIF
#SET(%dctOpt01, EXTRACT(%dctUserOptions, %dctSchema, 1))
#SET(%dctOpt02, EXTRACT(%dctUserOptions, %dctSchema, 2))
#SET(%dctOpt03, EXTRACT(%dctUserOptions, %dctSchema, 3))
#IF(UPPER(SUB(%dctOpt01,1,1)) <> 'N')
#ADD(%dctSC, %dctOpt02)
#SET(%SCid, %dctSchemaID)
#SET(%SCname, %dctOpt02)
#SET(%SCtext, %dctOpt03)
#ENDIF
#SET(%dctSchemaID, %dctSchemaID + 1)
#SET(%dctSchema, 'SCHEMA_' & FORMAT(%dctSchemaID,@N03))
#ENDLOOP
#!-----------------------------------------------------------------------------------!
#! RA.2009.04.11 - This is procedure that loads the options from a file. !
#!-----------------------------------------------------------------------------------!
#IF(FILEEXISTS(%dctsch)) #! RA.2009.04.11
#! #! RA.2009.04.11
#OPEN(%dctsch),READ
#LOOP
#READ(%dctschRec)
#IF(%dctschRec = %EOF)
#BREAK
#ELSE
#!
#SET(%dctOpt01, '')
#SET(%A1, 1)
#SET(%A2, INSTRING('|', %dctschRec, 1, %A1))
#SET(%A3, %A2 - %A1)
#!! (01) %A1, %A2, %A3
#SET(%dctOpt01, SUB(%dctschRec, %A1, %A3))
#!
#SET(%dctOpt02, '')
#SET(%A1, %A2 + 1)
#SET(%A2, INSTRING('|', %dctschRec, 1, %A1))
#SET(%A3, %A2 - %A1)
#!! (02) %A1, %A2, %A3
#SET(%dctOpt02, SUB(%dctschRec, %A1, %A3))
#!
#SET(%dctOpt03, '')
#SET(%A1, %A2 + 1)
#SET(%A2, LEN(%dctschRec) + 1)
#SET(%A3, %A2 - %A1)
#!! (03) %A1, %A2, %A3
#SET(%dctOpt03, SUB(%dctschRec, %A1, %A3))
#!
#CASE(UPPER(%dctOpt01))
#OF('DATABASE')
#FIND(%DBname,%dctOpt02)
#IF(%DBName = '')
#SET(%dctDatabaseID, ITEMS(%dctDB) + 1)
#ADD(%dctDB, %dctOpt02)
#SET(%DBid, %dctDatabaseID)
#SET(%DBname, %dctOpt02)
#SET(%DBtext, %dctOpt03)
#ENDIF
#OF('SCHEMA')
#FIND(%SCname,%dctOpt02)
#IF(%SCName = '')
#SET(%dctSchemaID, ITEMS(%dctSC) + 1)
#ADD(%dctSC, %dctOpt02)
#SET(%SCid, %dctSchemaID)
#SET(%SCname, %dctOpt02)
#SET(%SCtext, %dctOpt03)
#ENDIF
#ENDCASE
#ENDIF
#ENDLOOP
#CLOSE(%dctsch),READ
#! #! RA.2009.04.11
#ENDIF #! RA.2009.04.11
#!-----------------------------------------------------------------------------------!
#!-----------------------------------------------------------------------------------!
#!
#!-----------------------------------------------------------------------------------!
#! RA.2008.09.17 - Created this in the front of template. !
#! This will create the correct tables list for the SPECIFIC table grant area. !
#!-----------------------------------------------------------------------------------!
#DECLARE(%GrantFile),MULTI,UNIQUE #! RA.2008.09.17
#FOR(%File) #! RA.2008.09.17
#!---------------------------------------------------------------!
#! RA.2001.12.01 - Ignore file place marker definitions. !
#! These are files that are defined but that contain no fields. !
#!---------------------------------------------------------------!
#IF(NOT ITEMS(%Field)) #! RA.2001.12.01
#CYCLE #! RA.2001.12.01
#ENDIF #! RA.2001.12.01
#SET(%nLoc1,INSTRING('RASQL(',%FileUserOptions,1,1))
#SET(%nLoc2,INSTRING(')',%FileUserOptions,1,%nLoc1))
#SET(%aFileUser,UPPER(SUB(%FileUserOptions,%nLoc1,%nLoc2)))
#!SET(%aFileUser,UPPER(EXTRACT(%FileUserOptions,'RASQL')))
#SET(%aFileOpt1,EXTRACT(%aFileUser,'RASQL',1))
#SET(%aFileOpt2,EXTRACT(%aFileUser,'RASQL',2))
#SET(%aFileOpt3,EXTRACT(%aFileUser,'RASQL',3))
#SET(%aFileOpt4,EXTRACT(%aFileUser,'RASQL',4))
#!-------------------------------------------------------------!
#! The first option RASQL(NO) controls if a file is processed. !
#!-------------------------------------------------------------!
#IF(SUB(%aFileOpt1,1,1)='N')
#CYCLE
#ENDIF
#!
#CASE(%FileType)
#OF ('VIEW')
#OROF('ALIAS')
#CYCLE
#ENDCASE
#!
#CASE(%FileDriver)
#OF('ASCII')
#OROF('BASIC')
#OROF('DOS')
#OROF('IN-MEMORY')
#CYCLE
#ENDCASE
#!
#ADD(%GrantFile,%File) #! RA.2008.09.17
#ENDFOR #! RA.2008.09.17
#ENDPREPARE
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#BOXED('POSTGRESQL SCRIPT CREATOR')
#DISPLAY('')
#DISPLAY('The POSTGRESQL Script Creator.')
#DISPLAY('Copyright 2002-2015 © by Roberto Artigas Jr.')
#DISPLAY('All Rights Reserved World Wide.')
#DISPLAY('')
#ENDBOXED
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#SHEET,HSCROLL,AT(,,288)
#INSERT(%MITLicenseD2PGRERA)
#TAB('Multi-Process')
#BOXED(''),AT(,,278)
#DISPLAY('Allowable options are:')
#DISPLAY('')
#DISPLAY('1) DATABASE: ALL, SCHEMA: ALL')
#DISPLAY('Default option. Everything generated in public'),AT(20)
#DISPLAY('unless other options change the behavior.'),AT(20)
#DISPLAY('')
#DISPLAY('2) DATABASE: <*>, SCHEMA: ALL')
#DISPLAY('Specific database and all schemas generated.'),AT(20)
#DISPLAY('')
#DISPLAY('3) DATABASE: <*>, SCHEMA: <*>')
#DISPLAY('Specific database and specific schema generated.'),AT(20)
#DISPLAY('')
#DISPLAY('4) DATABASE: ALL, SCHEMA: <*>')
#DISPLAY('All databases and specific schema generated.'),AT(20)
#DISPLAY('')
#DISPLAY('')
#DISPLAY('')
#PROMPT('DATABASE: ' ,FROM(%dctDB)),%multiDB,AT(82,,100),DEFAULT('ALL')
#DISPLAY('')
#PROMPT('SCHEMA: ' ,FROM(%dctSC)),%multiSC,AT(82,,100),DEFAULT('ALL')
#!VALIDATE((%multiSC = 'ALL') AND (%multiDB <> 'ALL'), 'This option is NOT suported.')
#ENDBOXED
#ENDTAB
#!
#TAB('General')
#BOXED(''),AT(,,278)
#DISPLAY('')
#PROMPT('Directory:' ,@S80 ),%SQLDirectory,AT(66,,210)
#DISPLAY('')
#PROMPT('Generate CREATE?' ,CHECK),%SQLTable,DEFAULT(%TRUE),AT(10)
#PROMPT('CREATE Script:' ,@S40 ),%SQLTableName,AT(96,,180)
#!DISPLAY('')
#PROMPT('Generate GRANT? ' ,CHECK),%SQLGrant,DEFAULT(%FALSE),AT(10)
#PROMPT('GRANT Script:' ,@S40 ),%SQLGrantName,AT(96,,180)
#!DISPLAY('')
#PROMPT('Generate KEYS?' ,CHECK),%SQLIndex,DEFAULT(%TRUE),AT(10)
#PROMPT('KEYS Script:' ,@S40 ),%SQLIndexName,AT(96,,180)
#!DISPLAY('')
#PROMPT('Generate RELATION?' ,CHECK),%SQLRelat,DEFAULT(%FALSE),AT(10)
#ENABLE(%SQLRelat)
#PROMPT('Allow for Deferrable Constraints?',CHECK),%SQLRelatDefer,DEFAULT(%FALSE),AT(20)
#ENDENABLE
#PROMPT('RELATION Script:' ,@S40 ),%SQLRelatName,AT(96,,180)
#!DISPLAY('')
#PROMPT('Generate VALIDATE?' ,CHECK),%SQLValid,DEFAULT(%FALSE),AT(10)
#ENABLE(%SQLValid)
#PROMPT('Generate CHECK?' ,CHECK),%SQLValidCheck,DEFAULT(%FALSE),AT(20)
#ENDENABLE
#PROMPT('VALIDATE Script:' ,@S40 ),%SQLValidName,AT(96,,180)
#!DISPLAY('')
#PROMPT('Generate TRIGGER?' ,CHECK),%SQLTrigr,DEFAULT(%FALSE),AT(10)
#ENABLE(%SQLTrigr)
#PROMPT('Connect TRIGGER?' ,CHECK),%SQLTrigrConnect,DEFAULT(%FALSE),AT(20)
#ENDENABLE
#PROMPT('TRIGGER Script:' ,@S40 ),%SQLTrigrName,AT(96,,180)
#!DISPLAY('')
#PROMPT('Generate VIEWS?' ,CHECK),%SQLViews,DEFAULT(%TRUE),AT(10)
#PROMPT('VIEWS Script:' ,@S40 ),%SQLViewsName,AT(96,,180)
#ENDBOXED
#ENDTAB
#!----------------------------------------------------------!
#! RA.2008.06.29 - Added enhancements to the GRANT options. !
#!----------------------------------------------------------!
#TAB('Grant')
#BOXED(''),AT(,,278)
#PROMPT('Grant ALL priviledges ', CHECK), %optGrantAll, DEFAULT(0), AT(10)
#ENABLE(%optGrantAll)
#PROMPT('Grant ALL user name: ', @S40), %optGrantAllUser, DEFAULT('postgres')
#ENDENABLE
#DISPLAY('')
#!#DISPLAY('Grants for ALL tables')
#PROMPT('Generate grants for ALL tables ', CHECK), %optGrantDoit, DEFAULT(1), AT(10)
#BUTTON('Tables: User or Groups'), MULTI(%optGrant, %optGrantType & ' ' & %optGrantName & ' ' & (%optGrantSelect + %optGrantInsert + %optGrantUpdate + %optGrantDelete)), INLINE, AT(,,,100)
#PROMPT('Type of Grant: ',OPTION), %optGrantType, AT(,,100), DEFAULT('User'), REQ
#PROMPT('&User', RADIO)
#PROMPT('&Group', RADIO)
#PROMPT('User or Group Name: ', @S40 ), %optGrantName, DEFAULT('public'), REQ
#PROMPT('Select', CHECK), %optGrantSelect, DEFAULT(1)
#PROMPT('Insert', CHECK), %optGrantInsert, DEFAULT(1)
#PROMPT('Update', CHECK), %optGrantUpdate, DEFAULT(1)
#PROMPT('Delete', CHECK), %optGrantDelete, DEFAULT(1)
#ENDBUTTON
#DISPLAY('')
#!#DISPLAY('Grants for SPECIFIC tables')
#PROMPT('Generate grants for SPECIFIC tables ', CHECK), %opt2GrantDoit, DEFAULT(0), AT(10)
#BUTTON('Table: User or Groups'), MULTI(%opt2Grant, %opt2GrantTable & ' ' & %opt2GrantType & ' ' & %opt2GrantName & ' ' & (%opt2GrantSelect + %opt2GrantInsert + %opt2GrantUpdate + %opt2GrantDelete)), INLINE, AT(,,,100)
#PROMPT('Table: ', FROM (%GrantFile)), %opt2GrantTable, AT(,,100), DEFAULT(''), REQ
#PROMPT('Type of Grant: ',OPTION), %opt2GrantType, AT(,,100), DEFAULT('User'), REQ
#PROMPT('&User', RADIO)
#PROMPT('&Group', RADIO)
#PROMPT('User or Group Name: ', @S40 ), %opt2GrantName, DEFAULT('public'), REQ
#PROMPT('Select', CHECK), %opt2GrantSelect, DEFAULT(1)
#PROMPT('Insert', CHECK), %opt2GrantInsert, DEFAULT(1)
#PROMPT('Update', CHECK), %opt2GrantUpdate, DEFAULT(1)
#PROMPT('Delete', CHECK), %opt2GrantDelete, DEFAULT(1)
#ENDBUTTON
#ENDBOXED
#ENDTAB
#PREPARE
#IF(~ITEMS(%optGrant))
#ADD(%optGrant,'User')
#ENDIF
#ENDPREPARE
#!----------------------------------------------------------------------!
#! RA.2007.07.17 - Added ignore reserved words and schema name options. !
#!----------------------------------------------------------------------!
#TAB('Options')
#BOXED('General'),AT(,,278)
#PROMPT('Ignore NOT NULL for all keys ',CHECK),%IgnoreNotNullKeys,AT(10),DEFAULT(%TRUE)
#PROMPT('Ignore NOT NULL for all fields ',CHECK),%IgnoreNotNullFields,AT(10),DEFAULT(%TRUE)
#DISPLAY('')
#PROMPT('Use SEQUENCE for auto-increment ',CHECK),%UseSerial,AT(10),DEFAULT(%TRUE)
#PROMPT('Key fields type INT to BIGINT ', CHECK),%MakeIntBigint,AT(10),DEFAULT(%True) #! RA.2008.07.01
#DISPLAY('')
#PROMPT('Ignore reserved words ',CHECK),%IgnoreReserved,AT(10),DEFAULT(%TRUE)
#DISPLAY('')
#PROMPT('Create table and column names in lowercase',CHECK),%UseLowerCase,AT(10),Default(%True) #! MAG 02.21.08
#DISPLAY('')
#! #PROMPT('Generate OWNER for tables ',CHECK),%UseOwner,AT(10),DEFAULT(0)
#! #ENABLE(%UseOwner)
#! #PROMPT('Owner Name:',@S40),%OwnerName,AT(82,,100),DEFAULT('postgres')
#! #ENDENABLE
#! #DISPLAY('')
#PROMPT('Use schema name ',CHECK),%UseSchemaName,AT(10),DEFAULT(%FALSE)
#ENABLE(%UseSchemaName)
#PROMPT('Schema Name:',@S40),%SchemaName,AT(82,,100),DEFAULT('public')
#ENDENABLE
#ENDBOXED
#BOXED('Prefixes'),AT(,,278)
#PROMPT('Prefix Options ',OPTION),%UsePrefix,DEFAULT('None')
#PROMPT('None',RADIO),AT(15)
#PROMPT('All',RADIO),AT(15)
#PROMPT('Some',RADIO),AT(15)
#ENABLE(%UsePrefix='Some')
#PROMPT('Use file prefix for table names ',CHECK),%PrefixTables,AT(10),DEFAULT(%False)
#PROMPT('Use file prefix for column names ',CHECK),%PrefixColumns,AT(10),DEFAULT(%False)
#PROMPT('Use file prefix for key names ',CHECK),%PrefixKeys,AT(10),DEFAULT(%False)
#PROMPT('Use file prefix for relation names ',CHECK),%PrefixRelations,AT(10),DEFAULT(%False)
#ENDENABLE
#ENDBOXED
#BOXED('Comments'),AT(,,278)
#PROMPT('Generate database comments ',CHECK),%CommentsDB,AT(10),DEFAULT(%True)
#PROMPT('Generate SQL script comments ',CHECK),%CommentsSQL,AT(10),DEFAULT(%True)
#ENDBOXED
#ENDTAB
#!$$------------------------------------------------!
#!$$ DEJ.2007.05.08 - Use External Field Name
#!$$------------------------------------------------!
#TAB('External/Arrays') #!$$
#BOXED('External'),AT(,,278) #!$$
#DISPLAY('The default is to generate a create script that uses') #!$$
#DISPLAY('any EXTERNAL names defined in your dictionary.') #!$$
#DISPLAY('In addition, Postgres will create the column names in') #!$$
#DISPLAY('lowercase unless the name is generated within quotes.') #!$$
#DISPLAY('') #!$$
#PROMPT('Generate EXTERNAL Table Name',CHECK),%UseExtTableName,AT(10),DEFAULT(0) #! RA.2010.09.13
#PROMPT('Generate EXTERNAL Key Name',CHECK),%UseExtKeyName,AT(10),DEFAULT(0) #! RA.2010.09.13
#ENABLE(%UseExtKeyName) #!$$
#PROMPT('Generate Key in Quotes',CHECK),%UseExtKeyQuote,AT(25),DEFAULT(0) #!$$
#ENDENABLE #!$$
#PROMPT('Generate EXTERNAL Column Name',CHECK),%UseExtFieldName,AT(10),DEFAULT(0) #!$$
#ENABLE(%UseExtFieldName) #!$$
#PROMPT('Generate Column in Quotes',CHECK),%UseExtFieldQuote,AT(25),DEFAULT(0) #!$$
#ENDENABLE #!$$
#ENDBOXED #!$$
#BOXED('Arrays'),AT(,,278)
#DISPLAY('There are two ways to handle arrays:')
#DISPLAY('')
#DISPLAY('1) You defined a group with all the fields of the array')
#DISPLAY('inside this group. Then you put the array OVER the')
#DISPLAY('group. If you set up your arrays in this manner, then')
#DISPLAY('LEAVE the checkbox below OFF.')
#DISPLAY('')
#DISPLAY('2) You define your arrays as actual fields using no')
#DISPLAY('groups whatsoever. If you set up your arrays this way,')
#DISPLAY('then TURN ON the checkbox bellow.')
#DISPLAY('')
#PROMPT('I DO NOT HAVE ARRAYS OVER GROUPS. ',CHECK),%NoArrayOver,AT(10),DEFAULT(0)
#ENDBOXED
#ENDTAB #!$$
#!$$------------------------------------------------!
#!$$ End of Use External Name
#!$$------------------------------------------------!
#! #TAB('Arrays')
#! #BOXED('')
#! #DISPLAY('There are two ways to handle arrays:')
#! #DISPLAY('')
#! #DISPLAY('1) You defined a group with all the fields of the array')
#! #DISPLAY('inside this group. Then you put the array OVER the')
#! #DISPLAY('group. If you set up your arrays in this manner, then')
#! #DISPLAY('LEAVE the checkbox below OFF.')
#! #DISPLAY('')
#! #DISPLAY('2) You define your arrays as actual fields using no')
#! #DISPLAY('groups whatsoever. If you set up your arrays this way,')
#! #DISPLAY('then TURN ON the checkbox bellow.')
#! #DISPLAY('')
#! #PROMPT('I DO NOT HAVE ARRAYS OVER GROUPS. ',CHECK),%NoArrayOver,AT(10),DEFAULT(0)
#! #ENDBOXED
#! #ENDTAB
#TAB('Varchar/Picture')
#BOXED('Varchar'),AT(,,278)
#DISPLAY('PostgreSQL supports two primary character types.')
#DISPLAY(' char(n) - fixed length, blank padded')
#DISPLAY(' varchar(n) - variable length with limit')
#DISPLAY('')
#DISPLAY('The storage requirement for varchar(n) is four bytes plus the')
#DISPLAY('actual string with a maximum limit of n characters.')
#DISPLAY('So, if you expect a string to be less than max characters')
#DISPLAY('minus 4 bytes for more than half of the records, you should')
#DISPLAY('probably turn it into a varchar.')
#DISPLAY('To optimaly tune this, you have to edit the script produced by')
#DISPLAY('the template on a field to field basis.')
#DISPLAY('')
#PROMPT('Varchar special processing ', CHECK), %VarCharSpecial, AT(10),DEFAULT(0)
#DISPLAY('')
#ENABLE(%VarCharSpecial)
#PROMPT('Varchar for strings >= : ', @N4),%VarcharLen,AT(,,30,10),DEFAULT(256)
#ENDENABLE
#ENDBOXED
#BOXED('Picture'),AT(,,278)
#DISPLAY('You may convert PICTURE-fields to CHAR(n) by')
#DISPLAY('checking this option.')
#DISPLAY('')
#DISPLAY('If not, the fields are showing up as ***PICTURE***')
#DISPLAY('and you have to edit the generated script for this')
#DISPLAY('fieldtype.')
#DISPLAY('')
#PROMPT('Make PICTURES as CHAR(n). ',CHECK),%PicAsChar,AT(10),DEFAULT(0)
#ENDBOXED
#ENDTAB
#! #TAB('Picture')
#! #BOXED('')
#! #DISPLAY('You may convert PICTURE-fields to CHAR(n) by')
#! #DISPLAY('checking this option.')
#! #DISPLAY('')
#! #DISPLAY('If not, the fields are showing up as ***PICTURE***')
#! #DISPLAY('and you have to edit the generated script for this')
#! #DISPLAY('fieldtype.')
#! #DISPLAY('')
#! #PROMPT('Make PICTURES as CHAR(n). ',CHECK),%PicAsChar,AT(10),DEFAULT(0)
#! #DISPLAY('')
#! #ENDBOXED
#! #ENDTAB
#TAB('UUID Processing')
#BOXED(''),AT(,,278)
#DISPLAY('PostGreSQL 8.3.x now supports UUID/GUID data types.')
#DISPLAY('')
#DISPLAY('For this to work in these templates the contrib module')
#DISPLAY('uuid-ossp must be installed in the database - PostGreSQL')
#DISPLAY('manual - Appendix F')
#DISPLAY('')
#DISPLAY('You can also set up to call a default algorithm just like')
#DISPLAY('an auto-incremented column.')
#DISPLAY('')
#DISPLAY('This is accomplished by using the field options in the')
#DISPLAY('dictionary as follows:')
#DISPLAY('')
#DISPLAY('RASQL(,UUID) will generate the UUID with no default')
#DISPLAY('UUID')
#DISPLAY('')
#DISPLAY('RASQL(,UUID1) will generate the statement')
#DISPLAY('UUID DEFAULT uuid_generate_v1()')
#DISPLAY('')
#DISPLAY('RASQL(,UUID2) will generate the statement')
#DISPLAY('UUID DEFAULT uuid_generate_v1mc()')
#DISPLAY('')
#DISPLAY('RASQL(,UUID3) will generate the statement')
#DISPLAY('UUID DEFAULT uuid_generate_v4()')
#DISPLAY('')
#DISPLAY('On the Clarion dictionary side this needs to be defined')
#DISPLAY('as a CSTRING(37).')
#ENDBOXED
#ENDTAB
#ENDSHEET
#! #BOXED('Hidden Variables'),HIDE
#! #PROMPT('Force BOOLEAN validation to [Y,N] choices? ',CHECK),%ForceBoolYN,AT(10),DEFAULT(1)
#! #PROMPT('Do KEYFIELD-FILE list to create relations? ',CHECK),%DoFieldFile,AT(10),DEFAULT(0)
#! #PROMPT('True Value (Y,T,1): ',@S1),%TrueValue,REQ,DEFAULT('Y')
#! #PROMPT('False Value (N,F,0): ',@S1),%FalseValue,REQ,DEFAULT('N')
#! #ENDBOXED
#EQUATE(%ForceBoolYN, 1) #! RA.2012.02.22
#EQUATE(%DoFieldFile, 0) #! RA.2012.02.22
#EQUATE(%TrueValue, 'Y') #! RA.2012.02.22
#EQUATE(%FalseValue, 'N') #! RA.2012.02.22
#!---------------------------------------------------------------------
#! #PREPARE
#! #!
#! #SET(%nLoc1,INSTRING('\',%DictionaryFile,1,1))
#! #LOOP,WHILE(%nLoc1)
#! #SET(%nLoc2,%nLoc1)
#! #SET(%nLoc1,INSTRING('\',%DictionaryFile,1,%nLoc1+1))
#! #ENDLOOP
#! #SET(%DictName,UPPER(SUB(%DictionaryFile,%nLoc2+1,LEN(%DictionaryFile)-%nLoc2-4)))
#! #SET(%DictPath,UPPER(SUB(%DictionaryFile,1,%nLoc2)))
#! #!
#! #SET(%SQLDirectory,%DictPath)
#! #SET(%SQLTableName,%DictName & '_01_PG.SQL')
#! #SET(%SQLGrantName,%DictName & '_02_PG.SQL')
#! #SET(%SQLIndexName,%DictName & '_03_PG.SQL')
#! #SET(%SQLRelatName,%DictName & '_04_PG.SQL')
#! #SET(%SQLValidName,%DictName & '_05_PG.SQL')
#! #SET(%SQLTrigrName,%DictName & '_06_PG.SQL')
#! #SET(%SQLViewsName,%Name & '_07_PG.SQL')
#! #!
#! #ENDPREPARE
#!---------------------------------------------------------------------
#! SQL specific options
#DECLARE(%NFileOpt0) #! File option 0 - EMULATEAUTONUMKEY(TRUE)
#DECLARE(%NFieldOpt0) #! Field option 0 - IsIdentity(TRUE)
#DECLARE(%NComment) #! Generate comments with QUOTE'd strings
#DECLARE(%WorkIt) #! Temporary variable
#!
#DECLARE(%NFileUser) #! File user options [RASQL]
#DECLARE(%NFileOpt1) #! File option 1 [NO]
#DECLARE(%NFileOpt2) #! File option 2 [EXTERNAL table name]
#DECLARE(%NFileOpt3) #! File option 3
#DECLARE(%NFileOpt4) #! File option 4
#!
#DECLARE(%NFieldUser) #! Field user options [RASQL]
#DECLARE(%NFieldOpt1) #! Field option 1 [NO]
#DECLARE(%NFieldOpt2) #! Field option 2 [<backend database type override>]
#DECLARE(%NFieldOpt3) #! Field option 3 [<backend database default value>]
#DECLARE(%NFieldOpt4) #! Field option 4
#!
#DECLARE(%NKeyUser) #! Key user options [RASQL]
#DECLARE(%NKeyOpt1) #! Key option 1 [NO]
#DECLARE(%NKeyOpt2) #! Key option 2 [EXTERNAL key name]
#DECLARE(%NKeyOpt3) #! Key option 3
#DECLARE(%NKeyOpt4) #! Key option 4
#!
#DECLARE(%NRelationUser) #! Relation user options [RASQL]
#DECLARE(%NRelationOpt1) #! Relation option 1 [NO]
#DECLARE(%NRelationOpt2) #! Relation option 2
#DECLARE(%NRelationOpt3) #! Relation option 3
#DECLARE(%NRelationOpt4) #! Relation option 4
#!
#DECLARE(%NFileName) #! File Name
#DECLARE(%NFilePrefix) #! File Prefix - RA.2008.07.19
#DECLARE(%NFileSequence) #! SQL CREATE SEQUENCE - RA.2010.09.13
#DECLARE(%NFileType) #! TYPE=???
#DECLARE(%NFileRowFormat) #! ROW_FORMAT=???
#!
#DECLARE(%NFieldPKey) #! Field is Primary Key?
#DECLARE(%NFieldLine) #! Field line
#DECLARE(%NFieldName) #! Field Name
#DECLARE(%NFieldTSql) #! Field SQL type
#DECLARE(%NFieldTDef) #! Field SQL default
#DECLARE(%NFieldType) #! Field type
#DECLARE(%NFieldDef) #! Field default
#DECLARE(%NFieldCheck) #! Field CHECK(...) constraint
#DECLARE(%NFieldCheckOver) #! Field CHECK(...) constraint override
#DECLARE(%NFieldAuto) #! Field AUTO_INCREMENT
#DECLARE(%NFieldNN) #! Field NOT NULL
#DECLARE(%NFieldSize ,LONG) #! Field decimal size
#DECLARE(%NFieldDec ,LONG) #! Field decimal places
#DECLARE(%NLeadZero ,LONG) #! Have a leading zero
#DECLARE(%NFNameAuto) #! Field Name Auto Increment
#DECLARE(%NVarcharLen ,LONG) #! Length before turning to VARCHAR
#SET(%NVarcharLen, %VarcharLen) #! Turn the input field to a long
#!
#DECLARE(%NKeyName) #! Key name
#DECLARE(%NKeyFields) #! Key building line
#DECLARE(%NKeyField) #! Key field current
#DECLARE(%NKeyField1) #! Key field current (from)
#DECLARE(%NKeyField2) #! Key field current (to)
#DECLARE(%NKeyFieldCount) #! Number of key fields
#DECLARE(%PrimaryCount) #! Primary key exists
#DECLARE(%SecondaryCount) #! Secondary key exists
#DECLARE(%AutoIncrementCount) #! Auto-Increment key exists
#!
#DECLARE(%NRelateSequence) #! Unique relation sequence number #! RA.2010.09.25
#SET(%NRelateSequence, 0) #! RA.2010.09.25
#DECLARE(%NRelateSave) #! Save the current relationship #! RA.2010.09.25
#DECLARE(%FileKeyFieldSave) #! Save the current file-key-field #! RA.2010.09.25
#DECLARE(%NRelateItem) #! Working item number
#DECLARE(%NRelateCount) #! Total items to work
#DECLARE(%NRelateComma) #! Number of commas
#DECLARE(%NRelateFile) #! Related file
#DECLARE(%NRelateField1) #! Related origin field
#DECLARE(%NRelateField2) #! Related relate field
#DECLARE(%RelationField1Count) #! Count of origin file key items
#DECLARE(%RelationField2Count) #! Count of relate file key items
#DECLARE(%RelationField1) #! The origin file keys
#DECLARE(%RelationField2) #! The relate file keys
#DECLARE(%RelationUpdate) #! ON UPDATE ?
#DECLARE(%RelationDelete) #! ON DELETE ?
#DECLARE(%NRelateSchema1) #! Related origin schema
#DECLARE(%NRelateSchema2) #! Related relate schema
#!
#!#DECLARE(%nLoc1 ,LONG) #! Location 1
#!#DECLARE(%nLoc2 ,LONG) #! Location 2
#DECLARE(%TheCount ,LONG) #! Count posible relations
#DECLARE(%CStringLen) #! Length of types shortened by one
#DECLARE(%LastField) #! Last field processed
#DECLARE(%InnerGroup) #! Inner group level processed
#DECLARE(%FieldComma) #! Number of fields - 1
#!
#DECLARE(%ChoiceLine) #! Build ENUM field type line
#DECLARE(%ChoiceComma) #! Count ENUM values - 1
#!
#DECLARE(%DocVar) #! Documentation Variable
#DECLARE(%TableCount, 0) #! Count of tables
#DECLARE(%AutoCount, 0) #! Count of Auto-Increment Keys
#DECLARE(%PKeyCount, 0) #! Count of primary keys (From Keys)
#DECLARE(%PKeyOption, 0) #! Count of primary keys (From Options)
#DECLARE(%KeyOthers, 0) #! Count of secondary keys
#DECLARE(%KeyCount, 0) #! Count of all keys combined
#DECLARE(%FieldCount, 0) #! Count of fields
#DECLARE(%RelCount, 0) #! Relationship Counts
#DECLARE(%FieldTable),MULTI,UNIQUE #! List of unique fields
#DECLARE(%FieldUnique, 0) #! Count of unique fields
#!
#DECLARE(%TheDate) #! Display current date
#DECLARE(%TheTime) #! Display current time
#SET(%TheDate,FORMAT(TODAY(),@D010)) #! Set current date
#SET(%TheTime,FORMAT(CLOCK(),@T06)) #! Set current time
#!
#DECLARE(%DimCount ,LONG) #! Number of dimensions
#DECLARE(%FldArray ,STRING) #! Created field name from array
#DECLARE(%FD1S ,STRING) #! Number for dimension 1
#DECLARE(%FD2S ,STRING) #! Number for dimension 2
#DECLARE(%FD3S ,STRING) #! Number for dimension 3
#DECLARE(%FD4S ,STRING) #! Number for dimension 4
#DECLARE(%FD1I ,LONG) #! Looping in Dimension 1
#DECLARE(%FD2I ,LONG) #! Looping in Dimension 2
#DECLARE(%FD3I ,LONG) #! Looping in Dimension 3
#DECLARE(%FD4I ,LONG) #! Looping in Dimension 4
#DECLARE(%FD1L ,LONG) #! Set to %FieldDimension1
#DECLARE(%FD2L ,LONG) #! Set to %FieldDimension2
#DECLARE(%FD3L ,LONG) #! Set to %FieldDimension3
#DECLARE(%FD4L ,LONG) #! Set to %fieldDimension4
#!
#DECLARE(%ValTypeDCT ,STRING) #! Original type from dictionary
#DECLARE(%ValType ,STRING) #! Validation type for backend
#DECLARE(%ValLength ,LONG) #! String length
#DECLARE(%ValNumeric ,LONG) #! Length of entry box
#DECLARE(%ValDecimal ,LONG) #! Number of decimal places
#DECLARE(%ValTrue ,STRING) #! TRUE Value
#DECLARE(%ValFalse ,STRING) #! FALSE Value
#DECLARE(%ValI ,LONG) #! Index into list of values
#DECLARE(%ValList),MULTI,UNIQUE #! List of Values
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#DECLARE(%optGrantCount ,LONG) #! Number of options created #! RA.2008.06.29
#DECLARE(%optGrantTotal ,LONG) #! Number of options checked on #! RA.2008.06.29
#DECLARE(%optGrantLine ,STRING) #! Grant option line #! RA.2008.06.29
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#DECLARE(%TriggerBefore ,LONG) #! RA.2008.06.30
#DECLARE(%TriggerAfter ,LONG) #! RA.2008.06.30
#DECLARE(%TriggerList),MULTI #! RA.2008.06.30
#DECLARE(%TriggerWhen ,%TriggerList) #! RA.2008.06.30
#DECLARE(%TriggerOp ,%TriggerList) #! RA.2008.06.30
#DECLARE(%TriggerStmt ,%TriggerList) #! RA.2008.06.30
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#DECLARE(%ProcFileFound, LONG) #! RA.2009.01.20
#DECLARE(%ProcFile),MULTI,UNIQUE #! RA.2007.11.25
#DECLARE(%ProcFileName,%ProcFile) #! RA.2009.01.01
#DECLARE(%ProcFileDB,%ProcFile) #! RA.2009.01.01
#DECLARE(%ProcFileSC,%ProcFile) #! RA.2009.01.01
#FOR(%File)
#ADD(%ProcFile,%File)
#SET(%ProcFileName,%File) #! RA.2009.01.01
#!
#SET(%dctOpt01, '')
#SET(%dctOpt02, '')
#SET(%dctOpt03, '')
#SET(%dctOpt04, '')
#SET(%dctOpt01, EXTRACT(%FileUserOptions, 'DATABASE', 1))
#SET(%dctOpt02, EXTRACT(%FileUserOptions, 'DATABASE', 2))
#SET(%dctOpt03, EXTRACT(%FileUserOptions, 'SCHEMA' , 1))
#SET(%dctOpt04, EXTRACT(%FileUserOptions, 'SCHEMA' , 2))
#!
#IF(NOT %dctOpt02)
#SET(%dctOpt02, '?')
#ELSE
#FIND(%DBname, %dctOpt02)
#IF(NOT %DBname)
#SET(%dctOpt02, '*BAD*')
#ENDIF
#ENDIF
#IF(NOT %dctOpt04)
#SET(%dctOpt04, 'public')
#ELSE
#IF(%dctOpt04 <> 'public')
#FIND(%SCname, %dctOpt04)
#IF(NOT %SCname)
#SET(%dctOpt04, '*BAD*')
#ENDIF
#ENDIF
#ENDIF
#!
#SET(%ProcFileDB,%dctOpt02) #! RA.2009.01.01
#SET(%ProcFileSC,%dctOpt04) #! RA.2009.01.01
#ENDFOR
#!--------------------------------------------------------------!
#! List of table, key and rows names found in reserved list. !
#!--------------------------------------------------------------!
#DECLARE(%ReservedFieldList),MULTI,UNIQUE
#EQUATE (%FoundFileName ,'File Name: ')
#EQUATE (%FoundFileField ,'File Field Name: ')
#EQUATE (%FoundAIKeyName ,'AI Key Name: ')
#EQUATE (%FoundKeyName ,'Key Name: ')
#EQUATE (%FoundKeyField ,'Key Field Name: ')
#EQUATE (%FoundRelatedName ,'Related File Name: ')
#EQUATE (%FoundRelatedField ,'Related Field Name: ')
#!-------------------------------------------------------------------------!
#! RA.2001.12.15 - Table of field-table to be able to create relations. !
#!-------------------------------------------------------------------------!
#DECLARE(%FieldTableList),MULTI,UNIQUE
#!---------------------------------------------------------------------
#DECLARE(%LFieldInKey),MULTI,UNIQUE #! Field list that are used in keys
#DECLARE(%IFieldInKey) #! Field item to search for in list
#DECLARE(%LFieldInKeyCount) #! How many fields are in keys?
#FREE(%LFieldInKey)
#FOR(%ProcFile) #! RA.2007.11.25
#FIX(%File,%ProcFile) #! RA.2007.11.25
#IF(ITEMS(%Key))
#FOR(%Key)
#FOR(%KeyField)
#ADD(%LFieldInKey,%KeyField)
#ENDFOR
#ENDFOR
#ENDIF
#ENDFOR
#SET(%LFieldInKeyCount,ITEMS(%LFieldInKey))
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#INSERT(%ClarionReservedWords)
#INSERT(%SqlReservedWords)
#FOR(%ClarionWordList)
#ADD(%SqlWordList,%ClarionWordList)
#ENDFOR
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#SET(%TableCount, 0) #! Count of tables
#SET(%AutoCount, 0) #! Count of Auto-Increment Keys
#SET(%PKeyCount, 0) #! Count of primary keys (From Keys)
#SET(%PKeyOption, 0) #! Count of primary keys (From Options)
#SET(%KeyOthers, 0) #! Count of secondary keys
#SET(%KeyCount, 0) #! Count of all keys combined
#SET(%FieldCount, 0) #! Count of fields
#SET(%RelCount, 0) #! Relationship Counts
#SET(%FieldUnique, 0) #! Count of unique fields
#!---------------------------------------------------------------------
#! RA.2009.01.01 - Set the multi-database, multi-schema options now.
#!---------------------------------------------------------------------
#IF (%multiDB <> 'ALL' AND %multiSC = 'ALL')
#SET(%multiOpt, 2) #! RA.2009.01.01 - <*>,ALL
#ELSIF(%multiDB <> 'ALL' AND %multiSC <> 'ALL')
#SET(%multiOpt, 3) #! RA.2009.01.01 - <*>,<*>
#ELSIF(%multiDB = 'ALL' AND %multiSC <> 'ALL')
#SET(%multiOpt, 4) #! RA.2009.01.01 - ALL,<*>
#ELSE
#SET(%multiOpt, 1) #! RA.2009.01.01 - ALL,ALL
#ENDIF
#!---------------------------------------------------------------------
#!---------------------------------------------------------------------
#IF(%SQLTable)
#CASE(%multiOpt)
#OF(2) #! RA.2009.01.01 - <*>,ALL
#SET(%SQLTableName,%DictName & '__' & %multiDB & '__01_PG.SQL')
#SET(%OutTableName,%SQLDirectory & %SQLTableName)
#MESSAGE('The POSTGRESQL Script Creator [<*>,ALL]',0)
#MESSAGE('Creating: ' & %SQLTableName,1)
#CREATE(%OutTableName)
--
-- BOF: %OutTableName
--
-- SQL script creator for POSTGRESQL database %multiDB [TABLE]
-- Created: %TheDate - %TheTime
--
#FIND(%DBname,%multiDB)
COMMENT ON DATABASE %DBname IS '%DBtext' ;
#FOR(%dctSC),WHERE(%SCid > 0)
DROP SCHEMA IF EXISTS %SCname CASCADE ;
CREATE SCHEMA %SCname ;
COMMENT ON SCHEMA %SCname IS '%SCtext' ;
#ENDFOR
#FOR(%dctSC),WHERE(%SCid > 0)
#SET(%ProcFileFound, %FALSE)
#FOR(%ProcFile),WHERE((%ProcFileDB = %multiDB) AND (%ProcFileSC = %SCname))
#!--00-- ProcFile='%ProcFile', ProcFileDB='%ProcFileDB', ProcFileSC='%ProcFileSC'
#SET(%ProcFileFound, %TRUE)
#BREAK
#ENDFOR
#IF(NOT %ProcFileFound)
#CYCLE
#ENDIF
SET search_path TO %SCname, public, pg_catalog ;
#FOR(%ProcFile),WHERE((%ProcFileDB = %multiDB) AND (%ProcFileSC = %SCname))
#INSERT(%SchemaTable) #! RA.2009.02.25
#ENDFOR
#ENDFOR
--
-- EOF: %OutTableName
--
#CLOSE(%OutTableName)
#RUN('NOTEPAD.EXE "' & %OutTableName & '"')
#OF(3) #! RA.2009.01.01 - <*>,<*>
#SET(%SQLTableName,%DictName & '__' & %multiDB & '__' & %multiSC & '__01_PG.SQL')
#SET(%OutTableName,%SQLDirectory & %SQLTableName)
#MESSAGE('The POSTGRESQL Script Creator [<*>,<*>]',0)
#MESSAGE('Creating: ' & %SQLTableName,1)
#CREATE(%OutTableName)
--
-- BOF: %OutTableName
--
-- SQL script creator for POSTGRESQL database %multiDB [TABLE]
-- Created: %TheDate - %TheTime
--
#FOR(%dctSC),WHERE((%SCid > 0) AND (%SCname = %multiSC))
#MESSAGE('The POSTGRESQL Script Creator [<*>,<*>]',0)
DROP SCHEMA IF EXISTS %SCname CASCADE ;
CREATE SCHEMA %SCname ;
COMMENT ON SCHEMA %SCname IS '%SCtext' ;
#ENDFOR
#FOR(%dctSC),WHERE((%SCid > 0) AND (%SCname = %multiSC))
#SET(%ProcFileFound, %FALSE)
#FOR(%ProcFile),WHERE((%ProcFileDB = %multiDB) AND (%ProcFileSC = %SCname))
#!--00-- ProcFile='%ProcFile', ProcFileDB='%ProcFileDB', ProcFileSC='%ProcFileSC'