Search in sources :

Example 6 with GString

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);
    }
}
Also used : NullObject(org.codehaus.groovy.runtime.NullObject) GString(groovy.lang.GString) GString(groovy.lang.GString) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Example 7 with GString

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);
}
Also used : GString(groovy.lang.GString)

Example 8 with GString

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);
}
Also used : GString(groovy.lang.GString)

Example 9 with GString

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);
}
Also used : GString(groovy.lang.GString)

Example 10 with GString

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);
}
Also used : GString(groovy.lang.GString)

Aggregations

GString (groovy.lang.GString)52 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 Closure (groovy.lang.Closure)3 IOException (java.io.IOException)3 Map (java.util.Map)3 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 NullObject (org.codehaus.groovy.runtime.NullObject)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)1 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)1