Search in sources :

Example 76 with DbRelationship

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

the class TokensReverseTest method testReverses.

@Test
public void testReverses() {
    DbAttribute attr = dbAttr().build();
    DbEntity entity = dbEntity().attributes(attr).build();
    DbRelationship rel = new DbRelationship("rel");
    rel.setSourceEntity(entity);
    rel.addJoin(new DbJoin(rel, attr.getName(), "dontKnow"));
    testOneToOneReverse(factory().createAddColumnToDb(entity, attr));
    testOneToOneReverse(factory().createAddColumnToModel(entity, attr));
    testOneToOneReverse(factory().createDropColumnToDb(entity, attr));
    testOneToOneReverse(factory().createDropColumnToModel(entity, attr));
    testOneToOneReverse(factory().createAddRelationshipToDb(entity, rel));
    testOneToOneReverse(factory().createAddRelationshipToModel(entity, rel));
    testOneToOneReverse(factory().createDropRelationshipToDb(entity, rel));
    testOneToOneReverse(factory().createDropRelationshipToModel(entity, rel));
    testOneToOneReverse(factory().createCreateTableToDb(entity));
    testOneToOneReverse(factory().createCreateTableToModel(entity));
    testOneToOneReverse(factory().createDropTableToDb(entity));
    testOneToOneReverse(factory().createDropTableToModel(entity));
    testOneToOneReverse(factory().createSetAllowNullToDb(entity, attr));
    testOneToOneReverse(factory().createSetAllowNullToModel(entity, attr));
    testOneToOneReverse(factory().createSetNotNullToDb(entity, attr));
    testOneToOneReverse(factory().createSetNotNullToModel(entity, attr));
    DbAttribute attr2 = dbAttr().build();
    testOneToOneReverse(factory().createSetColumnTypeToDb(entity, attr, attr2));
    testOneToOneReverse(factory().createSetColumnTypeToModel(entity, attr, attr2));
    testOneToOneReverse(factory().createSetPrimaryKeyToDb(entity, Collections.singleton(attr), Collections.singleton(attr2), "PK"));
    testOneToOneReverse(factory().createSetPrimaryKeyToModel(entity, Collections.singleton(attr), Collections.singleton(attr2), "PK"));
    testOneToOneReverse(factory().createSetValueForNullToDb(entity, attr, new DefaultValueForNullProvider()));
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) DbJoin(org.apache.cayenne.map.DbJoin) Test(org.junit.Test)

Example 77 with DbRelationship

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

the class DbEntityMerger method createTokensForMissingImported.

/**
 * Generate Create Table in model token
 * @param original DbEntity found in model but not found in DB
 */
@Override
Collection<MergerToken> createTokensForMissingImported(DbEntity original) {
    Collection<MergerToken> tokens = new LinkedList<>();
    // add entity
    tokens.add(getTokenFactory().createCreateTableToDb(original));
    // add it's relationships
    for (DbRelationship rel : original.getRelationships()) {
        tokens.add(getTokenFactory().createAddRelationshipToDb(original, rel));
    }
    return tokens;
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) LinkedList(java.util.LinkedList)

Example 78 with DbRelationship

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

the class DbRelationshipDictionary method filter.

/**
 * @since 4.1
 */
private Collection<DbRelationship> filter() {
    if (filtersConfig == null) {
        return container.getRelationships();
    }
    Collection<DbRelationship> existingFiltered = new LinkedList<>();
    TableFilter tableFilter = filtersConfig.tableFilter(container.getCatalog(), container.getSchema());
    if (tableFilter != null && tableFilter.isIncludeTable(container.getName())) {
        PatternFilter patternFilter = tableFilter.getIncludeTableColumnFilter(container.getName());
        for (DbRelationship rel : container.getRelationships()) {
            if (patternFilter.isIncluded(rel.getName())) {
                existingFiltered.add(rel);
            }
        }
    }
    return existingFiltered;
}
Also used : PatternFilter(org.apache.cayenne.dbsync.reverse.filters.PatternFilter) TableFilter(org.apache.cayenne.dbsync.reverse.filters.TableFilter) DbRelationship(org.apache.cayenne.map.DbRelationship) LinkedList(java.util.LinkedList)

Example 79 with DbRelationship

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

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

the class EJBQLPathAnaliserTranslator method processFlattenedRelationShip.

private String processFlattenedRelationShip(String subqueryRootAlias, ObjRelationship relationship) {
    QuotingStrategy quoter = context.getQuotingStrategy();
    List<DbRelationship> dbRelationships = relationship.getDbRelationships();
    // relation
    for (int i = dbRelationships.size() - 1; i > 0; i--) {
        DbRelationship dbRelationship = dbRelationships.get(i);
        String subqueryTargetTableName = quoter.quotedFullyQualifiedName((DbEntity) dbRelationship.getTargetEntity());
        String subqueryTargetAlias;
        if (i == dbRelationships.size() - 1) {
            subqueryTargetAlias = subqueryRootAlias;
            context.append(subqueryTargetTableName).append(' ').append(subqueryTargetAlias);
        } else {
            subqueryTargetAlias = context.getTableAlias(subqueryTargetTableName, subqueryTargetTableName);
        }
        context.append(" JOIN ");
        String subquerySourceTableName = quoter.quotedFullyQualifiedName((DbEntity) dbRelationship.getSourceEntity());
        String subquerySourceAlias = context.getTableAlias(subquerySourceTableName, subquerySourceTableName);
        context.append(subquerySourceTableName).append(' ').append(subquerySourceAlias);
        context.append(" ON (");
        List<DbJoin> joins = dbRelationship.getJoins();
        Iterator<DbJoin> it = joins.iterator();
        while (it.hasNext()) {
            DbJoin join = it.next();
            context.append(' ').append(subqueryTargetAlias).append('.').append(join.getTargetName()).append(" = ");
            context.append(subquerySourceAlias).append('.').append(join.getSourceName());
            if (it.hasNext()) {
                context.append(" AND");
            }
        }
        context.append(" )");
        subqueryRootAlias = subquerySourceAlias;
    }
    return subqueryRootAlias;
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Aggregations

DbRelationship (org.apache.cayenne.map.DbRelationship)106 DbEntity (org.apache.cayenne.map.DbEntity)59 DbAttribute (org.apache.cayenne.map.DbAttribute)35 DbJoin (org.apache.cayenne.map.DbJoin)35 ObjEntity (org.apache.cayenne.map.ObjEntity)30 ObjRelationship (org.apache.cayenne.map.ObjRelationship)28 ObjAttribute (org.apache.cayenne.map.ObjAttribute)20 Test (org.junit.Test)15 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)13 ArrayList (java.util.ArrayList)11 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DataMap (org.apache.cayenne.map.DataMap)9 Entity (org.apache.cayenne.map.Entity)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 HashMap (java.util.HashMap)6 ObjectId (org.apache.cayenne.ObjectId)6