Search in sources :

Example 1 with EJBQLBaseVisitor

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

the class EJBQLJoinAppender method ejbqlQualifierForEntityAndSubclasses.

private EJBQLExpression ejbqlQualifierForEntityAndSubclasses(Expression qualifier, String entityId) {
    // parser only works on full queries, so prepend a dummy query and then strip it out...
    String ejbqlChunk = qualifier.toEJBQL(entityId);
    EJBQLExpression expression = EJBQLParserFactory.getParser().parse("DELETE FROM DUMMY WHERE " + ejbqlChunk);
    final EJBQLExpression[] result = new EJBQLExpression[1];
    expression.visit(new EJBQLBaseVisitor() {

        @Override
        public boolean visitWhere(EJBQLExpression expression) {
            result[0] = expression.getChild(0);
            return false;
        }
    });
    return result[0];
}
Also used : EJBQLBaseVisitor(org.apache.cayenne.ejbql.EJBQLBaseVisitor) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression)

Example 2 with EJBQLBaseVisitor

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

the class EJBQLFromItem method getSyntheticId.

/**
 * Returns an id generated from the entity name. It is used when no user-specified id
 * exists.
 */
public String getSyntheticId() {
    int len = getChildrenCount();
    if (len < 1) {
        return null;
    }
    final String[] entityNames = new String[1];
    getChild(0).visit(new EJBQLBaseVisitor() {

        @Override
        public boolean visitIdentificationVariable(EJBQLExpression expression) {
            entityNames[0] = expression.getText();
            return false;
        }
    });
    if (entityNames[0] == null) {
        return null;
    }
    // id's are case insensitive, while entity names are. Using simple encoding to
    // transform the entity name in such way that two entities that differ only in
    // capitalization would produce different lowercase ids
    StringBuilder id = new StringBuilder(entityNames[0].length() + 2);
    for (int i = 0; i < entityNames[0].length(); i++) {
        char c = entityNames[0].charAt(i);
        if (Character.isUpperCase(c)) {
            id.append('%').append(Character.toLowerCase(c));
        } else {
            id.append(c);
        }
    }
    return id.toString();
}
Also used : EJBQLBaseVisitor(org.apache.cayenne.ejbql.EJBQLBaseVisitor) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression)

Example 3 with EJBQLBaseVisitor

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

the class EJBQLAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, 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());
    }
    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

EJBQLBaseVisitor (org.apache.cayenne.ejbql.EJBQLBaseVisitor)3 EJBQLExpression (org.apache.cayenne.ejbql.EJBQLExpression)3 EJBQLTranslationContext (org.apache.cayenne.access.translator.ejbql.EJBQLTranslationContext)1 EJBQLTranslatorFactory (org.apache.cayenne.access.translator.ejbql.EJBQLTranslatorFactory)1 EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)1 EJBQLExpressionVisitor (org.apache.cayenne.ejbql.EJBQLExpressionVisitor)1 QueryMetadata (org.apache.cayenne.query.QueryMetadata)1 SQLTemplate (org.apache.cayenne.query.SQLTemplate)1