File: sql_to_c.awk

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (813 lines) | stat: -rw-r--r-- 25,746 bytes parent folder | download | duplicates (2)
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
#
# sql_to_c.awk
#
# $Id$
#
#  Embeds SQL code into a C file
#
#  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
#  project.
#
#  Copyright (C) 1998-2012 OpenLink Software
#
#  This project is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; only version 2 of the License, dated June 1991.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#

function strip_comments(text, pl_stats, arr)
{
  #print "***IN_STRIP***" text "***"
  output = ""
  inx = 1
  first_line = -1
  nlines = split (text, lines, "\n")
  while (inx <= nlines)
   {
     curline = lines[inx]
     res_line = ""
     had_comment = 0
     #print "***LINE " inx "***" curline "***"
     comm_start = index (curline, "--")
     #print "***COM_START=" comm_start "***"
     special_comment = index (curline, "--!AWK")
     special_comment2 = index (curline, "--!AFTER")
     if_comment = index (curline, "--#IF VER=")
     endif_comment = index (curline, "--#ENDIF")
     if (in_if && endif_comment > 0)
       {
	 in_if = 0
       }	

     if (if_comment > 0)
       {
	 match (curline, /[0-9]+/, arr)
	 ver = arr[0] + 0
	 if (ver == srv_ver)
	   {
	      inx = inx + 1
	      continue
	   }
	 else
	   in_if = 1
       }	

     if (in_if)
       {
	 inx = inx + 1
	 continue
       }
     else if (special_comment > 0)
       {
	 res_line = substr (curline, special_comment + 3) "@"
       }
     else if (special_comment2 > 0)
       {
	 res_line = substr (curline, special_comment2 + 3) "@"
	 print AFTER
       }
     else
       {
	 while (comm_start > 0)
	   {
	     before_comment = substr (curline, 1, comm_start - 1)
	     #print "***BEFORE_COMMENT=" before_comment "***"

	     after_comment = substr (curline, comm_start + 2)
	     # print "***AFTER_COMMENT=" after_comment "***"

	     n_sk = split (before_comment, dummy, "'") - 1
	     # print "***N_SK=" n_sk "***"

	     res_line = res_line before_comment
	     if (n_sk % 2 > 0)
	       { # have open single quotes
		 res_line = res_line "--"
		 curline = after_comment
	       }
	     else
	       {
		 # This was = 1, but for PL stats we need lines to be preserved
		 if (pl_stats == "PLDBG")
		   had_comment = 0
		 else
		   had_comment = 1
		 curline = ""
	       }
	     comm_start = index (curline, "--")
	   }
	 gsub (/[\t\n\r\ ]+$/, "", res_line)
	 if (inx < nlines)
	   res_line = res_line curline "\n"
	 else
	   res_line = res_line curline
       }

     if (first_line == -1 && length (res_line) > 2 && special_comment == 0 && special_comment2 == 0)
       {
         first_line = inx - 1
       }
     if (match (res_line, /[^\t\n\r\ ]/) > 0 || had_comment == 0)
       {
         output = output res_line
       }
     else
       while (inx < nlines && length (lines[inx + 1]) == 0)
	 {
           inx = inx + 1
	 }
     inx = inx + 1
   }
  #print "***END_STRIP***" text "***"
  arr[0] = output
  arr[1] = first_line
#return (arr)
}


