Search in sources :

Example 6 with ParameterBinding

use of org.apache.cayenne.access.translator.ParameterBinding in project cayenne by apache.

the class Bind method bindValue.

protected void bindValue(Context context, Object value, String jdbcTypeName, int scale) {
    int jdbcType;
    if (jdbcTypeName != null) {
        jdbcType = TypesMapping.getSqlTypeByName(jdbcTypeName);
    } else if (value != null) {
        jdbcType = TypesMapping.getSqlTypeByJava(value.getClass());
    } else {
        jdbcType = TypesMapping.getSqlTypeByName(TypesMapping.SQL_NULL);
    }
    processBinding(context, new ParameterBinding(value, jdbcType, scale));
}
Also used : ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 7 with ParameterBinding

use of org.apache.cayenne.access.translator.ParameterBinding in project cayenne by apache.

the class BindObjectEqual method apply.

@Override
public void apply(Context context, ASTExpression... expressions) {
    Object object = expressions[0].evaluateAsObject(context);
    Map<String, Object> idMap = toIdMap(object);
    Object sqlColumns = null;
    Object idColumns = null;
    if (expressions.length > 1) {
        sqlColumns = expressions[1].evaluateAsObject(context);
    }
    if (expressions.length > 2) {
        idColumns = expressions[2].evaluateAsObject(context);
    }
    if (idMap == null) {
        // assume null object, and bind all null values
        if (sqlColumns == null || idColumns == null) {
            throw new CayenneRuntimeException("Invalid parameters. " + "Either object has to be set or sqlColumns and idColumns or both.");
        }
        idMap = Collections.emptyMap();
    } else if (sqlColumns == null || idColumns == null) {
        // infer SQL columns from ID columns
        sqlColumns = idMap.keySet().toArray();
        idColumns = sqlColumns;
    }
    String[] sqlColumnsArray = toArray(sqlColumns);
    String[] idColumnsArray = toArray(idColumns);
    if (sqlColumnsArray.length != idColumnsArray.length) {
        throw new CayenneRuntimeException("SQL columns and ID columns arrays have different sizes.");
    }
    for (int i = 0; i < sqlColumnsArray.length; i++) {
        Object value = idMap.get(idColumnsArray[i]);
        int jdbcType = (value != null) ? TypesMapping.getSqlTypeByJava(value.getClass()) : Types.INTEGER;
        renderColumn(context, sqlColumnsArray[i], i);
        render(context, new ParameterBinding(value, jdbcType, -1));
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 8 with ParameterBinding

use of org.apache.cayenne.access.translator.ParameterBinding in project cayenne by apache.

the class Slf4jJdbcEventLogger method appendParameters.

@SuppressWarnings("unchecked")
private void appendParameters(StringBuilder buffer, String label, ParameterBinding[] bindings) {
    int len = bindings.length;
    if (len > 0) {
        boolean hasIncluded = false;
        for (int i = 0, j = 1; i < len; i++) {
            ParameterBinding b = bindings[i];
            if (b.isExcluded()) {
                continue;
            }
            if (hasIncluded) {
                buffer.append(", ");
            } else {
                hasIncluded = true;
                buffer.append("[").append(label).append(": ");
            }
            buffer.append(j++);
            if (b instanceof DbAttributeBinding) {
                DbAttribute attribute = ((DbAttributeBinding) b).getAttribute();
                if (attribute != null) {
                    buffer.append("->");
                    buffer.append(attribute.getName());
                }
            }
            buffer.append(":");
            if (b.getExtendedType() != null) {
                buffer.append(b.getExtendedType().toString(b.getValue()));
            } else if (b.getValue() == null) {
                buffer.append("NULL");
            } else {
                buffer.append(b.getValue().getClass().getName()).append("@").append(System.identityHashCode(b.getValue()));
            }
        }
        if (hasIncluded) {
            buffer.append("]");
        }
    }
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Aggregations

ParameterBinding (org.apache.cayenne.access.translator.ParameterBinding)8 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ParseErrorException (org.apache.velocity.exception.ParseErrorException)2 CallableStatement (java.sql.CallableStatement)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)1 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)1 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)1 ProcedureParameterBinding (org.apache.cayenne.access.translator.ProcedureParameterBinding)1 ExtendedType (org.apache.cayenne.access.types.ExtendedType)1 ExpressionException (org.apache.cayenne.exp.ExpressionException)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 ProcedureParameter (org.apache.cayenne.map.ProcedureParameter)1 VelocityContext (org.apache.velocity.VelocityContext)1 ParseException (org.apache.velocity.runtime.parser.ParseException)1