sql.xsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. version='1.0'
  4. >
  5. <xsl:import href="common.xsl"/>
  6. <xsl:template match="/">
  7. <xsl:variable name="createfile" select="concat($dir, concat('/', concat($prefix, 'create.sql')))"/>
  8. <xsl:document href="{$createfile}" method="text" indent="no" omit-xml-declaration="yes">
  9. <xsl:apply-templates select="/database[1]"/>
  10. <!-- This is a hack to ensure that the file gets created when
  11. nothing is written
  12. -->
  13. <xsl:text> </xsl:text>
  14. </xsl:document>
  15. <xsl:variable name="datafile" select="concat($dir, concat('/', concat($prefix, 'data.sql')))"/>
  16. <xsl:document href="{$datafile}" method="text" indent="no" omit-xml-declaration="yes">
  17. <xsl:apply-templates mode="data" select="/database[1]"/>
  18. <!-- This is a hack to ensure that the file gets created when
  19. nothing is written
  20. -->
  21. <xsl:text> </xsl:text>
  22. </xsl:document>
  23. <xsl:variable name="dropfile" select="concat($dir, concat('/', concat($prefix, 'drop.sql')))"/>
  24. <xsl:document href="{$dropfile}" method="text" indent="no" omit-xml-declaration="yes">
  25. <xsl:apply-templates mode="drop" select="/database[1]"/>
  26. <!-- This is a hack to ensure that the file gets created when
  27. nothing is written
  28. -->
  29. <xsl:text> </xsl:text>
  30. </xsl:document>
  31. </xsl:template>
  32. <!-- ################ DATABASE ################# -->
  33. <!-- ################ /DATABASE ################# -->
  34. <!-- ################ TABLE ################# -->
  35. <xsl:template match="table">
  36. <xsl:variable name="table.name">
  37. <xsl:call-template name="get-name"/>
  38. </xsl:variable>
  39. <xsl:text>CREATE TABLE </xsl:text>
  40. <xsl:value-of select="$table.name"/>
  41. <xsl:text> (&#x0A;</xsl:text>
  42. <!-- Process all columns -->
  43. <xsl:apply-templates select="column"/>
  44. <!-- Process all indexes -->
  45. <xsl:apply-templates select="index"/>
  46. <xsl:text>&#x0A;</xsl:text>
  47. <xsl:call-template name="table.close"/>
  48. </xsl:template>
  49. <xsl:template match="table" mode="data">
  50. <!-- Process initial data -->
  51. <xsl:apply-templates select="row"/>
  52. </xsl:template>
  53. <xsl:template name="table.close">
  54. <xsl:text>);&#x0A;&#x0A;</xsl:text>
  55. </xsl:template>
  56. <!-- ################ /TABLE ################ -->
  57. <!-- ################ COLUMN ################ -->
  58. <xsl:template match="column">
  59. <xsl:text> </xsl:text>
  60. <xsl:call-template name="get-name"/>
  61. <xsl:text> </xsl:text>
  62. <xsl:call-template name="column.type"/>
  63. <xsl:variable name="null">
  64. <xsl:call-template name="get-null"/>
  65. </xsl:variable>
  66. <xsl:if test="$null=0">
  67. <xsl:text> NOT NULL</xsl:text>
  68. </xsl:if>
  69. <xsl:choose>
  70. <xsl:when test="default[@db=$db]">
  71. <xsl:text> DEFAULT </xsl:text>
  72. <xsl:choose>
  73. <xsl:when test="default[@db=$db]/null">
  74. <xsl:text>NULL</xsl:text>
  75. </xsl:when>
  76. <xsl:otherwise>
  77. <xsl:text>'</xsl:text>
  78. <xsl:value-of select="default[@db=$db]"/>
  79. <xsl:text>'</xsl:text>
  80. </xsl:otherwise>
  81. </xsl:choose>
  82. </xsl:when>
  83. <xsl:when test="default">
  84. <xsl:text> DEFAULT </xsl:text>
  85. <xsl:choose>
  86. <xsl:when test="default/null">
  87. <xsl:text>NULL</xsl:text>
  88. </xsl:when>
  89. <xsl:otherwise>
  90. <xsl:text>'</xsl:text>
  91. <xsl:value-of select="default"/>
  92. <xsl:text>'</xsl:text>
  93. </xsl:otherwise>
  94. </xsl:choose>
  95. </xsl:when>
  96. </xsl:choose>
  97. <xsl:if test="not(position()=last())">
  98. <xsl:text>,</xsl:text>
  99. <xsl:text>&#x0A;</xsl:text>
  100. </xsl:if>
  101. </xsl:template>
  102. <xsl:template name="column.type">
  103. <!-- FIXME -->
  104. <xsl:call-template name="get-type"/>
  105. <xsl:call-template name="column.size"/>
  106. <xsl:call-template name="column.trailing"/>
  107. </xsl:template>
  108. <xsl:template name="column.size">
  109. <xsl:variable name="size">
  110. <xsl:call-template name="get-size"/>
  111. </xsl:variable>
  112. <xsl:if test="not($size='')">
  113. <xsl:text>(</xsl:text>
  114. <xsl:value-of select="$size"/>
  115. <xsl:text>)</xsl:text>
  116. </xsl:if>
  117. </xsl:template>
  118. <xsl:template name="column.trailing"/>
  119. <!-- ################ /COLUMN ################ -->
  120. <!-- ################ INDEX ################ -->
  121. <xsl:template match="index">
  122. <!-- Translate unique indexes into SQL92 unique constraints -->
  123. <xsl:if test="unique">
  124. <xsl:if test="position()=1">
  125. <xsl:text>,&#x0A;</xsl:text>
  126. </xsl:if>
  127. <xsl:text> </xsl:text>
  128. <xsl:call-template name="get-name"/>
  129. <xsl:text> UNIQUE (</xsl:text>
  130. <xsl:apply-templates match="colref"/>
  131. <xsl:text>)</xsl:text>
  132. <xsl:if test="not(position()=last())">
  133. <xsl:text>,</xsl:text>
  134. <xsl:text>&#x0A;</xsl:text>
  135. </xsl:if>
  136. </xsl:if>
  137. </xsl:template>
  138. <!-- ################ /INDEX ################ -->
  139. <!-- ################ COLREF ################ -->
  140. <xsl:template match="colref">
  141. <xsl:call-template name="get-column-name">
  142. <xsl:with-param name="select" select="@linkend"/>
  143. </xsl:call-template>
  144. <xsl:if test="not(position()=last())">
  145. <xsl:text>, </xsl:text>
  146. </xsl:if>
  147. </xsl:template>
  148. <!-- ################ /COLREF ################ -->
  149. <!-- ################ ROW ################ -->
  150. <xsl:template match="row">
  151. <xsl:if test="@vendor-controlled[1]">
  152. <xsl:text>DELETE FROM </xsl:text>
  153. <xsl:call-template name="get-name">
  154. <xsl:with-param name="select" select="parent::table"/>
  155. </xsl:call-template>
  156. <xsl:text> WHERE </xsl:text>
  157. <xsl:call-template name="row-identification"/>
  158. <xsl:text>;&#x0A;</xsl:text>
  159. </xsl:if>
  160. <xsl:text>INSERT INTO </xsl:text>
  161. <xsl:call-template name="get-name">
  162. <xsl:with-param name="select" select="parent::table"/>
  163. </xsl:call-template>
  164. <xsl:text> (</xsl:text>
  165. <xsl:apply-templates select="value" mode="colname"/>
  166. <xsl:text>) VALUES (</xsl:text>
  167. <xsl:apply-imports/>
  168. <xsl:text>);&#x0A;</xsl:text>
  169. <xsl:if test="position()=last()">
  170. <xsl:text>&#x0A;</xsl:text>
  171. </xsl:if>
  172. </xsl:template>
  173. <xsl:template name="row-identification">
  174. <xsl:variable name="row-ident" select="parent::table/row-identificator"/>
  175. <xsl:variable name="row" select="."/>
  176. <xsl:variable name="columns" select="$row-ident/colref"/>
  177. <xsl:choose>
  178. <xsl:when test="count($row-ident) = 0">
  179. <xsl:message terminate="yes">
  180. <xsl:text>ERROR: row-identificator does not exists.</xsl:text>
  181. </xsl:message>
  182. </xsl:when>
  183. <xsl:when test="count($columns) = 0">
  184. <xsl:message terminate="yes">
  185. <xsl:text>ERROR: row-identificator does not have any column.</xsl:text>
  186. </xsl:message>
  187. </xsl:when>
  188. <xsl:otherwise>
  189. <xsl:for-each select="$columns">
  190. <xsl:variable name="col-id" select="@linkend"/>
  191. <!-- column name -->
  192. <xsl:call-template name="get-column-name">
  193. <xsl:with-param name="select" select="$col-id"/>
  194. </xsl:call-template>
  195. <xsl:text>=</xsl:text>
  196. <!-- value of column -->
  197. <xsl:variable name="value" select="$row/value[@col=$col-id]"/>
  198. <xsl:choose>
  199. <xsl:when test="count($value) = 0">
  200. <xsl:message terminate="yes">
  201. <xsl:text>ERROR: Value of column with id '</xsl:text>
  202. <xsl:value-of select="$col-id"/>
  203. <xsl:text>' does not exist.</xsl:text>
  204. </xsl:message>
  205. </xsl:when>
  206. <xsl:otherwise>
  207. <xsl:text>'</xsl:text>
  208. <xsl:value-of select="$value"/>
  209. <xsl:text>'</xsl:text>
  210. </xsl:otherwise>
  211. </xsl:choose>
  212. <xsl:if test="not(position()=last())">
  213. <xsl:text> AND </xsl:text>
  214. </xsl:if>
  215. </xsl:for-each>
  216. </xsl:otherwise>
  217. </xsl:choose>
  218. </xsl:template>
  219. <!-- ################ /ROW ################ -->
  220. <!-- ################ VALUE ################ -->
  221. <xsl:template match="value">
  222. <xsl:choose>
  223. <xsl:when test="null">
  224. <xsl:text>NULL</xsl:text>
  225. </xsl:when>
  226. <xsl:otherwise>
  227. <xsl:text>'</xsl:text>
  228. <xsl:value-of select="text()"/>
  229. <xsl:text>'</xsl:text>
  230. </xsl:otherwise>
  231. </xsl:choose>
  232. <xsl:if test="not(position()=last())">
  233. <xsl:text>, </xsl:text>
  234. </xsl:if>
  235. </xsl:template>
  236. <xsl:template match="value" mode="colname">
  237. <xsl:call-template name="get-column-name">
  238. <xsl:with-param name="select" select="@col"/>
  239. </xsl:call-template>
  240. <xsl:if test="not(position()=last())">
  241. <xsl:text>, </xsl:text>
  242. </xsl:if>
  243. </xsl:template>
  244. <!-- ################ /VALUE ################ -->
  245. </xsl:stylesheet>