use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method eachRow.
/**
* Performs the given SQL query calling the given Closure with each row of the result set.
* The row will be a <code>GroovyResultSet</code> which is a <code>ResultSet</code>
* that supports accessing the fields using property style notation and ordinal index values.
* <p>
* In addition, the <code>metaClosure</code> will be called once passing in the
* <code>ResultSetMetaData</code> as argument.
* The query may contain GString expressions.
* <p>
* Example usage:
* <pre>
* def location = 25
* def printColNames = { meta ->
* (1..meta.columnCount).each {
* print meta.getColumnLabel(it).padRight(20)
* }
* println()
* }
* def printRow = { row ->
* row.toRowResult().values().each{ print it.toString().padRight(20) }
* println()
* }
* sql.eachRow("select * from PERSON where location_id < $location", printColNames, printRow)
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @param metaClosure called for meta data (only once after sql execution)
* @param rowClosure called for each row with a GroovyResultSet
* @throws SQLException if a database access error occurs
* @see #expand(Object)
*/
public void eachRow(GString gstring, Closure metaClosure, Closure rowClosure) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
eachRow(sql, params, metaClosure, rowClosure);
}
use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method rows.
/**
* Performs the given SQL query and return the rows of the result set.
* In addition, the <code>metaClosure</code> will be called once passing in the
* <code>ResultSetMetaData</code> as argument.
* The query may contain GString expressions.
* <p>
* Example usage:
* <pre>
* def location = 25
* def printNumCols = { meta -> println "Found $meta.columnCount columns" }
* def ans = sql.rows("select * from PERSON where location_id < $location", printNumCols)
* println "Found ${ans.size()} rows"
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @param metaClosure called with meta data of the ResultSet
* @return a list of GroovyRowResult objects
* @throws SQLException if a database access error occurs
* @see #expand(Object)
*/
public List<GroovyRowResult> rows(GString gstring, Closure metaClosure) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return rows(sql, params, metaClosure);
}
use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method firstRow.
/**
* Performs the given SQL query and return
* the first row of the result set.
* The query may contain GString expressions.
* <p>
* Example usage:
* <pre>
* def location = 25
* def ans = sql.firstRow("select * from PERSON where location_id < $location")
* println ans.firstname
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @return a GroovyRowResult object or <code>null</code> if no row is found
* @throws SQLException if a database access error occurs
* @see #expand(Object)
*/
public GroovyRowResult firstRow(GString gstring) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return firstRow(sql, params);
}
use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method execute.
/**
* Executes the given SQL with embedded expressions inside.
* Also saves the updateCount, if any, for subsequent examination.
* <p>
* Example usage:
* <pre>
* def scott = [firstname: "Scott", lastname: "Davis", id: 5, location_id: 50]
* sql.execute """
* insert into PERSON (id, firstname, lastname, location_id) values ($scott.id, $scott.firstname, $scott.lastname, $scott.location_id)
* """
* assert sql.updateCount == 1
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @return <code>true</code> if the first result is a <code>ResultSet</code>
* object; <code>false</code> if it is an update count or there are
* no results
* @throws SQLException if a database access error occurs
* @see #expand(Object)
*/
public boolean execute(GString gstring) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return execute(sql, params);
}
use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method rows.
/**
* Performs the given SQL query and return a "page" of rows from the result set. A page is defined as starting at
* a 1-based offset, and containing a maximum number of rows.
* In addition, the <code>metaClosure</code> will be called once passing in the
* <code>ResultSetMetaData</code> as argument.
* The query may contain GString expressions.
* <p>
* Note that the underlying implementation is based on either invoking <code>ResultSet.absolute()</code>,
* or if the ResultSet type is <code>ResultSet.TYPE_FORWARD_ONLY</code>, the <code>ResultSet.next()</code> method
* is invoked equivalently. The first row of a ResultSet is 1, so passing in an offset of 1 or less has no effect
* on the initial positioning within the result set.
* <p>
* Note that different database and JDBC driver implementations may work differently with respect to this method.
* Specifically, one should expect that <code>ResultSet.TYPE_FORWARD_ONLY</code> may be less efficient than a
* "scrollable" type.
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring the SQL statement
* @param offset the 1-based offset for the first row to be processed
* @param maxRows the maximum number of rows to be processed
* @param metaClosure called for meta data (only once after sql execution)
* @return a list of GroovyRowResult objects
* @throws SQLException if a database access error occurs
*/
public List<GroovyRowResult> rows(GString gstring, int offset, int maxRows, Closure metaClosure) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return rows(sql, params, offset, maxRows, metaClosure);
}
Aggregations