use of groovy.lang.GString in project groovy-core by groovy.
the class InvokerTest method testAsBoolean.
public void testAsBoolean() {
assertAsBoolean(true, Boolean.TRUE);
assertAsBoolean(true, "true");
assertAsBoolean(true, "TRUE");
assertAsBoolean(true, "false");
assertAsBoolean(false, Boolean.FALSE);
assertAsBoolean(false, (String) null);
assertAsBoolean(false, "");
GString emptyGString = new GString(new Object[] { "" }) {
public String[] getStrings() {
return new String[] { "" };
}
};
assertAsBoolean(false, emptyGString);
GString nonEmptyGString = new GString(new Object[] { "x" }) {
public String[] getStrings() {
return new String[] { "x" };
}
};
assertAsBoolean(true, nonEmptyGString);
assertAsBoolean(true, new Integer(1234));
assertAsBoolean(false, new Integer(0));
assertAsBoolean(true, new Float(0.3f));
assertAsBoolean(true, new Double(3.0f));
assertAsBoolean(false, new Float(0.0f));
assertAsBoolean(true, new Character((char) 1));
assertAsBoolean(false, new Character((char) 0));
assertAsBoolean(false, Collections.EMPTY_LIST);
assertAsBoolean(true, Arrays.asList(new Integer[] { new Integer(1) }));
}
use of groovy.lang.GString in project groovy by apache.
the class Sql method callWithAllRows.
/**
* Performs a stored procedure call with the given parameters,
* calling the closure once with all result objects,
* and also returning a list of lists with the rows of the ResultSet(s).
* <p>
* Use this when calling a stored procedure that utilizes both
* output parameters and returns multiple ResultSets.
* <p>
* Once created, the stored procedure can be called like this:
* <pre>
* def first = 'Jeff'
* def last = 'Sheets'
* def rowsList = sql.callWithAllRows "{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 containing lists of GroovyRowResult objects
* @throws SQLException if a database access error occurs
* @see #callWithAllRows(String, List, Closure)
*/
public List<List<GroovyRowResult>> callWithAllRows(GString gstring, Closure closure) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return callWithAllRows(sql, params, closure);
}
use of groovy.lang.GString in project groovy by apache.
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 by apache.
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 by apache.
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);
}
Aggregations