简体   繁体   English

jOOQ-jOOQ是否支持定义文件或创建SQL创建脚本?

[英]jOOQ - Does jOOQ support definition files or creation of SQL creation scripts?

In our project, concepts are definied in a configuration file. 在我们的项目中, concepts在配置文件中定义。 To give an example: 举个例子:

<concepts>
    <concept name="person">
        <property name="age" type="integer"/>
        ...
    </concept>
    ...
</concepts>

Although this has not much to do with SQL, this configuration file happens to be mappable to SQL tables, columns, ... 尽管这与SQL无关,但是此配置文件恰好可映射到SQL表,列,...。

Starting from this configuration file, I need to be able to do 2 things: 从此配置文件开始,我需要能够做两件事:

  • generation SQL creation scripts ( CREATE TABLE person ( ... ) ). 生成SQL创建脚本( CREATE TABLE person ( ... ) )。
  • generation of jOOQ POJOs, tables, ... which can be used by the developers 开发人员可以使用的jOOQ POJO,表...的生成

I would like to start using jOOQ in this project. 我想在这个项目中开始使用jOOQ。 Does jOOQ support any kind of generation (both SQL creation scripts and its POJOs, tables, ...) which does not start from an existing database? jOOQ是否支持不从现有数据库开始的任何类型的生成(SQL创建脚本及其POJO,表等)? I looked through the documentation, but could not find much. 我浏览了文档,但是找不到很多。

If not, I am contemplating between two options: 如果没有,我正在考虑两种选择:

  1. generation of jOOQ POJOs, tables, ... based on the configuration file (custom development) 根据配置文件生成jOOQ POJO,表...(定制开发)
  2. generation of SQL creation scripts based on jOOQ POJOs, tables, .... as generated in step 1 (custom development) 根据步骤1(定制开发)中生成的jOOQ POJO,表等生成SQL创建脚本

or 要么

  1. generation of SQL creation scripts based on the configuration file (custom development) 根据配置文件生成SQL创建脚本(定制开发)
  2. execute these scripts in an embeddable database, such as H2 or SQLite (pretty straight-forward) (although the scripts will also be executed in the 'real' database, it's probably best to use an in-memory database here, to avoid any dependencies) 在可嵌入的数据库(例如H2或SQLite)中执行这些脚本(相当简单)(尽管这些脚本也将在“真实”数据库中执行,但最好在此处使用内存数据库,以避免任何依赖关系) )
  3. generation of jOOQ POJOs, tables, ... based on this database (provided by jOOQ library) 基于此数据库生成jOOQ POJO,表...(由jOOQ库提供)

Although I believe fhe first option requires more effort, it has currently my favor, since step 3 in the second option might introduce loss of information. 尽管我认为第一种选择需要付出更多的努力,但它目前对我有利,因为第二种选择中的步骤3可能会导致信息丢失。

This should obviously be solved with XSLT 显然应该使用XSLT解决

Generate the SQL script: 生成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>

Generate the jOOQ meta XML 生成jOOQ元XML

jOOQ-meta supports importing schema meta information from XML using the XMLDatabase 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>

Just transform your XML file into the following format: http://www.jooq.org/xsd/jooq-meta-3.5.4.xsd 只需将XML文件转换为以下格式即可: http : //www.jooq.org/xsd/jooq-meta-3.5.4.xsd

... eg using the following XSLT: ...例如,使用以下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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM