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"?>
<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:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="avoidletters">_0123456789</xsl:variable>
<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="enums">
/* Dictionaries */
<xsl:apply-templates select="*" mode="enums"/>
</xsl:template>
<xsl:template match="objects">
/* Business objects */
<xsl:apply-templates select="*" mode="objects"/>
</xsl:template>
<xsl:template match="views">
/* views */
</xsl:template>
<xsl:template match="types">
-- Data types
<xsl:apply-templates select="*" mode="types"/>
</xsl:template>
<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:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="*" mode="enums">
-- <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>
CREATE TABLE IF NOT EXISTS <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*" mode="field"/>);
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>';
<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: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"/>(<xsl:apply-templates select="*[@name or @type]" mode="field"/>);
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>';
<xsl:apply-templates select="*[@name or @type]" mode="comment-objects"/>
<xsl:if test="@logUpdates">
-- History log of <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='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"/>);
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/>_HISTORY IS 'История изменений таблицы <xsl:value-of select="name()"/>';
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.<xsl:value-of select="$dbTableName"/>_ID IS 'Идентификатор записи в таблице <xsl:value-of select="$dbTableName"/>';
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_TIME IS 'Дата и время изменения';
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_USER_ID IS 'Инициатор изменения';
<xsl:apply-templates select="*[@name or @type]" mode="comment-history-objects"/>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="field" >
<xsl:variable name="tp" select="@type"/>
<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:when test="@linkKeyCode"></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:choose>
<xsl:choose>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise>
<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:otherwise>
</xsl:choose>
</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>
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 match="*" mode="comment-objects">
<xsl:choose>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable>
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: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:template>
<xsl:template match="*" mode="comment-history-objects"><!-- todo похож на comment-objects только именем таблицы различается попробовать объединить -->
<xsl:choose>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable>
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:choose>
</xsl:template>
<xsl:template name='convertDbStyle'>
<xsl:param name='toconvert' />
<xsl:variable name="added_">
<xsl:call-template name='add_'>
<xsl:with-param name='toconvert' select='$toconvert' />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$added_' />
<xsl:with-param name='conversion' select="'upper'" />
</xsl:call-template>
</xsl:template>
<xsl:template name='convertcase'>
<xsl:param name='toconvert' />
<xsl:param name='conversion' />
<xsl:choose>
<xsl:when test='$conversion="lower"'>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:when>
<xsl:when test='$conversion="upper"'>
<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>
</xsl:when>
<xsl:when test='$conversion="proper"'>
<xsl:call-template name='convertpropercase'>
<xsl:with-param name='toconvert'>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$toconvert' />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name='convertpropercase'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$f' />
<xsl:with-param name='conversion'>upper</xsl:with-param>
</xsl:call-template>
<xsl:choose>
<xsl:when test="contains($s,' ')">
<xsl:value-of select='substring-before($s," ")'/>
<xsl:call-template name='convertpropercase'>
<xsl:with-param name='toconvert' select='substring-after($s," ")' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$s'/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name='add_'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:choose>
<xsl:when test="$f = translate($f, $lcletters,$ucletters)"><xsl:if test="translate($f, $avoidletters,'')">_</xsl:if><xsl:value-of select='$f'/></xsl:when>
<xsl:otherwise><xsl:value-of select='$f'/></xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($toconvert) > 1">
<xsl:call-template name='add_'>
<xsl:with-param name='toconvert' select='$s'/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:variable name="avoidletters">_0123456789</xsl:variable>
<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="enums">
/* Dictionaries */
<xsl:apply-templates select="*" mode="enums"/>
</xsl:template>
<xsl:template match="objects">
/* Business objects */
<xsl:apply-templates select="*" mode="objects"/>
</xsl:template>
<xsl:template match="views">
/* views */
</xsl:template>
<xsl:template match="types">
-- Data types
<xsl:apply-templates select="*" mode="types"/>
</xsl:template>
<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:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="*" mode="enums">
-- <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>
DROP TABLE IF EXISTS <xsl:value-of select="$dbTableName"/>;
CREATE TABLE <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*" mode="field"/>);
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>';
<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:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name()'/></xsl:call-template></xsl:variable>
DROP TABLE IF EXISTS <xsl:value-of select="$dbTableName"/>;
CREATE TABLE <xsl:value-of select="$dbTableName"/>(<xsl:apply-templates select="*[@name or @type]" mode="field"/>);
COMMENT ON TABLE <xsl:value-of select="$dbTableName"/> IS '<xsl:value-of select="@name"/>';
<xsl:apply-templates select="*[@name or @type]" mode="comment-objects"/>
<xsl:if test="@logUpdates">
-- History log of <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='name()'/></xsl:call-template></xsl:variable>
DROP TABLE IF EXISTS <xsl:value-of select="$dbTableName"/>_HISTORY;
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 TABLE <xsl:value-of select="$dbTableName"/>_HISTORY IS 'История изменений таблицы <xsl:value-of select="name()"/>';
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.<xsl:value-of select="$dbTableName"/>_ID IS 'Идентификатор записи в таблице <xsl:value-of select="$dbTableName"/>';
COMMENT ON COLUMN <xsl:value-of select="$dbTableName"/>_HISTORY.EVENT_TIME IS 'Дата и время изменения';
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:if>
</xsl:template>
<xsl:template match="*" mode="field" >
<xsl:variable name="tp" select="@type"/>
<xsl:choose>
<xsl:when test="@dbfield"><xsl:value-of select="@dbfield"/></xsl:when>
<xsl:when test="@linkKeyCode"></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:choose>
<xsl:choose>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise>
<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:otherwise>
</xsl:choose>
</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>
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 match="*" mode="comment-objects">
<xsl:choose>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable>
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:otherwise></xsl:choose></xsl:template>
<xsl:template match="*" mode="comment-history-objects"><!-- todo похож на comment-objects только именем таблицы различается попробовать объединить -->
<xsl:choose>
<xsl:when test="@linkKeyCode"></xsl:when>
<xsl:when test="@extends"></xsl:when>
<xsl:otherwise>
<xsl:variable name="dbTableName"><xsl:call-template name='convertDbStyle'><xsl:with-param name='toconvert' select='name(parent::*)'/></xsl:call-template></xsl:variable>
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:choose>
</xsl:template>
<xsl:template name='convertDbStyle'>
<xsl:param name='toconvert' />
<xsl:variable name="added_">
<xsl:call-template name='add_'>
<xsl:with-param name='toconvert' select='$toconvert' />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$added_' />
<xsl:with-param name='conversion' select="'upper'" />
</xsl:call-template>
</xsl:template>
<xsl:template name='convertcase'>
<xsl:param name='toconvert' />
<xsl:param name='conversion' />
<xsl:choose>
<xsl:when test='$conversion="lower"'>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:when>
<xsl:when test='$conversion="upper"'>
<xsl:value-of select="translate($toconvert,$lcletters,$ucletters)"/>
</xsl:when>
<xsl:when test='$conversion="proper"'>
<xsl:call-template name='convertpropercase'>
<xsl:with-param name='toconvert'>
<xsl:value-of select="translate($toconvert,$ucletters,$lcletters)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$toconvert' />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name='convertpropercase'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:call-template name='convertcase'>
<xsl:with-param name='toconvert' select='$f' />
<xsl:with-param name='conversion'>upper</xsl:with-param>
</xsl:call-template>
<xsl:choose>
<xsl:when test="contains($s,' ')">
<xsl:value-of select='substring-before($s," ")'/>
<xsl:call-template name='convertpropercase'>
<xsl:with-param name='toconvert' select='substring-after($s," ")' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$s'/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name='add_'>
<xsl:param name='toconvert' />
<xsl:if test="string-length($toconvert) > 0">
<xsl:variable name='f' select='substring($toconvert, 1, 1)' />
<xsl:variable name='s' select='substring($toconvert, 2)' />
<xsl:choose>
<xsl:when test="$f = translate($f, $lcletters,$ucletters)"><xsl:if test="translate($f, $avoidletters,'')">_</xsl:if><xsl:value-of select='$f'/></xsl:when>
<xsl:otherwise><xsl:value-of select='$f'/></xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($toconvert) > 1">
<xsl:call-template name='add_'>
<xsl:with-param name='toconvert' select='$s'/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>