Browse code

added ability to upgrade initial data in DB - xx_create.sql script is now separated to two scripts: xx_create.sql and xx_data.sql xx_create.sql contain tables definitions and xx_data.sql contain all initial data (<row> elements from XML files) - each <row> element have new optional attribute 'vendor-controlled'. If vendor-controled='1' for a row, SQL DELETE statement is generated before INSERT statement for this row. In this case is necessary to define <row-identificator> element wich contain list of <colref> elements. This is a list of columns which will be in DELETE WHERE clause. - From now is also necessary to define an unique index on all tables which contain an initial data. Otherwise data will be duplicated on each update.

Karel Kozlik authored on 20/07/2006 12:54:24
Showing 5 changed files
... ...
@@ -42,7 +42,12 @@
42 42
     linkend IDREF #REQUIRED
43 43
 >
44 44
 
45
+<!ELEMENT row-identificator (colref+) >
46
+
45 47
 <!ELEMENT row (value*) >
48
+<!ATTLIST row
49
+    vendor-controlled (0|1) #IMPLIED
50
+>
46 51
 
47 52
 <!ELEMENT value (#PCDATA | null)* >
48 53
 <!ATTLIST value
... ...
@@ -60,6 +60,12 @@
60 60
 	<xsl:apply-templates select="user"/>
61 61
     </xsl:template>
62 62
 
63
+    <xsl:template match="database" mode="data">
64
+
65
+	<!-- Insert initial data -->
66
+	<xsl:apply-templates select="table" mode="data"/>
67
+    </xsl:template>
68
+
63 69
     <!-- ################ /DATABASE ################# -->
64 70
 
65 71
     <!-- ################ TABLE ################# -->
... ...
@@ -146,6 +152,28 @@
146 152
 	</xsl:choose>
147 153
     </xsl:template>
148 154
 
155
+
156
+    <!-- column ID to column name -->
157
+    <xsl:template name="get-column-name">
158
+	<xsl:param name="select" select="."/>
159
+
160
+	<xsl:variable name="columns" select="key('column_id', $select)"/>
161
+	<xsl:variable name="column" select="$columns[1]"/>
162
+	<xsl:choose>
163
+	    <xsl:when test="count($column) = 0">
164
+		<xsl:message terminate="yes">
165
+		    <xsl:text>ERROR: Column with id '</xsl:text>
166
+		    <xsl:value-of select="$select"/>
167
+		    <xsl:text>' does not exist.</xsl:text>
168
+		</xsl:message>
169
+	    </xsl:when>
170
+	    <xsl:otherwise>
171
+		<xsl:call-template name="get-name">
172
+		    <xsl:with-param name="select" select="$column"/>
173
+		</xsl:call-template>
174
+	    </xsl:otherwise>
175
+	</xsl:choose>
176
+    </xsl:template>
149 177
     <!-- ################ /COLUMN ################# -->
150 178
 
151 179
     <!-- ################ INDEX ################# -->
... ...
@@ -6,10 +6,6 @@
6 6
 
7 7
     <xsl:import href="sql.xsl"/>
8 8
 
9
-    <xsl:template match="database">
10
-	<xsl:apply-imports/>
11
-    </xsl:template>
12
-
13 9
     <xsl:template match="database" mode="drop">
14 10
 	<xsl:apply-templates mode="drop"/>
15 11
     </xsl:template>
... ...
@@ -152,4 +148,36 @@
152 148
 	<xsl:text>'</xsl:text>
153 149
     </xsl:template>
154 150
 
151
+<!-- ################ ROW ################  -->
152
+
153
+    <!-- override common template for ROW. Create INSERT statements 
154
+         with IGNORE keyword
155
+      -->
156
+    <xsl:template match="row">
157
+	<xsl:if test="@vendor-controlled[1]">
158
+	    <xsl:text>DELETE FROM </xsl:text>	    
159
+	    <xsl:call-template name="get-name">
160
+		<xsl:with-param name="select" select="parent::table"/>
161
+	    </xsl:call-template>
162
+	    <xsl:text> WHERE </xsl:text>	    
163
+	    <xsl:call-template name="row-identification"/>
164
+	    <xsl:text>;&#x0A;</xsl:text>	    
165
+	</xsl:if>
166
+
167
+	<xsl:text>INSERT IGNORE INTO </xsl:text>
168
+	<xsl:call-template name="get-name">
169
+	    <xsl:with-param name="select" select="parent::table"/>
170
+	</xsl:call-template>
171
+	<xsl:text> (</xsl:text>
172
+	<xsl:apply-templates select="value" mode="colname"/>
173
+	<xsl:text>) VALUES (</xsl:text>
174
+	<xsl:apply-templates select="value"/>
175
+	<xsl:text>);&#x0A;</xsl:text>
176
+	<xsl:if test="position()=last()">
177
+	    <xsl:text>&#x0A;</xsl:text>	    
178
+	</xsl:if>
179
+    </xsl:template>
180
+
181
+<!-- ################ /ROW ################  -->
182
+
155 183
 </xsl:stylesheet>
... ...
@@ -106,9 +106,6 @@
106 106
 	<xsl:for-each select="index[count(child::unique)=0]">
107 107
 	    <xsl:call-template name="create_index"/>
108 108
 	</xsl:for-each>
109
-
110
-	<!-- Process initial rows of data -->
111
-	<xsl:apply-templates select="row"/>
112 109
     </xsl:template>
113 110
 
114 111
     <xsl:template match="index">
... ...
@@ -15,6 +15,15 @@
15 15
 	    <xsl:text> </xsl:text>
16 16
 	</xsl:document>
17 17
 
18
+	<xsl:variable name="datafile" select="concat($dir, concat('/', concat($prefix, 'data.sql')))"/>
19
+	<xsl:document href="{$datafile}" method="text" indent="no" omit-xml-declaration="yes">
20
+	    <xsl:apply-templates mode="data" select="/database[1]"/>
21
+	    <!-- This is a hack to ensure that the file gets created when
22
+	    nothing is written
23
+	    -->
24
+	    <xsl:text> </xsl:text>
25
+	</xsl:document>
26
+
18 27
 	<xsl:variable name="dropfile" select="concat($dir, concat('/', concat($prefix, 'drop.sql')))"/>
19 28
 	<xsl:document href="{$dropfile}" method="text" indent="no" omit-xml-declaration="yes">
20 29
 	    <xsl:apply-templates mode="drop" select="/database[1]"/>
... ...
@@ -53,8 +62,10 @@
53 62
 	<xsl:text>&#x0A;</xsl:text>
54 63
 
55 64
 	<xsl:call-template name="table.close"/>
65
+    </xsl:template>
56 66
 
57
-	<!-- Process initial rows of data -->
67
+    <xsl:template match="table" mode="data">
68
+	<!-- Process initial data --> 
58 69
 	<xsl:apply-templates select="row"/>
59 70
     </xsl:template>
60 71
 
... ...
@@ -178,22 +189,9 @@
178 189
 <!-- ################ COLREF ################  -->
179 190
 
180 191
     <xsl:template match="colref">
181
-	<xsl:variable name="columns" select="key('column_id', @linkend)"/>
182
-	<xsl:variable name="column" select="$columns[1]"/>
183
-	<xsl:choose>
184
-	    <xsl:when test="count($column) = 0">
185
-		<xsl:message terminate="yes">
186
-		    <xsl:text>ERROR: Column with id '</xsl:text>
187
-		    <xsl:value-of select="@linkend"/>
188
-		    <xsl:text>' does not exist.</xsl:text>
189
-		</xsl:message>
190
-	    </xsl:when>
191
-	    <xsl:otherwise>
192
-		<xsl:call-template name="get-name">
193
-		    <xsl:with-param name="select" select="$column"/>
194
-		</xsl:call-template>
195
-	    </xsl:otherwise>
196
-	</xsl:choose>
192
+	<xsl:call-template name="get-column-name">
193
+	    <xsl:with-param name="select" select="@linkend"/>
194
+	</xsl:call-template>
197 195
 	<xsl:if test="not(position()=last())">
198 196
 	    <xsl:text>, </xsl:text>
199 197
 	</xsl:if>
... ...
@@ -204,6 +202,16 @@
204 202
 <!-- ################ ROW ################  -->
205 203
 
206 204
     <xsl:template match="row">
205
+	<xsl:if test="@vendor-controlled[1]">
206
+	    <xsl:text>DELETE FROM </xsl:text>	    
207
+	    <xsl:call-template name="get-name">
208
+		<xsl:with-param name="select" select="parent::table"/>
209
+	    </xsl:call-template>
210
+	    <xsl:text> WHERE </xsl:text>	    
211
+	    <xsl:call-template name="row-identification"/>
212
+	    <xsl:text>;&#x0A;</xsl:text>	    
213
+	</xsl:if>
214
+
207 215
 	<xsl:text>INSERT INTO </xsl:text>
208 216
 	<xsl:call-template name="get-name">
209 217
 	    <xsl:with-param name="select" select="parent::table"/>
... ...
@@ -218,6 +226,65 @@
218 226
 	</xsl:if>
219 227
     </xsl:template>
220 228
 
229
+    <xsl:template name="row-identification">
230
+	<xsl:variable name="row-ident" select="parent::table/row-identificator"/>
231
+	<xsl:variable name="row" select="."/>
232
+	<xsl:variable name="columns" select="$row-ident/colref"/>
233
+
234
+
235
+	<xsl:choose>
236
+	    <xsl:when test="count($row-ident) = 0">
237
+		<xsl:message terminate="yes">
238
+		    <xsl:text>ERROR: row-identificator does not exists.</xsl:text>
239
+		</xsl:message>
240
+	    </xsl:when>
241
+	    <xsl:when test="count($columns) = 0">
242
+		<xsl:message terminate="yes">
243
+		    <xsl:text>ERROR: row-identificator does not have any column.</xsl:text>
244
+		</xsl:message>
245
+	    </xsl:when>
246
+	    <xsl:otherwise>
247
+
248
+
249
+	<xsl:for-each select="$columns">
250
+	    <xsl:variable name="col-id" select="@linkend"/>
251
+	    
252
+	    <!-- column name -->
253
+	    <xsl:call-template name="get-column-name">
254
+		<xsl:with-param name="select" select="$col-id"/>
255
+	    </xsl:call-template>
256
+
257
+	    <xsl:text>=</xsl:text>	    
258
+
259
+	    <!-- value of column -->
260
+	    <xsl:variable name="value" select="$row/value[@col=$col-id]"/>
261
+	    <xsl:choose>
262
+		<xsl:when test="count($value) = 0">
263
+		    <xsl:message terminate="yes">
264
+			<xsl:text>ERROR: Value of column with id '</xsl:text>
265
+			<xsl:value-of select="$col-id"/>
266
+			<xsl:text>' does not exist.</xsl:text>
267
+		    </xsl:message>
268
+		</xsl:when>
269
+		<xsl:otherwise>
270
+		    <xsl:text>'</xsl:text>
271
+		    <xsl:value-of select="$value"/>
272
+		    <xsl:text>'</xsl:text>
273
+		</xsl:otherwise>
274
+	    </xsl:choose>
275
+
276
+
277
+	    <xsl:if test="not(position()=last())">
278
+		<xsl:text> AND </xsl:text>
279
+	    </xsl:if>
280
+	</xsl:for-each>
281
+
282
+
283
+	    </xsl:otherwise>
284
+	</xsl:choose>
285
+    	
286
+    </xsl:template>
287
+
221 288
 <!-- ################ /ROW ################  -->
222 289
 
223 290
 <!-- ################ VALUE ################  -->
... ...
@@ -239,22 +306,9 @@
239 306
     </xsl:template>
240 307
 
241 308
     <xsl:template match="value" mode="colname">
242
-	<xsl:variable name="columns" select="key('column_id', @col)"/>
243
-	<xsl:variable name="column" select="$columns[1]"/>
244
-	<xsl:choose>
245
-	    <xsl:when test="count($column) = 0">
246
-		<xsl:message terminate="yes">
247
-		    <xsl:text>ERROR: Column with id '</xsl:text>
248
-		    <xsl:value-of select="@col"/>
249
-		    <xsl:text>' does not exist.</xsl:text>
250
-		</xsl:message>
251
-	    </xsl:when>
252
-	    <xsl:otherwise>
253
-		<xsl:call-template name="get-name">
254
-		    <xsl:with-param name="select" select="$column"/>
255
-		</xsl:call-template>
256
-	    </xsl:otherwise>
257
-	</xsl:choose>
309
+	<xsl:call-template name="get-column-name">
310
+	    <xsl:with-param name="select" select="@col"/>
311
+	</xsl:call-template>
258 312
 	<xsl:if test="not(position()=last())">
259 313
 	    <xsl:text>, </xsl:text>
260 314
 	</xsl:if>