use of groovy.lang.GString in project groovy by apache.
the class DefaultTypeTransformation method compareToWithEqualityCheck.
private static int compareToWithEqualityCheck(Object left, Object right, boolean equalityCheckOnly) {
Exception cause = null;
if (left == right) {
return 0;
}
if (left == null) {
return -1;
} else if (right == null) {
return 1;
}
if (left instanceof Comparable || left instanceof Number) {
if (left instanceof Number) {
if (right instanceof Character || right instanceof Number) {
return DefaultGroovyMethods.compareTo((Number) left, castToNumber(right));
}
if (isValidCharacterString(right)) {
return DefaultGroovyMethods.compareTo((Number) left, ShortTypeHandling.castToChar(right));
}
} else if (left instanceof Character) {
if (isValidCharacterString(right)) {
return DefaultGroovyMethods.compareTo((Character) left, ShortTypeHandling.castToChar(right));
}
if (right instanceof Number) {
return DefaultGroovyMethods.compareTo((Character) left, (Number) right);
}
if (right instanceof String) {
return (left.toString()).compareTo((String) right);
}
if (right instanceof GString) {
return (left.toString()).compareTo(right.toString());
}
} else if (right instanceof Number) {
if (isValidCharacterString(left)) {
return DefaultGroovyMethods.compareTo(ShortTypeHandling.castToChar(left), (Number) right);
}
} else if (left instanceof String && right instanceof Character) {
return ((String) left).compareTo(right.toString());
} else if (left instanceof String && right instanceof GString) {
return ((String) left).compareTo(right.toString());
} else if (left instanceof GString && right instanceof String) {
return ((GString) left).compareTo(right);
}
if (!equalityCheckOnly || left.getClass().isAssignableFrom(right.getClass()) || //GROOVY-4046
(right.getClass() != Object.class && right.getClass().isAssignableFrom(left.getClass()))) {
Comparable comparable = (Comparable) left;
// up with the prospect of a ClassCastException which we want to ignore but only if testing equality
try {
return comparable.compareTo(right);
} catch (ClassCastException cce) {
if (!equalityCheckOnly)
cause = cce;
}
}
}
if (equalityCheckOnly) {
// anything other than 0
return -1;
}
String message = MessageFormat.format("Cannot compare {0} with value ''{1}'' and {2} with value ''{3}''", left.getClass().getName(), left, right.getClass().getName(), right);
if (cause != null) {
throw new IllegalArgumentException(message, cause);
} else {
throw new IllegalArgumentException(message);
}
}
use of groovy.lang.GString in project groovy by apache.
the class Sql method call.
/**
* Performs a stored procedure call with the given embedded parameters.
* <p>
* Example usage - see {@link #call(String)} for more details about
* creating a <code>HouseSwap(IN name1, IN name2)</code> stored procedure.
* Once created, it can be called like this:
* <pre>
* def p1 = 'Paul'
* def p2 = 'Guillaume'
* def rowsChanged = sql.call("{call HouseSwap($p1, $p2)}")
* assert rowsChanged == 2
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @return the number of rows updated or 0 for SQL statements that return nothing
* @throws SQLException if a database access error occurs
* @see #expand(Object)
* @see #call(String)
*/
public int call(GString gstring) throws Exception {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return call(sql, params);
}
use of groovy.lang.GString in project groovy by apache.
the class Sql method call.
/**
* Performs a stored procedure call with the given parameters,
* calling the closure once with all result objects.
* <p>
* See {@link #call(String, List, Closure)} for more details about
* creating a <code>Hemisphere(IN first, IN last, OUT dwells)</code> stored procedure.
* Once created, it can be called like this:
* <pre>
* def first = 'Scott'
* def last = 'Davis'
* sql.call "{call Hemisphere($first, $last, ${Sql.VARCHAR})}", { dwells ->
* println dwells
* }
* </pre>
* <p>
* As another example, see {@link #call(String, List, Closure)} for more details about
* creating a <code>FullName(IN first)</code> stored function.
* Once created, it can be called like this:
* <pre>
* def first = 'Sam'
* sql.call("{$Sql.VARCHAR = call FullName($first)}") { name ->
* assert name == 'Sam Pullara'
* }
* </pre>
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @param closure called for each row with a GroovyResultSet
* @throws SQLException if a database access error occurs
* @see #call(String, List, Closure)
* @see #expand(Object)
*/
public void call(GString gstring, Closure closure) throws Exception {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
call(sql, params, closure);
}
use of groovy.lang.GString in project groovy by apache.
the class Sql method executeUpdate.
/**
* Executes the given SQL update with embedded expressions inside.
* <p>
* Resource handling is performed automatically where appropriate.
*
* @param gstring a GString containing the SQL query with embedded params
* @return the number of rows updated or 0 for SQL statements that return nothing
* @throws SQLException if a database access error occurs
* @see #expand(Object)
*/
public int executeUpdate(GString gstring) throws SQLException {
List<Object> params = getParameters(gstring);
String sql = asSql(gstring, params);
return executeUpdate(sql, params);
}
use of groovy.lang.GString in project groovy by apache.
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);
}
Aggregations