Class DbGenerator

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

public class DbGenerator extends Object
Utility class that generates database schema based on Cayenne mapping. It is a logical counterpart of DbLoader class.
  • Field Details

    • adapter

      protected DbAdapter adapter
    • map

      protected DataMap map
    • domain

      protected DataDomain domain
    • jdbcEventLogger

      protected JdbcEventLogger jdbcEventLogger
    • dropTables

      protected Map<String,Collection<String>> dropTables
    • createTables

      protected Map<String,String> createTables
    • createConstraints

      protected Map<String,List<String>> createConstraints
    • createPK

      protected List<String> createPK
    • dropPK

      protected List<String> dropPK
    • dbEntitiesInInsertOrder

      protected List<DbEntity> dbEntitiesInInsertOrder
      Contains all DbEntities ordered considering their interdependencies. DerivedDbEntities are filtered out of this list.
    • dbEntitiesRequiringAutoPK

      protected List<DbEntity> 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 Details

    • DbGenerator

      public DbGenerator(DbAdapter adapter, DataMap map, JdbcEventLogger logger)
      Since:
      3.1
    • DbGenerator

      public DbGenerator(DbAdapter adapter, DataMap map, JdbcEventLogger logger, Collection<DbEntity> excludedEntities)
      Since:
      3.1
    • DbGenerator

      public DbGenerator(DbAdapter adapter, DataMap map, Collection<DbEntity> excludedEntities, DataDomain domain, JdbcEventLogger logger)
      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:
      3.1
  • Method Details

    • 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 List<String> configuredStatements()
      Returns a list of all schema statements that should be executed with the current configuration.
    • runGenerator

      public void runGenerator(DataSourceInfo dsi) throws Exception
      Creates a temporary DataSource out of DataSourceInfo and invokes public void runGenerator(DataSource ds).
      Throws:
      Exception
    • runGenerator

      public void runGenerator(DataSource ds) throws 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:
      Exception
    • safeExecute

      protected boolean safeExecute(Connection connection, String sql) throws SQLException
      Builds and executes a SQL statement, catching and storing SQL exceptions resulting from invalid SQL. Only non-recoverable exceptions are rethrown.
      Throws:
      SQLException
      Since:
      1.1
    • createConstraintsQueries

      public List<String> createConstraintsQueries(DbEntity table)
      Creates FK and UNIQUE constraint statements for a given table.
      Since:
      3.0
    • 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