use of org.apache.cayenne.query.Query in project cayenne by apache.
the class OrderingTranslator method doAppendPart.
/**
* Translates query Ordering list to SQL ORDER BY clause. Ordering list is
* obtained from <code>queryAssembler</code>'s query object. In a process of
* building of ORDER BY clause, <code>queryAssembler</code> is notified when
* a join needs to be added.
*
* @since 3.0
*/
@Override
protected void doAppendPart() {
Query q = queryAssembler.getQuery();
// only select queries can have ordering...
if (q == null || !(q instanceof SelectQuery)) {
return;
}
Iterator<Ordering> it = ((SelectQuery<?>) q).getOrderings().iterator();
StringBuilder mainBuffer = this.out;
try {
while (it.hasNext()) {
Ordering ord = it.next();
// reset buffer to collect SQL for the single column, that we'll
// be reusing
this.out = new StringBuilder();
if (ord.isCaseInsensitive()) {
out.append("UPPER(");
}
Expression exp = ord.getSortSpec();
if (exp.getType() == Expression.OBJ_PATH) {
appendObjPath(exp);
} else if (exp.getType() == Expression.DB_PATH) {
appendDbPath(exp);
} else if (exp.getType() == Expression.FUNCTION_CALL) {
appendFunction(exp);
} else {
throw new CayenneRuntimeException("Unsupported ordering expression: %s", exp);
}
// Close UPPER() modifier
if (ord.isCaseInsensitive()) {
out.append(")");
}
String columnSQL = out.toString();
mainBuffer.append(columnSQL);
orderByColumnList.add(columnSQL);
// "ASC" is a noop, omit it from the query
if (!ord.isAscending()) {
mainBuffer.append(" DESC");
}
if (it.hasNext()) {
mainBuffer.append(", ");
}
}
} finally {
this.out = mainBuffer;
}
}
use of org.apache.cayenne.query.Query in project cayenne by apache.
the class QualifierTranslator method extractQualifier.
protected Expression extractQualifier() {
// if additional qualifier is set, use it
if (this.qualifier != null) {
return this.qualifier;
}
Query q = queryAssembler.getQuery();
Expression qualifier = ((SelectQuery<?>) q).getQualifier();
// append Entity qualifiers, taking inheritance into account
ObjEntity entity = getObjEntity();
if (entity != null) {
ClassDescriptor descriptor = queryAssembler.getEntityResolver().getClassDescriptor(entity.getName());
Expression entityQualifier = descriptor.getEntityInheritanceTree().qualifierForEntityAndSubclasses();
if (entityQualifier != null) {
qualifier = (qualifier != null) ? qualifier.andExp(entityQualifier) : entityQualifier;
}
}
// Attaching root Db entity's qualifier
if (getDbEntity() != null) {
Expression dbQualifier = getDbEntity().getQualifier();
if (dbQualifier != null) {
dbQualifier = dbQualifier.transform(new DbEntityQualifierTransformer());
qualifier = qualifier == null ? dbQualifier : qualifier.andExp(dbQualifier);
}
}
return qualifier;
}
use of org.apache.cayenne.query.Query in project cayenne by apache.
the class DefaultOperationObserver method printExceptions.
/**
* Prints the information about query and global exceptions.
*/
public void printExceptions(PrintWriter out) {
if (globalExceptions.size() > 0) {
if (globalExceptions.size() == 1) {
out.println("Global Exception:");
} else {
out.println("Global Exceptions:");
}
for (final Throwable th : globalExceptions) {
th.printStackTrace(out);
}
}
if (queryExceptions.size() > 0) {
if (queryExceptions.size() == 1) {
out.println("Query Exception:");
} else {
out.println("Query Exceptions:");
}
for (final Query query : queryExceptions.keySet()) {
Throwable th = queryExceptions.get(query);
th.printStackTrace(out);
}
}
}
Aggregations