Search in sources :

Example 1 with DefaultObjectNameGenerator

use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.

the class DefaultDbImportActionTest method testImportWithFieldChanged.

@Test
public void testImportWithFieldChanged() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.getTargetDataMap()).thenReturn(FILE_STUB);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap dataMap = new DataMap();
            new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("NAME_01").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME")));
            return dataMap;
        }
    };
    final boolean[] haveWeTriedToSave = { false };
    DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.<ProjectExtension>emptyList()) {

        @Override
        public void save(Project project) {
            haveWeTriedToSave[0] = true;
            // Validation phase
            DataMap rootNode = (DataMap) project.getRootNode();
            assertEquals(1, rootNode.getObjEntities().size());
            assertEquals(1, rootNode.getDbEntityMap().size());
            DbEntity entity = rootNode.getDbEntity("ARTGROUP");
            assertNotNull(entity);
            assertEquals(4, entity.getAttributes().size());
            assertNotNull(entity.getAttribute("NAME_01"));
        }
    }, new DataMapLoader() {

        @Override
        public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
            return new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME"))).build();
        }
    }, dbLoader);
    action.execute(config);
    assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
Also used : FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Resource(org.apache.cayenne.resource.Resource) URLResource(org.apache.cayenne.resource.URLResource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) DbEntity(org.apache.cayenne.map.DbEntity) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) Test(org.junit.Test)

Example 2 with DefaultObjectNameGenerator

use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.

the class ManyToManyCandidateEntityTest method testOptimisationForManyToManyEntity.

@Test
public void testOptimisationForManyToManyEntity() {
    ObjEntity manyToManyEntity = map.getObjEntity("Table1Table2");
    ManyToManyCandidateEntity.build(manyToManyEntity).optimizeRelationships(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    ObjEntity table1Entity = map.getObjEntity("Table1");
    ObjEntity table2Entity = map.getObjEntity("Table2");
    assertEquals(1, table1Entity.getRelationships().size());
    assertEquals(table2Entity, new ArrayList<Relationship>(table1Entity.getRelationships()).get(0).getTargetEntity());
    assertEquals(1, table2Entity.getRelationships().size());
    assertEquals(table1Entity, new ArrayList<Relationship>(table2Entity.getRelationships()).get(0).getTargetEntity());
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) ObjEntity(org.apache.cayenne.map.ObjEntity) Relationship(org.apache.cayenne.map.Relationship) Test(org.junit.Test)

Example 3 with DefaultObjectNameGenerator

use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.

the class MergerOptions method prepareMigrator.

/**
 * check database and create the {@link List} of {@link MergerToken}s
 */
protected void prepareMigrator() {
    try {
        adapter = connectionInfo.makeAdapter(getApplication().getClassLoadingService());
        MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
        tokens.setMergerTokenFactory(mergerTokenFactory);
        FiltersConfig filters = FiltersConfig.create(defaultCatalog, defaultSchema, TableFilter.everything(), PatternFilter.INCLUDE_NOTHING);
        DataMapMerger merger = DataMapMerger.builder(mergerTokenFactory).filters(filters).build();
        DbLoaderConfiguration config = new DbLoaderConfiguration();
        config.setFiltersConfig(filters);
        DataSource dataSource = connectionInfo.makeDataSource(getApplication().getClassLoadingService());
        DataMap dbImport;
        try (Connection conn = dataSource.getConnection()) {
            dbImport = new DbLoader(adapter, conn, config, new LoggingDbLoaderDelegate(LoggerFactory.getLogger(DbLoader.class)), new DefaultObjectNameGenerator(NoStemStemmer.getInstance())).load();
        } catch (SQLException e) {
            throw new CayenneRuntimeException("Can't doLoad dataMap from db.", e);
        }
        tokens.setTokens(merger.createMergeTokens(dataMap, dbImport));
    } catch (Exception ex) {
        reportError("Error loading adapter", ex);
    }
}
Also used : LoggingDbLoaderDelegate(org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataSource(javax.sql.DataSource) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) DataMapMerger(org.apache.cayenne.dbsync.merge.DataMapMerger) FiltersConfig(org.apache.cayenne.dbsync.reverse.filters.FiltersConfig) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader)

Example 4 with DefaultObjectNameGenerator

use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.

the class DefaultDbImportActionTest method testNewDataMapImport.

