org.apache.cayenne.access
Class DbGenerator

java.lang.Object
  extended by org.apache.cayenne.access.DbGenerator

public class DbGenerator
extends java.lang.Object

Utility class that generates database schema based on Cayenne mapping. It is a logical counterpart of DbLoader class.

Author:
Andrus Adamchik

Field Summary
protected  DbAdapter adapter
           
protected  java.util.Map createFK
           
protected  java.util.List createPK
           
protected  java.util.Map createTables
           
protected  java.util.List dbEntitiesInInsertOrder
          Contains all DbEntities ordered considering their interdependencies.
protected  java.util.List dbEntitiesRequiringAutoPK
           
protected  DataDomain domain
           
protected  java.util.List dropPK
           
protected  java.util.Map dropTables
           
protected  ValidationResult failures
           
protected  DataMap map
           
protected  boolean shouldCreateFKConstraints
           
protected  boolean shouldCreatePKSupport
           
protected  boolean shouldCreateTables
           
protected  boolean shouldDropPKSupport
           
protected  boolean shouldDropTables
           
 
Constructor Summary
DbGenerator(DbAdapter adapter, DataMap map)
          Creates and initializes new DbGenerator.
DbGenerator(DbAdapter adapter, DataMap map, java.util.Collection excludedEntities)
          Creates and initializes new DbGenerator instance.
DbGenerator(DbAdapter adapter, DataMap map, java.util.Collection excludedEntities, DataDomain domain)
          Creates and initializes new DbGenerator instance.
 
Method Summary
protected  void buildStatements()
          Creates and stores internally a set of statements for database schema creation, ignoring configured schema creation preferences.
 java.util.List configuredStatements()
          Returns a list of all schema statements that should be executed with the current configuration.
 java.util.List createFkConstraintsQueries(DbEntity dbEnt)
          Returns an array of queries to create foreign key constraints for a particular DbEntity.
 DbAdapter getAdapter()
          Returns DbAdapter associated with this DbGenerator.
 DataDomain getDomain()
          Returns a DataDomain used by the DbGenerator to detect cross-database relationships.
 ValidationResult getFailures()
          Returns an object representing a collection of failures that occurred on the last "runGenerator" invocation, or null if there were no failures.
 boolean isEmpty(boolean respectConfiguredSettings)
          Returns true if there is nothing to be done by this generator.
protected  void resetToDefaults()
           
 void runGenerator(javax.sql.DataSource ds)
          Executes a set of commands to drop/create database objects.
 void runGenerator(DataSourceInfo dsi)
          Creates a temporary DataSource out of DataSourceInfo and invokes public void runGenerator(DataSource ds).
protected  boolean safeExecute(java.sql.Connection connection, java.lang.String sql)
          Builds and executes a SQL statement, catching and storing SQL exceptions resulting from invalid SQL.
 void setShouldCreateFKConstraints(boolean shouldCreateFKConstraints)
           
 void setShouldCreatePKSupport(boolean shouldCreatePKSupport)
           
 void setShouldCreateTables(boolean shouldCreateTables)
           
 void setShouldDropPKSupport(boolean shouldDropPKSupport)
           
 void setShouldDropTables(boolean shouldDropTables)
           
 boolean shouldCreateFKConstraints()
           
 boolean shouldCreatePKSupport()
          Returns whether DbGenerator is configured to create primary key support for DataMap entities.
 boolean shouldCreateTables()
          Returns whether DbGenerator is configured to create tables for DataMap entities.
 boolean shouldDropPKSupport()
           
 boolean shouldDropTables()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

adapter

protected DbAdapter adapter

map

protected DataMap map

domain

protected DataDomain domain

dropTables

protected java.util.Map dropTables

createTables

protected java.util.Map createTables

createFK

protected java.util.Map createFK

createPK

protected java.util.List createPK

dropPK

protected java.util.List dropPK

dbEntitiesInInsertOrder

protected java.util.List dbEntitiesInInsertOrder
Contains all DbEntities ordered considering their interdependencies. DerivedDbEntities are filtered out of this list.


dbEntitiesRequiringAutoPK

protected java.util.List dbEntitiesRequiringAutoPK

shouldDropTables

protected boolean shouldDropTables

shouldCreateTables

protected boolean shouldCreateTables

shouldDropPKSupport

protected boolean shouldDropPKSupport

shouldCreatePKSupport