function get_awk_macro_defines (awk_command)
{
  np = split (awk_command, p, "[ \t\n]+")
  if (toupper (p[1]) != "AWK")
    return ("")

  if (np > 1)
    p[2] = toupper (p[2])
  text = "/* AWK macro " awk_command "*/"
  if (np > 4 && p[2] == "UPGRADE" && toupper (p[3]) == "TABLE")
    {
      tb_name = p[4]
      col_name = p[5]

      text = "\n  macro_tb = sch_name_to_table (isp_schema(NULL), \"" tb_name "\");"
      text = text "\n  if (macro_tb && !tb_name_to_column (macro_tb, \"" col_name "\"))  "
      text = text "\n    ddl_ensure_table (\"drop dead\", \"drop table " tb_name "\");  "
      n_upgraded_tables = n_upgraded_tables + 1
    }
  if (np > 1 && p[2] == "PUBLIC")
    {
      text = " "
      define_proc_macro = "DEFINE_PUBLIC_PROC"
    }
  else if (np > 1 && p[2] == "OVERWRITE")
    {
      text = " "
      define_proc_macro = "DEFINE_OVERWRITE_PROC"
    }
  else if (np > 1 && p[2] == "STORED")
    {
      text = " "
      define_proc_macro = "DEFINE_STORED_PROC"
      stored_proc = 1
      if (np > 2 && p[3] == "AFTER")
        {
          _defines = defines_arfw
          after_rfw = 1
	}
    }
  else if (np > 2 && p[2] == "PLBIF")
    {
      bif_text = bif_text "\n  pl_bif_name_define (\"" p[3] "\");"
      define_proc_macro = "DEFINE_PUBLIC_PROC"
    }


  return (text)
}

