简体   繁体   English

在Groovy / Gradle中执行Sql脚本文件

[英]Executing Sql script file in Groovy/Gradle

def db = [
    moduleGroup: 'mysql',
    moduleName: 'mysql-connector-java',
    moduleVersion: '5.1.18',
    driver: "com.mysql.jdbc.Driver",
    url: 'jdbc:mysql://localhost:3306/bham',
    user: mySqlUser,
    password: mySqlPassword
]

configurations {
    sql
}    


task connect << {

        // This is needed to get mySql driver onto the Groovy/Gradle classpath 
        configurations.sql.each { file ->
          println "Adding URL: $file"
          gradle.class.classLoader.addURL(file.toURI().toURL())
        }

        def sql = groovy.sql.Sql.newInstance(db.url, db.user, db.password, db.driver)

        sql.execute("actStatusCodeLkp.sql") 
        String sqlFilePath = "src/main/resources/sqlscripts/actStatusCodeLkp.sql"
        String sqlString = new File(sqlFilePath).text
        sql.execute(sqlString)

        sql.close()

     }

actStatusCodeLkp.sql actStatusCodeLkp.sql

insert into act_status_code (id, code, display_name, code_system_name, code_system) values (1, 'active', 'active', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (2, 'cancelled', 'cancelled', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (3, 'aborted', 'aborted', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (4, 'completed', 'completed', 'ActStatus', '2.16.840.1.113883.5.14');

It seems that sql.execute command does not tokenize the file into 4 different insert statements and throws. 似乎sql.execute命令没有将文件标记为4个不同的insert语句并抛出。

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL
 server version for the right syntax to use near 'insert into act_status_code (id, code, display_name, code_system_name, code_syst' at line 2
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

It works if I just keep one insert statement in the file. 如果我只在文件中保留一个insert语句,它就可以工作。 What is the clean work around here, did not really find anything regarding this online. 这里有什么干净的工作,在网上找不到任何关于这个的东西。 Also, when using maven, I am able to run the same sql file using sql-maven-plugin. 此外,使用maven时,我可以使用sql-maven-plugin运行相同的sql文件。

Something like this helped me. 这样的事情对我有所帮助。 Notice getting allowMultiQueries: 'true' in the properties 注意在属性中获取allowMultiQueries:'true'

def props = [user: grailsApplication.config.dataSource.username, password: grailsApplication.config.dataSource.password, allowMultiQueries: 'true'] as Properties
    def url = grailsApplication.config.dataSource.url
    def driver = grailsApplication.config.dataSource.driverClassName


    def sql = Sql.newInstance(url, props, driver)

You could also change your sql to make the statement one query by: 您还可以通过以下方式更改sql以使语句成为一个查询:

insert into act_status_code (id, code, display_name, code_system_name, code_system) values 
(1, 'active', 'active', 'ActStatus', '2.16.840.1.113883.5.14'),
(2, 'cancelled', 'cancelled', 'ActStatus', '2.16.840.1.113883.5.14'),
(3, 'aborted', 'aborted', 'ActStatus', '2.16.840.1.113883.5.14'),
(4, 'completed', 'completed', 'ActStatus', '2.16.840.1.113883.5.14');

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

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