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++);
}
}
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;
}
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));
}
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;
}
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);
}
Aggregations