Search in sources :

Example 16 with EJBQLException

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

the class EJBQLPathTranslator method visitIdentifier.

@Override
public boolean visitIdentifier(EJBQLExpression expression) {
    ClassDescriptor descriptor = context.getEntityDescriptor(expression.getText());
    if (descriptor == null) {
        throw new EJBQLException("Invalid identification variable: " + expression.getText());
    }
    this.currentEntity = descriptor.getEntity();
    this.idPath = expression.getText();
    this.joinMarker = EJBQLJoinAppender.makeJoinTailMarker(idPath);
    this.fullPath = idPath;
    return true;
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) EJBQLException(org.apache.cayenne.ejbql.EJBQLException)

Example 17 with EJBQLException

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

the class EJBQLPathTranslator method processIntermediatePathComponent.

protected void processIntermediatePathComponent() {
    ObjRelationship relationship = currentEntity.getRelationship(lastPathComponent);
    if (relationship == null) {
        throw new EJBQLException("Unknown relationship '" + lastPathComponent + "' for entity '" + currentEntity.getName() + "'");
    }
    this.currentEntity = (ObjEntity) relationship.getTargetEntity();
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException)

Example 18 with EJBQLException

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

the class EJBQLSelectColumnsTranslator method visitDbPath.

@Override
public boolean visitDbPath(EJBQLExpression expression, int finishedChildIndex) {
    EJBQLDbPathTranslator pathTranslator = new EJBQLDbPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't use multi-column paths in column clause");
        }

        @Override
        protected void processTerminatingRelationship(DbRelationship relationship) {
            Map<String, String> xfields = null;
            if (context.isAppendingResultColumns()) {
                xfields = context.nextEntityResult().getFields();
            }
            final Map<String, String> fields = xfields;
            DbEntity table = (DbEntity) relationship.getTargetEntity();
            Collection<DbAttribute> dbAttr = table.getAttributes();
            Iterator<DbAttribute> it = dbAttr.iterator();
            if (dbAttr.size() > 0) {
                resolveJoin();
            }
            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();
                appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
                first = false;
            }
        }

        @Override
        protected void processTerminatingAttribute(DbAttribute attribute) {
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(currentEntity));
            appendColumn(TypesMapping.getJavaBySqlType(attribute.getType()), alias, attribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
        }
    };
    expression.visit(pathTranslator);
    return false;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 19 with EJBQLException

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

the class EJBQLSelectColumnsTranslator method visitPath.

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

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't use multi-column paths in column clause");
        }

        @Override
        protected void processTerminatingRelationship(ObjRelationship relationship) {
            Map<String, String> xfields = null;
            if (context.isAppendingResultColumns()) {
                xfields = context.nextEntityResult().getFields();
            }
            final Map<String, String> fields = xfields;
            Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
            DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
            DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
            Iterator<DbAttribute> it = dbAttr.iterator();
            if (dbAttr.size() > 0) {
                resolveJoin();
            }
            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();
                appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
                first = false;
            }
        }

        @Override
        protected void processTerminatingAttribute(ObjAttribute attribute) {
            DbEntity table = currentEntity.getDbEntity();
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            if (attribute.isFlattened()) {
                Iterator<?> dbPathIterator = attribute.getDbPathIterator();
                EJBQLTableId lhsId = new EJBQLTableId(idPath);
                while (dbPathIterator.hasNext()) {
                    Object pathPart = dbPathIterator.next();
                    // later when appending table
                    if (pathPart == null) {
                        throw new CayenneRuntimeException("ObjAttribute has no component: %s", attribute.getName());
                    } else if (pathPart instanceof DbAttribute) {
                        DbAttribute dbAttribute = (DbAttribute) pathPart;
                        appendColumn(attribute.getType(), context.getTableAlias(lhsId.getEntityId(), context.getQuotingStrategy().quotedFullyQualifiedName((DbEntity) dbAttribute.getEntity())), dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
                    }
                }
            } else {
                DbAttribute dbAttribute = attribute.getDbAttribute();
                appendColumn(attribute.getType(), alias, dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
            }
        }
    };
    expression.visit(pathTranslator);
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Aggregations

EJBQLException (org.apache.cayenne.ejbql.EJBQLException)19 DbRelationship (org.apache.cayenne.map.DbRelationship)9 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)9 DbAttribute (org.apache.cayenne.map.DbAttribute)7 DbEntity (org.apache.cayenne.map.DbEntity)7 ObjRelationship (org.apache.cayenne.map.ObjRelationship)7 DbJoin (org.apache.cayenne.map.DbJoin)5 HashMap (java.util.HashMap)3 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)3 EJBQLExpression (org.apache.cayenne.ejbql.EJBQLExpression)2 EJBQLExpressionVisitor (org.apache.cayenne.ejbql.EJBQLExpressionVisitor)2 EJBQLPath (org.apache.cayenne.ejbql.parser.EJBQLPath)2 EntityResult (org.apache.cayenne.map.EntityResult)2 ObjAttribute (org.apache.cayenne.map.ObjAttribute)2 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)2 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)2 BigDecimal (java.math.BigDecimal)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1