Class JDBCUtilities


  • public class JDBCUtilities
    extends Object
    DBMS should follow standard but it is not always the case, this class do some common operations. Compatible with H2 and PostgreSQL.
    Author:
    Nicolas Fortin, Erwan Bocher, Adam Gouge, Sylvain PALOMINOS (UBS chaire GEOTERA 2020)
    • Method Detail

      • hasField

        public static boolean hasField​(Connection connection,
                                       TableLocation table,
                                       String fieldName)
                                throws SQLException
        Return true if table table contains field fieldName.
        Parameters:
        connection - Connection
        table - a TableLocation
        fieldName - Field name
        Returns:
        True if the table contains the field
        Throws:
        SQLException
      • hasField

        public static boolean hasField​(Connection connection,
                                       String tableName,
                                       String fieldName)
                                throws SQLException
        Return true if table tableName contains field fieldName.
        Parameters:
        connection - Connection
        tableName - a table name in the form CATALOG.SCHEMA.TABLE
        fieldName - Field name
        Returns:
        True if the table contains the field
        Throws:
        SQLException
      • getFieldIndex

        public static int getFieldIndex​(ResultSetMetaData resultSetMetaData,
                                        String fieldName)
                                 throws SQLException
        Fetch the metadata, and check field name
        Parameters:
        resultSetMetaData - Active result set meta data.
        fieldName - Field name, ignore case
        Returns:
        The field index [1-n]; -1 if the field is not found
        Throws:
        SQLException
      • getColumnName

        public static String getColumnName​(ResultSetMetaData resultSetMetaData,
                                           Integer columnIndex)
                                    throws SQLException
        Check column name from its index
        Parameters:
        resultSetMetaData - Active result set meta data.
        columnIndex - Column index
        Returns:
        The column name
        Throws:
        SQLException
      • getColumnName

        public static String getColumnName​(Connection connection,
                                           TableLocation table,
                                           int columnIndex)
                                    throws SQLException
        Parameters:
        connection - Active connection to the database
        table - a TableLocation
        columnIndex - Field ordinal position [1-n]
        Returns:
        The field name, empty if the field position or table is not found
        Throws:
        SQLException - If jdbc throws an error
      • getColumnName

        public static String getColumnName​(Connection connection,
                                           String tableName,
                                           int columnIndex)
                                    throws SQLException
        Parameters:
        connection - Active connection to the database
        tableName - a table name in the form CATALOG.SCHEMA.TABLE
        columnIndex - Field ordinal position [1-n]
        Returns:
        The field name, empty if the field position or table is not found
        Throws:
        SQLException - If jdbc throws an error
      • getColumnNames

        public static List<String> getColumnNames​(Connection connection,
                                                  TableLocation table)
                                           throws SQLException
        Returns the list of all the column names of a table.
        Parameters:
        connection - Active connection to the database
        table - a TableLocation
        Returns:
        The list of field name.
        Throws:
        SQLException - If jdbc throws an error
      • getColumnNames

        public static List<String> getColumnNames​(Connection connection,
                                                  String tableName)
                                           throws SQLException
        Returns the list of all the column names of a table.
        Parameters:
        connection - Active connection to the database
        tableName - a table name in the form CATALOG.SCHEMA.TABLE
        Returns:
        The list of field name.
        Throws:
        SQLException - If jdbc throws an error
      • getColumnNamesAndIndexes

        public static List<Tuple<String,​Integer>> getColumnNamesAndIndexes​(Connection connection,
                                                                                 TableLocation table)
                                                                          throws SQLException
        Returns the list of all the column names and indexes of a table.
        Parameters:
        connection - Active connection to the database
        table - a TableLocation
        Returns:
        The list of field name.
        Throws:
        SQLException - If jdbc throws an error
      • getColumnNamesAndIndexes

        public static List<Tuple<String,​Integer>> getColumnNamesAndIndexes​(Connection connection,
                                                                                 String tableName)
                                                                          throws SQLException
        Returns the list of all the column names and indexes of a table.
        Parameters:
        connection - Active connection to the database
        tableName - a table name in the form CATALOG.SCHEMA.TABLE
        Returns:
        The list of field name.
        Throws:
        SQLException - If jdbc throws an error
      • getRowCount

        public static int getRowCount​(Connection connection,
                                      TableLocation table)
                               throws SQLException
        Fetch the row count of a table.
        Parameters:
        connection - Active connection.
        table - a TableLocation
        Returns:
        Row count
        Throws:
        SQLException - If the table does not exists, or sql request fail.
      • getRowCount

        public static int getRowCount​(Connection connection,
                                      String tableName)
                               throws SQLException
        Fetch the row count of a table.
        Parameters:
        connection - Active connection.
        tableName - a table name in the form CATALOG.SCHEMA.TABLE
        Returns:
        Row count
        Throws:
        SQLException - If the table does not exists, or sql request fail.
      • isTemporaryTable

        public static boolean isTemporaryTable​(Connection connection,
                                               TableLocation tableLocation)
                                        throws SQLException
        Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a temporary table.
        Parameters:
        connection - Active connection not closed by this method
        tableLocation - Table reference
        Returns:
        True if the provided table is temporary.
        Throws:
        SQLException - If the table does not exists.
      • isLinkedTable

        public static boolean isLinkedTable​(Connection connection,
                                            TableLocation table)
                                     throws SQLException
        Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a linked table.
        Parameters:
        connection - Active connection not closed by this method
        table - TableLocation
        Returns:
        True if the provided table is linked.
        Throws:
        SQLException - If the table does not exists.
      • isLinkedTable

        public static boolean isLinkedTable​(Connection connection,
                                            String tableReference)
                                     throws SQLException
        Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a linked table.
        Parameters:
        connection - Active connection not closed by this method
        tableReference - Table reference
        Returns:
        True if the provided table is linked.
        Throws:
        SQLException - If the table does not exists.
      • isH2DataBase

        public static boolean isH2DataBase​(Connection connection)
                                    throws SQLException
        Parameters:
        connection - to the
        Returns:
        True if the provided metadata is a h2 database connection.
        Throws:
        SQLException
      • getIntegerPrimaryKey

        public static int getIntegerPrimaryKey​(Connection connection,
                                               TableLocation tableLocation)
                                        throws SQLException
        Parameters:
        connection - Connection
        tableLocation - table identifier
        Returns:
        The integer primary key used for edition[1-n]; 0 if the source is closed or if the table has no primary key or more than one column as primary key
        Throws:
        SQLException
      • getIntegerPrimaryKeyNameAndIndex

        public static Tuple<String,​Integer> getIntegerPrimaryKeyNameAndIndex​(Connection connection,
                                                                                   TableLocation tableLocation)
                                                                            throws SQLException
        Method to fetch an integer primary key (name + index). Return null otherwise
        Parameters:
        connection - Connection
        tableLocation - table identifier
        Returns:
        The name and the index of an integer primary key used for edition[1-n]; 0 if the source is closed or if the table has no primary key or more than one column as primary key
        Throws:
        SQLException
      • tableExists

        public static boolean tableExists​(Connection connection,
                                          TableLocation tableLocation)
                                   throws SQLException
        Return true if the table exists.
        Parameters:
        connection - Connection
        tableLocation - Table name
        Returns:
        true if the table exists
        Throws:
        SQLException
      • tableExists

        public static boolean tableExists​(Connection connection,
                                          String tableName)
                                   throws SQLException
        Return true if the table exists.
        Parameters:
        connection - Connection
        tableName - Table name
        Returns:
        true if the table exists
        Throws:
        SQLException
      • getTableNames

        public static List<String> getTableNames​(Connection connection,
                                                 TableLocation tableLocation,
                                                 String[] types)
                                          throws SQLException
        Returns the list of table names.
        Parameters:
        connection - Active connection to the database
        tableLocation - Table name
        types - A list of table types, which must be from the list of table types returned from getTableTypes(), to include. null returns all types
        Returns:
        The integer primary key used for edition[1-n]; 0 if the source is closed or if the table has no primary key or more than one column as primary key
        Throws:
        SQLException
      • getTableNames

        public static List<String> getTableNames​(Connection connection,
                                                 String catalog,
                                                 String schemaPattern,
                                                 String tableNamePattern,
                                                 String[] types)
                                          throws SQLException
        Returns the list of table names.
        Parameters:
        connection - Active connection to the database
        catalog - A catalog name. Must match the catalog name as it is stored in the database. "" retrieves those without a catalog; null means that the catalog name should not be used to narrow the search
        schemaPattern - A schema name pattern. Must match the schema name as it is stored in the database. "" retrieves those without a schema. null means that the schema name should not be used to narrow the search
        tableNamePattern - A table name pattern. Must match the table name as it is stored in the database
        types - A list of table types, which must be from the list of table types returned from getTableTypes(), to include. null returns all types
        Returns:
        The integer primary key used for edition[1-n]; 0 if the source is closed or if the table has no primary key or more than one column as primary key
        Throws:
        SQLException
      • getUniqueFieldValues

        public static List<String> getUniqueFieldValues​(Connection connection,
                                                        TableLocation table,
                                                        String fieldName)
                                                 throws SQLException
        Returns the list of distinct values contained by a field from a table from the database
        Parameters:
        connection - Connection
        table - Name of the table containing the field.
        fieldName - Name of the field containing the values.
        Returns:
        The list of distinct values of the field.
        Throws:
        SQLException
      • getUniqueFieldValues

        public static List<String> getUniqueFieldValues​(Connection connection,
                                                        String tableName,
                                                        String fieldName)
                                                 throws SQLException
        Returns the list of distinct values contained by a field from a table from the database
        Parameters:
        connection - Connection
        tableName - Name of the table containing the field.
        fieldName - Name of the field containing the values.
        Returns:
        The list of distinct values of the field.
        Throws:
        SQLException
      • createEmptyTable

        public static void createEmptyTable​(Connection connection,
                                            TableLocation table)
                                     throws SQLException
        A method to create an empty table (no columns)
        Parameters:
        connection - Connection
        table - Table name
        Throws:
        SQLException
      • createEmptyTable

        public static void createEmptyTable​(Connection connection,
                                            String tableReference)
                                     throws SQLException
        A method to create an empty table (no columns)
        Parameters:
        connection - Connection
        tableReference - Table name
        Throws:
        SQLException
      • getColumnNames

        public static List<String> getColumnNames​(ResultSetMetaData resultSetMetaData)
                                           throws SQLException
        Fetch the name of columns
        Parameters:
        resultSetMetaData - Active result set meta data.
        Returns:
        An array with all column names
        Throws:
        SQLException
      • attachCancelResultSet

        public static PropertyChangeListener attachCancelResultSet​(Statement st,
                                                                   org.h2gis.api.ProgressVisitor progressVisitor)
        Parameters:
        st - Statement to cancel
        progressVisitor - Progress to link with
        Returns:
        call ProgressVisitor.removePropertyChangeListener(java.beans.PropertyChangeListener) with this object as argument
      • createTableDDL

        public static String createTableDDL​(Connection connection,
                                            TableLocation sourceTable,
                                            TableLocation targetTable)
                                     throws SQLException
        A simple method to generate a DDL create table command from a table name Takes into account only data types
        Parameters:
        connection -
        sourceTable -
        targetTable -
        Returns:
        a create table ddl command
        Throws:
        SQLException
      • createTableDDL

        public static String createTableDDL​(Connection connection,
                                            TableLocation location)
                                     throws SQLException
        A simple method to generate a DDL create table command from a table name Takes into account only data types
        Parameters:
        connection -
        location -
        Returns:
        a create table ddl command
        Throws:
        SQLException
      • createTableDDL

        public static String createTableDDL​(ResultSet resultSet,
                                            String outputTableName)
                                     throws SQLException
        A simple method to generate a DDL create table command from a query Takes into account only data types
        Parameters:
        outputTableName -
        resultSet -
        Returns:
        a create table ddl command
        Throws:
        SQLException
      • createTableDDL

        public static String createTableDDL​(ResultSetMetaData metadata,
                                            String outputTableName)
                                     throws SQLException
        A simple method to generate a DDL create table command from ResultSetMetaData Takes into account only data types
        Parameters:
        outputTableName -
        metadata -
        Returns:
        a create table ddl command
        Throws:
        SQLException
      • isIndexed

        public static boolean isIndexed​(Connection connection,
                                        String tableName,
                                        String columnName)
                                 throws SQLException
        Returns true if the given column name from the given table is indexed, return false otherwise.
        Parameters:
        connection - Connection containing the table to check.
        tableName - Name of the table to check.
        columnName - Name of the column to check.
        Returns:
        True if the given column is indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • isIndexed

        public static boolean isIndexed​(Connection connection,
                                        TableLocation table,
                                        String columnName)
                                 throws SQLException
        Returns true if the given column name from the given table has an indexed, return false otherwise.
        Parameters:
        connection - Connection containing the table to check.
        table - TableLocation of the table to check.
        columnName - Name of the column to check.
        Returns:
        True if the given column is indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • isSpatialIndexed

        public static boolean isSpatialIndexed​(Connection connection,
                                               String tableName,
                                               String columnName)
                                        throws SQLException
        Returns true if the given column name from the given table is indexed, return false otherwise.
        Parameters:
        connection - Connection containing the table to check.
        tableName - Name of the table to check.
        columnName - Name of the column to check.
        Returns:
        True if the given column is indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • isSpatialIndexed

        public static boolean isSpatialIndexed​(Connection connection,
                                               TableLocation table,
                                               String columnName)
                                        throws SQLException
        Returns true if the given column name from the given table is indexed, return false otherwise.
        Parameters:
        connection - Connection containing the table to check.
        table - TableLocation of the table to check.
        columnName - Name of the column to check.
        Returns:
        True if the given column is indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • createIndex

        public static boolean createIndex​(Connection connection,
                                          TableLocation table,
                                          String columnName)
                                   throws SQLException
        Create an index on the given column of the given table on the given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to index.
        columnName - Name of the column to index.
        Returns:
        True if the column have been indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • createIndex

        public static boolean createIndex​(Connection connection,
                                          String table,
                                          String columnName)
                                   throws SQLException
        Create an index on the given column of the given table on the given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Name of the table containing the column to index.
        columnName - Name of the column to index.
        Returns:
        True if the column have been indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • createSpatialIndex

        public static boolean createSpatialIndex​(Connection connection,
                                                 TableLocation table,
                                                 String columnName)
                                          throws SQLException
        Create a spatial index on the given column of the given table on the given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to index.
        columnName - Name of the column to index.
        Returns:
        True if the column have been indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • createSpatialIndex

        public static boolean createSpatialIndex​(Connection connection,
                                                 String table,
                                                 String columnName)
                                          throws SQLException
        Create a spatial index on the given column of the given table on the given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Name of the table containing the column to index.
        columnName - Name of the column to index.
        Returns:
        True if the column have been indexed, false otherwise.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • dropIndex

        public static void dropIndex​(Connection connection,
                                     TableLocation table,
                                     String columnName)
                              throws SQLException
        Drop the index of the given column of the given table on yhe given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to drop index.
        columnName - Name of the column to drop index.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • getIndexNames

        public static Map<String,​String> getIndexNames​(Connection connection,
                                                             String table)
                                                      throws SQLException
        Return the name of all indexes for a given table
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to get the index names.
        Returns:
        a map with the index name and its column name
        Throws:
        SQLException
      • getIndexNames

        public static Map<String,​String> getIndexNames​(Connection connection,
                                                             TableLocation table)
                                                      throws SQLException
        Return the name of all indexes for a given table
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to get the index names.
        Returns:
        a map with the index name and its column name
        Throws:
        SQLException
      • getIndexNames

        public static List<String> getIndexNames​(Connection connection,
                                                 String table,
                                                 String columnName)
                                          throws SQLException
        Return the name of indexes for a given table and column
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to get the index names.
        columnName - Name of the column.
        Returns:
        Throws:
        SQLException
      • getIndexNames

        public static List<String> getIndexNames​(Connection connection,
                                                 TableLocation table,
                                                 String columnName)
                                          throws SQLException
        Return the name of indexes for a given table and column
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to get the index names.
        columnName - Name of the column.
        Returns:
        Throws:
        SQLException
      • dropIndex

        public static void dropIndex​(Connection connection,
                                     String table,
                                     String columnName)
                              throws SQLException
        Drop the index of the given column of the given table on yhe given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Name of the table containing the column to drop index.
        columnName - Name of the column to drop index.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • dropIndex

        public static void dropIndex​(Connection connection,
                                     TableLocation table)
                              throws SQLException
        Drop the all the indexes of the given table on the given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Table containing the column to drop index.
        Throws:
        SQLException - Exception thrown on SQL execution error.
      • dropIndex

        public static void dropIndex​(Connection connection,
                                     String table)
                              throws SQLException
        Drop the all the indexes of the given table on yhe given connection.
        Parameters:
        connection - Connection to access to the desired table.
        table - Name of the table containing the column to drop index.
        Throws:
        SQLException - Exception thrown on SQL execution error.