File: ferretml.tcl

package info (click to toggle)
ferret 0.7-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,820 kB
  • ctags: 1,494
  • sloc: tcl: 22,364; makefile: 112
file content (260 lines) | stat: -rw-r--r-- 6,101 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
;# gerwinml.tcl -- Gerwin
;#
;# GerwinML tables generation routine
;#
;# Copyright (C) 2002-2008 Jose E. Marchesi
;#
;# Time-stamp: "08/11/16 19:20:07 jemarch"

;# This program 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; either version 3 of
;# the License, or (at your option) any later version.

;# 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, see
;# <http://www.gnu.org/licenses/>.

;# Register the format
lappend gerwin_output_formats GerwinML

;# Set the color scheme for this output format
set output_colors(GerwinML) {

    {{<!--.*} red}
    {{.*-->} red}

    {{<[a-zA-Z_]+} blue}
    {{</[a-zA-Z_]+} blue}
    {{>} blue}
    {{/>} blue}
    {{=} blue}
    {{\".*?\"} orange}

}

;# Set the type scheme for this output format

set gerwin_output_format_types(GerwinML) {

    {{DYNAMIC_STRING(%d)}  {DYNAMIC_STRING(%d)}}
    {{FIXED_STRING(%d)} {FIXED_STRING(%d)}}
    {INTEGER INTEGER}
    {FLOAT FLOAT}
    {NATURAL INTEGER}
    {CHARACTER CHARACTER}
}



;# The generation routine

proc gen_TD_to_format_GerwinML {} {

    global gerwin_cproject_name
    global gerwin_cproject_author
    global gerwin_cproject_file
    global gerwin_output_active_format
    global gerwin_tables
    global gerwin_table

    ;# Ok, generate the GerwinML
    omit_line "<gerwinml>"
    omit_line ""
    omit_line "  <project name=\"$gerwin_cproject_name\" file=\"$gerwin_cproject_file\" author=\"$gerwin_cproject_author\">"
    omit_line ""
    omit_line "    <tables>"
    ;# Iterate on the tables
    foreach t $gerwin_tables {

	set table_name $t

	;# Some blank space before each table
	omit_line ""
	omit_line ""

	omit_line "      <table name=\"$t\">"
	omit_line ""

	;# Emit the table attributes
	omit_line "        <table_attributes>"      
	omit_line ""

	set nattributes [gob_table_get_nattributes $t]
	set i 0
	foreach a [gob_table_get_attributes $t] {
	    incr i

	    ;# See if the name contain any %
	    ;# If it is the case, then this is a repeated foreign key
	    ;# and the name in gob_table_get_foreign_keys is without the #!

	    if {[regexp \% $a]} then {

		;# Convert the # to an underscore
		set nname [string map {% _} $a]

	    } else {
		set nname $a
	    }

	    ;# Write the attribute name and the attribute domain
	    append line  "          <table_attribute name=\"$nname\" domain=\"[gob_table_get_attribute_domain $t $a]\"/>"

	    ;# Ok, omit the line
	    omit_line $line
	    set line {}
	}

	omit_line ""
	omit_line "        </table_attributes>"      


	;# Now, emit the table restrictions
	omit_line ""

	;# Begin of table restrictions
	omit_line ""
	omit_line "        <table_restrictions>"

	;# Begin of primary key
	omit_line ""
	omit_line "          <primary_key>"

	;# Begin of primary_key_attributes
	omit_line "            <primary_key_attributes>"

	;# Generate primary key attributes
	omit_line ""
	foreach a [gob_table_get_key $t] {
	    ;# Obtain the original name
	    ;#regsub {.+%([^%]+)} $a "\\1" a
	    set a [string map "% _" $a]
	    omit_line "              <primary_key_attribute name=\"$a\"/>"
	}

	omit_line ""
	omit_line "            </primary_key_attributes>"
	;# End of primary_key_attributes

	omit_line "          </primary_key>"
	;# End of primary key

	;# Begin of not_nulleable
	omit_line ""
	omit_line "          <not_nulleable>"

	;# Begin of not_nulleable_attributes
	omit_line "            <not_nulleable_attributes>"

	;# Generate not-nulleable attributes
	omit_line ""
	foreach a [gob_table_get_attributes $t] {
	    if {![gob_table_get_attribute_null $t $a]} then {
		;# Normalize the name
		;#regsub {.+%([^%]+)} $a "\\1" a
		set a [string map "% _" $a]
		
		omit_line "              <not_nulleable_attribute name=\"$a\"/>"
	    }
	}


	omit_line ""
	omit_line "            </not_nulleable_attributes>"
	;# End of not _nulleable_attributes

	omit_line ""
	omit_line "          </not_nulleable>"
	;# End of not_nulleable

	;# Begin of foreign_keys
	omit_line ""
	omit_line "          <foreign_keys>"
	omit_line ""

	;# Generate foreign_key elements
	set rtables {}
	foreach fkd [gob_table_get_foreign_keys $t] {

	    set rtable [lindex $fkd 1]

	    if {[lsearch $rtables $rtable] == -1} then {
		lappend rtables $rtable
	    }
	}
	;# Ok, now we have all the rtables into $rtables
	
	foreach rtable $rtables {

	    ;# Begin of foreign_key
	    omit_line "            <foreign_key reference_table=\"$rtable\">"
	    omit_line ""

	    ;# Begin of foreign_key_attributes
	    omit_line "              <foreign_key_attributes>"
	    omit_line ""

	    ;# Emit the foreign keys for this rtable
	    foreach a [gob_table_get_attributes $t] {

		;# Get the remote name for the attribute
		regsub {.+%([^%]+)} $a "\\1" nname
		set nname [string map "% _" $nname]


		if {[gob_table_is_foreign_key $t $a]} then {


		    ;# Get the foreign key for the attribute
		    set art [gob_table_get_foreign_key_rtable $t $a]

		    if {$art == $rtable} then {

			set a [string map "% _" $a]
			;# Begin foreign_key_attribute
			omit_line "                <foreign_key_attribute name=\"$a\" rattribute=\"$nname\"/>"

		    }
		}
	    }


	    omit_line ""
	    omit_line "              </foreign_key_attributes>"
	    ;# End of foreign_key_attributes

	    omit_line ""
	    omit_line "            </foreign_key>"
	    ;# End of foreign_key
	    omit_line ""

	}


	omit_line ""
	omit_line "          </foreign_keys>"
	;# End of foreign_keys

	omit_line ""
	omit_line "        </table_restrictions>"
	;# End of table restrictions

	omit_line ""
	omit_line "      </table>"
	omit_line ""
	
    }

    omit_line "    </tables>"
    omit_line ""
    omit_line "  </project>"
    omit_line ""
    omit_line "</gerwinml>"
}