<?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:space="preserve">
<xsl:output method="text" encoding="us-ascii" indent="yes"/>

<xsl:variable name="ProjectName">SampleCase_</xsl:variable>
<xsl:variable name="Author">&lt;Gen-it&gt;</xsl:variable>
<xsl:variable name="CreationDate">24-01-2001</xsl:variable>

<xsl:template match="/">
<xsl:for-each select="//Class">
DROP PROCEDURE <xsl:value-of select="$ProjectName"/>GET_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
go
DROP PROCEDURE <xsl:value-of select="$ProjectName"/>DELETE_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
go
DROP PROCEDURE <xsl:value-of select="$ProjectName"/>INSERT_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
go
</xsl:for-each>
<xsl:for-each select="//Class">-- ***********************************************************************************************************************************
-- <xsl:value-of select="$ProjectName"/>GET_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
--
-- ***********************************************************************************************************************************
-- Gets a specific <xsl:value-of select="@tablename"/> entry based on values passed through parameters.
-- ***********************************************************************************************************************************
-- copyright 2000 <xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
-- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- When:  <xsl:value-of select="$CreationDate"/> 
-- Who:    <xsl:value-of select="$Author"/> 
-- What:   Initial release
--************************************************************************************************************************************
CREATE PROCEDURE <xsl:value-of select="$ProjectName"/>GET_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>( <xsl:for-each select="Attribute[Attribute.stereotype='PK']">
      @<xsl:value-of select="Attribute.name"/><![CDATA[ ]]><xsl:apply-templates select="." mode="datatype"/><xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>)
AS
BEGIN
	SELECT <xsl:for-each select="Attribute">
		<xsl:value-of select="Attribute.name"/><xsl:if test="position()!=last()">,
		</xsl:if></xsl:for-each>
	FROM <xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
	WHERE (<xsl:for-each select="Attribute[Attribute.stereotype='PK']">
		<xsl:if test="position()!=1"><![CDATA[
	AND ]]></xsl:if>
		<xsl:value-of select="Attribute.name"/> = @<xsl:value-of select="Attribute.name"/>
		</xsl:for-each>)
	RETURN(0)
END
go	
-- ***********************************************************************************************************************************
-- <xsl:value-of select="$ProjectName"/>DELETE_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
-- 
-- ***********************************************************************************************************************************
-- Deletes a specific <xsl:value-of select="Gen-it:CapFirst(Class.name)"/> entry based on values passed through parameters.
-- ***********************************************************************************************************************************
-- copyright 2000 <xsl:value-of select="$Author"/>
-- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- When:  <xsl:value-of select="$CreationDate"/> 
-- Who:    <xsl:value-of select="$Author"/> 
-- What:   Initial release
--************************************************************************************************************************************
CREATE PROCEDURE <xsl:value-of select="$ProjectName"/>DELETE_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>( <xsl:for-each select="Attribute[Attribute.stereotype='PK']">
      @<xsl:value-of select="Attribute.name"/><![CDATA[ ]]><xsl:apply-templates select="." mode="datatype"/><xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>)
AS
BEGIN
--************************************************************************************************************************************
-- Declare Variables [Start]
 	Declare
 	@CurrentError  int
-- Declare Variables [End]
--************************************************************************************************************************************
-- Begin Procedure Body
	DELETE
	FROM <xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
	WHERE (<xsl:for-each select="Attribute[Attribute.stereotype='PK']">
		<xsl:if test="position()!=1"><![CDATA[
	AND ]]></xsl:if>
		<xsl:value-of select="Attribute.name"/> = @<xsl:value-of select="Attribute.name"/>
		</xsl:for-each>
	)
 	BEGIN
  		SELECT @CurrentError= @@error
  		IF (@CurrentError &lt;&gt; 0)
   			RETURN @CurrentError
 	END
	RETURN(0)
END
go
-- ***********************************************************************************************************************************
-- <xsl:value-of select="$ProjectName"/>INSERT_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>
-- 
-- ***********************************************************************************************************************************
-- Inserts a new <xsl:value-of select="Gen-it:CapFirst(Class.name)"/> entry based on values passed through parameters.
-- ***********************************************************************************************************************************
-- copyright 2000 <xsl:value-of select="$Author"/>
-- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- When:  <xsl:value-of select="$CreationDate"/> 
-- Who:    <xsl:value-of select="$Author"/> 
-- What:   Initial release
--************************************************************************************************************************************
CREATE PROCEDURE <xsl:value-of select="$ProjectName"/>INSERT_<xsl:value-of select="Gen-it:CapFirst(Class.name)"/>( <xsl:for-each select="Attribute">
      @<xsl:value-of select="Attribute.name"/><![CDATA[ ]]><xsl:apply-templates select="." mode="datatype"/><xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>)
AS
BEGIN
--************************************************************************************************************************************
-- Declare Variables [Start]
 	Declare
 	@CurrentError  int
-- Declare Variables [End]
--************************************************************************************************************************************
-- Begin Procedure Body 
	INSERT INTO <xsl:value-of select="Gen-it:CapFirst(Class.name)"/>(
		<xsl:for-each select="Attribute"><xsl:value-of select="Attribute.name"/><xsl:if test="position()!=last()">,
		</xsl:if>
		</xsl:for-each>)	
     	VALUES (
     		<xsl:for-each select="Attribute">@<xsl:value-of select="Attribute.name"/><xsl:if test="position()!=last()">,
     	</xsl:if>
</xsl:for-each>)	
  	BEGIN
  		SELECT @CurrentError = @@error
  		IF (@CurrentError &lt;&gt; 0)
   			RETURN @CurrentError
 	END
END
go

</xsl:for-each>
</xsl:template>

<xsl:template match="Attribute" mode="datatype">
	<xsl:if test="substring(Gen-it:Lower(Attribute.datatype),1,7)='varchar'">VARCHAR</xsl:if>
	<xsl:if test="substring(Gen-it:Lower(Attribute.datatype),1,7)='numeric'">NUMERIC</xsl:if>
	<xsl:if test="substring(Gen-it:Lower(Attribute.datatype),1,8)='datetime'">DATETIME</xsl:if>
	<xsl:if test="substring(Gen-it:Lower(Attribute.datatype),1,4)='real'">REAL</xsl:if>
	<xsl:if test="substring(Gen-it:Lower(Attribute.datatype),1,5)='text'">TEXT</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
</msxsl:script>

</xsl:stylesheet>

