Search in sources :

Example 1 with ParameterBinding

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

the class SQLTemplateAction method bindExtendedTypes.

private void bindExtendedTypes(ParameterBinding[] bindings) {
    int i = 1;
    for (ParameterBinding binding : bindings) {
        Object value = binding.getValue();
        ExtendedType extendedType = value != null ? getAdapter().getExtendedTypes().getRegisteredType(value.getClass()) : getAdapter().getExtendedTypes().getDefaultType();
        binding.setExtendedType(extendedType);
        binding.setStatementPosition(i++);
    }
}
Also used : ExtendedType(org.apache.cayenne.access.types.ExtendedType) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 2 with ParameterBinding

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

the class ProcedureTranslator method createStatement.

/**
 * Creates and binds a PreparedStatement to execute query SQL via JDBC.
 */
public PreparedStatement createStatement() throws Exception {
    this.callParams = getProcedure().getCallParameters();
    this.values = new ArrayList<>(callParams.size());
    initValues();
    String sqlStr = createSqlString();
    if (logger.isLoggable()) {
        // need to convert OUT/VOID parameters to loggable strings
        ParameterBinding[] parameterBindings = new ParameterBinding[values.size()];
        for (int i = 0; i < values.size(); i++) {
            ProcedureParameter procedureParameter = callParams.get(i);
            Object value = values.get(i);
            if (value instanceof NotInParam) {
                value = value.toString();
            }
            parameterBindings[i] = new ParameterBinding(value, procedureParameter.getType(), procedureParameter.getPrecision());
        }
        logger.logQuery(sqlStr, parameterBindings);
    }
    CallableStatement stmt = connection.prepareCall(sqlStr);
    initStatement(stmt);
    return stmt;
}
Also used : ProcedureParameter(org.apache.cayenne.map.ProcedureParameter) CallableStatement(java.sql.CallableStatement) ProcedureParameterBinding(org.apache.cayenne.access.translator.ProcedureParameterBinding) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 3 with ParameterBinding

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

the class BindDirective method render.

/**
 * @since 3.0
 */
protected void render(InternalContextAdapter context, Writer writer, Node node, Object value, String typeString, int scale) throws IOException, ParseErrorException {
    int jdbcType = TypesMapping.NOT_DEFINED;
    if (typeString != null) {
        jdbcType = TypesMapping.getSqlTypeByName(typeString);
    } else if (value != null) {
        jdbcType = TypesMapping.getSqlTypeByJava(value.getClass());
    } else {
        // value is null, set JDBC type to NULL
        jdbcType = TypesMapping.getSqlTypeByName(TypesMapping.SQL_NULL);
    }
    if (jdbcType == TypesMapping.NOT_DEFINED) {
        throw new ParseErrorException("Can't determine JDBC type of binding (" + value + ", " + typeString + ") at line " + node.getLine() + ", column " + node.getColumn());
    }
    render(context, writer, new ParameterBinding(value, jdbcType, scale));
}
Also used : ParseErrorException(org.apache.velocity.exception.ParseErrorException) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 4 with ParameterBinding

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

the class BindObjectEqualDirective method render.

@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
    Object object = getChild(context, node, 0);
    Map idMap = toIdMap(object);
    Object sqlColumns = getChild(context, node, 1);
    Object idColumns = getChild(context, node, 2);
    if (idMap == null) {
        if (sqlColumns == null || idColumns == null) {
            throw new ParseErrorException("Invalid parameters. " + "Either object has to be set " + "or sqlColumns and idColumns or both.");
        }
        idMap = Collections.EMPTY_MAP;
    } else if (sqlColumns == null || idColumns == null) {
        // infer SQL columns from ID columns
        sqlColumns = idMap.keySet().toArray();
        idColumns = sqlColumns;
    }
    Object[] sqlColumnsArray = toArray(sqlColumns);
    Object[] idColumnsArray = toArray(idColumns);
    if (sqlColumnsArray.length != idColumnsArray.length) {
        throw new ParseErrorException("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, writer, sqlColumnsArray[i], i);
        writer.write(' ');
        render(context, writer, new ParameterBinding(value, jdbcType, -1));
    }
    return true;
}
Also used : ParseErrorException(org.apache.velocity.exception.ParseErrorException) Map(java.util.Map) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding)

Example 5 with ParameterBinding

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

the class VelocitySQLTemplateProcessor method processTemplate.

SQLStatement processTemplate(String template, SimpleNode parsedTemplate, Map<String, Object> parameters) {
    List<ParameterBinding> bindings = new ArrayList<>();
    List<ColumnDescriptor> results = new ArrayList<>();
    parameters.put(BINDINGS_LIST_KEY, bindings);
    parameters.put(RESULT_COLUMNS_LIST_KEY, results);
    parameters.put(HELPER_KEY, renderingUtils);
    String sql;
    try {
        sql = buildStatement(new VelocityContext(parameters), template, parsedTemplate);
    } catch (Exception e) {
        throw new CayenneRuntimeException("Error processing Velocity template", e);
    }
    ParameterBinding[] bindingsArray = new ParameterBinding[bindings.size()];
    bindings.toArray(bindingsArray);
    ColumnDescriptor[] resultsArray = new ColumnDescriptor[results.size()];
    results.toArray(resultsArray);
    return new SQLStatement(sql, resultsArray, bindingsArray);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) ArrayList(java.util.ArrayList) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) ParameterBinding(org.apache.cayenne.access.translator.ParameterBinding) ExpressionException(org.apache.cayenne.exp.ExpressionException) ParseException(org.apache.velocity.runtime.parser.ParseException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

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