@Test
public void testNewDataMapImport() throws Exception {
    DbImportConfiguration config = mock(DbImportConfiguration.class);
    when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
    when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
    when(config.getTargetDataMap()).thenReturn(new File("xyz.map.xml"));
    when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
    when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
    DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {

        @Override
        public DataMap load() throws SQLException {
            DataMap map = new DataMap();
            new DataMapBuilder(map).withDbEntities(2).build();
            return map;
        }
    };
    final boolean[] haveWeTriedToSave = { false };
    DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.emptyList()) {

        @Override
        public void save(Project project) {
            haveWeTriedToSave[0] = true;
            // Validation phase
            assertTrue(project.getRootNode() instanceof DataMap);
        }
    }, null, dbLoader);
    action.execute(config);
    assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
Also used : FileProjectSaver(org.apache.cayenne.project.FileProjectSaver) DefaultModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) Project(org.apache.cayenne.project.Project) DataMapBuilder(org.apache.cayenne.dbsync.merge.builders.DataMapBuilder) File(java.io.File) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader) Test(org.junit.Test)

Example 5 with DefaultObjectNameGenerator

use of org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator in project cayenne by apache.

the class RelationshipsLoaderIT method testRelationshipLoad.

@Test
public void testRelationshipLoad() throws Exception {
    boolean supportsFK = accessStackAdapter.supportsFKConstraints();
    if (!supportsFK) {
        return;
    }
    DatabaseMetaData metaData = connection.getMetaData();
    DbLoaderDelegate delegate = new DefaultDbLoaderDelegate();
    // We need all data to check relationships, so simply load it all
    EntityLoader entityLoader = new EntityLoader(adapter, EMPTY_CONFIG, delegate);
    AttributeLoader attributeLoader = new AttributeLoader(adapter, EMPTY_CONFIG, delegate);
    PrimaryKeyLoader primaryKeyLoader = new PrimaryKeyLoader(EMPTY_CONFIG, delegate);
    ExportedKeyLoader exportedKeyLoader = new ExportedKeyLoader(EMPTY_CONFIG, delegate);
    entityLoader.load(metaData, store);
    attributeLoader.load(metaData, store);
    primaryKeyLoader.load(metaData, store);
    exportedKeyLoader.load(metaData, store);
    // *** TESTING THIS ***
    RelationshipLoader relationshipLoader = new RelationshipLoader(EMPTY_CONFIG, delegate, new DefaultObjectNameGenerator());
    relationshipLoader.load(metaData, store);
    Collection<DbRelationship> rels = getDbEntity("ARTIST").getRelationships();
    assertNotNull(rels);
    assertTrue(!rels.isEmpty());
    // test one-to-one
    rels = getDbEntity("PAINTING").getRelationships();
    assertNotNull(rels);
    // find relationship to PAINTING_INFO
    DbRelationship oneToOne = null;
    for (DbRelationship rel : rels) {
        if ("PAINTING_INFO".equalsIgnoreCase(rel.getTargetEntityName())) {
            oneToOne = rel;
            break;
        }
    }
    assertNotNull("No relationship to PAINTING_INFO", oneToOne);
    assertFalse("Relationship to PAINTING_INFO must be to-one", oneToOne.isToMany());
    assertTrue("Relationship to PAINTING_INFO must be to-one", oneToOne.isToDependentPK());
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) DbRelationship(org.apache.cayenne.map.DbRelationship) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Aggregations

DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)8 DataMap (org.apache.cayenne.map.DataMap)5 Test (org.junit.Test)5 DbLoader (org.apache.cayenne.dbsync.reverse.dbload.DbLoader)4 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)4 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)3 DbEntity (org.apache.cayenne.map.DbEntity)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DataMapBuilder (org.apache.cayenne.dbsync.merge.builders.DataMapBuilder)2 EntityMergeSupport (org.apache.cayenne.dbsync.merge.context.EntityMergeSupport)2 DefaultModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.DefaultModelMergeDelegate)2 LoggingDbLoaderDelegate (org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate)2 FiltersConfig (org.apache.cayenne.dbsync.reverse.filters.FiltersConfig)2 DbRelationship (org.apache.cayenne.map.DbRelationship)2 FileProjectSaver (org.apache.cayenne.project.FileProjectSaver)2 Project (org.apache.cayenne.project.Project)2 File (java.io.File)1 IOException (java.io.IOException)1