sql.xsl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?xml version='1.0'?>
  2. <!--
  3. * $Id: sql.xsl 4518 2008-07-28 15:39:28Z henningw $
  4. *
  5. * XSL converter script for SQL
  6. *
  7. * Copyright (C) 2001-2007 FhG Fokus
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. */
  26. -->
  27. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  28. version='1.0'
  29. >
  30. <xsl:import href="common.xsl"/>
  31. <xsl:template match="/">
  32. <xsl:variable name="createfile" select="concat($dir, concat('/', concat($prefix, 'create.sql')))"/>
  33. <xsl:document href="{$createfile}" method="text" indent="no" omit-xml-declaration="yes">
  34. <xsl:apply-templates select="/database[1]"/>
  35. </xsl:document>
  36. </xsl:template>
  37. <!-- ################ DATABASE ################# -->
  38. <!-- ################ /DATABASE ################# -->
  39. <!-- ################ TABLE ################# -->
  40. <xsl:template match="table">
  41. <xsl:variable name="table.name">
  42. <xsl:call-template name="get-name"/>
  43. </xsl:variable>
  44. <xsl:text>CREATE TABLE </xsl:text>
  45. <xsl:call-template name="quotechar"/>
  46. <xsl:value-of select="$table.name"/>
  47. <xsl:call-template name="quotechar"/>
  48. <xsl:text> (&#x0A;</xsl:text>
  49. <!-- Process all columns -->
  50. <xsl:apply-templates select="column"/>
  51. <!-- Process all unique indexes -->
  52. <xsl:apply-templates select="index[child::unique]"/>
  53. <!-- Process all primary indexes -->
  54. <xsl:apply-templates select="index[child::primary]"/>
  55. <xsl:text>&#x0A;</xsl:text>
  56. <xsl:call-template name="table.close"/>
  57. <!-- Create indexes for table -->
  58. <xsl:for-each select="index[count(child::unique)=0]">
  59. <xsl:if test="not(child::primary)">
  60. <xsl:call-template name="create_index"/>
  61. </xsl:if>
  62. </xsl:for-each>
  63. <!-- Create row in version table -->
  64. <xsl:apply-templates select="version"/>
  65. <xsl:text>&#x0A;</xsl:text>
  66. </xsl:template>
  67. <!-- ################ /TABLE ################ -->
  68. <!-- ################ /VERSION ################ -->
  69. <xsl:template match="version">
  70. <xsl:text>INSERT INTO version (table_name, table_version) values ('</xsl:text>
  71. <xsl:call-template name="get-name">
  72. <xsl:with-param name="select" select="parent::table"/>
  73. </xsl:call-template>
  74. <xsl:text>','</xsl:text>
  75. <xsl:value-of select="text()"/>
  76. <xsl:text>');&#x0A;</xsl:text>
  77. </xsl:template>
  78. <!-- ################ /VERSION ################ -->
  79. <!-- ################ INDEX (constraint) ################ -->
  80. <xsl:template match="index">
  81. <xsl:variable name="index.name">
  82. <xsl:call-template name="get-name"/>
  83. </xsl:variable>
  84. <xsl:if test="position()=1">
  85. <xsl:text>,&#x0A;</xsl:text>
  86. </xsl:if>
  87. <xsl:text> </xsl:text>
  88. <xsl:if test="not($index.name='')">
  89. <xsl:text>CONSTRAINT </xsl:text>
  90. <xsl:call-template name="get-index-name"/>
  91. </xsl:if>
  92. <xsl:if test="unique">
  93. <xsl:text> UNIQUE (</xsl:text>
  94. <xsl:apply-templates select="colref"/>
  95. <xsl:text>)</xsl:text>
  96. <xsl:if test="not(position()=last())">
  97. <xsl:text>,</xsl:text>
  98. <xsl:text>&#x0A;</xsl:text>
  99. </xsl:if>
  100. </xsl:if>
  101. <!-- PRIMARY KEY standalone definition -->
  102. <xsl:if test="primary">
  103. <xsl:text> PRIMARY KEY </xsl:text>
  104. <xsl:text> (</xsl:text>
  105. <xsl:apply-templates select="colref"/>
  106. <xsl:text>)</xsl:text>
  107. <xsl:if test="not(position()=last())">
  108. <xsl:text>,</xsl:text>
  109. <xsl:text>&#x0A;</xsl:text>
  110. </xsl:if>
  111. </xsl:if>
  112. </xsl:template>
  113. <!-- ################ /INDEX (constraint) ################ -->
  114. <!-- ################ INDEX (create) ################ -->
  115. <xsl:template name="create_index">
  116. <xsl:variable name="index.name">
  117. <xsl:call-template name="get-name"/>
  118. </xsl:variable>
  119. <xsl:variable name="table.name">
  120. <xsl:call-template name="get-name">
  121. <xsl:with-param name="select" select="parent::table"/>
  122. </xsl:call-template>
  123. </xsl:variable>
  124. <xsl:text>CREATE </xsl:text>
  125. <xsl:if test="unique">
  126. <xsl:text>UNIQUE </xsl:text>
  127. </xsl:if>
  128. <xsl:text>INDEX </xsl:text>
  129. <xsl:if test="not($index.name='')">
  130. <xsl:call-template name="get-index-name"/>
  131. </xsl:if>
  132. <xsl:text> ON </xsl:text>
  133. <xsl:value-of select="$table.name"/>
  134. <xsl:text> (</xsl:text>
  135. <xsl:apply-templates select="colref"/>
  136. <xsl:text>);&#x0A;</xsl:text>
  137. <xsl:if test="position()=last()">
  138. <xsl:text>&#x0A;</xsl:text>
  139. </xsl:if>
  140. </xsl:template>
  141. <!-- ################ /INDEX (create) ################ -->
  142. <!-- ################ COLUMN ################ -->
  143. <xsl:template match="column">
  144. <xsl:text> </xsl:text>
  145. <xsl:call-template name="quotechar"/>
  146. <xsl:call-template name="get-name"/>
  147. <xsl:call-template name="quotechar"/>
  148. <xsl:text> </xsl:text>
  149. <xsl:call-template name="column.type"/>
  150. <xsl:choose>
  151. <xsl:when test="default[@db=$db]">
  152. <xsl:text> DEFAULT </xsl:text>
  153. <xsl:choose>
  154. <xsl:when test="default[@db=$db]/null">
  155. <xsl:text>NULL</xsl:text>
  156. </xsl:when>
  157. <xsl:otherwise>
  158. <xsl:value-of select="default[@db=$db]"/>
  159. </xsl:otherwise>
  160. </xsl:choose>
  161. </xsl:when>
  162. <xsl:when test="default">
  163. <xsl:text> DEFAULT </xsl:text>
  164. <xsl:choose>
  165. <xsl:when test="default/null">
  166. <xsl:text>NULL</xsl:text>
  167. </xsl:when>
  168. <xsl:when test="string(number(default))='NaN'"><!-- test for string value -->
  169. <xsl:text>'</xsl:text>
  170. <xsl:value-of select="default"/>
  171. <xsl:text>'</xsl:text>
  172. </xsl:when>
  173. <xsl:otherwise>
  174. <xsl:value-of select="default"/><!-- ommit the quotes for numbers -->
  175. </xsl:otherwise>
  176. </xsl:choose>
  177. </xsl:when>
  178. </xsl:choose>
  179. <xsl:variable name="null">
  180. <xsl:call-template name="get-null"/>
  181. </xsl:variable>
  182. <xsl:if test="$null=0">
  183. <xsl:text> NOT NULL</xsl:text>
  184. </xsl:if>
  185. <xsl:if test="not(position()=last())">
  186. <xsl:text>,</xsl:text>
  187. <xsl:text>&#x0A;</xsl:text>
  188. </xsl:if>
  189. </xsl:template>
  190. <xsl:template name="column.type">
  191. <!-- FIXME -->
  192. <xsl:call-template name="get-type"/>
  193. <xsl:call-template name="column.size"/>
  194. </xsl:template>
  195. <xsl:template name="column.size">
  196. <xsl:variable name="size">
  197. <xsl:call-template name="get-size"/>
  198. </xsl:variable>
  199. <xsl:if test="not($size='')">
  200. <xsl:text>(</xsl:text>
  201. <xsl:value-of select="$size"/>
  202. <xsl:text>)</xsl:text>
  203. </xsl:if>
  204. </xsl:template>
  205. <!-- ################ /COLUMN ################ -->
  206. <!-- ################ COLREF ################ -->
  207. <xsl:template match="colref">
  208. <xsl:call-template name="quotechar"/>
  209. <xsl:call-template name="get-column-name">
  210. <xsl:with-param name="select" select="@linkend"/>
  211. </xsl:call-template>
  212. <xsl:call-template name="quotechar"/>
  213. <xsl:if test="not(position()=last())">
  214. <xsl:text>, </xsl:text>
  215. </xsl:if>
  216. </xsl:template>
  217. <!-- ################ /COLREF ################ -->
  218. </xsl:stylesheet>