Search in sources :

Example 21 with GString

use of groovy.lang.GString in project groovy-core by groovy.

the class InvokeMethodTest method testCoerceGStringToString.

public void testCoerceGStringToString() throws Throwable {
    GString param = new GString(new Object[] { "James" }) {

        public String[] getStrings() {
            return new String[] { "Hello " };
        }
    };
    Object value = invoke(this, "methodTakesString", new Object[] { param });
    assertEquals("converted GString to string", param.toString(), value);
}
Also used : GString(groovy.lang.GString) GString(groovy.lang.GString)

Example 22 with GString

use of groovy.lang.GString in project groovy-core by groovy.

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 23 with GString

use of groovy.lang.GString in project groovy-core by groovy.

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 24 with GString

use of groovy.lang.GString in project groovy-core by groovy.

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

Example 25 with GString

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 calls the provided processResults Closure to process any ResultSet or UpdateCount results that executing the SQL might produce.
     * Resource handling is performed automatically where appropriate.
     *
     * @param gstring a GString containing the SQL query with embedded params
     * @param processResults a Closure which will be passed two parameters: either {@code true} plus a list of GroovyRowResult values
     *                       derived from {@code statement.getResultSet()} or {@code false} plus the update count from {@code statement.getUpdateCount()}.
     *                       The closure will be called for each result produced from executing the SQL.
     * @throws SQLException if a database access error occurs
     * @see #expand(Object)
     * @see #execute(String, List, Closure)
     * @since 2.3.2
     */
public void execute(GString gstring, Closure processResults) throws SQLException {
    List<Object> params = getParameters(gstring);
    String sql = asSql(gstring, params);
    execute(sql, params, processResults);
}
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