Fixes to escape SQL keywords when used as column names

This commit is contained in:
sb 2008-03-19 15:37:48 +00:00
parent 74be55668f
commit b240169fc7
3 changed files with 199 additions and 50 deletions

View file

@ -12,7 +12,7 @@
Transform ADL to Hibernate Transform ADL to Hibernate
$Author: sb $ $Author: sb $
$Revision: 1.9 $ $Revision: 1.10 $
--> -->
<!-- <!--
@ -44,6 +44,72 @@
</xsl:choose> </xsl:choose>
</xsl:variable> </xsl:variable>
<!-- define upper and lower case letters to enable case conversion -->
<xsl:variable name="ucase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="lcase">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<!-- define SQL keywords to police these out of field names -->
<xsl:variable name="sqlkeywords-multiline">
ADD EXCEPT PERCENT
ALL EXEC PLAN
ALTER EXECUTE PRECISION
AND EXISTS PRIMARY
ANY EXIT PRINT
AS FETCH PROC
ASC FILE PROCEDURE
AUTHORIZATION FILLFACTOR PUBLIC
BACKUP FOR RAISERROR
BEGIN FOREIGN READ
BETWEEN FREETEXT READTEXT
BREAK FREETEXTTABLE RECONFIGURE
BROWSE FROM REFERENCES
BULK FULL REPLICATION
BY FUNCTION RESTORE
CASCADE GOTO RESTRICT
CASE GRANT RETURN
CHECK GROUP REVOKE
CHECKPOINT HAVING RIGHT
CLOSE HOLDLOCK ROLLBACK
CLUSTERED IDENTITY ROWCOUNT
COALESCE IDENTITY_INSERT ROWGUIDCOL
COLLATE IDENTITYCOL RULE
COLUMN IF SAVE
COMMIT IN SCHEMA
COMPUTE INDEX SELECT
CONSTRAINT INNER SESSION_USER
CONTAINS INSERT SET
CONTAINSTABLE INTERSECT SETUSER
CONTINUE INTO SHUTDOWN
CONVERT IS SOME
CREATE JOIN STATISTICS
CROSS KEY SYSTEM_USER
CURRENT KILL TABLE
CURRENT_DATE LEFT TEXTSIZE
CURRENT_TIME LIKE THEN
CURRENT_TIMESTAMP LINENO TO
CURRENT_USER LOAD TOP
CURSOR NATIONAL TRAN
DATABASE NOCHECK TRANSACTION
DBCC NONCLUSTERED TRIGGER
DEALLOCATE NOT TRUNCATE
DECLARE NULL TSEQUAL
DEFAULT NULLIF UNION
DELETE OF UNIQUE
DENY OFF UPDATE
DESC OFFSETS UPDATETEXT
DISK ON USE
DISTINCT OPEN USER
DISTRIBUTED OPENDATASOURCE VALUES
DOUBLE OPENQUERY VARYING
DROP OPENROWSET VIEW
DUMMY OPENXML WAITFOR
DUMP OPTION WHEN
ELSE OR WHERE
END ORDER WHILE
ERRLVL OUTER WITH
ESCAPE OVER WRITETEXT
</xsl:variable>
<xsl:variable name="sqlkeywords" select="concat(' ', normalize-space($sqlkeywords-multiline), ' ')"/>
<xsl:template match="adl:application"> <xsl:template match="adl:application">
<hibernate-mapping> <hibernate-mapping>
<xsl:attribute name="namespace"> <xsl:attribute name="namespace">
@ -63,7 +129,7 @@
* THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NOT * THIS FILE IS AUTOMATICALLY GENERATED AND SHOULD NOT
* BE MANUALLY EDITED. * BE MANUALLY EDITED.
* *
* Generated using adl2hibernate.xslt revision <xsl:value-of select="substring('$Revision: 1.9 $', 12)"/> * Generated using adl2hibernate.xslt revision <xsl:value-of select="substring('$Revision: 1.10 $', 12)"/>
* *
*************************************************************************** ***************************************************************************
</xsl:comment> </xsl:comment>
@ -114,14 +180,9 @@
<xsl:value-of select="adl:property[position()=1]/@name"/> <xsl:value-of select="adl:property[position()=1]/@name"/>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="column"> <xsl:attribute name="column">
<xsl:choose> <xsl:call-template name="property-column-name">
<xsl:when test="adl:property[position()=1]/@column"> <xsl:with-param name="property" select="adl:property[position()=1]"/>
<xsl:value-of select="adl:property[position()=1]/@column"/> </xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="adl:property[position()=1]/@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="type"> <xsl:attribute name="type">
<xsl:call-template name="csharp-base-type"> <xsl:call-template name="csharp-base-type">
@ -193,14 +254,9 @@
<xsl:value-of select="@name"/> <xsl:value-of select="@name"/>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="column"> <xsl:attribute name="column">
<xsl:choose> <xsl:call-template name="property-column-name">
<xsl:when test="@column"> <xsl:with-param name="property" select="."/>
<xsl:value-of select="@column"/> </xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="type"> <xsl:attribute name="type">
<xsl:call-template name="csharp-type"> <xsl:call-template name="csharp-type">
@ -216,14 +272,9 @@
<xsl:value-of select="@name"/> <xsl:value-of select="@name"/>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="column"> <xsl:attribute name="column">
<xsl:choose> <xsl:call-template name="property-column-name">
<xsl:when test="@column"> <xsl:with-param name="property" select="."/>
<xsl:value-of select="@column"/> </xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="class"> <xsl:attribute name="class">
<xsl:value-of select="@entity"/> <xsl:value-of select="@entity"/>
@ -271,7 +322,9 @@
</xsl:attribute> </xsl:attribute>
<xsl:if test="@column"> <xsl:if test="@column">
<xsl:attribute name="column"> <xsl:attribute name="column">
<xsl:value-of select="@column"/> <xsl:call-template name="property-column-name">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:attribute> </xsl:attribute>
</xsl:if> </xsl:if>
</many-to-one> </many-to-one>
@ -315,14 +368,9 @@
<xsl:value-of select="@entity"/> <xsl:value-of select="@entity"/>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="column"> <xsl:attribute name="column">
<xsl:choose> <xsl:call-template name="property-column-name">
<xsl:when test="@column"> <xsl:with-param name="property" select="."/>
<xsl:value-of select="@column"/> </xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute> </xsl:attribute>
<xsl:if test="@farkey"> <xsl:if test="@farkey">
<xsl:attribute name="property-ref"> <xsl:attribute name="property-ref">
@ -454,14 +502,9 @@
</xsl:call-template> </xsl:call-template>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="column"> <xsl:attribute name="column">
<xsl:choose> <xsl:call-template name="property-column-name">
<xsl:when test="@column"> <xsl:with-param name="property" select="."/>
<xsl:value-of select="@column"/> </xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute> </xsl:attribute>
<xsl:apply-templates select="adl:documentation"/> <xsl:apply-templates select="adl:documentation"/>
</property> </property>
@ -473,6 +516,31 @@
</xsl:comment> </xsl:comment>
</xsl:template> </xsl:template>
<!-- consistent, repeatable way of getting the column name for a given property -->
<xsl:template name="property-column-name">
<!-- a property element -->
<xsl:param name="property"/>
<xsl:variable name="unescaped">
<xsl:choose>
<xsl:when test="$property/@column">
<xsl:value-of select="$property/@column"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$property/@name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains( $sqlkeywords, concat(' ', translate( $unescaped, $lcase, $ucase),' '))">
<xsl:value-of select="concat( '[', $unescaped, ']')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$unescaped"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- <!--
horrible, horrible hackery. Compare two strings and return horrible, horrible hackery. Compare two strings and return
* 0 if they are identical, * 0 if they are identical,

