Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have written a helper which allows me to work with stored procedures that return a single ResultSet in a way that is similar to working with queries with <code>groovy.sql.Sql</code>. This could easily be adapted to process multiple ResultSets (I assume each would need it's own closure).</p> <p>Usage:</p> <pre><code>Sql sql = Sql.newInstance(dataSource) SqlHelper helper = new SqlHelper(sql); helper.eachSprocRow('EXEC sp_my_sproc ?, ?, ?', ['a', 'b', 'c']) { row -&gt; println "foo=${row.foo}, bar=${row.bar}, baz=${row.baz}" } </code></pre> <p>Code:</p> <pre><code>class SqlHelper { private Sql sql; SqlHelper(Sql sql) { this.sql = sql; } public void eachSprocRow(String query, List parameters, Closure closure) { sql.cacheConnection { Connection con -&gt; CallableStatement proc = con.prepareCall(query) try { parameters.eachWithIndex { param, i -&gt; proc.setObject(i+1, param) } boolean result = proc.execute() boolean found = false while (!found) { if (result) { ResultSet rs = proc.getResultSet() ResultSetMetaData md = rs.getMetaData() int columnCount = md.getColumnCount() while (rs.next()) { // use case insensitive map Map row = new TreeMap(String.CASE_INSENSITIVE_ORDER) for (int i = 0; i &lt; columnCount; ++ i) { row[md.getColumnName(i+1)] = rs.getObject(i+1) } closure.call(row) } found = true; } else if (proc.getUpdateCount() &lt; 0) { throw new RuntimeException("Sproc ${query} did not return a result set") } result = proc.getMoreResults() } } finally { proc.close() } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload