bhasker
bhasker

Reputation: 655

Can i save result of a sql query in a property directly in ant

I want to save the sql query result into a property in ant. I know i can do it through a file. But can i assign it to a property by directly declaring a property.

eg: select count(colname) from tablename.

So i want to assign count value to a property.

Upvotes: 1

Views: 1310

Answers (1)

Mark O'Connor
Mark O'Connor

Reputation: 77991

Can't do this using the standard ant sql task.

Use a groovy script to set the property as follows:

<target name="query">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <groovy>
    import groovy.sql.Sql

    def sql = Sql.newInstance(properties."db.url", properties."db.user", properties."db.pass", properties."db.driver")
    def row = sql.firstRow("SELECT count(*) from example1")

    properties."row.count" = row[0]
    </groovy>
</target>

<target name="result" depends="query">
    <echo message="Row count: ${row.count}"/>
</target>

Upvotes: 1

Related Questions