use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method processParameters.
/**
* Internal method to add the parameters values to the query prior to
* execution. Returns a list of parameter values in the order the parameters
* are defined for the databaseQuery.
*/
protected List<Object> processParameters() {
DatabaseQuery query = getDatabaseQueryInternal();
List<String> arguments = query.getArguments();
if (arguments.isEmpty()) {
// This occurs for native queries, as the query does not know of its arguments.
// This may have issues, it is better if the query set its arguments
// when parsing the SQL.
arguments = new ArrayList<>(this.parameterValues.keySet());
query.setArguments(arguments);
}
// now create parameterValues in the same order as the argument list
int size = arguments.size();
List<Object> parameterValues = new ArrayList<Object>(size);
for (int index = 0; index < size; index++) {
String name = arguments.get(index);
Object parameter = this.parameterValues.get(name);
if ((parameter != null) || this.parameterValues.containsKey(name)) {
parameterValues.add(parameter);
} else if (query.hasNullableArguments() && query.getNullableArguments().contains(new DatabaseField(name))) {
parameterValues.add(null);
} else {
// Error: missing actual parameter value
throw new IllegalStateException(ExceptionLocalization.buildMessage("missing_parameter_value", new Object[] { name }));
}
}
return parameterValues;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method propagateResultProperties.
/**
* Configure the firstResult, maxRows and lock mode in the EclipseLink
* ReadQuery.
*/
protected void propagateResultProperties() {
DatabaseQuery databaseQuery = getDatabaseQueryInternal();
if (databaseQuery.isReadQuery()) {
ReadQuery readQuery = (ReadQuery) databaseQuery;
if (maxResults >= 0) {
cloneSharedQuery();
readQuery = (ReadQuery) getDatabaseQueryInternal();
int maxRows = maxResults + ((firstResultIndex >= 0) ? firstResultIndex : 0);
readQuery.setMaxRows(maxRows);
}
if (firstResultIndex > UNDEFINED) {
cloneSharedQuery();
readQuery = (ReadQuery) getDatabaseQueryInternal();
readQuery.setFirstResult(firstResultIndex);
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method getResultList.
/**
* Execute the query and return the query results as a List.
*
* @return a list of the results
*/
public List getResultList() {
// bug51411440: need to throw IllegalStateException if query
// executed on closed em
this.entityManager.verifyOpenWithSetRollbackOnly();
try {
setAsSQLReadQuery();
propagateResultProperties();
// bug:4297903, check container policy class and throw exception if
// its not the right type
DatabaseQuery query = getDatabaseQueryInternal();
if (query.isReadAllQuery()) {
Class<?> containerClass = ((ReadAllQuery) query).getContainerPolicy().getContainerClass();
if (!Helper.classImplementsInterface(containerClass, ClassConstants.List_Class)) {
throw QueryException.invalidContainerClass(containerClass, ClassConstants.List_Class);
}
} else if (query.isReadObjectQuery()) {
List<Object> resultList = new ArrayList<>();
Object result = executeReadQuery();
if (result != null) {
resultList.add(result);
}
return resultList;
} else if (!query.isReadQuery()) {
throw new IllegalStateException(ExceptionLocalization.buildMessage("incorrect_query_for_get_result_list"));
}
return (List) executeReadQuery();
} catch (LockTimeoutException exception) {
throw exception;
} catch (PersistenceException exception) {
setRollbackOnly();
throw exception;
} catch (IllegalStateException exception) {
setRollbackOnly();
throw exception;
} catch (RuntimeException exception) {
setRollbackOnly();
throw new PersistenceException(exception);
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method cloneSharedQuery.
/**
* If the query was from the jpql parse cache it must be cloned before being
* modified.
*/
protected void cloneSharedQuery() {
DatabaseQuery query = getDatabaseQueryInternal();
if (this.isShared) {
// Clone to allow setting of hints or other properties without
// corrupting original query.
query = (DatabaseQuery) databaseQuery.clone();
setDatabaseQuery(query);
this.isShared = false;
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method setParameterInternal.
/**
* Bind an argument to a named or indexed parameter.
*
* @param name
* the parameter name
* @param value
* to bind
* @param isIndex
* defines if index or named
*/
protected void setParameterInternal(String name, Object value, boolean isIndex) {
DatabaseQuery query = getDatabaseQueryInternal();
if (query.getQueryMechanism().isJPQLCallQueryMechanism()) {
// only non native queries
int index = query.getArguments().indexOf(name);
if (index == -1) {
if (isIndex) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-wrong-argument-index", new Object[] { name, query.getEJBQLString() }));
} else {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-wrong-argument-name", new Object[] { name, query.getEJBQLString() }));
}
}
Class<?> type = query.getArgumentTypes().get(index);
if (!isValidActualParameter(value, type)) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-incorrect-parameter-type", new Object[] { name, value.getClass(), query.getArgumentTypes().get(index), query.getEJBQLString() }));
}
} else {
// native queries start a 1 not 0.
if (isIndex && name.equals("0")) {
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-wrong-argument-index", new Object[] { name, query.getSQLString() }));
}
}
this.parameterValues.put(name, value);
}
Aggregations