Search in sources :

Example 46 with GString

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

the class Sql method query.

/**
     * Performs the given SQL query, which should return a single
     * <code>ResultSet</code> object. The given closure is called
     * with the <code>ResultSet</code> as its argument.
     * The query may contain GString expressions.
     * <p>
     * Example usage:
     * <pre>
     * def location = 25
     * sql.query "select * from PERSON where location_id < $location", { ResultSet rs ->
     *     while (rs.next()) println rs.getString('firstname')
     * }
     * </pre>
     * <p>
     * All resources including the ResultSet are closed automatically
     * after the closure is called.
     *
     * @param gstring a GString containing the SQL query with embedded params
     * @param closure called for each row with a <code>ResultSet</code>
     * @throws SQLException if a database access error occurs
     * @see #expand(Object)
     */
public void query(GString gstring, Closure closure) throws SQLException {
    List<Object> params = getParameters(gstring);
    String sql = asSql(gstring, params);
    query(sql, params, closure);
}
Also used : GString(groovy.lang.GString)

Example 47 with GString

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

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)

Example 48 with GString

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

the class GPathResult method setProperty.

/**
     * Replaces the specified property of this GPathResult with a new value.
     *
     * @param property the property of this GPathResult to replace
     * @param newValue the new value of the property
     */
public void setProperty(final String property, final Object newValue) {
    if (property.startsWith("@")) {
        if (newValue instanceof String || newValue instanceof GString) {
            final Iterator iter = iterator();
            while (iter.hasNext()) {
                final NodeChild child = (NodeChild) iter.next();
                child.attributes().put(property.substring(1), newValue);
            }
        }
    } else {
        final GPathResult result = new NodeChildren(this, property, this.namespaceTagHints);
        if (newValue instanceof Map) {
            for (Object o : ((Map) newValue).entrySet()) {
                final Map.Entry entry = (Map.Entry) o;
                result.setProperty("@" + entry.getKey(), entry.getValue());
            }
        } else {
            if (newValue instanceof Closure) {
                result.replaceNode((Closure) newValue);
            } else {
                result.replaceBody(newValue);
            }
        }
    }
}
Also used : Closure(groovy.lang.Closure) Iterator(java.util.Iterator) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) GString(groovy.lang.GString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with GString

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

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)

Example 50 with GString

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

the class ObjectArrayPutAtMetaMethod method adjustNewValue.

private static Object adjustNewValue(Object[] objects, Object newValue) {
    Class arrayComponentClass = objects.getClass().getComponentType();
    Object adjustedNewVal = newValue;
    if (newValue instanceof Number) {
        if (!arrayComponentClass.equals(newValue.getClass())) {
            adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
        }
    } else if (Character.class.isAssignableFrom(arrayComponentClass)) {
        adjustedNewVal = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
    } else if (Number.class.isAssignableFrom(arrayComponentClass)) {
        if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) {
            Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
            adjustedNewVal = DefaultTypeTransformation.castToType(ch, arrayComponentClass);
        }
    } else if (arrayComponentClass.isArray()) {
        adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
    }
    return adjustedNewVal;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) GString(groovy.lang.GString) 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