# 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 Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected DbAdapter`
  `adapter`
  `protected Map<String, List<String>>`
  `createConstraints`
  `protected List<String>`
  `createPK`
  `protected Map<String,String>`
  `createTables`
  `protected List<DbEntity>`
  `dbEntitiesInInsertOrder`
  Contains all DbEntities ordered considering their interdependencies.
  `protected List<DbEntity>`
  `dbEntitiesRequiringAutoPK`
  `protected DataDomain`
  `domain`
  `protected List<String>`
  `dropPK`
  `protected Map<String, Collection<String>>`
  `dropTables`
  `protected ValidationResult`
  `failures`
  `protected DataMap`
  `map`
  `protected boolean`
  `shouldCreateFKConstraints`
  `protected boolean`
  `shouldCreatePKSupport`
  `protected boolean`
  `shouldCreateTables`
  `protected boolean`
  `shouldDropPKSupport`
  `protected boolean`
  `shouldDropTables`
  `protected SQLLogger`
  `sqlLogger`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DbGenerator(DbAdapter adapter, DataMap map, Collection<DbEntity> excludedEntities, DataDomain domain, SQLLogger logger)`
  Creates and initializes new DbGenerator instance.
  `DbGenerator(DbAdapter adapter, DataMap map, SQLLogger logger)`
  `DbGenerator(DbAdapter adapter, DataMap map, SQLLogger logger, Collection<DbEntity> excludedEntities)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `buildStatements()`
  Creates and stores internally a set of statements for database schema creation, ignoring configured schema creation preferences.
  `List<String>`
  `configuredStatements()`
  Returns a list of all schema statements that should be executed with the current configuration.
  `List<String>`
  `createConstraintsQueries(DbEntity table)`
  Creates FK and UNIQUE constraint statements for a given table.
  `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(DataSource ds)`
  Executes a set of commands to drop/create database objects.
  `protected boolean`
  `safeExecute(Connection connection, 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 Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### adapter

    protected DbAdapter adapter

  - ### map

    protected DataMap map

  - ### domain

    protected DataDomain domain

  - ### sqlLogger

    protected SQLLogger sqlLogger

  - ### 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, SQLLogger logger)
    Since:
      3.1

  - ### DbGenerator

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

  - ### DbGenerator

    public DbGenerator(DbAdapter adapter, DataMap map, Collection<DbEntity> excludedEntities, DataDomain domain, SQLLogger 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(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)
    Builds and executes a SQL statement, catching and storing SQL exceptions resulting from invalid SQL. Only non-recoverable exceptions are rethrown.
    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