/[volute]/trunk/projects/theory/snapdm/res/intermediate2ddl.xsl
ViewVC logotype

Contents of /trunk/projects/theory/snapdm/res/intermediate2ddl.xsl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 230 - (show annotations)
Wed Apr 16 06:39:25 2008 UTC (12 years, 7 months ago) by gerard.lemson
File MIME type: application/xml
File size: 7533 byte(s)


1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
3 This XSLT script transforms a data model from our
4 interediate representation to a relational database
5 Data Definition Language script.
6
7 It depends on our assumed object-relational mapping
8 prescription and should be compatible with the persistence
9 framework used for example in the Java/JPA generation.
10
11 We also generate view definitions representing each objectType.
12
13 We assume that all tables are in a single schema.
14 For now we assume that objectType's names are unique over the complete model.
15 TODO We need to check this explicitly and modify the generation if not.
16
17 -->
18
19 <!DOCTYPE stylesheet [
20 <!ENTITY cr "<xsl:text>
21 </xsl:text>">
22 <!ENTITY bl "<xsl:text> </xsl:text>">
23 <!ENTITY tab "<xsl:text> </xsl:text>">
24 ]>
25
26 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
27
28
29 <xsl:output method="text" encoding="UTF-8" indent="yes" />
30
31 <xsl:output name="packageInfo" method="html" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
32
33 <xsl:strip-space elements="*" />
34
35 <xsl:key name="element" match="*//*" use="@xmiid"/>
36
37
38 <xsl:param name="xmlLastModified"/>
39 <xsl:param name="xslLastModified"/>
40
41
42 <xsl:param name="schema"/> <!-- select="'SNAP.'" -->
43
44
45 <xsl:variable name="lastModified">
46 <xsl:choose>
47 <xsl:when test="number(/model/lastModifiedDate) > number($xslLastModified)"><xsl:value-of select="/model/lastModifiedDate"/></xsl:when>
48 <xsl:otherwise><xsl:value-of select="$xslLastModified"/></xsl:otherwise>
49 </xsl:choose>
50 </xsl:variable>
51
52 <!-- next two might also be parameters, or obtained from a config file -->
53 <xsl:variable name="defaultVarcharLength" select="'256'"/>
54 <xsl:variable name="longstringtype" select="'text'"/> <!-- SQLServer specific -->
55
56 <xsl:template match="/">
57 <xsl:apply-templates select="model"/>
58 </xsl:template>
59
60
61
62
63 <xsl:template match="model">
64 <xsl:message>Model = <xsl:value-of select="name"></xsl:value-of></xsl:message>
65
66
67 -- last modification date of the UML model <xsl:value-of select="$lastModified"/>&cr;
68
69 <!--
70 <xsl:apply-templates select=".//objectType[not(extends)]" mode="singleTable"/>
71 -->
72 <xsl:apply-templates select=".//objectType" mode="classPerTable"/>
73 </xsl:template>
74
75
76 <xsl:template match="objectType" mode="singleTable">
77 <!-- generate a single table for the whole object hierarchy below the matched objectType -->
78 <xsl:variable name="tableName" select="name" />
79 <xsl:variable name="containers">
80 </xsl:variable>
81 CREATE TABLE <xsl:value-of select="$tableName"/> (
82 id bigint not null
83 <xsl:apply-templates select="." mode="containers"/>
84 <xsl:apply-templates select="." mode="hierarchy"/>
85 );&cr;&cr;
86 </xsl:template>
87
88
89 <xsl:template match="objectType" mode="classPerTable">
90 <!-- generate a single table for the whole object hierarchy below the matched objectType -->
91 <xsl:variable name="tableName" select="name" />
92 <xsl:variable name="containers">
93 </xsl:variable>
94 CREATE TABLE <xsl:value-of select="$tableName"/> (
95 id bigint not null
96 <xsl:apply-templates select="." mode="container"/>
97 <xsl:apply-templates select="attribute" />
98 <xsl:apply-templates select="reference" />
99 );&cr;&cr;
100 </xsl:template>
101
102
103 <xsl:template match="objectType" mode="container">
104 <xsl:variable name="xmiid" select="@xmiid"/>
105 <xsl:if test="/model//objectType[collection/datatype/@xmiidref = $xmiid]">
106 , containerId bigint not null -- <xsl:value-of select="/model//objectType[collection/datatype/@xmiidref = $xmiid]/name"/>
107 </xsl:if>
108 </xsl:template>
109
110
111
112
113 <xsl:template match="objectType" mode="containers">
114 <xsl:variable name="xmiid" select="@xmiid"/>
115 <xsl:if test="/model//objectType[collection/datatype/@xmiidref = $xmiid]">
116 , containerId bigint not null -- <xsl:value-of select="/model//objectType[collection/datatype/@xmiidref = $xmiid]/name"/>
117 </xsl:if>
118 <xsl:apply-templates select="/model//objectType[extends/datatype/@xmiidref = $xmiid]" mode="containers"/>
119 </xsl:template>
120
121
122
123
124 <xsl:template match="objectType" mode="hierarchy">
125 <xsl:variable name="xmiid" select="@xmiid"/>
126 -- fields from <xsl:value-of select="name"/>
127 <xsl:apply-templates select="attribute" />
128 <xsl:apply-templates select="reference" />
129 <xsl:apply-templates select="/model//objectType[extends/datatype/@xmiidref = $xmiid]" mode="hierarchy"/>
130 </xsl:template>
131
132
133
134 <xsl:template match="attribute" >
135 <xsl:choose>
136 <xsl:when test="datatype/@class">
137 <xsl:apply-templates select="." mode="complex"/>
138 </xsl:when>
139 <xsl:otherwise>
140 <xsl:apply-templates select="." mode="simple"/>
141 </xsl:otherwise>
142 </xsl:choose>
143 </xsl:template>
144
145
146
147 <xsl:template match="attribute" mode="simple">
148 <xsl:variable name="constraints" select="constraints"/>
149 <xsl:variable name="type">
150 <xsl:apply-templates select="datatype" mode="sqltype">
151 <xsl:with-param name="constraints" select="constraints"/>
152 </xsl:apply-templates>
153 </xsl:variable>
154 ,&bl;&bl;<xsl:value-of select="name"/>&bl;<xsl:value-of select="$type"/><xsl:if test="multiplicity = '1'"> not null</xsl:if>
155 </xsl:template>
156
157
158
159
160 <xsl:template match="attribute" mode="complex">
161 <xsl:apply-templates select="key('element',datatype/@xmiidref)" mode="columns">
162 <xsl:with-param name="prefix" select="concat(name,'_')"/>
163 </xsl:apply-templates>
164 </xsl:template>
165
166
167
168
169
170
171 <!-- We need lengths for (var)char datatypes -->
172 <xsl:template match="datatype" mode="sqltype">
173 <xsl:param name="constraints"/>
174
175 <xsl:choose>
176 <xsl:when test="@type = 'boolean'">bit</xsl:when>
177 <xsl:when test="@type = 'integer'">integer</xsl:when>
178 <xsl:when test="@type = 'long'">bigint</xsl:when>
179 <xsl:when test="@type = 'datetime'">dateTime</xsl:when>
180 <xsl:when test="@type = 'real'">float</xsl:when>
181 <xsl:when test="@type = 'double'">real</xsl:when>
182 <xsl:otherwise>
183 <xsl:variable name="length">
184 <xsl:choose>
185 <xsl:when test="$constraints/length">
186 <xsl:value-of select="$constraints/length"/>
187 </xsl:when>
188 <xsl:when test="$constraints/maxLength">
189 <xsl:value-of select="$constraints/maxLength"/>
190 </xsl:when>
191 <xsl:otherwise>
192 <xsl:value-of select="$defaultVarcharLength"/>
193 </xsl:otherwise>
194 </xsl:choose>
195 </xsl:variable>
196 <xsl:choose>
197 <xsl:when test="$length &lt;= 0"><xsl:value-of select="$longstringtype"/></xsl:when>
198 <xsl:otherwise>varchar(<xsl:value-of select="$length"/>)</xsl:otherwise>
199 </xsl:choose></xsl:otherwise>
200 </xsl:choose>
201 </xsl:template>
202
203
204
205
206 <xsl:template match="reference">
207 <xsl:if test="not(subsets)">
208 ,&tab;<xsl:value-of select="name"/>Id&bl;bigint<xsl:if test="multiplicity = '1'"> not null</xsl:if>
209 </xsl:if>
210 </xsl:template>
211
212
213
214 <!-- TODO
215 Add templates retrieving for a given objectType the table it is in
216 for a given attribute the column(s) it is in
217 for a given reference the column it is in
218 for a given collection the containerId column it is in.
219 -->
220
221
222 </xsl:stylesheet>

msdemlei@ari.uni-heidelberg.de
ViewVC Help
Powered by ViewVC 1.1.26