Search in sources :

Example 11 with GString

use of groovy.lang.GString in project groovy by apache.

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

Example 12 with GString

use of groovy.lang.GString in project groovy by apache.

the class Sql method rows.

/**
     * Performs the given SQL query and return a "page" of rows from the result set.  A page is defined as starting at
     * a 1-based offset, and containing a maximum number of rows.
     * 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>
     * Note that the underlying implementation is based on either invoking <code>ResultSet.absolute()</code>,
     * or if the ResultSet type is <code>ResultSet.TYPE_FORWARD_ONLY</code>, the <code>ResultSet.next()</code> method
     * is invoked equivalently.  The first row of a ResultSet is 1, so passing in an offset of 1 or less has no effect
     * on the initial positioning within the result set.
     * <p>
     * Note that different database and JDBC driver implementations may work differently with respect to this method.
     * Specifically, one should expect that <code>ResultSet.TYPE_FORWARD_ONLY</code> may be less efficient than a
     * "scrollable" type.
     * <p>
     * Resource handling is performed automatically where appropriate.
     *
     * @param gstring     the SQL statement
     * @param offset      the 1-based offset for the first row to be processed
     * @param maxRows     the maximum number of rows to be processed
     * @param metaClosure called for meta data (only once after sql execution)
     * @return a list of GroovyRowResult objects
     * @throws SQLException if a database access error occurs
     */
public List<GroovyRowResult> rows(GString gstring, int offset, int maxRows, Closure metaClosure) throws SQLException {
    List<Object> params = getParameters(gstring);
    String sql = asSql(gstring, params);
    return rows(sql, params, offset, maxRows, metaClosure);
}
Also used : GString(groovy.lang.GString)

Example 13 with GString

use of groovy.lang.GString in project groovy by apache.

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

Example 14 with GString

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 (or fields) and you know the column name(s) of the ID field(s).
     * <p>
     * Resource handling is performed automatically where appropriate.
     *
     * @param gstring        a GString containing the SQL query with embedded params
     * @param keyColumnNames a list of column names indicating the columns that should be returned from the
     *                       inserted row or rows (some drivers may be case sensitive, e.g. may require uppercase names)
     * @return A list of the auto-generated row results for each inserted row (typically auto-generated keys)
     * @throws SQLException if a database access error occurs
     * @see Connection#prepareStatement(String, String[])
     * @see #expand(Object)
     * @since 2.3.2
     */
public List<GroovyRowResult> executeInsert(GString gstring, List<String> keyColumnNames) throws SQLException {
    List<Object> params = getParameters(gstring);
    String sql = asSql(gstring, params);
    return executeInsert(sql, params, keyColumnNames);
}
Also used : GString(groovy.lang.GString)

Example 15 with GString

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

the class ArrayCachedClass method coerceArgument.

public Object coerceArgument(Object argument) {
    Class argumentClass = argument.getClass();
    if (argumentClass.getName().charAt(0) != '[')
        return argument;
    Class argumentComponent = argumentClass.getComponentType();
    Class paramComponent = getTheClass().getComponentType();
    if (paramComponent.isPrimitive()) {
        argument = DefaultTypeTransformation.convertToPrimitiveArray(argument, paramComponent);
    } else if (paramComponent == String.class && argument instanceof GString[]) {
        GString[] strings = (GString[]) argument;
        String[] ret = new String[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ret[i] = strings[i].toString();
        }
        argument = ret;
    } else if (paramComponent == Object.class && argumentComponent.isPrimitive()) {
        argument = DefaultTypeTransformation.primitiveArrayBox(argument);
    }
    return argument;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) 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