BEGIN   {
          while (getline < "sqlver.h" > 0)
	  {
	     if(match ($0, /^#define DBMS_SRV_VER_ONLY/))
	       {	
	         res = match ($0, /[0-9]+/, arr)
		 srv_ver = arr[0] + 0
                 break	
	       }
	  }
	  close ("sqlver.h")
	  defines = ""
	  defines_arfw = ""
	  nproc = 0
	  ntable = 0
	  nview = 0
	  nindexes = 0
	  ntriggers = 0
	  nother = 0
	  nalter = 0
	  nudt = 0
	  nalterudt = 0
	  stored_proc = 0
	  bif_text = ""
	  RS = "\n;\n"
	  print "/* This file is automatically generated by sql_to_c.awk */\n"
	  print "#include \"sqlnode.h\"\n#include \"sqlfn.h\"\n#include \"sqltype.h\"\n"
	  filename = FILENAME
	  nfiles = 1
	  end_name = ""
	  qualifier = "DB"
	  n_upgraded_tables = 0
	  n_upgraded_tables_arfw = 0
	  define_proc_macro = "DEFINE_PROC"
	  has_qualifier = 0
	  has_qualifier_arfw = 0
	  cur_line_no = 1
	  in_xsl_mode = 0
	  first_xsl_rec = 1
	  in_xsd_mode = 0
	  first_xsd_rec = 1
	  n_xslts = 0
	  n_xsds = 0
	  in_if = 0
	}

	{
	        if (filename != FILENAME)
		  {
#		    print "/*  processing file " FILENAME " */"
		    if (in_xsl_mode > 0)
		      {
			#print "\"'))\";\n"
			print ",\n NULL };\n"
			in_xsl_mode = 0
		      }
		    if (in_xsd_mode > 0)
		      {
			#print "\"')\";\n"
			print ",\n NULL };\n"
			in_xsd_mode = 0
		      }
		    cur_line_no = 1
		    n = split (FILENAME, path_parts_array, "/")
		    print "/* " path_parts_array[n] " */\n"
		    end_name = path_parts_array[n]
		    if (filename != "")
		      {
			n = split (filename, path_parts_array, "/")
			if (length (defines) > 0)
			  {
			    if (qualifier != "DB")
			      {
				has_qualifier = 1
				files[nfiles] = \
				  "  /* " path_parts_array[n] " */\n" \
				  "  bootstrap_cli->cli_qualifier = box_string (\"" qualifier "\");\n" \
				  defines "\n\n" \
				  "  dk_free_box (bootstrap_cli->cli_qualifier);\n" \
				  "  bootstrap_cli->cli_qualifier =  saved_qualifier;"
			      }
			    else
				files[nfiles] = "  /* " path_parts_array[n] " */\n" defines
			  }
		        if (length (defines_arfw) > 0)
			  {
			    if (qualifier != "DB")
			      {
				has_qualifier_arfw = 1
				files_arfw[nfiles] = \
				  "  /* " path_parts_array[n] " */\n" \
				  "  bootstrap_cli->cli_qualifier = box_string (\"" qualifier "\");\n" \
				  defines_arfw "\n\n" \
				  "  dk_free_box (bootstrap_cli->cli_qualifier);\n" \
				  "  bootstrap_cli->cli_qualifier =  saved_qualifier;"
			      }
			    else
				files_arfw[nfiles] = "  /* " path_parts_array[n] " */\n" defines_arfw
			  }

			defines = ""
			defines_arfw = ""
			qualifier = "DB"
			nfiles = nfiles + 1
		      }
		    if (0 < match (FILENAME, /.*\.xsl/))
		      {
		        n2 = split (FILENAME, path_parts_array2, "/")
			stylesheet_name = substr (path_parts_array2[n2], 0, index (path_parts_array2[n2], ".") - 1)
#			stylesheet_name = path_parts_array2[n]
#			print "static const char *xsl" nfiles "= \"xslt_sheet ('__" stylesheet_name "', xml_tree_doc ('\""
			print "static const char *xsl" nfiles "[]= {"
			in_xsl_mode = 1
			first_xsl_rec = 1
			n_xslts = n_xslts + 1
#			defines = \
#			  defines "\n ddl_ensure_table (\"do this always\", xsl" nfiles ");"
			defines = \
			  defines "\n  XSLT_DEFINE (\"http://local.virt/" stylesheet_name  "\", xsl" nfiles ");"
		      }
		    if (0 < match (FILENAME, /.*\.xsd/))
		      {
		        n2 = split (FILENAME, path_parts_array2, "/")
			schema_name = substr (path_parts_array2[n], 0, index (path_parts_array2[n], ".") - 1)
#			schema_name = path_parts_array2[n]
#			print "static const char *xsd" nfiles "= \"xml_reload_schema_decl ('http://local.virt/" schema_name ".xsd', 'http://local.virt/" schema_name ".xsd', 'UTF-8', 'x-any', '\""
			print "static const char *xsd" nfiles "[]= {"
			in_xsd_mode = 1
			first_xsd_rec = 1
			n_xsds = n_xsds + 1
#			defines = \
#			  defines "\n ddl_ensure_table (\"do this always\", xsd" nfiles ");"
			defines = \
			  defines "\n  XSD_DEFINE (\"http://local.virt/" schema_name  ".xsd\", xsd" nfiles ");"
		      }
		    filename = FILENAME
		  }
		last_line_no = cur_line_no
		cur_line_no = cur_line_no + split ($0, tmp_lines, "\n") + 1

                #print "/*\n" $0 "\n*/\n"

                # trims the comments
                #gsub (/--[^\n]*[\n]+/, "")
		if (in_xsl_mode == 0 && in_xsd_mode == 0)
		  {
		    strip_comments($0, pl_stats, arr)
		    $0 = arr[0]
		    line_begin_no = (arr[1] + last_line_no - 1)

		    # trims the leading space
		    sub (/^[\t\n\r\ ]+/, "")
		    if (length() < 2)
		      next
		  }

                # does escape the symbols
		fun = $0
		gsub ("\\\\", "&&", fun)

		# remove whitespace except when there is just a semicolon
		if ((in_xsl_mode == 0) && (in_xsd_mode == 0))
  		  {
		    gsub (/\n[\t\ ]+/, "\n", fun)
		    gsub (/\n;\n/, "\n ;\n", fun)
		  }
		if ((in_xsl_mode == 0) && (in_xsd_mode == 0))
	          gsub ( /'/, "\\'", fun)
		#else
                  #gsub ( /'/, "''", fun)

	        gsub ( /"/, "\\\"", fun)
		if ((in_xsl_mode == 0) && (in_xsd_mode == 0))
	          gsub ( /[^\n]*/, "\"&\\n\"", fun)
		else
	          gsub ( /[^\n]*/, "\"&\\n\"", fun)

		if (in_xsl_mode == 1)
		  {
	            if (first_xsl_rec == 0)
		      print "\";\\n\", "
                    else
  		      first_xsl_rec = 0
		    print fun
		    next
		  }
		if (in_xsd_mode == 1)
		  {
	            if (first_xsd_rec == 0)
		      print "\";\\n\", "
                    else
  		      first_xsd_rec = 0
		    print fun
		    next
		  }
		_pre_code = ""
		_defines = defines
		after_rfw = 0
		after_and_before_rfw = 0
		npieces = split ($0, pieces, "[ \t\n(]+")

		if (index ($0, "@") > 0)
		  {
		    sp_comment_start = index ($0, "@")
	            sp_comment = substr ($0, 0, sp_comment_start - 1)
		    n_deps = split (sp_comment, _deps, "[ \t\n]+")
		  }
                else
                  n_deps = 0

		if (npieces)
		  pieces[1] = toupper (pieces[1])
		if (npieces > 1 && pieces[1] == "USE")
		  {
		    qualifier = pieces[2]
	            next
		  }
		else if (npieces > 2 && substr (pieces[1], 1, 3) == "AWK" && index ($0, "@") > 0)
		  {
		    null_at_inx = index ($0, "@")
		    fun_at_inx = index (fun, "@")
		    macro_command = substr ($0, 0, null_at_inx - 1)
		    $0 = substr ($0, null_at_inx + 1, length ($0))
		    fun = "\" " substr (fun, fun_at_inx + 1, length (fun))
		    npieces = split ($0, pieces, "[ \t\n]+")
		    if (npieces)
		      pieces[1] = toupper (pieces[1])

	            new_define = get_awk_macro_defines(macro_command)
		    if (length (new_define) > 0)
		      _defines = _defines new_define
		    else
	              next
		  }
		else if (substr (pieces[1], 1, 16) == "AFTER_AND_BEFORE" && index ($0, "@") > 0)
		  {
		    if (npieces > 2)
		      {
			dep_tb = pieces[2]
		        dep_col = pieces[3]
			_text = "\n  macro_tb = sch_name_to_table (isp_schema(NULL), \"" dep_tb "\");"
			_text = _text "\n  if (macro_tb && tb_name_to_column (macro_tb, \"" dep_col "\"))  "
			n_upgraded_tables = n_upgraded_tables + 1
			_pre_code = _text
		      }
		    null_at_inx = index ($0, "@")
		    fun_at_inx = index (fun, "@")
		    macro_command = substr ($0, 0, null_at_inx - 1)
		    $0 = substr ($0, null_at_inx + 1, length ($0))
		    fun = "\" " substr (fun, fun_at_inx + 1, length (fun))
		    npieces = split ($0, pieces, "[ \t\n]+")
		    if (npieces)
		      pieces[1] = toupper (pieces[1])
	            _defines = defines_arfw
		    after_and_before_rfw = 1
		  }
		else if (substr (pieces[1], 1, 5) == "AFTER" && index ($0, "@") > 0)
		  {
		    if (n_deps > 2)
		      {
			dep_tb = _deps[2]
		        dep_col = _deps[3]
			if (dep_tb == "__PROCEDURE__")
			  {
			    _text = "\n if (sch_proc_def_exists (bootstrap_cli, \"" dep_col "\", 0))"
			  }
			else
			  {
			    _text = "\n  macro_tb = sch_name_to_table (isp_schema (db_main_tree->it_commit_space), \"" dep_tb "\");"
		 	    _text = _text "\n  if (macro_tb && tb_name_to_column (macro_tb, \"" dep_col "\"))  "
			  }
			_pre_code = _text
		      }
		    null_at_inx = index ($0, "@")
		    fun_at_inx = index (fun, "@")
		    macro_command = substr ($0, 0, null_at_inx - 1)
		    $0 = substr ($0, null_at_inx + 1, length ($0))
		    fun = "\" " substr (fun, fun_at_inx + 1, length (fun))
		    npieces = split ($0, pieces, "[ \t\n]+")
		    if (npieces)
		      pieces[1] = toupper (pieces[1])
	            _defines = defines_arfw
		    after_rfw = 1
		  }
                # adds the definition in the appropriate branch
		_defines1 = ""
		if (npieces > 2 && pieces[1] == "CREATE")
		  {
		    is_escaped = 0
		    pieces[2] = toupper (pieces[2])
	            three_part = toupper (pieces[3])
		    if (index (pieces[3], "\"") > 0)
		      is_escaped = 1
		    gsub (/\"/, "", pieces[3])
		    if ((pieces[2] == "PROCEDURE" || pieces[2] == "FUNCTION") &&  three_part != "VIEW" )
		      {
			_defines1 = "\n  " define_proc_macro " (\"" pieces[3] "\", proc" nproc ");"
			print "static const char *proc" nproc " = \n\"#line " line_begin_no+1 " \\\"[executable]/" end_name "\\\"\\n\"\n" fun
                        # Here is a debug comment code
			if (pl_stats == "PLDBG")
			  print "\"--src " end_name ":" line_begin_no-1 "\\n\";\n"
			else
			  print ";\n"
			nproc = nproc + 1
		      }
	            else if (pieces[2] == "TYPE")
		      {
			if (is_escaped == 0 && toupper (pieces[3]) != pieces[3])
 			  _defines1 = "\n  DEFINE_UDT (case_mode == CM_UPPER ? \"" toupper (pieces[3]) "\" : \"" pieces[3] "\", udt" nudt ");"
			else
 			  _defines1 = "\n  DEFINE_UDT (\"" pieces[3] "\", udt" nudt ");"
		        print "static const char *udt" nudt " = \n" fun ";\n"
	                nudt = nudt + 1
		      }
		    else if (pieces[2] == "TRIGGER")
		      {
			_defines1 = "\n  ddl_std_proc (trig" ntriggers ", 0x0);"
			print "static const char *trig" ntriggers " = \n\"#line " line_begin_no+1 " \\\"[executable]/" end_name "\\\"\\n\"\n" fun
			if (pl_stats == "PLDBG")
			  print "\"--src " end_name ":" line_begin_no-1 "\\n\";\n"
			else
			  print ";\n"
			ntriggers = ntriggers + 1
		      }
		    else if (pieces[2] == "INDEX")
		      {
		        _defines1 = "\n  ddl_ensure_table (\"do this always\", inx" nindexes ");"
		        print "static const char *inx" nindexes " = \n" fun ";\n"
			nindexes = nindexes + 1
		      }
		    else if (pieces[2] == "TABLE")
		      {
			tablename = pieces[3]
		        if (index (tablename, ".") == 0)
		          tablename = "DB.DBA." tablename
		        _defines1 = "\n  ddl_ensure_table (\"" tablename "\", tbl" ntable ");"
		        print "static const char *tbl" ntable " = \n" fun ";\n"
		        ntable = ntable + 1
		      }
		    else if (pieces[2] == "VIEW" || (pieces[2] == "PROCEDURE" && three_part == "VIEW"))
		      {
			if (three_part == "VIEW")
			  tablename = toupper(pieces[4])
			else
			  tablename = pieces[3]
		        if (index (tablename, ".") == 0)
		          tablename = "DB.DBA." tablename
		        _defines1 = "\n  ddl_ensure_table (\"" tablename "\", view" nview ");"
		        print "static const char *view" nview " = \n" fun ";\n"
		        nview = nview + 1
		      }
		    else
		      {
		        _defines1 = "\n  ddl_ensure_table (\"do this always\", other" nother ");"
		        print "static const char *other" nother " = \n" fun ";\n"
		        nother = nother + 1
		      }
		  }
		else if (npieces > 5 && pieces[1] == "ALTER" && toupper (pieces[2]) == "TABLE" && toupper (pieces[4]) != "MODIFY")
		  {
		    is_drop = 0
	            if (toupper (pieces[4]) == "DROP")
		      is_drop = 1
		    _defines1 = "\n  ddl_ensure_column (\"" pieces[3] "\", \"" pieces[5] "\", alter" nalter ", " is_drop ");"
		    print "static const char *alter" nalter " = \n" fun ";\n"
		    nalter = nalter + 1
		  }
		else if (npieces > 5 && pieces[1] == "ALTER" && toupper (pieces[2]) == "TYPE" && toupper (pieces[4]) == "ADD" && toupper (pieces[5]) == "ATTRIBUTE")
		  {
		    udt_name = pieces[3]
                    gsub (/\"/, "", udt_name)
		    _defines1 = "\n  udt_ensure_attribute (\"" udt_name "\", \"" pieces[6] "\", alter" nalter ");"
		    print "static const char *alter" nalter " = \n" fun ";\n"
		    nalter = nalter + 1
		    nalterudt = nalterudt + 1
		  }
		else
		  {
		    _defines1 = "\n  ddl_ensure_table (\"do this always\", other" nother ");"
		    print "static const char *other" nother " = \n" fun ";"
		    nother = nother + 1
		  }
		if (after_rfw)
		  defines_arfw = _defines _pre_code _defines1
		else if (after_and_before_rfw)
		  {
		    defines = defines _pre_code _defines1
		    defines_arfw = defines_arfw _defines1
		  }
		else
		  defines = _defines _defines1

	    define_proc_macro = "DEFINE_PROC"
	}

END 	{
	    if (in_xsl_mode)
	      {
		#print "\"'))\";\n"
		print ",\n NULL };\n"
		in_xsl_mode = 0

	      }
	    if (pass_bootstrap_cli != 0)
	      {
	       print "#undef isp_schema\n#define isp_schema(x) isp_schema_1(x)\n"
	      }
	    if (n_xslts > 0)
	      {
		print "static const char * xslt_define = \"xslt_sheet (?, xtree_doc (?, 128, ?, 'LATIN-1', 'x-any', 'BuildStandalone=ENABLE'))\";"
		print "static query_t *xslt_define_qr;"
		print ""


		print "#define XSLT_DEFINE(name, text1) \\"
		print "  { \\"
		print "    caddr_t text = NULL;\\"
		print "    static const char *elm;\\"
		print "    int inx, len = 0;\\"
		print "    for (inx = 0; ; inx++) { \\"
		print "        elm = text1[inx];\\"
		print "        if (!elm) break;\\"
		print "        len += (int) strlen (elm);\\"
		print "      }\\"
		print "    text = dk_alloc_box (len + 1, DV_STRING);\\"
		print "    text[0] = 0;\\"
		print "    for (inx = 0; ; inx++) { \\"
		print "        elm = text1[inx];\\"
		print "        if (!elm) break;\\"
		print "        strcat_box_ck (text, elm);\\"
		print "      }\\"
		print "    \\"
		print "    if (!xslt_define_qr) \\"
		print "      xslt_define_qr = sql_compile (xslt_define, bootstrap_cli, NULL, SQLC_DEFAULT); \\"
		print "    if (xslt_define_qr) \\"
		print "      { \\"
		print "        caddr_t err = NULL; \\"
		print "        err = qr_quick_exec (xslt_define_qr, bootstrap_cli, NULL, NULL, 3, \\"
		print "               \":0\", name, QRP_STR, \":1\", text, QRP_STR, \":2\", name, QRP_STR); \\"
		print "        if (err) { \\"
		print "          log_error (\"Error executing a server init statement : %s: %s -- %.50s\", \\"
		print "          ((caddr_t *) err)[QC_ERRNO], ((caddr_t *) err)[QC_ERROR_STRING], \\"
		print "                      text); \\"
		print "          dk_free_tree (err); \\"
		print "	    } \\"
		print "	  local_commit (bootstrap_cli); \\"
		print "      } \\"
		print "    dk_free_box (text);\\"
		print "  }"
		print ""
	    }

	    if (in_xsd_mode)
	      {
		#print "\"')\";\n"
		print ",\n NULL };\n"
		in_xsd_mode = 0

	      }
	    if (n_xsds > 0)
	      {
		print "static const char * xsd_define = \"xml_reload_schema_decl (?, ?, 'UTF-8', 'x-any', ?)\";"
		print "static query_t *xsd_define_qr;"
		print ""


		print "#define XSD_DEFINE(name, text1) \\"
		print "  { \\"
		print "    caddr_t text = NULL;\\"
		print "    static const char *elm;\\"
		print "    int inx, len = 0;\\"
		print "    for (inx = 0; ; inx++) { \\"
		print "        elm = text1[inx];\\"
		print "        if (!elm) break;\\"
		print "        len += strlen (elm);\\"
		print "      }\\"
		print "    text = dk_alloc_box (len + 1, DV_STRING);\\"
		print "    text[0] = 0;\\"
		print "    for (inx = 0; ; inx++) { \\"
		print "        elm = text1[inx];\\"
		print "        if (!elm) break;\\"
		print "        strcat_box_ck (text, elm);\\"
		print "      }\\"
		print "    \\"
		print "    if (!xsd_define_qr) \\"
		print "      xsd_define_qr = sql_compile (xsd_define, bootstrap_cli, NULL, SQLC_DEFAULT); \\"
		print "    if (xsd_define_qr) \\"
		print "      { \\"
		print "        caddr_t err = NULL; \\"
		print "        err = qr_quick_exec (xsd_define_qr, bootstrap_cli, NULL, NULL, 3, \\"
		print "               \":0\", name, QRP_STR, \":1\", name, QRP_STR, \":2\", text, QRP_STR); \\"
		print "        if (err) { \\"
		print "          log_error (\"Error executing a server init statement : %s: %s -- %.50s\", \\"
		print "          ((caddr_t *) err)[QC_ERRNO], ((caddr_t *) err)[QC_ERROR_STRING], \\"
		print "                      text); \\"
		print "          dk_free_tree (err); \\"
		print "	    } \\"
		print "	  local_commit (bootstrap_cli); \\"
		print "      } \\"
		print "    dk_free_box (text);\\"
		print "  }"
		print ""
	    }

           print "static int\nsch_proc_def_exists (client_connection_t *cli, const char *proc_name, const int report)\n{"
	   print "  query_t *proc = NULL;"
	   print "  char *full_name = sch_full_proc_name (isp_schema(NULL), proc_name,"
	   print "	cli->cli_qualifier, CLI_OWNER (cli));"
	   print "  if (full_name)"
	   print "    proc = sch_proc_def (isp_schema(NULL), full_name);"
	   print "  if (report && proc != NULL)"
	   print "     log_debug (\"built-in procedure \\\"%s\\\" overruled by the RDBMS\", proc_name);"
	   print "  return (proc != NULL);"
	   print "}\n"

	   print "#define DEFINE_PROC(name, proc) \\"
	   print "   if (!sch_proc_def_exists (bootstrap_cli, (name), log_proc_overwrite)) \\"
	   print "     ddl_std_proc_1 (proc, 0x0, 1)\n\n"

	   print "#define DEFINE_PUBLIC_PROC(name, proc) \\"
	   print "   if (!sch_proc_def_exists (bootstrap_cli, (name), log_proc_overwrite)) \\"
	   print "     ddl_std_proc_1 (proc, 0x1, 1)\n\n"

	   print "#define DEFINE_OVERWRITE_PROC(name, proc) \\"
	   print "   ddl_std_proc_1 (proc, 0x1, 1)\n\n"
	   if (stored_proc > 0)
	   {
	   print "#define DEFINE_STORED_PROC(name, proc) \\"
	   print "   if (!sch_proc_def_exists (bootstrap_cli, (name), log_proc_overwrite)) \\"
           print "     ddl_ensure_table (\"do this always\", proc);\n\n"
	   }
           if (nudt > 0)
	   {
	   print "static int\nsch_udt_def_exists (client_connection_t *cli, const char *udt_name)\n{"
	   print "  sql_class_t *udt = sch_name_to_type (isp_schema (NULL), udt_name);"
	   print "  if (udt &&  UDT_IS_INSTANTIABLE(udt))"
	   print "    {"
	   #print "      log_error (\"Built-in User Defined Type already exists: %s\\n\", udt_name);"
	   print "      return 1;"
	   print "    }"
	   print "  return 0;"
	   print "}\n"

           print "#define DEFINE_UDT(name, udt) \\"
           print "   if (!sch_udt_def_exists (bootstrap_cli, (name))) \\"
           print "     ddl_ensure_table (\"do this always\", udt);\n\n"
	   }
	   if (nalterudt > 0)
           {
	     print "static void"
	     print "udt_ensure_attribute (const char * udt_name, const char * attr, const char * text)"
	     print "{"
	     print "  sql_class_t *udt = sch_name_to_type (isp_schema (NULL), udt_name);"
	     print "  if (udt &&  UDT_IS_INSTANTIABLE(udt))"
	     print "    {"
	     print "      caddr_t attr1 = sqlp_box_id_upcase (attr);"
	     print "      if (-1 == udt_find_field (udt->scl_member_map, attr1))"
             print "        ddl_ensure_table (\"do this always\", text);"
	     print "      dk_free_box (attr1);"
	     print "    }"
	     print "}"
	   }

	   if (pass_bootstrap_cli != 0)
	     print "void\nsqls_define" init_name " (client_connection_t *bootstrap_cli)\n{"
           else
	     print "void\nsqls_define" init_name " (void)\n{"

	   if (has_qualifier > 0 || (length (defines) > 0 && qualifier != "DB"))
	     print "  caddr_t saved_qualifier = bootstrap_cli->cli_qualifier;\n"
	   if (n_upgraded_tables > 0)
	     print "  dbe_table_t *macro_tb;\n"
	   for (i = 1; i <= nfiles; i++)
	     if (length (files[i]) > 0)
	       print files[i] "\n"

           if (length (defines) > 0)
	     {
	       if (qualifier != "DB")
		 {
		   print \
		     "  /* " end_name " */\n" \
		     "  bootstrap_cli->cli_qualifier = box_string (\"" qualifier "\");\n" \
		     defines "\n\n" \
		     "  dk_free_box (bootstrap_cli->cli_qualifier);\n" \
		     "  bootstrap_cli->cli_qualifier =  saved_qualifier;"
		 }
	       else
		 print "  /* " end_name " */\n" defines
	     }

	   print"}"

	   print "\n\nvoid\nsqls_arfw_define" init_name " (void)\n{"

	   if (has_qualifier_arfw > 0 || (length (defines_arfw) > 0 && qualifier != "DB"))
	     print "  caddr_t saved_qualifier = bootstrap_cli->cli_qualifier;\n"
	   if (n_upgraded_tables_arfw > 0)
	     print "  dbe_table_t *macro_tb;\n"
	   for (i = 1; i <= nfiles; i++)
	     if (length (files_arfw[i]) > 0)
	       print files_arfw[i] "\n"

	   if (length (defines_arfw) > 0)
	     {
	       if (qualifier != "DB")
		 {
		   print \
		     "  /* " filename " */\n" \
		     "  bootstrap_cli->cli_qualifier = box_string (\"" qualifier "\");\n" \
		     defines_arfw "\n\n" \
		     "  dk_free_box (bootstrap_cli->cli_qualifier);\n" \
		     "  bootstrap_cli->cli_qualifier =  saved_qualifier;"
		 }
	       else
		 print "  /* " filename " */\n" defines_arfw
	     }
	   print"}"

	   if (length (bif_text) > 0)
	   {
	       print "\n\nvoid\nsqls_bif_init" init_name " (void)\n{"
               print  bif_text
	       print"}"
	   }
        }