Search in sources :

Example 26 with GString

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

the class Sql method eachRow.

/**
     * Performs the given SQL query calling the given <code>closure</code> with each row of the result set starting at
     * the provided <code>offset</code>, and including up to <code>maxRows</code> number of rows.
     * 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.
     * 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.
     *
     * @param gstring     a GString containing the SQL query with embedded params
     * @param metaClosure called for meta data (only once after sql execution)
     * @param offset      the 1-based offset for the first row to be processed
     * @param maxRows     the maximum number of rows to be processed
     * @param rowClosure  called for each row with a GroovyResultSet
     * @throws SQLException if a database access error occurs
     */
public void eachRow(GString gstring, Closure metaClosure, int offset, int maxRows, Closure rowClosure) throws SQLException {
    List<Object> params = getParameters(gstring);
    String sql = asSql(gstring, params);
    eachRow(sql, params, metaClosure, offset, maxRows, rowClosure);
}
Also used : GString(groovy.lang.GString)

Example 27 with GString

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

the class Sql method eachRow.

/**
     * Performs the given SQL query calling the given <code>closure</code> with each row of the result set starting at
     * the provided <code>offset</code>, and including up to <code>maxRows</code> number of rows.
     * 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.
     * 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.
     *
     * @param gstring a GString containing the SQL query with embedded params
     * @param offset  the 1-based offset for the first row to be processed
     * @param maxRows the maximum number of rows to be processed
     * @param closure called for each row with a GroovyResultSet
     * @throws SQLException if a database access error occurs
     */
public void eachRow(GString gstring, int offset, int maxRows, Closure closure) throws SQLException {
    List<Object> params = getParameters(gstring);
    String sql = asSql(gstring, params);
    eachRow(sql, params, offset, maxRows, closure);
}
Also used : GString(groovy.lang.GString)

Example 28 with GString

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

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

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

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

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

the class Sql method asSql.

/**
     * Hook to allow derived classes to override sql generation from GStrings.
     *
     * @param gstring a GString containing the SQL query with embedded params
     * @param values  the values to embed
     * @return the SQL version of the given query using ? instead of any parameter
     * @see #expand(Object)
     */
protected String asSql(GString gstring, List<Object> values) {
    String[] strings = gstring.getStrings();
    if (strings.length <= 0) {
        throw new IllegalArgumentException("No SQL specified in GString: " + gstring);
    }
    boolean nulls = false;
    StringBuilder buffer = new StringBuilder();
    boolean warned = false;
    Iterator<Object> iter = values.iterator();
    for (int i = 0; i < strings.length; i++) {
        String text = strings[i];
        if (text != null) {
            buffer.append(text);
        }
        if (iter.hasNext()) {
            Object value = iter.next();
            if (value != null) {
                if (value instanceof ExpandedVariable) {
                    buffer.append(((ExpandedVariable) value).getObject());
                    iter.remove();
                } else {
                    boolean validBinding = true;
                    if (i < strings.length - 1) {
                        String nextText = strings[i + 1];
                        if ((text.endsWith("\"") || text.endsWith("'")) && (nextText.startsWith("'") || nextText.startsWith("\""))) {
                            if (!warned) {
                                LOG.warning("In Groovy SQL please do not use quotes around dynamic expressions " + "(which start with $) as this means we cannot use a JDBC PreparedStatement " + "and so is a security hole. Groovy has worked around your mistake but the security hole is still there. " + "The expression so far is: " + buffer.toString() + "?" + nextText);
                                warned = true;
                            }
                            buffer.append(value);
                            iter.remove();
                            validBinding = false;
                        }
                    }
                    if (validBinding) {
                        buffer.append("?");
                    }
                }
            } else {
                nulls = true;
                iter.remove();
                // will replace these with nullish values
                buffer.append("?'\"?");
            }
        }
    }
    String sql = buffer.toString();
    if (nulls) {
        sql = nullify(sql);
    }
    return sql;
}
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