View file

@ -12,7 +12,7 @@
Convert ADL to MS-SQL Convert ADL to MS-SQL
$Author: sb $ $Author: sb $
$Revision: 1.11 $ $Revision: 1.12 $
--> -->
<xsl:output indent="no" encoding="UTF-8" method="text"/> <xsl:output indent="no" encoding="UTF-8" method="text"/>
@ -28,13 +28,80 @@
<xsl:param name="abstract-key-name-convention" select="Id"/> <xsl:param name="abstract-key-name-convention" select="Id"/>
<xsl:param name="database"/> <xsl:param name="database"/>
<!-- define upper and lower case letters to enable case conversion -->
<xsl:variable name="ucase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="lcase">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<!-- define SQL keywords to police these out of field names -->
<xsl:variable name="sqlkeywords-multiline">
ADD EXCEPT PERCENT
ALL EXEC PLAN
ALTER EXECUTE PRECISION
AND EXISTS PRIMARY
ANY EXIT PRINT
AS FETCH PROC
ASC FILE PROCEDURE
AUTHORIZATION FILLFACTOR PUBLIC
BACKUP FOR RAISERROR
BEGIN FOREIGN READ
BETWEEN FREETEXT READTEXT
BREAK FREETEXTTABLE RECONFIGURE
BROWSE FROM REFERENCES
BULK FULL REPLICATION
BY FUNCTION RESTORE
CASCADE GOTO RESTRICT
CASE GRANT RETURN
CHECK GROUP REVOKE
CHECKPOINT HAVING RIGHT
CLOSE HOLDLOCK ROLLBACK
CLUSTERED IDENTITY ROWCOUNT
COALESCE IDENTITY_INSERT ROWGUIDCOL
COLLATE IDENTITYCOL RULE
COLUMN IF SAVE
COMMIT IN SCHEMA
COMPUTE INDEX SELECT
CONSTRAINT INNER SESSION_USER
CONTAINS INSERT SET
CONTAINSTABLE INTERSECT SETUSER
CONTINUE INTO SHUTDOWN
CONVERT IS SOME
CREATE JOIN STATISTICS
CROSS KEY SYSTEM_USER
CURRENT KILL TABLE
CURRENT_DATE LEFT TEXTSIZE
CURRENT_TIME LIKE THEN
CURRENT_TIMESTAMP LINENO TO
CURRENT_USER LOAD TOP
CURSOR NATIONAL TRAN
DATABASE NOCHECK TRANSACTION
DBCC NONCLUSTERED TRIGGER
DEALLOCATE NOT TRUNCATE
DECLARE NULL TSEQUAL
DEFAULT NULLIF UNION
DELETE OF UNIQUE
DENY OFF UPDATE
DESC OFFSETS UPDATETEXT
DISK ON USE
DISTINCT OPEN USER
DISTRIBUTED OPENDATASOURCE VALUES
DOUBLE OPENQUERY VARYING
DROP OPENROWSET VIEW
DUMMY OPENXML WAITFOR
DUMP OPTION WHEN
ELSE OR WHERE
END ORDER WHILE
ERRLVL OUTER WITH
ESCAPE OVER WRITETEXT
</xsl:variable>
<xsl:variable name="sqlkeywords" select="concat(' ', normalize-space($sqlkeywords-multiline), ' ')"/>
<xsl:template match="adl:application"> <xsl:template match="adl:application">
------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------
-- --
-- Application Description Language framework -- Application Description Language framework
-- --
-- Database for application <xsl:value-of select="@name"/> version <xsl:value-of select="@version"/> -- Database for application <xsl:value-of select="@name"/> version <xsl:value-of select="@version"/>
-- Generated for MS-SQL 2000+ using adl2mssql.xslt <xsl:value-of select="substring('$Revision: 1.11 $', 12)"/> -- Generated for MS-SQL 2000+ using adl2mssql.xslt <xsl:value-of select="substring('$Revision: 1.12 $', 12)"/>
-- --
-- Code generator (c) 2007 Cygnet Solutions Ltd -- Code generator (c) 2007 Cygnet Solutions Ltd
-- --
@ -523,7 +590,9 @@
<!-- consistent, repeatable way of getting the column name for a given property --> <!-- consistent, repeatable way of getting the column name for a given property -->
<xsl:template name="property-column-name"> <xsl:template name="property-column-name">
<!-- a property element -->
<xsl:param name="property"/> <xsl:param name="property"/>
<xsl:variable name="unescaped">
<xsl:choose> <xsl:choose>
<xsl:when test="$property/@column"> <xsl:when test="$property/@column">
<xsl:value-of select="$property/@column"/> <xsl:value-of select="$property/@column"/>
@ -532,6 +601,15 @@
<xsl:value-of select="$property/@name"/> <xsl:value-of select="$property/@name"/>
</xsl:otherwise> </xsl:otherwise>
</xsl:choose> </xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains( $sqlkeywords, concat(' ', translate( $unescaped, $lcase, $ucase),' '))">
<xsl:value-of select="concat( '[', $unescaped, ']')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$unescaped"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template> </xsl:template>
<xsl:template name="primary-key-name"> <xsl:template name="primary-key-name">

