<?xml version='1.0'?>
<!DOCTYPE xsl:stylesheet SYSTEM "xsl.dtd">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method ="text" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" standalone="yes" />

<!--
  doctype-public = string
  doctype-system = string
  cdata-section-elements = qnames
  indent = "yes" | "no"
  media-type = string />
-->


<!--
///////////////////////////////////////////////////////////////////////////////
///// BEGIN SQL TEMPLATES /////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
-->
<xsl:template match="/">
	<xsl:apply-templates />
</xsl:template>

<xsl:template match="table_definition">
CREATE TABLE `<xsl:value-of select="/mysql_table/@tbl_name" />` (
<xsl:apply-templates />
) <xsl:apply-templates select="/mysql_table/table_options" /> ;
</xsl:template>

<xsl:template match="column_definition" >
	`<xsl:value-of select="@col_name" />` <xsl:apply-templates select="type" /> <xsl:if test="@null='NOT_NULL'">NOT NULL </xsl:if><xsl:if test="count(@DEFAULT)>0">DEFAULT '<xsl:value-of select="@DEFAULT" />' </xsl:if><xsl:if test="count(@AUTO_INCREMENT)>0">AUTO_INCREMENT </xsl:if><xsl:if test="count(@COMMENT)>0">COMMENT '<xsl:value-of select="@COMMENT" />'</xsl:if><xsl:apply-templates select="reference_definition" />,
</xsl:template>

<!-- generic rule for type, invokes more specific roles -->
<xsl:template match="type">
	<xsl:apply-templates />
</xsl:template>

<!-- the specific roles -->

<!-- int types -->
<xsl:template match="TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT">
<xsl:value-of select="name()" /><xsl:if test="count(@length)>0">(<xsl:value-of select="@length" />) </xsl:if><xsl:if test="count(@UNSIGNED)>0">UNSIGNED </xsl:if><xsl:if test="count(@ZEROFILL)">ZEROFILL</xsl:if>
</xsl:template>

<!-- numeric types -->
<xsl:template match="REAL|DOUBLE|FLOAT|DECIMAL|NUMERIC">
<xsl:value-of select="name()" /><xsl:if test="count(@length)>0">(<xsl:value-of select="@length" />,<xsl:value-of select="@decimals" />) </xsl:if><xsl:if test="count(@UNSIGNED)>0">UNSIGNED </xsl:if><xsl:if test="count(@ZEROFILL)">ZEROFILL</xsl:if>
</xsl:template>

<!-- types with no need for attributes -->
<xsl:template match="DATE|TIME|TIMESTAMP|DATETIME|TINYBLOB|BLOB|MEDIUMBLOB|LONGBLOB">
<xsl:value-of select="name()" />
</xsl:template>

<!-- char and varchar -->
<xsl:template match="CHAR|VARCHAR">
<xsl:value-of select="name()" />(<xsl:value-of select="@length" />) <xsl:if test="count(@BINARY)>0">BINARY </xsl:if><xsl:if test="count(@ASCII)>0">ASCII </xsl:if><xsl:if test="count(@UNICODE)">UNICODE</xsl:if>
</xsl:template>

<!-- text -->
<xsl:template match="TINYTEXT|TEXT|MEDIUMTEXT|LONGTEXT">
<xsl:value-of select="name()" /> <xsl:if test="count(@BINARY)>0">BINARY</xsl:if>
</xsl:template>

<!-- enum/set -->
<xsl:template match="ENUM|SET">
<xsl:value-of select="name()" /> (<xsl:apply-templates select="SET_ITEM" />)
</xsl:template>

<!-- SET_ITEM for enum/set -->
<xsl:template match="SET_ITEM">
'<xsl:value-of select="@value" />'<xsl:if test="not(position() = last())">,</xsl:if>
</xsl:template>

<!--
///////////////////////////////////////////////////////////////////////////////
///// BEGIN POST COLUMN STUFF /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
-->

<xsl:template match="PRIMARY_KEY">
PRIMARY KEY (<xsl:apply-templates select="index_col_name" />),
</xsl:template>

<xsl:template match="KEY">
KEY (<xsl:apply-templates select="index_col_name" />),
</xsl:template>

<xsl:template match="index_col_name">
`<xsl:apply-templates />`<xsl:if test="count(@length)>0">(<xsl:value-of select="@length" />) </xsl:if><xsl:if test="count(@order)>0"><xsl:value-of select="@order" /></xsl:if>
</xsl:template>

<xsl:template match="table_options">
<xsl:apply-templates select="TYPE|ENGINE" /> <xsl:apply-templates select="@*" />
</xsl:template>

<xsl:template match="TYPE|ENGINE">
<xsl:value-of select="name()" />=<xsl:value-of select="@value" />
</xsl:template>

<xsl:template match="table_options/@*">
 <xsl:value-of select="name()" />='<xsl:value-of select="." />'
</xsl:template>

</xsl:stylesheet>

