在我们的项目中,concepts定义在一个配置文件中。举一个例子:
<concepts>
<concept name="person">
<property name="age" type="integer"/>
...
</concept>
...
</concepts>虽然这与SQL没有多大关系,但这个配置文件恰好可以映射到SQL表、列、.
从这个配置文件开始,我需要能够做两件事:
CREATE TABLE person ( ... ))。我想在这个项目中开始使用jOOQ。jOOQ是否支持任何类型的生成( SQL创建脚本及其POJO、表、.)哪一个不是从现有的数据库开始的?我翻阅了这些文件,但找不到多少。
如果没有,我正在考虑两种选择:
或
虽然我认为第一个选项需要更多的努力,但它目前得到了我的青睐,因为第二个选项中的步骤3可能会导致信息丢失。
发布于 2015-11-16 21:08:46
这显然应该用XSLT解决。
生成SQL脚本:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<result>
<xsl:apply-templates select="concepts/concept"/>
</result>
</xsl:template>
<xsl:template match="concept">
<xsl:text>CREATE TABLE </xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>(</xsl:text>
<xsl:apply-templates select="property"/>
<xsl:text>
);
</xsl:text>
</xsl:template>
<xsl:template match="property">
<xsl:choose>
<xsl:when test="position() > 1">
<xsl:text>
, </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="@name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@type"/>
</xsl:template>
</xsl:stylesheet>生成jOOQ元XML
jOOQ-meta支持使用XMLDatabase从XML导入模式元信息。
<configuration>
<generator>
<database>
<name>org.jooq.util.xml.XMLDatabase</name>
<properties>
<property>
<key>dialect</key>
<value>ORACLE</value>
</property>
<property>
<key>xml-file</key>
<value>src/main/resources/concepts-transformed.xml</value>
</property>
</properties>只需将XML文件转换为以下格式:http://www.jooq.org/xsd/jooq-meta-3.5.4.xsd
..。例如,使用以下XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="schema" select="'MY_SCHEMA'"/>
<xsl:template match="/">
<information_schema xmlns="http://www.jooq.org/xsd/jooq-meta-3.5.4.xsd">
<schemata>
<schema>
<schema_name><xsl:value-of select="$schema"/></schema_name>
</schema>
</schemata>
<tables>
<xsl:apply-templates select="concepts/concept"/>
</tables>
<columns>
<xsl:apply-templates select="concepts/concept/property"/>
</columns>
</information_schema>
</xsl:template>
<xsl:template match="concept">
<table>
<schema_name><xsl:value-of select="$schema"/></schema_name>
<table_name><xsl:value-of select="@name"/></table_name>
</table>
</xsl:template>
<xsl:template match="property">
<column>
<schema_name><xsl:value-of select="$schema"/></schema_name>
<table_name><xsl:value-of select="../@name"/></table_name>
<column_name><xsl:value-of select="@name"/></column_name>
<data_type><xsl:value-of select="@type"/></data_type>
</column>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/33742190
复制相似问题