Search in sources :

Example 6 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 7 with DbGenerator

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

the class SchemaBuilder method tableCreateQueries.

/**
 * Returns iterator of preprocessed table create queries.
 */
private Collection<String> tableCreateQueries(DataNode node, DataMap map) throws Exception {
    DbAdapter adapter = node.getAdapter();
    DbGenerator gen = new DbGenerator(adapter, map, null, domain, jdbcEventLogger);
    List<DbEntity> orderedEnts = dbEntitiesInInsertOrder(node, map);
    List<String> queries = new ArrayList<String>();
    // table definitions
    for (DbEntity ent : orderedEnts) {
        queries.add(adapter.createTable(ent));
    }
    // FK constraints
    for (DbEntity ent : orderedEnts) {
        if (!unitDbAdapter.supportsFKConstraints(ent)) {
            continue;
        }
        List<String> qs = gen.createConstraintsQueries(ent);
        queries.addAll(qs);
    }
    return queries;
}
Also used : UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 8 with DbGenerator

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

the class DbGenerateTask method createGenerator.

DbGenerator createGenerator(DataMap dataMap) {
    DbGenerator generator = new DbGenerator(createDbAdapter(), dataMap, NoopJdbcEventLogger.getInstance());
    generator.setShouldCreateFKConstraints(createFK);
    generator.setShouldCreatePKSupport(createPK);
    generator.setShouldCreateTables(createTables);
    generator.setShouldDropPKSupport(dropPK);
    generator.setShouldDropTables(dropTables);
    return generator;
}
Also used : DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 9 with DbGenerator

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

the class ThrowOnPartialOrCreateSchemaStrategy 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(false);
        gen.setShouldCreatePKSupport(false);
        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)

Example 10 with DbGenerator

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

the class DBGeneratorOptions method createSQL.

/**
 * Returns SQL statements generated for selected schema generation options.
 */
protected void createSQL() {
    // convert them to string representation for display
    StringBuilder buf = new StringBuilder();
    for (DbGenerator generator : generators) {
        Iterator<String> it = generator.configuredStatements().iterator();
        String batchTerminator = generator.getAdapter().getBatchTerminator();
        String lineEnd = (batchTerminator != null) ? "\n" + batchTerminator + "\n\n" : "\n\n";
        while (it.hasNext()) {
            buf.append(it.next()).append(lineEnd);
        }
    }
    textForSQL = buf.toString();
}
Also used : DbGenerator(org.apache.cayenne.access.DbGenerator)

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