Package org.ofbiz.core.entity.jdbc
Class DatabaseUtil
java.lang.Object
org.ofbiz.core.entity.jdbc.DatabaseUtil
Utilities for Entity Database Maintenance
- Since:
- 2.0
- Version:
- $Revision: 1.3 $
- Author:
- David E. Jones
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final DatasourceInfoprotected final Stringprotected final ModelFieldTypeReaderstatic final Stringprotected final SqlEscapeHelper -
Constructor Summary
ConstructorsConstructorDescriptionDatabaseUtil(String helperName) Constructs with the name of a helper that is used to loadDatasourceInfofromEntityConfigUtiland uses the staticConnectionFactoryfor connections. -
Method Summary
Modifier and TypeMethodDescriptionaddColumn(ModelEntity entity, ModelField field) addVirtualColumn(ModelEntity entity, ModelFunctionBasedIndex index) voidcheckDb(Map<String, ? extends ModelEntity> modelEntities, Collection<String> messages, boolean addMissing) Does a gzillion things to upgrade the database to the entitymodel by adding tables etc.voidcheckDb(Map<String, ? extends ModelEntity> modelEntities, Collection<String> messages, boolean addMissing, boolean promote, boolean widen) Does a gzillion things to upgrade the database to the entitymodel by adding tables etc.createDeclaredIndex(ModelEntity entity, ModelIndex modelIndex) createDeclaredIndices(ModelEntity entity) Creates a database index for every declared index on the given entity.createForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) createForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) createForeignKeyIndices(ModelEntity entity, int constraintNameClipLength) createForeignKeys(ModelEntity entity, Map<String, ? extends ModelEntity> modelEntities, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) createFunctionBasedIndex(ModelEntity entity, ModelFunctionBasedIndex fbIndex) Creates a function based index for every declared index on the given entity.createTable(ModelEntity entity, Map<String, ? extends ModelEntity> modelEntities, boolean addFks, boolean usePkConstraintNames, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) deleteDeclaredIndex(ModelEntity entity, ModelIndex modelIndex) deleteDeclaredIndices(ModelEntity entity) deleteForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength) deleteForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) deleteForeignKeyIndices(ModelEntity entity, int constraintNameClipLength) deleteForeignKeys(ModelEntity entity, Map<String, ? extends ModelEntity> modelEntities, int constraintNameClipLength) getColumnInfo(Set<String> tableNames, Collection<String> messages) Uses the configuredConnectionProviderto get aConnectionbased on the configured helper name.getIndexInfo(Set<String> tableNames, Collection<String> messages) getIndexInfo(Set<String> tableNames, Collection<String> messages, boolean includeUnique) Gets index information from the database for the given table names only, optionally including unique indexes.getReferenceInfo(Set<String> tableNames, Collection<String> messages) static StringgetSchemaPattern(DatabaseMetaData dbData, String schemaName) Lookup schema name according do database metadata see JIRA-28526 this method needs to be coherent withconvertToSchemaTableName(String, java.sql.DatabaseMetaData)andModelEntity.getTableName(org.ofbiz.core.entity.config.DatasourceInfo)getTableNames(Collection<String> messages) induceModelFromDb(Collection<String> messages) Creates a list of ModelEntity objects based on metadata from the databasemakeFkConstraintClause(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) makeFkConstraintName(ModelRelation modelRelation, int constraintNameClipLength) makeFkIndexClause(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) makeFunctionBasedIndexClause(ModelEntity entity, ModelFunctionBasedIndex fbIndex, DatabaseType databaseType) makeIndexClause(ModelEntity entity, ModelIndex modelIndex)
-
Field Details
-
module
-
helperName
-
modelFieldTypeReader
-
datasourceInfo
-
sqlEscapeHelper
-
-
Constructor Details
-
DatabaseUtil
Constructs with the name of a helper that is used to loadDatasourceInfofromEntityConfigUtiland uses the staticConnectionFactoryfor connections.- Parameters:
helperName-
-
-
Method Details
-
getConnection
Uses the configuredConnectionProviderto get aConnectionbased on the configured helper name.- Returns:
- the
Connection - Throws:
SQLExceptionGenericEntityException
-
checkDb
public void checkDb(Map<String, ? extends ModelEntity> modelEntities, Collection<String> messages, boolean addMissing) Does a gzillion things to upgrade the database to the entitymodel by adding tables etc.- Parameters:
modelEntities- Model entity names to ModelEntity objects.messages- a thing to collect errors.addMissing- if true, will attempt to add tables and columns, fks, indices etc are added always.
-
checkDb
public void checkDb(Map<String, ? extends ModelEntity> modelEntities, Collection<String> messages, boolean addMissing, boolean promote, boolean widen) Does a gzillion things to upgrade the database to the entitymodel by adding tables etc.- Parameters:
modelEntities- Model entity names to ModelEntity objects.messages- a thing to collect errors.addMissing- if true, will attempt to add tables and columns, fks, indices etc are added always.promote- if true, will attempt to promote types to wider types, as defined inallowedFieldTypePromotions.widen- if true, will attempt to widen types with size (only widen, never shorten)
-
induceModelFromDb
Creates a list of ModelEntity objects based on metadata from the database -
getTableNames
-
getSchemaPattern
public static String getSchemaPattern(DatabaseMetaData dbData, String schemaName) throws SQLException Lookup schema name according do database metadata see JIRA-28526 this method needs to be coherent withconvertToSchemaTableName(String, java.sql.DatabaseMetaData)andModelEntity.getTableName(org.ofbiz.core.entity.config.DatasourceInfo)- Throws:
SQLException
-
getColumnInfo
public Map<String,List<DatabaseUtil.ColumnCheckInfo>> getColumnInfo(Set<String> tableNames, Collection<String> messages) -
getReferenceInfo
public Map<String,Map<String, getReferenceInfoDatabaseUtil.ReferenceCheckInfo>> (Set<String> tableNames, Collection<String> messages) -
getIndexInfo
-
getIndexInfo
public Map<String,Set<String>> getIndexInfo(Set<String> tableNames, Collection<String> messages, boolean includeUnique) Gets index information from the database for the given table names only, optionally including unique indexes.- Parameters:
tableNames- the names of tables to get indexes for.messages- a collector of errors.includeUnique- if true, the index info will include unique indexes which could include pk indexes.- Returns:
- a map of table names to sets of index names or null on failure.
-
createTable
public String createTable(ModelEntity entity, Map<String, ? extends ModelEntity> modelEntities, boolean addFks, boolean usePkConstraintNames, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) -
addColumn
-
addVirtualColumn
-
makeFkConstraintName
-
createForeignKeys
public String createForeignKeys(ModelEntity entity, Map<String, ? extends ModelEntity> modelEntities, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) -
createForeignKey
public String createForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) -
makeFkConstraintClause
public String makeFkConstraintClause(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred) -
deleteForeignKeys
public String deleteForeignKeys(ModelEntity entity, Map<String, ? extends ModelEntity> modelEntities, int constraintNameClipLength) -
deleteForeignKey
public String deleteForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength) -
createDeclaredIndices
Creates a database index for every declared index on the given entity.- Parameters:
entity-- Returns:
- an error message if there is an error, or null if it worked.
-
createDeclaredIndex
-
makeIndexClause
-
deleteDeclaredIndices
-
deleteDeclaredIndex
-
createForeignKeyIndices
-
createForeignKeyIndex
public String createForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) -
makeFkIndexClause
public String makeFkIndexClause(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) -
deleteForeignKeyIndices
-
deleteForeignKeyIndex
public String deleteForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength) -
createFunctionBasedIndices
Creates a function based index for every declared index on the given entity.- Parameters:
entity-- Returns:
- an error message if there is an error, or null if it worked.
-
createFunctionBasedIndex
-
makeFunctionBasedIndexClause
public String makeFunctionBasedIndexClause(ModelEntity entity, ModelFunctionBasedIndex fbIndex, DatabaseType databaseType)
-