带有多个分隔符的sql-maven-plugin

时间:2012-10-25 15:46:35

标签: mysql maven sql-maven-plugin

我正在使用sql-maven-plugin在几个数据库上执行一些MySQL脚本。 我想在相同的SQL脚本中部署表,数据,触发器,事件和存储过程。

我的行分隔符有问题,因为对于INSERT或CREATE,我使用;,但对于我的触发器,我必须使用DELIMITER //更改分隔符。

我知道插件允许更改分隔符,但它适用于所有脚本,我想仅为部分独特脚本更改分隔符。

这是我的maven配置:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.5</version>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
    </dependencies>
    <configuration>
        <driver>com.mysql.jdbc.Driver</driver>
        <username>${db.user}</username>
        <password>${db.passwd}</password>
        <encoding>UTF-8</encoding>
        <orderFile>ascending</orderFile>
        <keepFormat>true</keepFormat>
        <driverProperties>characterEncoding=utf8,
                          connectionCollation=utf8_general_ci,
                          sql_mode=STRICT_TRANS_TABLES</driverProperties>
    </configuration>
    <executions>
        <execution>
            <id>execution-mysql</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <url>jdbc:mysql://${db.url}:${db.port}</url
                <delimiterType>normal</delimiterType>
                <fileset>
                    <basedir>${project.build.directory}/sql/</basedir>
                    <includes>
                        <include>${file.sql}</include>
                    </includes>
                </fileset>
            </configuration>
        </execution>
     </executions>
</plugin>

1 个答案:

答案 0 :(得分:4)

您可以在配置块中将delimeterType设置为“row”。

<configuration>
...
   <delimiterType>row</delimiterType>
...
</configuration>

delimiterType

  

正常意味着任何出现的分隔符都会终止SQL   命令而有行,只有一行只包含分隔符   被认为是命令的结束。

http://mojo.codehaus.org/sql-maven-plugin/execute-mojo.html#delimiterType

了解详情

对于examlpe:create-proc.sql

CREATE PROCEDURE ADD_BOOK (IN title VARCHAR(100))
BEGIN
   -- your sql code 
   INSERT INTO Book (title) VALUES (title);
END
; -- this means the end of the sql command