Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

ant

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?

like image 720
zakvdm Avatar asked Jun 30 '09 10:06

zakvdm


People also ask

How do I see the results of a SQL query?

You have the option of displaying your query results on the Run SQL window, as opposed to Data Display windows. To do this, go to View > Data Grid (Ctrl+G). Once you have selected this option, a panel will appear at the bottom of the window - your query results will be displayed there.

How do I display a SQL query?

The DISPLAY command must be placed immediately after the query statement on which you want it to take effect. For example: SELECT pno, pname FROM part WHERE color='BLUE'; DISPLAY; When the system encounters this DISPLAY command, it displays the Result window containing the part number and name for all blue parts.

What is from in SQL query?

The FROM command is used to specify which table to select or delete data from.


2 Answers

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!).

like image 78
zakvdm Avatar answered Nov 02 '22 23:11

zakvdm


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.

like image 37
Brian Agnew Avatar answered Nov 03 '22 01:11

Brian Agnew