Class OldDatabaseUtil


  • public class OldDatabaseUtil
    extends Object
    This class provides utility methods to perform DDL operations in database-agnostic way. Currently supported databases: HSQL(default diualect), MySQL (4+), PostgreSQL (8+), Oracle (10g+). TODO: Refactor: method are inconsistend some are only construct SQL DDL and some execute it.
    • Method Detail

      • getDatabaseType

        public static DatabaseType getDatabaseType​(DBFunctions dbf)
        Only to compatibility with legacy converters.
      • getColumnTypes

        @NotNull
        public static DBColumnTypes getColumnTypes​(DBFunctions dbf)
        Provides with old-style column types.
        Parameters:
        dbf - db fucntions.
        Returns:
        column types.
      • LONG_STRING

        public static String LONG_STRING​(int length,
                                         DBFunctions dbf)
        Text column type string depending on size
        Parameters:
        length - desired lenght
        connection - connection
        Returns:
        database-specific type
      • LONG_STRING

        public static String LONG_STRING​(int length,
                                         Connection connection)
      • SHORT_STRING

        public static String SHORT_STRING​(int length,
                                          DBFunctions dbf)
        Short string (up to 2048 characters) column type
        Parameters:
        length - desired lenght - should be less than or equal 2048
        c -
        Returns:
        database-specific type
      • SHORT_STRING

        public static String SHORT_STRING​(int length,
                                          Connection c)
      • INT

        public static String INT​(DBFunctions dbf)
        Integer column type
        Parameters:
        connection - connection
        Returns:
        database-specific type
      • LONG

        public static String LONG​(DBFunctions dbf)
        Long (BIGINT) column type
        Parameters:
        connection - connection
        Returns:
        database-specific type
      • TIMESTAMP

        public static String TIMESTAMP​(DBFunctions dbf)
        Long (BIGINT) column type
        Parameters:
        connection - connection
        Returns:
        database-specific type
      • tableExists

        public static boolean tableExists​(@NotNull
                                          DBFunctions dbf,
                                          @NotNull
                                          String tableName)
                                   throws SQLException
        Check table existence via database metadata.
        Parameters:
        connection - connection
        tableName - table name
        Returns:
        flag
        Throws:
        SQLException - raw database exception
      • renameTable

        public static void renameTable​(@NotNull
                                       DBFunctions dbf,
                                       @NotNull
                                       String oldName,
                                       @NotNull
                                       String newName)
                                throws SQLException
        Rename database table.
        Parameters:
        connection - connection
        oldName - old name
        newName - name name
        Throws:
        SQLException - raw database exception
      • getNextId

        public static long getNextId​(DBFunctions dbf,
                                     String tableName,
                                     String idColumnName)
                              throws SQLException
        Get next id by finding max value for given column. TODO: no better way yet but we should use hibernate genereator approach
        Parameters:
        dbf - connection
        tableName - table name
        idColumnName - column name to check
        Returns:
        next id
        Throws:
        SQLException - raw database exception
      • renameColumn

        @Deprecated
        public static void renameColumn​(DBFunctions dbf,
                                        String tableName,
                                        String columnName,
                                        String newColumnName,
                                        String definition)
                                 throws SQLException
        Deprecated.
        use #RENAME_COLUMN and use dbf.DBFunctions.executeDdls(java.lang.String...)
        Change column name. WARNING: do use this to change type or nullability or default value of column!
        Parameters:
        connection - connection
        tableName - table name
        columnName - column name
        newColumnName - new column name
        definition - original type declaration - actually used on MySQL only
        Throws:
        SQLException - raw database exception
      • addColumn

        public static void addColumn​(DBFunctions dbf,
                                     String tableName,
                                     String columnName,
                                     String columnDefinition)
                              throws SQLException
        Add column.
        Parameters:
        connection - connection
        tableName - table name
        columnName - column name
        columnDefinition - column type declaration
        Throws:
        SQLException - raw database exception
      • dropColumn

        public static void dropColumn​(Connection connection,
                                      String tableName,
                                      String columnName)
                               throws SQLException
        Drop column. Drops dependent constraints.
        Parameters:
        connection - connection
        tableName - table name
        columnName - column name
        Throws:
        SQLException - raw database exception
      • changeColumn

        public static void changeColumn​(DBFunctions dbf,
                                        String tableName,
                                        String columnName,
                                        String columnDefinition)
                                 throws SQLException
        Change column type. WARNING: do use this to change nullability or default value of column!
        Parameters:
        dbf - dbf
        tableName - table name
        columnName - column name
        columnDefinition - column type declaration
        Throws:
        SQLException
      • changeColumn

        @Deprecated
        public static void changeColumn​(Connection connection,
                                        String tableName,
                                        String columnName,
                                        String columnDefinition)
                                 throws SQLException
        Deprecated.
        Change column type. WARNING: do use this to change nullability or default value of column!
        Parameters:
        connection - connection
        tableName - table name
        columnName - column name
        columnDefinition - column type declaration
        Throws:
        SQLException - raw database exception
      • setColumnNullable

        public static void setColumnNullable​(DBFunctions dbf,
                                             String tableName,
                                             String columnName,
                                             String columnDefinition,
                                             boolean nullable)
        Change nullability of given column.
        Parameters:
        connection - connection
        tableName - table name
        columnName - column name
        columnDefinition - column type declaration
        nullable - nullability flag
        Throws:
        SQLException - raw database exception
      • dropColumnDefault

        @Deprecated
        public static void dropColumnDefault​(String columnName,
                                             String tableName,
                                             DBFunctions dbf)
        Deprecated.
        this method is highly unportable, please avoid explicit default expectations. (set default via UPDATE).
        Drop table column default value (set it to NULL).
        Parameters:
        columnName - column name
        tableName - table name
        connection - connection
        Throws:
        SQLException - raw database exception
      • dropColumnDefault

        @Deprecated
        public static void dropColumnDefault​(String columnName,
                                             String tableName,
                                             Connection connection)
                                      throws SQLException
        Deprecated.
        this method is highly unportable, please avoid explicit default expectations. (set default via UPDATE).
        Throws:
        SQLException
      • dropIndex

        public static void dropIndex​(String indexName,
                                     String tableName,
                                     DBFunctions dbf)
                              throws SQLException
        Drop table index by name
        Parameters:
        indexName - index name
        tableName - table name
        connection - connection
        Throws:
        SQLException - raw database exception
      • execute

        public static void execute​(@NotNull
                                   DBFunctions dbf,
                                   String sqlText)
      • execute

        public static void execute​(@NotNull
                                   Connection conn,
                                   String sqlText)
      • execute

        public static void execute​(@NotNull
                                   Statement stmt,
                                   String sqlText)
        Executes statement gathering as many error detail as possible
        Parameters:
        stmt - statement
        sqlText - original SQL or code description
      • close

        public static void close​(Object... args)
        Close anything closable
        Parameters:
        args - closeable objects to close