Class DatabaseUtil

java.lang.Object
org.apache.ofbiz.entity.jdbc.DatabaseUtil

public class DatabaseUtil extends Object
Utilities for Entity Database Maintenance
  • Constructor Details

  • Method Details

    • getConnection

      protected Connection getConnection() throws SQLException, GenericEntityException
      Gets connection.
      Returns:
      the connection
      Throws:
      SQLException - the sql exception
      GenericEntityException - the generic entity exception
    • getConnectionLogged

      protected Connection getConnectionLogged(Collection<String> messages)
      Gets connection logged.
      Parameters:
      messages - the messages
      Returns:
      the connection logged
    • getDatasource

      public Datasource getDatasource()
      Gets datasource.
      Returns:
      the datasource
    • checkDb

      public void checkDb(Map<String,ModelEntity> modelEntities, List<String> messages, boolean addMissing)
      Check db.
      Parameters:
      modelEntities - the model entities
      messages - the messages
      addMissing - the add missing
    • checkDb

      public void checkDb(Map<String,ModelEntity> modelEntities, List<String> colWrongSize, List<String> messages, boolean checkPks, boolean checkFks, boolean checkFkIdx, boolean addMissing)
      Check db.
      Parameters:
      modelEntities - the model entities
      colWrongSize - the col wrong size
      messages - the messages
      checkPks - the check pks
      checkFks - the check fks
      checkFkIdx - the check fk idx
      addMissing - the add missing
    • induceModelFromDb

      public List<ModelEntity> induceModelFromDb(Collection<String> messages)
      Creates a list of ModelEntity objects based on meta data from the database
    • getDatabaseMetaData

      public DatabaseMetaData getDatabaseMetaData(Connection connection, Collection<String> messages)
      Gets database meta data.
      Parameters:
      connection - the connection
      messages - the messages
      Returns:
      the database meta data
    • printDbMiscData

      public void printDbMiscData(DatabaseMetaData dbData, Connection con)
      Print db misc data.
      Parameters:
      dbData - the db data
      con - the con
    • getTableNames

      public TreeSet<String> getTableNames(Collection<String> messages)
      Gets table names.
      Parameters:
      messages - the messages
      Returns:
      the table names
    • checkPrimaryKeyInfo

      public int checkPrimaryKeyInfo(ResultSet rsPks, String lookupSchemaName, boolean needsUpperCase, Map<String,Map<String,DatabaseUtil.ColumnCheckInfo>> colInfo, Collection<String> messages) throws SQLException
      Check primary key info int.
      Parameters:
      rsPks - the rs pks
      lookupSchemaName - the lookup schema name
      needsUpperCase - the needs upper case
      colInfo - the col info
      messages - the messages
      Returns:
      the int
      Throws:
      SQLException - the sql exception
    • getReferenceInfo

      public Map<String,Map<String,DatabaseUtil.ReferenceCheckInfo>> getReferenceInfo(Set<String> tableNames, Collection<String> messages)
      Gets reference info.
      Parameters:
      tableNames - the table names
      messages - the messages
      Returns:
      the reference info
    • getIndexInfo

      public Map<String,Set<String>> getIndexInfo(Set<String> tableNames, Collection<String> messages, boolean[] needsUpperCase)
      Gets index info.
      Parameters:
      tableNames - the table names
      messages - the messages
      needsUpperCase - the needs upper case
      Returns:
      the index info
    • createTable

      public String createTable(ModelEntity entity, Map<String,ModelEntity> modelEntities, boolean addFks)
      Create table string.
      Parameters:
      entity - the entity
      modelEntities - the model entities
      addFks - the add fks
      Returns:
      the string
    • deleteTable

      public void deleteTable(ModelEntity entity, List<String> messages)
      Delete table.
      Parameters:
      entity - the entity
      messages - the messages
    • addColumn

      public String addColumn(ModelEntity entity, ModelField field)
      Add column string.
      Parameters:
      entity - the entity
      field - the field
      Returns:
      the string
    • renameColumn

      public String renameColumn(ModelEntity entity, ModelField field, String newName)
      Rename column string.
      Parameters:
      entity - the entity
      field - the field
      newName - the new name
      Returns:
      the string
    • repairColumnSize

      public void repairColumnSize(ModelEntity entity, ModelField field, List<String> messages)
      Repair column size.
      Parameters:
      entity - the entity
      field - the field
      messages - the messages
    • repairColumnSizeChanges

      public void repairColumnSizeChanges(Map<String,ModelEntity> modelEntities, List<String> fieldsWrongSize, List<String> messages)
      Repair column size changes.
      Parameters:
      modelEntities - the model entities
      fieldsWrongSize - the fields wrong size
      messages - the messages
    • makePkConstraintName

      public String makePkConstraintName(ModelEntity entity, int constraintNameClipLength)
      Make pk constraint name string.
      Parameters:
      entity - the entity
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • makeFkConstraintName

      public String makeFkConstraintName(ModelRelation modelRelation, int constraintNameClipLength)
      Make fk constraint name string.
      Parameters:
      modelRelation - the model relation
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • makeIndexName

      public String makeIndexName(ModelIndex modelIndex, int constraintNameClipLength)
      Make index name string.
      Parameters:
      modelIndex - the model index
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • createForeignKeys

      public int createForeignKeys(ModelEntity entity, Map<String,ModelEntity> modelEntities, List<String> messages)
      Create foreign keys int.
      Parameters:
      entity - the entity
      modelEntities - the model entities
      messages - the messages
      Returns:
      the int
    • createForeignKeys

      public int createForeignKeys(ModelEntity entity, Map<String,ModelEntity> modelEntities, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred, List<String> messages)
      Create foreign keys int.
      Parameters:
      entity - the entity
      modelEntities - the model entities
      constraintNameClipLength - the constraint name clip length
      fkStyle - the fk style
      useFkInitiallyDeferred - the use fk initially deferred
      messages - the messages
      Returns:
      the int
    • createForeignKey

      public String createForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred)
      Create foreign key string.
      Parameters:
      entity - the entity
      modelRelation - the model relation
      relModelEntity - the rel model entity
      constraintNameClipLength - the constraint name clip length
      fkStyle - the fk style
      useFkInitiallyDeferred - the use fk initially deferred
      Returns:
      the string
    • makeFkConstraintClause

      public String makeFkConstraintClause(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength, String fkStyle, boolean useFkInitiallyDeferred)
      Make fk constraint clause string.
      Parameters:
      entity - the entity
      modelRelation - the model relation
      relModelEntity - the rel model entity
      constraintNameClipLength - the constraint name clip length
      fkStyle - the fk style
      useFkInitiallyDeferred - the use fk initially deferred
      Returns:
      the string
    • deleteForeignKeys

      public void deleteForeignKeys(ModelEntity entity, Map<String,ModelEntity> modelEntities, List<String> messages)
      Delete foreign keys.
      Parameters:
      entity - the entity
      modelEntities - the model entities
      messages - the messages
    • deleteForeignKeys

      public void deleteForeignKeys(ModelEntity entity, Map<String,ModelEntity> modelEntities, int constraintNameClipLength, List<String> messages)
      Delete foreign keys.
      Parameters:
      entity - the entity
      modelEntities - the model entities
      constraintNameClipLength - the constraint name clip length
      messages - the messages
    • deleteForeignKey

      public String deleteForeignKey(ModelEntity entity, ModelRelation modelRelation, ModelEntity relModelEntity, int constraintNameClipLength)
      Delete foreign key string.
      Parameters:
      entity - the entity
      modelRelation - the model relation
      relModelEntity - the rel model entity
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • createPrimaryKey

      public void createPrimaryKey(ModelEntity entity, boolean usePkConstraintNames, int constraintNameClipLength, List<String> messages)
      Create primary key.
      Parameters:
      entity - the entity
      usePkConstraintNames - the use pk constraint names
      constraintNameClipLength - the constraint name clip length
      messages - the messages
    • createPrimaryKey

      public void createPrimaryKey(ModelEntity entity, boolean usePkConstraintNames, List<String> messages)
      Create primary key.
      Parameters:
      entity - the entity
      usePkConstraintNames - the use pk constraint names
      messages - the messages
    • createPrimaryKey

      public void createPrimaryKey(ModelEntity entity, List<String> messages)
      Create primary key.
      Parameters:
      entity - the entity
      messages - the messages
    • createPrimaryKey

      public String createPrimaryKey(ModelEntity entity, boolean usePkConstraintNames, int constraintNameClipLength)
      Create primary key string.
      Parameters:
      entity - the entity
      usePkConstraintNames - the use pk constraint names
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • deletePrimaryKey

      public void deletePrimaryKey(ModelEntity entity, boolean usePkConstraintNames, int constraintNameClipLength, List<String> messages)
      Delete primary key.
      Parameters:
      entity - the entity
      usePkConstraintNames - the use pk constraint names
      constraintNameClipLength - the constraint name clip length
      messages - the messages
    • deletePrimaryKey

      public void deletePrimaryKey(ModelEntity entity, boolean usePkConstraintNames, List<String> messages)
      Delete primary key.
      Parameters:
      entity - the entity
      usePkConstraintNames - the use pk constraint names
      messages - the messages
    • deletePrimaryKey

      public void deletePrimaryKey(ModelEntity entity, List<String> messages)
      Delete primary key.
      Parameters:
      entity - the entity
      messages - the messages
    • deletePrimaryKey

      public String deletePrimaryKey(ModelEntity entity, boolean usePkConstraintNames, int constraintNameClipLength)
      Delete primary key string.
      Parameters:
      entity - the entity
      usePkConstraintNames - the use pk constraint names
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • createDeclaredIndices

      public int createDeclaredIndices(ModelEntity entity, List<String> messages)
      create declared indices
    • createDeclaredIndex

      public String createDeclaredIndex(ModelEntity entity, ModelIndex modelIndex)
      create declared index
    • makeIndexClause

      public String makeIndexClause(ModelEntity entity, ModelIndex modelIndex)
      make index clause
    • deleteDeclaredIndices

      public void deleteDeclaredIndices(ModelEntity entity, List<String> messages)
      Delete declared indices.
      Parameters:
      entity - the entity
      messages - the messages
    • deleteDeclaredIndices

      public String deleteDeclaredIndices(ModelEntity entity)
      Delete declared indices string.
      Parameters:
      entity - the entity
      Returns:
      the string
    • deleteDeclaredIndex

      public String deleteDeclaredIndex(ModelEntity entity, ModelIndex modelIndex)
      Delete declared index string.
      Parameters:
      entity - the entity
      modelIndex - the model index
      Returns:
      the string
    • createForeignKeyIndices

      public int createForeignKeyIndices(ModelEntity entity, List<String> messages)
      Create foreign key indices int.
      Parameters:
      entity - the entity
      messages - the messages
      Returns:
      the int
    • createForeignKeyIndices

      public int createForeignKeyIndices(ModelEntity entity, int constraintNameClipLength, List<String> messages)
      Create foreign key indices int.
      Parameters:
      entity - the entity
      constraintNameClipLength - the constraint name clip length
      messages - the messages
      Returns:
      the int
    • createForeignKeyIndex

      public String createForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength)
      Create foreign key index string.
      Parameters:
      entity - the entity
      modelRelation - the model relation
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • makeFkIndexClause

      public String makeFkIndexClause(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength)
      Make fk index clause string.
      Parameters:
      entity - the entity
      modelRelation - the model relation
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • deleteForeignKeyIndices

      public void deleteForeignKeyIndices(ModelEntity entity, List<String> messages)
      Delete foreign key indices.
      Parameters:
      entity - the entity
      messages - the messages
    • deleteForeignKeyIndices

      public String deleteForeignKeyIndices(ModelEntity entity, int constraintNameClipLength)
      Delete foreign key indices string.
      Parameters:
      entity - the entity
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • deleteForeignKeyIndex

      public String deleteForeignKeyIndex(ModelEntity entity, ModelRelation modelRelation, int constraintNameClipLength)
      Delete foreign key index string.
      Parameters:
      entity - the entity
      modelRelation - the model relation
      constraintNameClipLength - the constraint name clip length
      Returns:
      the string
    • getSchemaName

      public String getSchemaName(DatabaseMetaData dbData) throws SQLException
      Gets schema name.
      Parameters:
      dbData - the db data
      Returns:
      the schema name
      Throws:
      SQLException - the sql exception
    • updateCharacterSetAndCollation

      public void updateCharacterSetAndCollation(ModelEntity entity, List<String> messages)
      Update character set and collation.
      Parameters:
      entity - the entity
      messages - the messages