<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:Gen-it="http://mycompany.com/mynamespace"
	version="1.0">
<xsl:output method="text" encoding="us-ascii" indent="yes"/>

<xsl:param name="ProjectName">TEST</xsl:param>
<xsl:param name="Author">&lt;Gen-it&gt;</xsl:param>
<xsl:param name="CreationDate"><xsl:value-of select="Gen-it:GetDate()"/></xsl:param>

<xsl:param name="ProjectPrefix">T_</xsl:param>

<xsl:template match="/">-- ***********************************************************************************************************************************
-- CREATE TABLE script
--
-- copyright 2000 <xsl:value-of select="$Author"/>
-- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- When:   <xsl:value-of select="$CreationDate"/> 
-- Who:    <xsl:value-of select="$Author"/> 
-- What:   Initial release generated with &lt;Gen-it&gt; SQLCreate.xsl v1.0
--************************************************************************************************************************************

--************************************************************************************************************************************
-- Drop Constraints
--************************************************************************************************************************************
<xsl:for-each select="//Class">
	<xsl:for-each select="Attribute">
		<xsl:if test="Attribute.stereotype='FK'">
ALTER TABLE <xsl:value-of select="$ProjectPrefix"/><xsl:value-of select="../Class.name"/> DROP CONSTRAINT "FK_<xsl:value-of select="../Class.name"/>_<xsl:value-of select="Attribute.name"/>"</xsl:if>
	</xsl:for-each>
</xsl:for-each>

--************************************************************************************************************************************
-- Drop Tables
--************************************************************************************************************************************
<xsl:for-each select="//Class">
DROP TABLE <xsl:value-of select="$ProjectPrefix"/><xsl:value-of select="Gen-it:CapFirst(Class.name)"/>;</xsl:for-each>

--************************************************************************************************************************************
-- Create Tables
--************************************************************************************************************************************
<xsl:for-each select="//Class">
CREATE TABLE <xsl:value-of select="$ProjectPrefix"/><xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
(
	<xsl:for-each select="Attribute">
		<xsl:value-of select="Gen-it:FillSpace(Attribute.name, 30)"/>
		<xsl:apply-templates select="." mode="getdatatype"/><xsl:value-of select="' '"/><xsl:apply-templates select="." mode="NullOrNotNull"/>,
	</xsl:for-each>CONSTRAINT PK_<xsl:value-of select="Class.name"/> PRIMARY KEY (<xsl:apply-templates select="." mode="primarykeyattribute"/>)
);
</xsl:for-each>

--************************************************************************************************************************************
-- Create Constraints
--************************************************************************************************************************************
<xsl:for-each select="//Class">
	<xsl:for-each select="Attribute">
		<xsl:if test="Attribute.stereotype='FK'">
ALTER TABLE <xsl:value-of select="$ProjectPrefix"/><xsl:value-of select="../Class.name"/> ADD CONSTRAINT "FK_<xsl:value-of select="../Class.name"/>_<xsl:value-of select="Attribute.name"/>"
FOREIGN KEY ("<xsl:value-of select="Attribute.name"/>") REFERENCES <xsl:value-of select="$ProjectPrefix"/><xsl:value-of select="Reference/Reference.class"/> ("<xsl:value-of select="Reference/Reference.attribute"/>");
		</xsl:if>
	</xsl:for-each>
</xsl:for-each>
</xsl:template>

<xsl:template match="Class" mode="primarykeyattribute">
	<xsl:for-each select="Attribute">
		<xsl:if test="Attribute.stereotype='PK'"><xsl:value-of select="Attribute.name"/></xsl:if>
	</xsl:for-each>
</xsl:template>

<xsl:template match="Attribute" mode="getdatatype">
	<!-- Datatype is defined by the datatype tag. -->
	<xsl:value-of select="Gen-it:FillSpace(Attribute.datatype, 20)"/>
</xsl:template>

<xsl:template match="Attribute" mode="NullOrNotNull">
	<!-- Datatype is defined by the stereotype tag. -->
	<!-- Except for attributes with a PK or FK stereotype. -->
	<xsl:if test="Attribute.stereotype='PK'">NOT NULL</xsl:if>
	<xsl:if test="Attribute.stereotype='FK'">
		<xsl:if test="Reference/Reference.optional='False'">NOT NULL</xsl:if>
		<xsl:if test="Reference/Reference.optional!='False'">NULL</xsl:if>
	</xsl:if>
	<xsl:if test="Gen-it:Lower(Attribute.stereotype)='not null'">NOT NULL</xsl:if>
	<xsl:if test="Gen-it:Lower(Attribute.stereotype)='null'">NULL</xsl:if>
	<xsl:if test="Gen-it:Lower(Attribute.stereotype)='optional'">NULL</xsl:if>
	<xsl:if test="Gen-it:Lower(Attribute.stereotype)='not optional'">NOT NULL</xsl:if>
</xsl:template>

<msxsl:script language="VBScript" implements-prefix="Gen-it">
	' <!-- Return the text of the node as all UPPERCASE -->
	Function Upper(nodelist) 
		Upper = UCASE(nodelist.nextNode().text)
	End Function
	' <!-- Return the text of the node as all lowercase -->
	Function Lower(nodelist) 
		Lower = LCASE(nodelist.nextNode().text)
	End Function
	' <!-- Return the text of the node with the first character converted to Uppercase -->
	Function CapFirst(nodelist) 
		dim strText
		dim strOutput
		strText = nodelist.nextNode().text
		strOutput = UCASE(Left( strText, 1))
		strOutput = strOutput + LCASE((Right( strText, LEN(strText)-1)))
		CapFirst = strOutput
	End Function
	' <!-- Return the text of the node plus spaces to create a string of the specified length -->
	Function FillSpace(nodelist, length) 
		dim strText
		dim strOutput
		strText = nodelist.nextNode().text
		strOutput = strText + String( length - LEN(strText) , " ")
		FillSpace = strOutput
	End Function
	' <!-- Return a string containing spaces of the specified length minus the length of a text node-->
	Function AddSpace(nodelist, length) 
		dim strText
		dim strOutput
		strText = nodelist.nextNode().text
		strOutput = String( length - LEN(strText) , " ")
		AddSpace = strOutput
	End Function
	Function GetDate()
		GetDate = Cstr(Date())
	End Function
</msxsl:script>



</xsl:stylesheet>

