meta ddl генератор: пересоздание таблиц через drop table.; поправил comment-objects COMMENT ON COLUMN;добавление поля *_HISTORY.EVENT_TYPE

This commit is contained in:
AKurakin 2022-12-29 11:17:03 +03:00
parent e8c4088976
commit 5d23338904

View file

@ -1,191 +1,194 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output version="1.0" method="text" indent="no" encoding="UTF-8"/> <xsl:output version="1.0" method="text" indent="no" encoding="UTF-8"/>
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable> <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="avoidletters">_0123456789</xsl:variable> <xsl:variable name="avoidletters">_0123456789</xsl:variable>
<xsl:template match="/"><xsl:apply-templates select="*"/></xsl:template> <xsl:template match="/"><xsl:apply-templates select="*"/></xsl:template>
<xsl:template match="meta">-- DB version: <xsl:value-of select="@version"/><xsl:apply-templates select="*"/></xsl:template> <xsl:template match="meta">-- DB version: <xsl:value-of select="@version"/><xsl:apply-templates select="*"/></xsl:template>
<xsl:template match="enums"> <xsl:template match="enums">
/* Dictionaries */ /* Dictionaries */
<xsl:apply-templates select="*" mode="enums"/> <xsl:apply-templates select="*" mode="enums"/>
</xsl:template> </xsl:template>
<xsl:template match="objects"> <xsl:template match="objects">
/* Business objects */ /* Business objects */
<xsl:apply-templates select="*" mode="objects"/> <xsl:apply-templates select="*" mode="objects"/>
</xsl:template> </xsl:template>
<xsl:template match="views"> <xsl:template match="views">
/* views */ /* views */
</xsl:template> </xsl:template>
<xsl:template match="types"> <xsl:template match="types">
-- Data types -- Data types
<xsl:apply-templates select="*" mode="types"/> <xsl:apply-templates select="*" mode="types"/>
</xsl:template> </xsl:template>
<xsl:template match="type" mode="types"> <xsl:template match="type" mode="types">
-- <xsl:value-of select="@id"/>. <xsl:value-of select="name()"/> : <xsl:value-of select="@type"/> - <xsl:value-of select="@name"/> -- <xsl:value-of select="@id"/>. <xsl:value-of select="name()"/> : <xsl:value-of select="@type"/> - <xsl:value-of select="@name"/>
<xsl:if test="position() != last()">,</xsl:if> <xsl:if test="position() != last()">,</xsl:if>
</xsl:template> </xsl:template>
<xsl:template match="*" mode="enums"> <xsl:template match="*" mode="enums">
-- <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/> -- <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='concat(name(),"Dictionary")'/></xsl:call-template></xsl:variable> <xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='concat(name(),"Dictionary")'/></xsl:call-template></xsl:variable>
CREATE TABLE IF NOT EXISTS <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*" mode="field"/>); DROP TABLE IF EXISTS <xsl:value-of select="$dbTableName"/>;
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>'; CREATE TABLE <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*" mode="field"/>);
<xsl:apply-templates select="*" mode="comment-enums"/> COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>';
</xsl:template> <xsl:apply-templates select="*" mode="comment-enums"/>
</xsl:template>
<xsl:template match="*" mode="objects">
-- <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/> <xsl:template match="*" mode="objects">
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable> -- <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/>
CREATE TABLE IF NOT EXISTS <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*[@name or @type]" mode="field"/>); <xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>'; DROP TABLE IF EXISTS <xsl:value-of select="$dbTableName"/>;
<xsl:apply-templates select="*[@name or @type]" mode="comment-objects"/> CREATE TABLE <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*[@name or @type]" mode="field"/>);
<xsl:if test="@logUpdates"> COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>';
<xsl:apply-templates select="*[@name or @type]" mode="comment-objects"/>
-- History log of <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/> <xsl:if test="@logUpdates">
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
CREATE TABLE IF NOT EXISTS <xsl:value-of select="$dbTableName"/>_HISTORY(<xsl:value-of select="$dbTableName"/>_ID BIGINT NOT NULL, EVENT_TIME timestamp, EVENT_USER_ID BIGINT, <xsl:apply-templates select="*[@name or @type]" mode="field"/>); -- History log of <xsl:value-of select="name()"/> - <xsl:value-of select="@name"/>
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/>_HISTORY IS 'История изменений таблицы <xsl:value-of select="name()"/>'; <xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.<xsl:value-of select="$dbTableName"/>_ID IS 'Идентификатор записи в таблице <xsl:value-of select="$dbTableName"/>'; DROP TABLE IF EXISTS <xsl:value-of select="$dbTableName"/>_HISTORY;
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_TIME IS 'Дата и время изменения'; CREATE TABLE <xsl:value-of select="$dbTableName"/>_HISTORY(<xsl:value-of select="$dbTableName"/>_ID BIGINT NOT NULL, EVENT_TIME timestamp, EVENT_USER_ID BIGINT, EVENT_TYPE VARCHAR(4), <xsl:apply-templates select="*[@name or @type]" mode="field"/>);
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_USER_ID IS 'Инициатор изменения'; COMMENT ON TABLE <xsl:value-of select="$dbTableName"/>_HISTORY IS 'История изменений таблицы <xsl:value-of select="name()"/>';
<xsl:apply-templates select="*[@name or @type]" mode="comment-history-objects"/> COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.<xsl:value-of select="$dbTableName"/>_ID IS 'Идентификатор записи в таблице <xsl:value-of select="$dbTableName"/>';
</xsl:if> COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_TIME IS 'Дата и время изменения';
</xsl:template> COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_USER_ID IS 'Инициатор изменения';
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_TYPE IS 'Тип изменения';
<xsl:apply-templates select="*[@name or @type]" mode="comment-history-objects"/>
<xsl:template match="*" mode="field" > </xsl:if>
<xsl:variable name="tp" select="@type"/> </xsl:template>
<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:when test="@linkKeyCode"></xsl:when> <xsl:template match="*" mode="field" >
<xsl:when test="@extends"></xsl:when> <xsl:variable name="tp" select="@type"/>
<xsl:otherwise><xsl:if test="position() != '1'">, </xsl:if><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise> <xsl:choose>
</xsl:choose> <xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:choose> <xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@linkKeyCode"></xsl:when> <xsl:when test="@extends"></xsl:when>
<xsl:when test="@extends"></xsl:when> <xsl:otherwise><xsl:if test="position() != '1'">, </xsl:if><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise>
<xsl:otherwise> </xsl:choose>
<xsl:text> </xsl:text><xsl:value-of select="/meta/types/*[@id=$tp]/@type"/> <xsl:choose>
<xsl:if test="@length">(<xsl:value-of select="@length"/>)</xsl:if><xsl:if test="name()='id'"> PRIMARY KEY</xsl:if> <xsl:when test="@linkKeyCode"></xsl:when>
</xsl:otherwise> <xsl:when test="@extends"></xsl:when>
</xsl:choose> <xsl:otherwise>
</xsl:template> <xsl:text> </xsl:text><xsl:value-of select="/meta/types/*[@id=$tp]/@type"/>
<xsl:if test="@length">(<xsl:value-of select="@length"/>)</xsl:if><xsl:if test="name()='id'"> PRIMARY KEY</xsl:if>
<xsl:template match="*" mode="comment-enums"> </xsl:otherwise>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='concat(name(parent::*),"Dictionary")'/></xsl:call-template></xsl:variable> </xsl:choose>
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>.<xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template> IS '<xsl:if test="@name"><xsl:value-of select="@name"/></xsl:if> <xsl:if test="@link"> (linked to <xsl:value-of select="@link"/>)</xsl:if>'; </xsl:template>
</xsl:template>
<xsl:template match="*" mode="comment-enums">
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='concat(name(parent::*),"Dictionary")'/></xsl:call-template></xsl:variable>
<xsl:template match="*" mode="comment-objects"> COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>.<xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template> IS '<xsl:if test="@name"><xsl:value-of select="@name"/></xsl:if> <xsl:if test="@link"> (linked to <xsl:value-of select="@link"/>)</xsl:if>';
<xsl:choose> </xsl:template>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise> <xsl:template match="*" mode="comment-objects">
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable> <xsl:choose>
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>.<xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:otherwise> <xsl:when test="@linkKeyCode"></xsl:when>
</xsl:choose> IS '<xsl:if test="@name"><xsl:value-of select="@name"/></xsl:if> <xsl:if test="@link"> (linked to <xsl:value-of select="@link"/>)</xsl:if>'; <xsl:when test="@extends"></xsl:when>
</xsl:template> <xsl:otherwise>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable>
<xsl:template match="*" mode="comment-history-objects"><!-- todo похож на comment-objects только именем таблицы различается попробовать объединить --> COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>.<xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template> IS '<xsl:if test="@name"><xsl:value-of select="@name"/></xsl:if> <xsl:if test="@link"> (linked to <xsl:value-of select="@link"/>)</xsl:if>';
<xsl:choose> </xsl:otherwise></xsl:choose></xsl:template>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when> <xsl:template match="*" mode="comment-history-objects"><!-- todo похож на comment-objects только именем таблицы различается попробовать объединить -->
<xsl:otherwise> <xsl:choose>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable> <xsl:when test="@linkKeyCode"></xsl:when>
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.<xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template> IS '<xsl:if test="@name"><xsl:value-of select="@name"/></xsl:if> <xsl:if test="@link"> (linked to <xsl:value-of select="@link"/>)</xsl:if>'; <xsl:when test="@extends"></xsl:when>
</xsl:otherwise> <xsl:otherwise>
</xsl:choose> <xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable>
</xsl:template> COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.<xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template> IS '<xsl:if test="@name"><xsl:value-of select="@name"/></xsl:if> <xsl:if test="@link"> (linked to <xsl:value-of select="@link"/>)</xsl:if>';
</xsl:otherwise>
<xsl:template name='convertDbStyle'> </xsl:choose>
<xsl:param name='toconvert' /> </xsl:template>
<xsl:variable name="added_">
<xsl:call-template name='add_'> <xsl:template name='convertDbStyle'>
<xsl:with-param name='toconvert' select='$toconvert' /> <xsl:param name='toconvert' />
</xsl:call-template> <xsl:variable name="added_">
</xsl:variable> <xsl:call-template name='add_'>
<xsl:call-template name='convertcase'> <xsl:with-param name='toconvert' select='$toconvert' />
<xsl:with-param name='toconvert' select='$added_' /> </xsl:call-template>
<xsl:with-param name='conversion' select="'upper'" /> </xsl:variable>
</xsl:call-template> <xsl:call-template name='convertcase'>
</xsl:template> <xsl:with-param name='toconvert' select='$added_' />
<xsl:with-param name='conversion' select="'upper'" />
</xsl:call-template>
<xsl:template name='convertcase'> </xsl:template>
<xsl:param name='toconvert' />
<xsl:param name='conversion' />
<xsl:template name='convertcase'>
<xsl:choose> <xsl:param name='toconvert' />
<xsl:when test='$conversion="lower"'> <xsl:param name='conversion' />
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:when> <xsl:choose>
<xsl:when test='$conversion="upper"'> <xsl:when test='$conversion="lower"'>
<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/> <xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:when> </xsl:when>
<xsl:when test='$conversion="proper"'> <xsl:when test='$conversion="upper"'>
<xsl:call-template name='convertpropercase'> <xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>
<xsl:with-param name='toconvert'> </xsl:when>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/> <xsl:when test='$conversion="proper"'>
</xsl:with-param> <xsl:call-template name='convertpropercase'>
</xsl:call-template> <xsl:with-param name='toconvert'>
</xsl:when> <xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
<xsl:otherwise> </xsl:with-param>
<xsl:value-of select='$toconvert' /> </xsl:call-template>
</xsl:otherwise> </xsl:when>
</xsl:choose> <xsl:otherwise>
</xsl:template> <xsl:value-of select='$toconvert' />
</xsl:otherwise>
<xsl:template name='convertpropercase'> </xsl:choose>
<xsl:param name='toconvert' /> </xsl:template>
<xsl:if test="string-length($toconvert) > 0"> <xsl:template name='convertpropercase'>
<xsl:variable name='f' select='substring($toconvert, 1, 1)' /> <xsl:param name='toconvert' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:call-template name='convertcase'> <xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:with-param name='toconvert' select='$f' /> <xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:with-param name='conversion'>upper</xsl:with-param>
</xsl:call-template> <xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$f' />
<xsl:choose> <xsl:with-param name='conversion'>upper</xsl:with-param>
<xsl:when test="contains($s,' ')"> </xsl:call-template>
<xsl:value-of select='substring-before($s," ")'/>
<xsl:call-template name='convertpropercase'> <xsl:choose>
<xsl:with-param name='toconvert' select='substring-after($s," ")' /> <xsl:when test="contains($s,' ')">
</xsl:call-template> <xsl:value-of select='substring-before($s," ")'/>
</xsl:when> <xsl:call-template name='convertpropercase'>
<xsl:otherwise> <xsl:with-param name='toconvert' select='substring-after($s," ")' />
<xsl:value-of select='$s'/> </xsl:call-template>
</xsl:otherwise> </xsl:when>
</xsl:choose> <xsl:otherwise>
</xsl:if> <xsl:value-of select='$s'/>
</xsl:template> </xsl:otherwise>
</xsl:choose>
<xsl:template name='add_'> </xsl:if>
<xsl:param name='toconvert' /> </xsl:template>
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' /> <xsl:template name='add_'>
<xsl:variable name='s' select='substring($toconvert, 2)' /> <xsl:param name='toconvert' />
<xsl:choose> <xsl:if test="string-length($toconvert) > 0">
<xsl:when test="$f = translate($f, $lcletters,$ucletters)"><xsl:if test="translate($f, $avoidletters,'')">_</xsl:if><xsl:value-of select='$f'/></xsl:when> <xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:otherwise><xsl:value-of select='$f'/></xsl:otherwise> <xsl:variable name='s' select='substring($toconvert, 2)' />
</xsl:choose> <xsl:choose>
<xsl:if test="string-length($toconvert) > 1"> <xsl:when test="$f = translate($f, $lcletters,$ucletters)"><xsl:if test="translate($f, $avoidletters,'')">_</xsl:if><xsl:value-of select='$f'/></xsl:when>
<xsl:call-template name='add_'> <xsl:otherwise><xsl:value-of select='$f'/></xsl:otherwise>
<xsl:with-param name='toconvert' select='$s'/> </xsl:choose>
</xsl:call-template> <xsl:if test="string-length($toconvert) > 1">
</xsl:if> <xsl:call-template name='add_'>
</xsl:if> <xsl:with-param name='toconvert' select='$s'/>
</xsl:template> </xsl:call-template>
</xsl:if>
</xsl:stylesheet> </xsl:if>
</xsl:template>
</xsl:stylesheet>