Search in sources :

Example 36 with DbEntity

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

the class DbAttributeMerger method checkType.

private void checkType(DbAttribute original, DbAttribute imported, List<MergerToken> tokens) {
    if (!needUpdateType(original, imported)) {
        return;
    }
    DbEntity originalDbEntity = original.getEntity();
    tokens.add(getTokenFactory().createSetColumnTypeToDb(originalDbEntity, imported, original));
}
Also used : DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity) DbEntity(org.apache.cayenne.map.DbEntity)

Example 37 with DbEntity

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

the class DbAttributeMerger method createTokensForMissingImported.

/**
 * Add column to db
 * @param original attribute found in model but missing in db
 */
@Override
Collection<MergerToken> createTokensForMissingImported(DbAttribute original) {
    DbEntity originalDbEntity = original.getEntity();
    List<MergerToken> tokens = new LinkedList<>();
    tokens.add(getTokenFactory().createAddColumnToDb(originalDbEntity, original));
    // Create not null check
    if (original.isMandatory()) {
        if (valueForNull.hasValueFor(originalDbEntity, original)) {
            tokens.add(getTokenFactory().createSetValueForNullToDb(originalDbEntity, original, valueForNull));
        }
        tokens.add(getTokenFactory().createSetNotNullToDb(originalDbEntity, original));
    }
    if (original.isPrimaryKey() && originalDbEntity instanceof DetectedDbEntity && "VIEW".equals(((DetectedDbEntity) originalDbEntity).getType())) {
        // Views doesn't has PKs in a database, but if the user selects some PKs in a model, we put these keys.
        return null;
    }
    return tokens;
}
Also used : DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity) DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity) LinkedList(java.util.LinkedList)

Example 38 with DbEntity

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

the class DbRelationshipMerger method createTokensForMissingImported.

/**
 * @param original DbRelationship that is in model but not in db
 * @return generated tokens
 */
@Override
Collection<MergerToken> createTokensForMissingImported(DbRelationship original) {
    if (skipRelationshipsTokens) {
        return null;
    }
    DbEntity originalDbEntity = getOriginalSourceDbEntity(original);
    MergerToken token = getTokenFactory().createAddRelationshipToDb(originalDbEntity, original);
    return Collections.singleton(token);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken)

Example 39 with DbEntity

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

the class DbRelationshipMerger method createTokensForMissingOriginal.

/**
 * @param imported DbRelationship that is in db but not in model
 * @return generated tokens
 */
@Override
Collection<MergerToken> createTokensForMissingOriginal(DbRelationship imported) {
    DbEntity originalDbEntity = getOriginalSourceDbEntity(imported);
    DbEntity targetEntity = getOriginalTargetDbEntity(imported);
    if (targetEntity != null) {
        imported.setTargetEntityName(targetEntity);
    }
    imported.setSourceEntity(originalDbEntity);
    // manipulate the joins to match the DbAttributes in the model
    for (DbJoin join : imported.getJoins()) {
        DbAttribute sourceAttr = findDbAttribute(originalDbEntity, join.getSourceName());
        if (sourceAttr != null) {
            join.setSourceName(sourceAttr.getName());
        }
        DbAttribute targetAttr = findDbAttribute(targetEntity, join.getTargetName());
        if (targetAttr != null) {
            join.setTargetName(targetAttr.getName());
        }
    }
    // Add all relationships. Tokens will decide whether or not to execute
    MergerToken token = getTokenFactory().createDropRelationshipToDb(originalDbEntity, imported);
    return Collections.singleton(token);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DbJoin(org.apache.cayenne.map.DbJoin)

Example 40 with DbEntity

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

the class EntityMergeSupport method addMissingRelationship.

private void addMissingRelationship(ObjEntity entity, DbRelationship dbRelationship) {
    // getting DataMap from DbRelationship's source entity. This is the only object in our arguments that
    // is guaranteed to be a part of the map....
    DataMap dataMap = dbRelationship.getSourceEntity().getDataMap();
    DbEntity targetEntity = dbRelationship.getTargetEntity();
    Collection<ObjEntity> mappedObjEntities = dataMap.getMappedEntities(targetEntity);
    if (mappedObjEntities.isEmpty()) {
        if (targetEntity == null) {
            targetEntity = new DbEntity(dbRelationship.getTargetEntityName());
        }
        if (dbRelationship.getTargetEntityName() != null) {
            boolean needGeneratedEntity = createObjRelationship(entity, dbRelationship, nameGenerator.objEntityName(targetEntity));
            if (needGeneratedEntity) {
                LOGGER.warn("Can't find ObjEntity for " + dbRelationship.getTargetEntityName());
                LOGGER.warn("Db Relationship (" + dbRelationship + ") will have GUESSED Obj Relationship reflection. ");
            }
        }
    } else {
        for (Entity mappedTarget : mappedObjEntities) {
            createObjRelationship(entity, dbRelationship, mappedTarget.getName());
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) DbEntity(org.apache.cayenne.map.DbEntity) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11