Search in sources :

Example 6 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken 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 7 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken 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 8 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken 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 9 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class DefaultDbImportAction method execute.

@Override
public void execute(DbImportConfiguration config) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("DB connection: " + config.getDataSourceInfo());
        logger.debug(String.valueOf(config));
    }
    boolean hasChanges = false;
    DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
    DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
    DbAdapter adapter = adapterFactory.createAdapter(dataNodeDescriptor, dataSource);
    DataMap sourceDataMap;
    try (Connection connection = dataSource.getConnection()) {
        sourceDataMap = load(config, adapter, connection);
    }
    DataMap targetDataMap = existingTargetMap(config);
    if (targetDataMap == null) {
        String path = config.getTargetDataMap() == null ? "null" : config.getTargetDataMap().getAbsolutePath() + "'";
        logger.info("");
        logger.info("Map file does not exist. Loaded db model will be saved into '" + path);
        hasChanges = true;
        targetDataMap = newTargetDataMap(config);
    }
    // transform source DataMap before merging
    transformSourceBeforeMerge(sourceDataMap, targetDataMap, config);
    MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
    DbLoaderConfiguration loaderConfig = config.getDbLoaderConfig();
    Collection<MergerToken> tokens = DataMapMerger.builder(mergerTokenFactory).filters(loaderConfig.getFiltersConfig()).skipPKTokens(loaderConfig.isSkipPrimaryKeyLoading()).skipRelationshipsTokens(loaderConfig.isSkipRelationshipsLoading()).build().createMergeTokens(targetDataMap, sourceDataMap);
    tokens = log(sort(reverse(mergerTokenFactory, tokens)));
    hasChanges |= syncDataMapProperties(targetDataMap, config);
    hasChanges |= applyTokens(targetDataMap, tokens, config);
    hasChanges |= syncProcedures(targetDataMap, sourceDataMap, loaderConfig.getFiltersConfig());
    if (hasChanges) {
        saveLoaded(targetDataMap, config);
    }
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) Connection(java.sql.Connection) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) MergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory) DataSource(javax.sql.DataSource) DataMap(org.apache.cayenne.map.DataMap) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)

Example 10 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class DataMapMergerTest method testAddRelationship1.

@Test
public void testAddRelationship1() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr01").join("rel1", "table1.attr01", "table2.attr03").build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr02").join("rel1", "table1.attr01", "table2.attr03").build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(2, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
    entity = db.getDbEntity("table1");
    assertEquals(factory().createAddRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)41 Test (org.junit.Test)25 DbEntity (org.apache.cayenne.map.DbEntity)20 DataMap (org.apache.cayenne.map.DataMap)17 DbAttribute (org.apache.cayenne.map.DbAttribute)10 ObjEntity (org.apache.cayenne.map.ObjEntity)8 LinkedList (java.util.LinkedList)5 ObjAttribute (org.apache.cayenne.map.ObjAttribute)5 SetColumnTypeToDb (org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb)4 DbJoin (org.apache.cayenne.map.DbJoin)3 DbRelationship (org.apache.cayenne.map.DbRelationship)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2 MergerContext (org.apache.cayenne.dbsync.merge.context.MergerContext)2 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 ArrayList (java.util.ArrayList)1 AbstractTableModel (javax.swing.table.AbstractTableModel)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1