| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?xml version='1.0'?>
- <!--
- * $Id: sql.xsl 4518 2008-07-28 15:39:28Z henningw $
- *
- * XSL converter script for SQL
- *
- * Copyright (C) 2001-2007 FhG Fokus
- *
- * This file is part of Kamailio, a free SIP server.
- *
- * Kamailio is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * Kamailio is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- */
- -->
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
- version='1.0'
- >
- <xsl:import href="common.xsl"/>
- <xsl:template match="/">
- <xsl:variable name="createfile" select="concat($dir, concat('/', concat($prefix, 'create.sql')))"/>
- <xsl:document href="{$createfile}" method="text" indent="no" omit-xml-declaration="yes">
- <xsl:apply-templates select="/database[1]"/>
- </xsl:document>
- </xsl:template>
- <!-- ################ DATABASE ################# -->
- <!-- ################ /DATABASE ################# -->
- <!-- ################ TABLE ################# -->
- <xsl:template match="table">
- <xsl:variable name="table.name">
- <xsl:call-template name="get-name"/>
- </xsl:variable>
- <xsl:text>CREATE TABLE </xsl:text>
- <xsl:call-template name="quotechar"/>
- <xsl:value-of select="$table.name"/>
- <xsl:call-template name="quotechar"/>
- <xsl:text> (
</xsl:text>
- <!-- Process all columns -->
- <xsl:apply-templates select="column"/>
- <!-- Process all unique indexes -->
- <xsl:apply-templates select="index[child::unique]"/>
- <!-- Process all primary indexes -->
- <xsl:apply-templates select="index[child::primary]"/>
- <xsl:text>
</xsl:text>
- <xsl:call-template name="table.close"/>
- <!-- Create indexes for table -->
- <xsl:for-each select="index[count(child::unique)=0]">
- <xsl:if test="not(child::primary)">
- <xsl:call-template name="create_index"/>
- </xsl:if>
- </xsl:for-each>
- <!-- Create row in version table -->
- <xsl:apply-templates select="version"/>
- <xsl:text>
</xsl:text>
- </xsl:template>
- <!-- ################ /TABLE ################ -->
- <!-- ################ /VERSION ################ -->
- <xsl:template match="version">
- <xsl:text>INSERT INTO version (table_name, table_version) values ('</xsl:text>
- <xsl:call-template name="get-name">
- <xsl:with-param name="select" select="parent::table"/>
- </xsl:call-template>
- <xsl:text>','</xsl:text>
- <xsl:value-of select="text()"/>
- <xsl:text>');
</xsl:text>
- </xsl:template>
- <!-- ################ /VERSION ################ -->
- <!-- ################ INDEX (constraint) ################ -->
- <xsl:template match="index">
- <xsl:variable name="index.name">
- <xsl:call-template name="get-name"/>
- </xsl:variable>
- <xsl:if test="position()=1">
- <xsl:text>,
</xsl:text>
- </xsl:if>
- <xsl:text> </xsl:text>
- <xsl:if test="not($index.name='')">
- <xsl:text>CONSTRAINT </xsl:text>
- <xsl:call-template name="get-index-name"/>
- </xsl:if>
- <xsl:if test="unique">
- <xsl:text> UNIQUE (</xsl:text>
- <xsl:apply-templates select="colref"/>
- <xsl:text>)</xsl:text>
- <xsl:if test="not(position()=last())">
- <xsl:text>,</xsl:text>
- <xsl:text>
</xsl:text>
- </xsl:if>
- </xsl:if>
- <!-- PRIMARY KEY standalone definition -->
- <xsl:if test="primary">
- <xsl:text> PRIMARY KEY </xsl:text>
- <xsl:text> (</xsl:text>
- <xsl:apply-templates select="colref"/>
- <xsl:text>)</xsl:text>
- <xsl:if test="not(position()=last())">
- <xsl:text>,</xsl:text>
- <xsl:text>
</xsl:text>
- </xsl:if>
- </xsl:if>
- </xsl:template>
- <!-- ################ /INDEX (constraint) ################ -->
- <!-- ################ INDEX (create) ################ -->
- <xsl:template name="create_index">
- <xsl:variable name="index.name">
- <xsl:call-template name="get-name"/>
- </xsl:variable>
- <xsl:variable name="table.name">
- <xsl:call-template name="get-name">
- <xsl:with-param name="select" select="parent::table"/>
- </xsl:call-template>
- </xsl:variable>
- <xsl:text>CREATE </xsl:text>
- <xsl:if test="unique">
- <xsl:text>UNIQUE </xsl:text>
- </xsl:if>
- <xsl:text>INDEX </xsl:text>
- <xsl:if test="not($index.name='')">
- <xsl:call-template name="get-index-name"/>
- </xsl:if>
- <xsl:text> ON </xsl:text>
- <xsl:value-of select="$table.name"/>
- <xsl:text> (</xsl:text>
- <xsl:apply-templates select="colref"/>
- <xsl:text>);
</xsl:text>
- <xsl:if test="position()=last()">
- <xsl:text>
</xsl:text>
- </xsl:if>
- </xsl:template>
- <!-- ################ /INDEX (create) ################ -->
- <!-- ################ COLUMN ################ -->
- <xsl:template match="column">
- <xsl:text> </xsl:text>
- <xsl:call-template name="quotechar"/>
- <xsl:call-template name="get-name"/>
- <xsl:call-template name="quotechar"/>
- <xsl:text> </xsl:text>
- <xsl:call-template name="column.type"/>
- <xsl:choose>
- <xsl:when test="default[@db=$db]">
- <xsl:text> DEFAULT </xsl:text>
- <xsl:choose>
- <xsl:when test="default[@db=$db]/null">
- <xsl:text>NULL</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="default[@db=$db]"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <xsl:when test="default">
- <xsl:text> DEFAULT </xsl:text>
- <xsl:choose>
- <xsl:when test="default/null">
- <xsl:text>NULL</xsl:text>
- </xsl:when>
- <xsl:when test="string(number(default))='NaN'"><!-- test for string value -->
- <xsl:text>'</xsl:text>
- <xsl:value-of select="default"/>
- <xsl:text>'</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="default"/><!-- ommit the quotes for numbers -->
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- </xsl:choose>
- <xsl:variable name="null">
- <xsl:call-template name="get-null"/>
- </xsl:variable>
- <xsl:if test="$null=0">
- <xsl:text> NOT NULL</xsl:text>
- </xsl:if>
- <xsl:if test="not(position()=last())">
- <xsl:text>,</xsl:text>
- <xsl:text>
</xsl:text>
- </xsl:if>
- </xsl:template>
- <xsl:template name="column.type">
- <!-- FIXME -->
- <xsl:call-template name="get-type"/>
- <xsl:call-template name="column.size"/>
- </xsl:template>
- <xsl:template name="column.size">
- <xsl:variable name="size">
- <xsl:call-template name="get-size"/>
- </xsl:variable>
- <xsl:if test="not($size='')">
- <xsl:text>(</xsl:text>
- <xsl:value-of select="$size"/>
- <xsl:text>)</xsl:text>
- </xsl:if>
- </xsl:template>
- <!-- ################ /COLUMN ################ -->
- <!-- ################ COLREF ################ -->
- <xsl:template match="colref">
- <xsl:call-template name="quotechar"/>
- <xsl:call-template name="get-column-name">
- <xsl:with-param name="select" select="@linkend"/>
- </xsl:call-template>
- <xsl:call-template name="quotechar"/>
- <xsl:if test="not(position()=last())">
- <xsl:text>, </xsl:text>
- </xsl:if>
- </xsl:template>
- <!-- ################ /COLREF ################ -->
- </xsl:stylesheet>
|