Package org.apache.cayenne.access
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 SummaryFields Modifier and Type Field Description protected DbAdapteradapterprotected Map<String,List<String>>createConstraintsprotected List<String>createPKprotected Map<String,String>createTablesprotected List<DbEntity>dbEntitiesInInsertOrderContains all DbEntities ordered considering their interdependencies.protected List<DbEntity>dbEntitiesRequiringAutoPKprotected DataDomaindomainprotected List<String>dropPKprotected Map<String,Collection<String>>dropTablesprotected ValidationResultfailuresprotected JdbcEventLoggerjdbcEventLoggerprotected DataMapmapprotected booleanshouldCreateFKConstraintsprotected booleanshouldCreatePKSupportprotected booleanshouldCreateTablesprotected booleanshouldDropPKSupportprotected booleanshouldDropTables
- 
Constructor SummaryConstructors Constructor Description DbGenerator(DbAdapter adapter, DataMap map, Collection<DbEntity> excludedEntities, DataDomain domain, JdbcEventLogger logger)Creates and initializes new DbGenerator instance.DbGenerator(DbAdapter adapter, DataMap map, JdbcEventLogger logger)DbGenerator(DbAdapter adapter, DataMap map, JdbcEventLogger logger, Collection<DbEntity> excludedEntities)
- 
Method SummaryModifier and Type Method Description protected voidbuildStatements()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.DbAdaptergetAdapter()Returns DbAdapter associated with this DbGenerator.DataDomaingetDomain()Returns a DataDomain used by the DbGenerator to detect cross-database relationships.ValidationResultgetFailures()Returns an object representing a collection of failures that occurred on the last "runGenerator" invocation, or null if there were no failures.booleanisEmpty(boolean respectConfiguredSettings)Returnstrueif there is nothing to be done by this generator.protected voidresetToDefaults()voidrunGenerator(DataSource ds)Executes a set of commands to drop/create database objects.voidrunGenerator(DataSourceInfo dsi)Creates a temporary DataSource out of DataSourceInfo and invokespublic void runGenerator(DataSource ds).protected booleansafeExecute(Connection connection, String sql)Builds and executes a SQL statement, catching and storing SQL exceptions resulting from invalid SQL.voidsetShouldCreateFKConstraints(boolean shouldCreateFKConstraints)voidsetShouldCreatePKSupport(boolean shouldCreatePKSupport)voidsetShouldCreateTables(boolean shouldCreateTables)voidsetShouldDropPKSupport(boolean shouldDropPKSupport)voidsetShouldDropTables(boolean shouldDropTables)booleanshouldCreateFKConstraints()booleanshouldCreatePKSupport()Returns whether DbGenerator is configured to create primary key support for DataMap entities.booleanshouldCreateTables()Returns whether DbGenerator is configured to create tables for DataMap entities.booleanshouldDropPKSupport()booleanshouldDropTables()
- 
Field Details- 
adapter
- 
map
- 
domain
- 
jdbcEventLogger
- 
dropTables
- 
createTables
- 
createConstraints
- 
createPK
- 
dropPK
- 
dbEntitiesInInsertOrderContains all DbEntities ordered considering their interdependencies. DerivedDbEntities are filtered out of this list.
- 
dbEntitiesRequiringAutoPK
- 
shouldDropTablesprotected boolean shouldDropTables
- 
shouldCreateTablesprotected boolean shouldCreateTables
- 
shouldDropPKSupportprotected boolean shouldDropPKSupport
- 
shouldCreatePKSupportprotected boolean shouldCreatePKSupport
- 
shouldCreateFKConstraintsprotected boolean shouldCreateFKConstraints
- 
failures
 
- 
- 
Constructor Details- 
DbGenerator- Since:
- 3.1
 
- 
DbGeneratorpublic DbGenerator(DbAdapter adapter, DataMap map, JdbcEventLogger logger, Collection<DbEntity> excludedEntities)- Since:
- 3.1
 
- 
DbGeneratorpublic 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- 
resetToDefaultsprotected void resetToDefaults()
- 
buildStatementsprotected 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.
- 
isEmptypublic boolean isEmpty(boolean respectConfiguredSettings)Returnstrueif there is nothing to be done by this generator. IfrespectConfiguredSettingsistrue, checks are done applying currently configured settings, otherwise check is done, assuming that all possible generated objects.
- 
getAdapterReturns DbAdapter associated with this DbGenerator.
- 
configuredStatementsReturns a list of all schema statements that should be executed with the current configuration.
- 
runGeneratorCreates a temporary DataSource out of DataSourceInfo and invokespublic void runGenerator(DataSource ds).- Throws:
- Exception
 
- 
runGeneratorExecutes 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
 
- 
safeExecuteBuilds 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
 
- 
createConstraintsQueriesCreates FK and UNIQUE constraint statements for a given table.- Since:
- 3.0
 
- 
getFailuresReturns 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
 
- 
shouldCreatePKSupportpublic boolean shouldCreatePKSupport()Returns whether DbGenerator is configured to create primary key support for DataMap entities.
- 
shouldCreateTablespublic boolean shouldCreateTables()Returns whether DbGenerator is configured to create tables for DataMap entities.
- 
shouldDropPKSupportpublic boolean shouldDropPKSupport()
- 
shouldDropTablespublic boolean shouldDropTables()
- 
shouldCreateFKConstraintspublic boolean shouldCreateFKConstraints()
- 
setShouldCreatePKSupportpublic void setShouldCreatePKSupport(boolean shouldCreatePKSupport)
- 
setShouldCreateTablespublic void setShouldCreateTables(boolean shouldCreateTables)
- 
setShouldDropPKSupportpublic void setShouldDropPKSupport(boolean shouldDropPKSupport)
- 
setShouldDropTablespublic void setShouldDropTables(boolean shouldDropTables)
- 
setShouldCreateFKConstraintspublic void setShouldCreateFKConstraints(boolean shouldCreateFKConstraints)
- 
getDomainReturns a DataDomain used by the DbGenerator to detect cross-database relationships. By default DataDomain is null.- Since:
- 1.2
 
 
-