Question

I'm trying to create an ANT task that takes some database properties and executes a MYSQL script. For a specific database, it's working fine, and what I have is the following:

A configurable properties file:

sql.driver=com.mysql.jdbc.Driver
sql.url=jdbc:mysql://127.0.0.1:3306/
sql.user=admin
sql.pass=admin

And the build.xml ant file:

<target name="rebuild-database">
     <sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}">
          <transaction src="db/rebuild.sql"/>
          <classpath>
               <path refid="project.class.path"/>
          </classpath>
     </sql>
</target>

Now, that works fine. The sql script rebuilds the database. However, I want this database name to be also configurable. I know you can append the database name after the sql.url property, but here's the catch, my rebuild.sql file has to drop the database if it exists and rebuild it. It looks like this:

rebuild.sql

DROP DATABASE IF EXISTS `client_database`;    
CREATE DATABASE `client_database` 
USE `client_database`;

CREATE TABLE `Customer` (
etc...

So my problem is, right now client_database is hardcoded, but I need it to be a configurable name in the properties file. How could I manage to pass around this name into the sql script and execute those 3 first statements?

Was it helpful?

Solution

You can

  • split the first 3 lines (prologue) from rebuild.sql from the rest
  • create the prologue with cat, echo, etc. making use of a parameter ${dbname} and redirect together with the (constant) body of rebuild.sql to a > parambuild.sql
  • call the generated parambuild.sql
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top