protected boolean shouldCreatePKSupport

shouldCreateFKConstraints

protected boolean shouldCreateFKConstraints

failures

protected ValidationResult failures
Constructor Detail

DbGenerator

public DbGenerator(DbAdapter adapter,
                   DataMap map)
Creates and initializes new DbGenerator.


DbGenerator

public DbGenerator(DbAdapter adapter,
                   DataMap map,
                   java.util.Collection excludedEntities)
Creates and initializes new DbGenerator instance.

Parameters:
adapter - DbAdapter corresponding to the database
map - DataMap whose entities will be used in schema generation
excludedEntities - entities that should be ignored during schema generation

DbGenerator

public DbGenerator(DbAdapter adapter,
                   DataMap map,
                   java.util.Collection excludedEntities,
                   DataDomain domain)
Creates and initializes new DbGenerator instance.

Parameters:
adapter - DbAdapter corresponding to the database
map - DataMap whose entities will be used in schema generation
excludedEntities - entities that should be ignored during schema generation
domain - optional DataDomain used to detect cross-database relationships.
Since:
1.2
Method Detail

resetToDefaults

protected void resetToDefaults()

buildStatements

protected void buildStatements()
Creates and stores internally a set of statements for database schema creation, ignoring configured schema creation preferences. Statements are NOT executed in this method.


isEmpty

public boolean isEmpty(boolean respectConfiguredSettings)
Returns true if there is nothing to be done by this generator. If respectConfiguredSettings is true, checks are done applying currently configured settings, otherwise check is done, assuming that all possible generated objects.


getAdapter

public DbAdapter getAdapter()
Returns DbAdapter associated with this DbGenerator.


configuredStatements

public java.util.List configuredStatements()
Returns a list of all schema statements that should be executed with the current configuration.


runGenerator

public void runGenerator(DataSourceInfo dsi)
                  throws java.lang.Exception
Creates a temporary DataSource out of DataSourceInfo and invokes public void runGenerator(DataSource ds).

Throws:
java.lang.Exception

runGenerator

public void runGenerator(javax.sql.DataSource ds)
                  throws java.lang.Exception
Executes a set of commands to drop/create database objects. This is the main worker method of DbGenerator. Command set is built based on pre-configured generator settings.

Throws:
java.lang.Exception

safeExecute

protected boolean safeExecute(java.sql.Connection connection,
                              java.lang.String sql)
                       throws java.sql.SQLException
Builds and executes a SQL statement, catching and storing SQL exceptions resulting from invalid SQL. Only non-recoverable exceptions are rethrown.

Throws:
java.sql.SQLException
Since:
1.1

createFkConstraintsQueries

public java.util.List createFkConstraintsQueries(DbEntity dbEnt)
Returns an array of queries to create foreign key constraints for a particular DbEntity. Throws CayenneRuntimeException, if called for adapter that does not support FK constraints.


getFailures

public ValidationResult getFailures()
Returns an object representing a collection of failures that occurred on the last "runGenerator" invocation, or null if there were no failures. Failures usually indicate problems with generated DDL (such as "create...", "drop...", etc.) and usually happen due to the DataMap being out of sync with the database.

Since:
1.1

shouldCreatePKSupport

public boolean shouldCreatePKSupport()
Returns whether DbGenerator is configured to create primary key support for DataMap entities.


shouldCreateTables

public boolean shouldCreateTables()
Returns whether DbGenerator is configured to create tables for DataMap entities.


shouldDropPKSupport

public boolean shouldDropPKSupport()

shouldDropTables

public boolean shouldDropTables()

shouldCreateFKConstraints

public boolean shouldCreateFKConstraints()

setShouldCreatePKSupport

public void setShouldCreatePKSupport(boolean shouldCreatePKSupport)

setShouldCreateTables

public void setShouldCreateTables(boolean shouldCreateTables)

setShouldDropPKSupport

public void setShouldDropPKSupport(boolean shouldDropPKSupport)

setShouldDropTables

public void setShouldDropTables(boolean shouldDropTables)

setShouldCreateFKConstraints

public void setShouldCreateFKConstraints(boolean shouldCreateFKConstraints)

getDomain

public DataDomain getDomain()
Returns a DataDomain used by the DbGenerator to detect cross-database relationships. By default DataDomain is null.

Since:
1.2


Copyright © 2001-2006 Apache.org All Rights Reserved.