use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method executeInsert.
/**
* Executes the given SQL statement (typically an INSERT statement).
* Use this variant when you want to receive the values of any
* auto-generated columns, such as an autoincrement ID field.
* The query may contain GString expressions.
* <p>
* Generated key values can be accessed using
* array notation. For example, to return the second auto-generated
* column value of the third row, use <code>keys[3][1]</code>. The
* method is designed to be used with SQL INSERT statements, but is
* not limited to them.
* <p>
* The standard use for this method is when a table has an
* autoincrement ID column and you want to know what the ID is for
* a newly inserted row. In this example, we insert a single row
* into a table in which the first column contains the autoincrement ID:
* <pre>
* def sql = Sql.newInstance("jdbc:mysql://localhost:3306/groovy",
* "user",
* "password",
* "com.mysql.jdbc.Driver")
*
* def keys = sql.executeInsert("insert into test_table (INT_DATA, STRING_DATA) "
* + "VALUES (1, 'Key Largo')")
*
* def id = keys[0][0]
*
* // 'id' now contains the value of the new row's ID column.
* // It can be used to update an object representation's
* // id attribute for example.
* ...
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @return A list of the auto-generated column values for each
* inserted row (typically auto-generated keys)
* @throws SQLException if a database access error occurs
* @see #expand(Object)
*/
public List<List<Object>> executeInsert(GString gstring) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return executeInsert(sql, params);
}
use of groovy.lang.GString in project groovy-core by groovy.
the class Sql method callWithRows.
/**
* Performs a stored procedure call with the given parameters,
* calling the closure once with all result objects,
* and also returning the rows of the ResultSet.
* <p>
* Use this when calling a stored procedure that utilizes both
* output parameters and returns a single ResultSet.
* <p>
* Once created, the stored procedure can be called like this:
* <pre>
* def first = 'Jeff'
* def last = 'Sheets'
* def rows = sql.callWithRows "{call Hemisphere2($first, $last, ${Sql.VARCHAR})}", { dwells ->
* println dwells
* }
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @param closure called once with all out parameter results
* @return a list of GroovyRowResult objects
* @throws SQLException if a database access error occurs
* @see #callWithRows(String, List, Closure)
*/
public List<GroovyRowResult> callWithRows(GString gstring, Closure closure) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return callWithRows(sql, params, closure);
}
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);
}
Aggregations