Search in sources :

Example 1 with DbGenerator

use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.

the class DbGeneratorTask method execute.

@Override
public void execute() {
    Logger logger = new AntLogger(this);
    log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver, url, userName), Project.MSG_VERBOSE);
    log(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
    validateAttributes();
    ClassLoader loader = null;
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
    try {
        loader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
        // Load the data map and run the db generator.
        DataMap dataMap = loadDataMap(injector);
        // load driver taking custom CLASSPATH into account...
        DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, userName, password);
        DbAdapter adapter = getAdapter(injector, dataSource);
        DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>emptyList(), null, NoopJdbcEventLogger.getInstance());
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
        generator.runGenerator(dataSource);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        log(message, Project.MSG_ERR);
        throw new BuildException(message, th);
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
        injector.shutdown();
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) Logger(org.slf4j.Logger) NoopJdbcEventLogger(org.apache.cayenne.log.NoopJdbcEventLogger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) BuildException(org.apache.tools.ant.BuildException) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 2 with DbGenerator

use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.

the class DbGenerateTask method generateDb.

@TaskAction
public void generateDb() throws GradleException {
    dataSource.validate();
    getLogger().info("connection settings - [driver: {}, url: {}, username: {}]", dataSource.getDriver(), dataSource.getUrl(), dataSource.getUsername());
    getLogger().info("generator options - " + "[dropTables: {}, dropPK: {}, createTables: {}, createPK: {}, createFK: {}]", dropTables, dropPK, createTables, createPK, createFK);
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(getLogger()));
    try {
        DbGenerator generator = createGenerator(loadDataMap(injector));
        generator.runGenerator(createDataSource());
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        getLogger().error(message);
        throw new GradleException(message, th);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) GradleException(org.gradle.api.GradleException) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) DbGenerator(org.apache.cayenne.access.DbGenerator) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) GradleException(org.gradle.api.GradleException) TaskAction(org.gradle.api.tasks.TaskAction)

Example 3 with DbGenerator

use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.

the class DBGeneratorOptions method prepareGenerator.

/**
 * Creates new internal DbGenerator instance.
 */
protected void prepareGenerator() {
    try {
        DbAdapter adapter = connectionInfo.makeAdapter(getApplication().getClassLoadingService());
        generators = new ArrayList<>();
        for (DataMap dataMap : dataMaps) {
            this.generators.add(new DbGenerator(adapter, dataMap, tables.getExcludedTables(), null, NoopJdbcEventLogger.getInstance()));
        }
    } catch (Exception ex) {
        reportError("Error loading adapter", ex);
    }
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbGenerator(org.apache.cayenne.access.DbGenerator) IOException(java.io.IOException) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with DbGenerator

use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.

the class DBGeneratorDefaults method configureGenerator.

/**
 * Updates DbGenerator settings, consulting its own state.
 */
public void configureGenerator(Collection<DbGenerator> generators) {
    setCreateFK(createFK);
    setCreatePK(createPK);
    setCreateTables(createTables);
    setDropPK(dropPK);
    setDropTables(dropTables);
    for (DbGenerator generator : generators) {
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
    }
}
Also used : DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 5 with DbGenerator

use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.

the class CreateIfNoSchemaStrategy method generate.

private void generate(DataNode dataNode) {
    Collection<DataMap> map = dataNode.getDataMaps();
    Iterator<DataMap> iterator = map.iterator();
    while (iterator.hasNext()) {
        DbGenerator gen = new DbGenerator(dataNode.getAdapter(), iterator.next(), dataNode.getJdbcEventLogger());
        gen.setShouldCreateTables(true);
        gen.setShouldDropTables(false);
        gen.setShouldCreateFKConstraints(true);
        gen.setShouldCreatePKSupport(true);
        gen.setShouldDropPKSupport(false);
        try {
            gen.runGenerator(dataNode.getDataSource());
        } catch (Exception e) {
            throw new CayenneRuntimeException(e);
        }
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbGenerator(org.apache.cayenne.access.DbGenerator) SQLException(java.sql.SQLException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

DbGenerator (org.apache.cayenne.access.DbGenerator)11 DataMap (org.apache.cayenne.map.DataMap)5 DbAdapter (org.apache.cayenne.dba.DbAdapter)4 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)3 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)3 Injector (org.apache.cayenne.di.Injector)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 DriverDataSource (org.apache.cayenne.datasource.DriverDataSource)2 NoopJdbcEventLogger (org.apache.cayenne.log.NoopJdbcEventLogger)2 Logger (org.slf4j.Logger)2 IOException (java.io.IOException)1 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)1 DbEntity (org.apache.cayenne.map.DbEntity)1 ValidationResultBrowser (org.apache.cayenne.modeler.dialog.ValidationResultBrowser)1 DataSourceWizard (org.apache.cayenne.modeler.dialog.db.DataSourceWizard)1 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)1 ValidationResult (org.apache.cayenne.validation.ValidationResult)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1