View file

@ -12,8 +12,8 @@
Transform ADL into velocity view templates Transform ADL into velocity view templates
$Author: sb $ $Author: sb $
$Revision: 1.11 $ $Revision: 1.12 $
$Date: 2008-03-12 13:46:10 $ $Date: 2008-03-19 15:37:48 $
--> -->
<!-- WARNING WARNING WARNING: Do NOT reformat this file! <!-- WARNING WARNING WARNING: Do NOT reformat this file!
Whitespace (or lack of it) is significant! --> Whitespace (or lack of it) is significant! -->
@ -44,7 +44,7 @@
stored to CVS --> stored to CVS -->
<xsl:variable name="transform-rev1" <xsl:variable name="transform-rev1"
select="substring( '$Revision: 1.11 $', 11)"/> select="substring( '$Revision: 1.12 $', 11)"/>
<xsl:variable name="transform-revision" <xsl:variable name="transform-revision"
select="substring( $transform-rev1, 0, string-length( $transform-rev1) - 1)"/> select="substring( $transform-rev1, 0, string-length( $transform-rev1) - 1)"/>
@ -925,7 +925,7 @@
<xsl:value-of select="//adl:definition[@name=$definition]/@size"/> <xsl:value-of select="//adl:definition[@name=$definition]/@size"/>
</xsl:variable> </xsl:variable>
<input type="text"> <input type="text">
<xsl:attribute name="class"> <xsl:variable name="cssclass">
<xsl:if test="$property/@required='true'">required </xsl:if> <xsl:if test="$property/@required='true'">required </xsl:if>
<xsl:choose> <xsl:choose>
<xsl:when test="//adl:definition[@name=$definition]/@pattern"> <xsl:when test="//adl:definition[@name=$definition]/@pattern">
@ -939,6 +939,9 @@
<xsl:when test="$definedtype='money'">validate-number</xsl:when> <xsl:when test="$definedtype='money'">validate-number</xsl:when>
<xsl:when test="$definedtype='date'">date-field validate-date</xsl:when> <xsl:when test="$definedtype='date'">date-field validate-date</xsl:when>
</xsl:choose> </xsl:choose>
</xsl:variable>
<xsl:attribute name="class">
<xsl:value-of select="normalize-space( cssclass)"/>
</xsl:attribute> </xsl:attribute>
<xsl:attribute name="id"> <xsl:attribute name="id">
<xsl:value-of select="concat( 'instance_', @name)"/> <xsl:value-of select="concat( 'instance_', @name)"/>
@ -1031,7 +1034,7 @@
<xsl:variable name="class"> <xsl:variable name="class">
<xsl:if test="$property/@required='true'">required </xsl:if>date-field validate-date <xsl:if test="$property/@required='true'">required </xsl:if>date-field validate-date
</xsl:variable> </xsl:variable>
${FormHelper.TextField( "instance.<xsl:value-of select="$property/@name"/>", "%{class='<xsl:value-of select="$class"/>', textformat='d', size='10', maxlength='10'}")} ${FormHelper.TextField( "instance.<xsl:value-of select="$property/@name"/>", "%{class='<xsl:value-of select="normalize-space( $class)"/>', textformat='d', size='10', maxlength='10'}")}
</xsl:when> </xsl:when>
<xsl:otherwise> <xsl:otherwise>
<xsl:variable name="class"> <xsl:variable name="class">