zakvdm
zakvdm

Reputation: 489

How can I read output of an sql query into an ant property?

I would like to feed the result of a simple SQL query (something like: select SP_NUMBER from SERVICE_PACK) which I run inside my ant script (using the sql task) back into an ant property (e.g. service.pack.number).

The sql task can output to a file, but is there a more direct way?

Upvotes: 8

Views: 12895

Answers (3)

Matt G
Matt G

Reputation: 81

MySQL users- I had to modify the query like so:

SELECT CONCAT('mytable.id=', CAST(ID as CHAR)) from mytable

Without the CONCAT function, I just got back the text "1" (representing my id) in the properties file. Also, the CAST is needed in a MySQL system, otherwise the concatenated field is returned as a BLOB.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272337

Perhaps the Ant exec task is more useful here ? You can execute a standalone and get the result in a property via outputproperty. You'll have to execute your SQL in some standalone fashion, unfortunately.

Alternatively is it worth looking at the code to the Ant Sql task and modify it to accept an outputproperty ? It sounds a bit of a pain, but I think it could well be a very simple modification if you can't find anything more direct.

Upvotes: 1

zakvdm
zakvdm

Reputation: 489

Although I would have preferred not creating a file, I eventually went with the following solution:

The sql task is called as follows

<sql ... print="yes" output="temp.properties"
        expandProperties="true" showheaders="false" showtrailers="false" >
        <![CDATA[
        select 'current.sp.version=' || NAME from SERVICE_PACK;
        select 'current.major.version=' || NAME from VERSION;
        ]]>
</sql>

The generated properties file will contain:

current.sp.version=03

current.major.version=5

Then you just load the properties file and delete it:

<property file="temp.properties" />
<delete file="temp.properties" />

<echo message="Current service pack version: ${current.sp.version}" />
<echo message="Current major version: ${current.major.version}" />

This works, and everything is right there in the ant script (even if it's not the prettiest thing in the world!).

Upvotes: 12

Related Questions