Development

HowtoUseDbDesigner4XML: dbd2propel_symfony.xsl

You must first sign up to be able to contribute.

HowtoUseDbDesigner4XML: dbd2propel_symfony.xsl

File dbd2propel_symfony.xsl, 13.1 kB (added by Petr.Rybak, 10 years ago)

DBD2Propel XSL Template with support for symfony _i18n automation.

Line 
1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
3 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
4 <xsl:strip-space elements="*"/>
5
6 <!--
7 DB Designer XML to Propel Schema XML
8
9 ==== Author: Jonathan Graham <jkgraham@gmail.com>
10 ==== Version: 0.5 (2008-04-16) (http://blog.tooleshed.com/?p=6)
11 ==== Description:
12 This XSL will transform a DB Designer 4 database model XML file into a
13 Propel database schema file.  This allows you to design your database
14 model using DB Designer 4 (models are saved in XML format) and then
15 easily create the Propel database schema file.
16
17 The PROPEL properties that this XSL will translate are listed below.
18
19 TABLE: name, description
20 COLUMN: name, primaryKey, required, type, size, scale, default, autoIncrement, description
21 FOREIGN-KEY: foreignTable, name, onDelete
22 REFERENCE: local, foreign
23 INDEX: index (not related to FK), unique, fulltext
24
25 ==== Usage:
26 - Simply feed this XSL into your favorite XSLT engine along with your DB Designer
27 XML model.  The resulting output is a Propel database schema XML.
28
29 ==== Collaboration:
30 Peter Banik <peter@froggle.org> - UNIQUE TAG
31 Benedicto Franco Jr. - MULTIPLE FOREIGN KEY, DATABASE NAME
32 Martin Kreidenweis <martin@kreidenweis.com> - Bug fixes, INDEX
33 Michiel Hakvoort - onDelete
34 Michel D'HOOGE - FULLTEXT
35 Petr Rybak <petr.rybak@rynawe.net> - support for symfony I18N autogenerating
36
37 ==== Software:
38 Propel: http://propel.phpdb.org/
39 DB Designer 4: http://www.fabforce.net/dbdesigner4/
40
41 ==== Copyright (c) 2004-2006, Jonathan Graham
42 Licensed under the GNU Lesser General Public License (LGPL) - http://www.gnu.org/copyleft/lgpl.html.
43
44 ==== Change Log
45 version 0.1 (2004-11-08) - initial version
46 version 0.2 (2006-10-18) - Added Peter and Benedicto's updates.
47 version 0.3 (2006-11-05) - added non-unique-INDEXes and onDelete
48 version 0.4 (2006-11-13) - added support for index names and FULLTEXT indexes, changed license to LGPL
49 version 0.5 (2006-04-16) - added support for symfony _i18n autogenerating, added support for DECIMAL
50                           (see http://www.symfony-project.org/book/1_0/13-I18n-and-L10n#Creating%20Localized%20Schema)
51
52 -->
53
54 <!-- ============================================================ DATABASE template -->
55 <xsl:template match="/">
56     <database defaultIdMethod="native" package="lib.model">
57         <xsl:attribute name="name">
58             <xsl:value-of select="/DBMODEL/SETTINGS/GLOBALSETTINGS/@ModelName"/>
59         </xsl:attribute>       
60         <xsl:apply-templates />
61     </database>
62 </xsl:template>
63
64 <!-- ============================================================ TABLES template -->
65 <xsl:template match="/DBMODEL/METADATA/TABLES/TABLE">
66     <table>
67         <xsl:variable name="tableName" select='@Tablename'/>
68    
69         <xsl:attribute name="name">
70             <xsl:value-of select="@Tablename"/>
71         </xsl:attribute>
72         <xsl:if test="@Comments != ''">
73             <xsl:attribute name="description">
74                 <xsl:value-of select="@Comments" />
75             </xsl:attribute>
76         </xsl:if>
77      
78         <!-- check if table with same name and _i18n suffix exists -->
79         <xsl:if test="/DBMODEL/METADATA/TABLES/TABLE[@Tablename=concat($tableName,'_i18n')]">
80             <xsl:attribute name="isI18N">true</xsl:attribute>
81             <xsl:attribute name="i18nTable"><xsl:value-of select="@Tablename"/>_i18n</xsl:attribute>
82         </xsl:if>
83  
84         <xsl:apply-templates />
85     </table>
86 </xsl:template>
87
88
89 <!-- ============================================================ COLUMNS template -->
90 <xsl:template match="COLUMNS/COLUMN">
91     <column>
92         <!-- get data type -->
93         <xsl:variable name="datatype">
94             <xsl:call-template name="get_datatype">
95                 <xsl:with-param name="id"><xsl:value-of select="@idDatatype"/></xsl:with-param>
96             </xsl:call-template>
97         </xsl:variable>
98        
99         <!-- remove parens from datatypeparams -->
100         <xsl:variable name="dtpclean">
101             <xsl:call-template name="clean_dataparams">
102                 <xsl:with-param name="dtp"><xsl:value-of select="@DatatypeParams"/></xsl:with-param>
103             </xsl:call-template>
104         </xsl:variable>
105      
106         <!-- table name for this column -->
107         <xsl:variable name="tablename">
108             <xsl:value-of select="../../@Tablename"/>
109         </xsl:variable>
110
111         <!-- ==== name ==== -->
112         <xsl:attribute name="name">
113             <xsl:value-of select="@ColName"/>
114         </xsl:attribute>
115        
116         <!-- ==== type ==== -->
117         <xsl:attribute name="type">
118             <xsl:value-of select="$datatype"/>
119         </xsl:attribute>
120
121         <xsl:if test="$dtpclean != ''">
122             <!-- ==== size ==== -->
123             <xsl:attribute name="size">
124                 <xsl:call-template name="get_datasize">
125                     <xsl:with-param name="dtpc"><xsl:value-of select="$dtpclean"/></xsl:with-param>
126                     <xsl:with-param name="dtype"><xsl:value-of select="$datatype"/></xsl:with-param>
127                 </xsl:call-template>
128             </xsl:attribute>
129        
130             <xsl:if test="contains('FLOAT,DOUBLE,DECIMAL',$datatype)">
131                 <!-- ==== scale ==== -->
132                 <xsl:attribute name="scale">
133                     <xsl:value-of select="substring-after($dtpclean,',')"/>
134                 </xsl:attribute>
135             </xsl:if>
136            
137         </xsl:if>
138                
139         <!-- ==== primaryKey ==== -->
140         <xsl:if test="@PrimaryKey = '1'">
141             <xsl:attribute name="primaryKey">true</xsl:attribute>
142         </xsl:if>
143        
144         <!-- ==== required ==== -->
145         <xsl:if test="@NotNull = '1'">
146             <xsl:attribute name="required">true</xsl:attribute>
147         </xsl:if>
148
149         <!-- ==== default ==== -->
150         <xsl:if test="@DefaultValue != ''">
151             <xsl:attribute name="default">
152                 <xsl:value-of select="@DefaultValue"/>
153             </xsl:attribute>
154         </xsl:if>
155        
156         <!-- ==== autoIncrement ==== -->
157         <xsl:if test="@AutoInc = '1'">
158             <xsl:attribute name="autoIncrement">true</xsl:attribute>
159         </xsl:if>
160
161         <!-- ==== description ==== -->
162         <xsl:if test="@Comments != ''">
163             <xsl:attribute name="description">
164                 <xsl:value-of select="@Comments"/>
165             </xsl:attribute>
166         </xsl:if>
167      
168         <!-- ==== i18n - culture ==== -->
169         <xsl:if test="contains('culture,CULTURE', @ColName) and contains($tablename,'18n')">
170             <xsl:attribute name="isCulture">true</xsl:attribute>
171         </xsl:if>
172
173     </column>
174 </xsl:template>
175
176 <!-- ============================================================ RELATIONS template -->
177 <xsl:template match="RELATIONS_END/RELATION_END">
178    
179     <xsl:variable name="id"><xsl:value-of select="@ID"/></xsl:variable>
180    
181     <xsl:call-template name="show_ForeignKey">
182         <xsl:with-param name="relation" select="/DBMODEL/METADATA/RELATIONS/RELATION[@ID=$id]"/>
183     </xsl:call-template>
184    
185 </xsl:template>
186
187 <!-- ============================================================ INDEX template -->
188 <xsl:template match="INDICES/INDEX">
189     <xsl:choose>
190         <xsl:when test="@IndexKind = '1' and @FKRefDef_Obj_id='-1'">
191             <index>
192                 <xsl:attribute name="name"><xsl:value-of select="@IndexName"/></xsl:attribute>
193                 <xsl:apply-templates select="INDEXCOLUMNS/INDEXCOLUMN" mode="normal"/>
194             </index>
195         </xsl:when>
196         <xsl:when test="@IndexKind = '2'">
197             <unique>
198                 <xsl:attribute name="name"><xsl:value-of select="@IndexName"/></xsl:attribute>
199                 <xsl:apply-templates select="INDEXCOLUMNS/INDEXCOLUMN" mode="unique"/>
200             </unique>
201         </xsl:when>
202         <xsl:when test="@IndexKind = '3'">
203             <index>
204                 <xsl:attribute name="name"><xsl:value-of select="@IndexName"/></xsl:attribute>
205                 <xsl:apply-templates select="INDEXCOLUMNS/INDEXCOLUMN" mode="normal"/>
206                 <vendor type="mysql">
207                     <parameter name="Index_type" value="FULLTEXT"/>
208                 </vendor>
209             </index>
210         </xsl:when>
211     </xsl:choose>
212 </xsl:template>
213
214
215 <!-- ============================================================ columns within an index -->
216 <xsl:template match="INDICES/INDEX/INDEXCOLUMNS/INDEXCOLUMN" mode="normal">
217     <xsl:variable name="columnId"><xsl:value-of select="@idColumn"/></xsl:variable>
218     <index-column>
219         <xsl:attribute name="name"><xsl:value-of select="//COLUMNS/COLUMN[@ID=$columnId]/@ColName"/></xsl:attribute>
220     </index-column>
221 </xsl:template>
222
223 <xsl:template match="INDICES/INDEX/INDEXCOLUMNS/INDEXCOLUMN" mode="unique">
224     <xsl:variable name="columnId"><xsl:value-of select="@idColumn"/></xsl:variable>
225     <unique-column>
226         <xsl:attribute name="name"><xsl:value-of select="//COLUMNS/COLUMN[@ID=$columnId]/@ColName"/></xsl:attribute>
227     </unique-column>
228 </xsl:template>
229
230
231 <!-- ============================================================ show_ForeignKey -->
232 <xsl:template name="show_ForeignKey">
233     <xsl:param name="relation"/>
234     <foreign-key>
235    
236         <!-- foreignTable -->
237         <xsl:attribute name="foreignTable">
238             <xsl:value-of select="/DBMODEL/METADATA/TABLES/TABLE[@ID=$relation/@SrcTable]/@Tablename"/>
239         </xsl:attribute>
240
241         <!-- name -->
242         <xsl:attribute name="name">
243             <xsl:value-of select="$relation/@RelationName"/>
244         </xsl:attribute>
245
246         <!-- onDelete -->
247         <xsl:attribute name="onDelete">
248
249             <xsl:variable name="actionId">
250                 <xsl:call-template name="str_replace">
251                     <xsl:with-param name="stringIn" select="substring-before(substring-after($relation/@RefDef,'\n'), '\n')"/>
252                     <xsl:with-param name="charsIn" select="'OnDelete='"/>
253                     <xsl:with-param name="charsOut" select="''"/>
254                 </xsl:call-template>
255             </xsl:variable>
256
257             <xsl:call-template name="get_actiontype">
258                 <xsl:with-param name="id" select="$actionId" />
259             </xsl:call-template>
260
261         </xsl:attribute>
262
263         <!-- === reference tag === -->
264         <xsl:call-template name="build_fk">
265             <xsl:with-param name="stringIn" select="$relation/@FKFields"/>
266         </xsl:call-template>
267
268     </foreign-key>
269 </xsl:template>
270
271 <!--
272 ============================================================
273 ============================================================ template "functions"
274 ============================================================
275 -->
276
277 <!-- ============================================================ get_datatype -->
278 <xsl:template name="get_datatype">
279     <xsl:param name="id"/>
280
281     <xsl:variable name="type">
282         <xsl:value-of select="/DBMODEL/SETTINGS/DATATYPES/DATATYPE[@ID=$id]/@TypeName"/>
283     </xsl:variable>
284
285     <xsl:choose>
286         <xsl:when test="$type = 'DATETIME'" >TIMESTAMP</xsl:when>
287         <xsl:when test="$type = 'TEXT'" >LONGVARCHAR</xsl:when>
288         <xsl:when test="$type = 'BOOL'" >BOOLEAN</xsl:when>
289         <xsl:otherwise>
290             <xsl:value-of select="$type"/>
291         </xsl:otherwise>
292     </xsl:choose>
293    
294 </xsl:template>
295
296
297 <!-- ============================================================ get_datasize -->
298 <xsl:template name="get_datasize">
299     <xsl:param name="dtpc"/>
300     <xsl:param name="dtype"/>
301
302     <xsl:choose>
303         <xsl:when test="contains('FLOAT,DOUBLE,DECIMAL',$dtype)" >
304             <xsl:value-of select="substring-before($dtpc,',')"/>
305         </xsl:when>
306         <xsl:otherwise>
307             <xsl:value-of select="$dtpc"/>
308         </xsl:otherwise>
309     </xsl:choose>
310    
311 </xsl:template>
312
313
314 <!-- ============================================================ clean_dataparams -->
315 <xsl:template name="clean_dataparams">
316     <xsl:param name="dtp"/>
317
318     <xsl:variable name="dtp2">
319         <xsl:call-template name="str_replace">
320             <xsl:with-param name="stringIn" select="$dtp"/>
321             <xsl:with-param name="charsIn" select="'('"/>
322             <xsl:with-param name="charsOut" select="''"/>
323         </xsl:call-template>
324     </xsl:variable>
325
326     <xsl:call-template name="str_replace">
327         <xsl:with-param name="stringIn" select="$dtp2"/>
328         <xsl:with-param name="charsIn" select="')'"/>
329         <xsl:with-param name="charsOut" select="''"/>
330     </xsl:call-template>
331    
332 </xsl:template>
333
334
335 <!-- ============================================================ str_replace -->
336 <xsl:template name="str_replace">
337     <xsl:param name="stringIn"/>
338     <xsl:param name="charsIn"/>
339     <xsl:param name="charsOut"/>
340     <xsl:choose>
341         <xsl:when test="contains($stringIn,$charsIn)">
342             <xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
343             <xsl:call-template name="str_replace">
344                 <xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
345                 <xsl:with-param name="charsIn" select="$charsIn"/>
346                 <xsl:with-param name="charsOut" select="$charsOut"/>
347             </xsl:call-template>
348         </xsl:when>
349         <xsl:otherwise>
350             <xsl:value-of select="$stringIn"/>
351         </xsl:otherwise>
352     </xsl:choose>
353 </xsl:template>
354
355
356 <!-- ============================================================== build_fk -->
357 <xsl:template name="build_fk">
358     <xsl:param name="stringIn"/>
359     <xsl:variable name="FKClean">
360         <xsl:value-of select="substring-before($stringIn, '\n')"/>
361     </xsl:variable>
362     <reference>
363     <xsl:attribute name="local">
364         <xsl:value-of select="substring-after($FKClean, '=')"/>
365     </xsl:attribute>
366     <xsl:attribute name="foreign">
367         <xsl:value-of select="substring-before($FKClean, '=')"/>
368     </xsl:attribute>
369     </reference>
370     <xsl:if test="contains(substring-after($stringIn,'\n'),'=')">
371         <xsl:call-template name="build_fk">
372             <xsl:with-param name="stringIn" select="substring-after($stringIn,'\n')"/>
373         </xsl:call-template>
374     </xsl:if>
375 </xsl:template>
376
377 <!-- ======================================================== get_actiontype -->
378
379 <xsl:template name="get_actiontype">
380     <xsl:param name="id"/>
381
382     <xsl:choose>
383         <xsl:when test="$id = 0">restrict</xsl:when>
384         <xsl:when test="$id = 1">cascade</xsl:when>
385         <xsl:when test="$id = 2">setnull</xsl:when>
386         <xsl:otherwise>restrict</xsl:otherwise>
387     </xsl:choose>
388 </xsl:template>
389
390
391 </xsl:stylesheet>