Search in sources :

Example 71 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class ManyToManyCandidateEntity method addFlattenedRelationship.

private void addFlattenedRelationship(ObjectNameGenerator nameGenerator, ObjEntity srcEntity, ObjEntity dstEntity, DbRelationship rel1, DbRelationship rel2) {
    if (rel1.getSourceAttributes().isEmpty() && rel2.getTargetAttributes().isEmpty()) {
        LOG.warn("Wrong call ManyToManyCandidateEntity.addFlattenedRelationship(... , " + srcEntity.getName() + ", " + dstEntity.getName() + ", ...)");
        return;
    }
    ObjRelationship newRelationship = new ObjRelationship();
    newRelationship.setName(NameBuilder.builder(newRelationship, srcEntity).baseName(nameGenerator.relationshipName(rel1, rel2)).name());
    newRelationship.setSourceEntity(srcEntity);
    newRelationship.setTargetEntityName(dstEntity);
    newRelationship.addDbRelationship(rel1);
    newRelationship.addDbRelationship(rel2);
    srcEntity.addRelationship(newRelationship);
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship)

Example 72 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class DiffProcessor method arcCreated.

@Override
public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
    ObjectId id = (ObjectId) nodeId;
    String relationshipName = arcId.toString();
    ObjEntity entity = entityResolver.getObjEntity(id.getEntityName());
    ObjRelationship relationship = entity.getRelationship(relationshipName);
    MutableObjectChange c = changeSet.getOrCreate(id, ObjectChangeType.UPDATE);
    ObjectId tid = (ObjectId) targetNodeId;
    if (relationship.isToMany()) {
        c.toManyRelationshipConnected(relationshipName, tid);
    } else {
        c.toOneRelationshipConnected(relationshipName, tid);
    }
}
Also used : MutableObjectChange(org.apache.cayenne.commitlog.model.MutableObjectChange) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectId(org.apache.cayenne.ObjectId)

Example 73 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship in project cayenne by apache.

the class EJBQLPathAnaliserTranslator method visitMemberOf.

@Override
public boolean visitMemberOf(EJBQLExpression expression) {
    if (expression.getChildrenCount() != 2) {
        throw new EJBQLException("MEMBER OF must have exactly two children, got: " + expression.getChildrenCount());
    }
    if (!(expression.getChild(1) instanceof EJBQLPath)) {
        throw new EJBQLException("Second child of the MEMBER OF must be a collection path, got: " + expression.getChild(1));
    }
    QuotingStrategy quoter = context.getQuotingStrategy();
    EJBQLPath path = (EJBQLPath) expression.getChild(1);
    // make sure the ID for the path does not overlap with other condition
    // joins...
    String id = path.getAbsolutePath();
    String correlatedEntityId = path.getId();
    ClassDescriptor correlatedEntityDescriptor = context.getEntityDescriptor(correlatedEntityId);
    String correlatedTableName = quoter.quotedFullyQualifiedName(correlatedEntityDescriptor.getEntity().getDbEntity());
    String correlatedTableAlias = context.getTableAlias(correlatedEntityId, correlatedTableName);
    String subqueryId = context.createIdAlias(id);
    ClassDescriptor targetDescriptor = context.getEntityDescriptor(subqueryId);
    if (expression.isNegated()) {
        context.append(" NOT");
    }
    context.append(" EXISTS (SELECT 1 FROM ");
    String subqueryTableName = quoter.quotedFullyQualifiedName(targetDescriptor.getEntity().getDbEntity());
    String subqueryRootAlias = context.getTableAlias(subqueryId, subqueryTableName);
    ObjRelationship relationship = correlatedEntityDescriptor.getEntity().getRelationship(path.getRelativePath());
    if (relationship.getDbRelationshipPath().contains(".")) {
        // if the DbRelationshipPath contains '.', the relationship is
        // flattened
        subqueryRootAlias = processFlattenedRelationShip(subqueryRootAlias, relationship);
    } else {
        // not using "AS" to separate table name and alias name - OpenBase
        // doesn't
        // support "AS", and the rest of the databases do not care
        context.append(subqueryTableName).append(' ').append(subqueryRootAlias);
    }
    context.append(" WHERE");
    DbRelationship correlatedJoinRelationship = context.getIncomingRelationships(new EJBQLTableId(id)).get(0);
    for (DbJoin join : correlatedJoinRelationship.getJoins()) {
        context.append(' ').append(subqueryRootAlias).append('.').append(join.getTargetName()).append(" = ");
        context.append(correlatedTableAlias).append('.').append(quoter.quotedSourceName(join));
        context.append(" AND");
    }
    // translate subquery_root_id = LHS_of_memberof
    EJBQLEquals equals = new EJBQLEquals(-1);
    EJBQLIdentificationVariable identifier = new EJBQLIdentificationVariable(-1);
    identifier.setText(subqueryId);
    equals.jjtAddChild(identifier, 0);
    equals.jjtAddChild((Node) expression.getChild(0), 1);
    equals.visit(this);
    context.append(")");
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) EJBQLIdentificationVariable(org.apache.cayenne.ejbql.parser.EJBQLIdentificationVariable) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy) EJBQLPath(org.apache.cayenne.ejbql.parser.EJBQLPath) EJBQLEquals(org.apache.cayenne.ejbql.parser.EJBQLEquals)

Example 74 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship 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 75 with ObjRelationship

use of org.apache.cayenne.map.ObjRelationship 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

ObjRelationship (org.apache.cayenne.map.ObjRelationship)84 ObjEntity (org.apache.cayenne.map.ObjEntity)48 ObjAttribute (org.apache.cayenne.map.ObjAttribute)27 DbRelationship (org.apache.cayenne.map.DbRelationship)26 Test (org.junit.Test)24 DbEntity (org.apache.cayenne.map.DbEntity)18 DbAttribute (org.apache.cayenne.map.DbAttribute)15 DbJoin (org.apache.cayenne.map.DbJoin)14 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)12 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)12 ObjectId (org.apache.cayenne.ObjectId)10 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)9 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)9 ArrayList (java.util.ArrayList)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 List (java.util.List)7 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)7 DataMap (org.apache.cayenne.map.DataMap)7 HashMap (java.util.HashMap)6