Class CoreSQLUtils


  • public class CoreSQLUtils
    extends Object
    Core SQL Utility methods
    Since:
    1.2.1
    Author:
    osbornb
    • Constructor Detail

      • CoreSQLUtils

        public CoreSQLUtils()
    • Method Detail

      • quoteWrap

        public static String quoteWrap​(String name)
        Wrap the name in double quotes
        Parameters:
        name - name
        Returns:
        quoted name
      • quoteWrap

        public static String[] quoteWrap​(String[] names)
        Wrap the names in double quotes
        Parameters:
        names - names
        Returns:
        quoted names
      • quoteUnwrap

        public static String quoteUnwrap​(String name)
        Remove double quotes from the name
        Parameters:
        name - name
        Returns:
        unquoted name
        Since:
        3.3.0
      • singleQuoteWrap

        public static String singleQuoteWrap​(String name)
        Wrap the name in single quotes
        Parameters:
        name - name
        Returns:
        quoted name
        Since:
        6.6.3
      • singleQuoteWrap

        public static String[] singleQuoteWrap​(String[] names)
        Wrap the names in single quotes
        Parameters:
        names - names
        Returns:
        quoted names
        Since:
        6.6.3
      • containsWhitespace

        public static boolean containsWhitespace​(String name)
        Determine if the name contains whitespace
        Parameters:
        name - name
        Returns:
        true if contains whitespace
        Since:
        6.6.3
      • isEmptyOrContainsWhitespace

        public static boolean isEmptyOrContainsWhitespace​(String name)
        Determine if the name is empty or contains whitespace
        Parameters:
        name - name
        Returns:
        true if empty or contains whitespace
        Since:
        6.6.3
      • buildColumnsAs

        public static String[] buildColumnsAs​(String[] columns,
                                              String[] columnsAs)
        Build the "columns as" query values for the provided columns and "columns as" values for use in select statements. The columns as size should equal the number of columns and only provide values at the indices for desired columns.

        Example: columns = [column1, column2], columnsAs = [null, value] creates [column1, value as column2]

        Parameters:
        columns - columns array
        columnsAs - columns as values
        Returns:
        columns with as values
        Since:
        2.0.0
      • createTableSQL

        public static <TColumn extends UserColumnString createTableSQL​(UserTable<TColumn> table)
        Create the user defined table SQL
        Type Parameters:
        TColumn - column type
        Parameters:
        table - user table
        Returns:
        create table SQL
        Since:
        3.3.0
      • columnSQL

        public static String columnSQL​(UserColumn column)
        Create the column SQL in the format:

        "column_name" column_type[(max)] [NOT NULL] [PRIMARY KEY AUTOINCREMENT]

        Parameters:
        column - user column
        Returns:
        column SQL
        Since:
        3.3.0
      • columnDefinition

        public static String columnDefinition​(UserColumn column)
        Create the column definition SQL in the format:

        column_type[(max)] [NOT NULL] [PRIMARY KEY AUTOINCREMENT]

        Parameters:
        column - user column
        Returns:
        column definition SQL
        Since:
        3.3.0
      • columnDefaultValue

        public static String columnDefaultValue​(UserColumn column)
        Get the column default value as a string
        Parameters:
        column - user column
        Returns:
        default value
        Since:
        3.3.0
      • columnDefaultValue

        public static String columnDefaultValue​(Object defaultValue,
                                                GeoPackageDataType dataType)
        Get the column default value as a string
        Parameters:
        defaultValue - default value
        dataType - data type
        Returns:
        default value
        Since:
        3.3.0
      • addColumn

        public static void addColumn​(GeoPackageCoreConnection db,
                                     String tableName,
                                     UserColumn column)
        Create SQL for adding a column
        Parameters:
        db - connection
        tableName - table name
        column - user column
        Since:
        3.3.0
      • foreignKeys

        public static boolean foreignKeys​(GeoPackageCoreConnection db)
        Query for the foreign keys value
        Parameters:
        db - connection
        Returns:
        true if enabled, false if disabled
        Since:
        3.3.0
      • foreignKeys

        public static boolean foreignKeys​(GeoPackageCoreConnection db,
                                          boolean on)
        Change the foreign keys state
        Parameters:
        db - connection
        on - true to turn on, false to turn off
        Returns:
        previous foreign keys value
        Since:
        3.3.0
      • foreignKeysSQL

        public static String foreignKeysSQL​(boolean on)
        Create the foreign keys SQL
        Parameters:
        on - true to turn on, false to turn off
        Returns:
        foreign keys SQL
        Since:
        3.3.0
      • foreignKeyCheck

        public static List<List<Object>> foreignKeyCheck​(GeoPackageCoreConnection db)
        Perform a foreign key check
        Parameters:
        db - connection
        Returns:
        empty list if valid or violation errors, 4 column values for each violation. see SQLite PRAGMA foreign_key_check
        Since:
        3.3.0
      • foreignKeyCheck

        public static List<List<Object>> foreignKeyCheck​(GeoPackageCoreConnection db,
                                                         String tableName)
        Perform a foreign key check
        Parameters:
        db - connection
        tableName - table name
        Returns:
        empty list if valid or violation errors, 4 column values for each violation. see SQLite PRAGMA foreign_key_check
        Since:
        3.3.0
      • foreignKeyCheckSQL

        public static String foreignKeyCheckSQL()
        Create the foreign key check SQL
        Returns:
        foreign key check SQL
        Since:
        3.3.0
      • foreignKeyCheckSQL

        public static String foreignKeyCheckSQL​(String tableName)
        Create the foreign key check SQL
        Parameters:
        tableName - table name
        Returns:
        foreign key check SQL
        Since:
        3.3.0
      • integrityCheckSQL

        public static String integrityCheckSQL()
        Create the integrity check SQL
        Returns:
        integrity check SQL
        Since:
        3.3.0
      • quickCheckSQL

        public static String quickCheckSQL()
        Create the quick check SQL
        Returns:
        quick check SQL
        Since:
        3.3.0
      • dropTable

        public static void dropTable​(GeoPackageCoreConnection db,
                                     String tableName)
        Drop the table if it exists
        Parameters:
        db - connection
        tableName - table name
        Since:
        3.3.0
      • dropTableSQL

        public static String dropTableSQL​(String tableName)
        Create the drop table if exists SQL
        Parameters:
        tableName - table name
        Returns:
        drop table SQL
        Since:
        3.3.0
      • dropView

        public static void dropView​(GeoPackageCoreConnection db,
                                    String viewName)
        Drop the view if it exists
        Parameters:
        db - connection
        viewName - view name
        Since:
        3.3.0
      • dropViewSQL

        public static String dropViewSQL​(String viewName)
        Create the drop view if exists SQL
        Parameters:
        viewName - view name
        Returns:
        drop view SQL
        Since:
        3.3.0
      • transferTableContent

        public static void transferTableContent​(GeoPackageCoreConnection db,
                                                TableMapping tableMapping)
        Transfer table content from one table to another
        Parameters:
        db - connection
        tableMapping - table mapping
        Since:
        3.3.0
      • transferTableContentSQL

        public static String transferTableContentSQL​(TableMapping tableMapping)
        Create insert SQL to transfer table content from one table to another
        Parameters:
        tableMapping - table mapping
        Returns:
        transfer SQL
        Since:
        3.3.0
      • transferTableContent

        public static void transferTableContent​(GeoPackageCoreConnection db,
                                                String tableName,
                                                String columnName,
                                                Object newColumnValue,
                                                Object currentColumnValue)
        Transfer table content to itself with new rows containing a new column value. All rows containing the current column value are inserted as new rows with the new column value.
        Parameters:
        db - connection
        tableName - table name
        columnName - column name
        newColumnValue - new column value for new rows
        currentColumnValue - column value for rows to insert as new rows
        Since:
        3.3.0
      • transferTableContent

        public static void transferTableContent​(GeoPackageCoreConnection db,
                                                String tableName,
                                                String columnName,
                                                Object newColumnValue,
                                                Object currentColumnValue,
                                                String idColumnName)
        Transfer table content to itself with new rows containing a new column value. All rows containing the current column value are inserted as new rows with the new column value.
        Parameters:
        db - connection
        tableName - table name
        columnName - column name
        newColumnValue - new column value for new rows
        currentColumnValue - column value for rows to insert as new rows
        idColumnName - id column name
        Since:
        3.3.0
      • tempTableName

        public static String tempTableName​(GeoPackageCoreConnection db,
                                           String prefix,
                                           String baseName)
        Get an available temporary table name. Starts with prefix_baseName and then continues with prefix#_baseName starting at 1 and increasing.
        Parameters:
        db - connection
        prefix - name prefix
        baseName - base name
        Returns:
        unused table name
        Since:
        3.3.0
      • modifySQL

        public static String modifySQL​(String name,
                                       String sql,
                                       TableMapping tableMapping)
        Modify the SQL with a name change and the table mapping modifications
        Parameters:
        name - statement name
        sql - SQL statement
        tableMapping - table mapping
        Returns:
        updated SQL, null if SQL contains a deleted column
        Since:
        3.3.0
      • modifySQL

        public static String modifySQL​(GeoPackageCoreConnection db,
                                       String name,
                                       String sql,
                                       TableMapping tableMapping)
        Modify the SQL with a name change and the table mapping modifications
        Parameters:
        db - optional connection, used for SQLite Master name conflict detection
        name - statement name
        sql - SQL statement
        tableMapping - table mapping
        Returns:
        updated SQL, null if SQL contains a deleted column
        Since:
        3.3.0
      • modifySQL

        public static String modifySQL​(String sql,
                                       TableMapping tableMapping)
        Modify the SQL with table mapping modifications
        Parameters:
        sql - SQL statement
        tableMapping - table mapping
        Returns:
        updated SQL, null if SQL contains a deleted column
        Since:
        3.3.0
      • replaceName

        public static String replaceName​(String sql,
                                         String name,
                                         String replacement)
        Replace the name (table, column, etc) in the SQL with the replacement. The name must be surrounded by non word characters (i.e. not a subset of another name).
        Parameters:
        sql - SQL statement
        name - name
        replacement - replacement value
        Returns:
        null if not modified, SQL value if replaced at least once
        Since:
        3.3.0
      • createName

        public static String createName​(String name,
                                        String replace,
                                        String replacement)
        Create a new name by replacing a case insensitive value with a new value. If no replacement is done, create a new name in the form name_#, where # is either 2 or one greater than an existing name number suffix.
        Parameters:
        name - current name
        replace - value to replace
        replacement - replacement value
        Returns:
        new name
        Since:
        3.3.0
      • createName

        public static String createName​(GeoPackageCoreConnection db,
                                        String name,
                                        String replace,
                                        String replacement)
        Create a new name by replacing a case insensitive value with a new value. If no replacement is done, create a new name in the form name_#, where # is either 2 or one greater than an existing name number suffix. When a db connection is provided, check for conflicting SQLite Master names and increment # until an available name is found.
        Parameters:
        db - optional connection, used for SQLite Master name conflict detection
        name - current name
        replace - value to replace
        replacement - replacement value
        Returns:
        new name
        Since:
        3.3.0
      • vacuum

        public static void vacuum​(GeoPackageCoreConnection db)
        Rebuild the GeoPackage, repacking it into a minimal amount of disk space
        Parameters:
        db - connection
        Since:
        3.3.0