Search in sources :

Example 1 with EJBQLExpressionVisitor

use of org.apache.cayenne.ejbql.EJBQLExpressionVisitor in project cayenne by apache.

the class EJBQLGroupByTranslator method visitPath.

@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
    if (itemCount++ > 0) {
        context.append(',');
    }
    EJBQLExpressionVisitor childVisitor = new EJBQLPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't GROUP BY on multi-column paths or objects");
        }

        @Override
        public boolean visitIdentificationVariable(EJBQLExpression expression) {
            String idVariableAbsolutePath = fullPath + "." + expression.getText();
            ClassDescriptor descriptor = context.getEntityDescriptor(idVariableAbsolutePath);
            if (descriptor != null) {
                this.lastAlias = context.getTableAlias(idVariableAbsolutePath, context.getQuotingStrategy().quotedFullyQualifiedName(descriptor.getEntity().getDbEntity()));
            }
            resolveLastPathComponent(expression.getText());
            this.fullPath = fullPath + '.' + lastPathComponent;
            return true;
        }

        @Override
        protected void processTerminatingRelationship(ObjRelationship relationship) {
            Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
            DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
            DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
            Iterator<DbAttribute> it = dbAttr.iterator();
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            boolean first = true;
            while (it.hasNext()) {
                context.append(!first ? ", " : " ");
                DbAttribute dbAttribute = it.next();
                context.append(alias).append('.').append(context.getQuotingStrategy().quotedName(dbAttribute));
                first = false;
            }
        }
    };
    expression.visit(childVisitor);
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute) EJBQLExpressionVisitor(org.apache.cayenne.ejbql.EJBQLExpressionVisitor) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression)

Example 2 with EJBQLExpressionVisitor

use of org.apache.cayenne.ejbql.EJBQLExpressionVisitor in project cayenne by apache.

the class EJBQLOrderByTranslator method visitPath.

@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
    EJBQLExpressionVisitor childVisitor = new EJBQLPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't order on multi-column paths or objects");
        }
    };
    expression.visit(childVisitor);
    return false;
}
Also used : EJBQLException(org.apache.cayenne.ejbql.EJBQLException) EJBQLExpressionVisitor(org.apache.cayenne.ejbql.EJBQLExpressionVisitor)

Example 3 with EJBQLExpressionVisitor

use of org.apache.cayenne.ejbql.EJBQLExpressionVisitor in project cayenne by apache.

the class EJBQLAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws Exception {
    EJBQLCompiledExpression compiledExpression = query.getExpression(dataNode.getEntityResolver());
    final EJBQLTranslatorFactory translatorFactory = dataNode.getAdapter().getEjbqlTranslatorFactory();
    final EJBQLTranslationContext context = new EJBQLTranslationContext(dataNode.getEntityResolver(), query, compiledExpression, translatorFactory, dataNode.getAdapter().getQuotingStrategy());
    compiledExpression.getExpression().visit(new EJBQLBaseVisitor(false) {

        @Override
        public boolean visitSelect(EJBQLExpression expression) {
            EJBQLExpressionVisitor visitor = translatorFactory.getSelectTranslator(context);
            expression.visit(visitor);
            return false;
        }

        @Override
        public boolean visitDelete(EJBQLExpression expression) {
            EJBQLExpressionVisitor visitor = translatorFactory.getDeleteTranslator(context);
            expression.visit(visitor);
            return false;
        }

        @Override
        public boolean visitUpdate(EJBQLExpression expression) {
            EJBQLExpressionVisitor visitor = translatorFactory.getUpdateTranslator(context);
            expression.visit(visitor);
            return false;
        }
    });
    SQLTemplate sqlQuery = context.getQuery();
    // update with metadata
    QueryMetadata md = query.getMetaData(dataNode.getEntityResolver());
    sqlQuery.setFetchLimit(md.getFetchLimit());
    sqlQuery.setFetchOffset(md.getFetchOffset());
    sqlQuery.setResult(compiledExpression.getResult());
    sqlQuery.setPageSize(md.getPageSize());
    if (md.getStatementFetchSize() != 0) {
        sqlQuery.setStatementFetchSize(md.getStatementFetchSize());
    }
    int queryTimeout = md.getQueryTimeout();
    if (queryTimeout != QueryMetadata.QUERY_TIMEOUT_DEFAULT) {
        sqlQuery.setQueryTimeout(queryTimeout);
    }
    actionFactory.sqlAction(sqlQuery).performAction(connection, observer);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) EJBQLTranslationContext(org.apache.cayenne.access.translator.ejbql.EJBQLTranslationContext) EJBQLBaseVisitor(org.apache.cayenne.ejbql.EJBQLBaseVisitor) QueryMetadata(org.apache.cayenne.query.QueryMetadata) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) EJBQLTranslatorFactory(org.apache.cayenne.access.translator.ejbql.EJBQLTranslatorFactory) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression) EJBQLExpressionVisitor(org.apache.cayenne.ejbql.EJBQLExpressionVisitor)

Aggregations

EJBQLExpressionVisitor (org.apache.cayenne.ejbql.EJBQLExpressionVisitor)3 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)2 EJBQLExpression (org.apache.cayenne.ejbql.EJBQLExpression)2 EJBQLTranslationContext (org.apache.cayenne.access.translator.ejbql.EJBQLTranslationContext)1 EJBQLTranslatorFactory (org.apache.cayenne.access.translator.ejbql.EJBQLTranslatorFactory)1 EJBQLBaseVisitor (org.apache.cayenne.ejbql.EJBQLBaseVisitor)1 EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbEntity (org.apache.cayenne.map.DbEntity)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 ObjRelationship (org.apache.cayenne.map.ObjRelationship)1 QueryMetadata (org.apache.cayenne.query.QueryMetadata)1 SQLTemplate (org.apache.cayenne.query.SQLTemplate)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1