Package mil.nga.geopackage.db
Class CoreSQLUtils
- java.lang.Object
-
- mil.nga.geopackage.db.CoreSQLUtils
-
public class CoreSQLUtils extends Object
Core SQL Utility methods- Since:
- 1.2.1
- Author:
- osbornb
-
-
Field Summary
Fields Modifier and Type Field Description static String
ROWID_COLUMN
SQLite table rowid column name
-
Constructor Summary
Constructors Constructor Description CoreSQLUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addColumn(GeoPackageCoreConnection db, String tableName, UserColumn column)
Create SQL for adding a columnstatic 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.static String
columnDefaultValue(Object defaultValue, GeoPackageDataType dataType)
Get the column default value as a stringstatic String
columnDefaultValue(UserColumn column)
Get the column default value as a stringstatic String
columnDefinition(UserColumn column)
Create the column definition SQL in the format:static String
columnSQL(UserColumn column)
Create the column SQL in the format:static boolean
containsWhitespace(String name)
Determine if the name contains whitespacestatic String
createName(String name, String replace, String replacement)
Create a new name by replacing a case insensitive value with a new value.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.static <TColumn extends UserColumn>
StringcreateTableSQL(UserTable<TColumn> table)
Create the user defined table SQLstatic void
dropTable(GeoPackageCoreConnection db, String tableName)
Drop the table if it existsstatic String
dropTableSQL(String tableName)
Create the drop table if exists SQLstatic void
dropView(GeoPackageCoreConnection db, String viewName)
Drop the view if it existsstatic String
dropViewSQL(String viewName)
Create the drop view if exists SQLstatic List<List<Object>>
foreignKeyCheck(GeoPackageCoreConnection db)
Perform a foreign key checkstatic List<List<Object>>
foreignKeyCheck(GeoPackageCoreConnection db, String tableName)
Perform a foreign key checkstatic String
foreignKeyCheckSQL()
Create the foreign key check SQLstatic String
foreignKeyCheckSQL(String tableName)
Create the foreign key check SQLstatic boolean
foreignKeys(GeoPackageCoreConnection db)
Query for the foreign keys valuestatic boolean
foreignKeys(GeoPackageCoreConnection db, boolean on)
Change the foreign keys statestatic String
foreignKeysSQL(boolean on)
Create the foreign keys SQLstatic String
integrityCheckSQL()
Create the integrity check SQLstatic boolean
isEmptyOrContainsWhitespace(String name)
Determine if the name is empty or contains whitespacestatic String
modifySQL(String name, String sql, TableMapping tableMapping)
Modify the SQL with a name change and the table mapping modificationsstatic String
modifySQL(String sql, TableMapping tableMapping)
Modify the SQL with table mapping modificationsstatic String
modifySQL(GeoPackageCoreConnection db, String name, String sql, TableMapping tableMapping)
Modify the SQL with a name change and the table mapping modificationsstatic String
quickCheckSQL()
Create the quick check SQLstatic String
quoteUnwrap(String name)
Remove double quotes from the namestatic String
quoteWrap(String name)
Wrap the name in double quotesstatic String[]
quoteWrap(String[] names)
Wrap the names in double quotesstatic String
replaceName(String sql, String name, String replacement)
Replace the name (table, column, etc) in the SQL with the replacement.static String
singleQuoteWrap(String name)
Wrap the name in single quotesstatic String[]
singleQuoteWrap(String[] names)
Wrap the names in single quotesstatic String
tempTableName(GeoPackageCoreConnection db, String prefix, String baseName)
Get an available temporary table name.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.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.static void
transferTableContent(GeoPackageCoreConnection db, TableMapping tableMapping)
Transfer table content from one table to anotherstatic String
transferTableContentSQL(TableMapping tableMapping)
Create insert SQL to transfer table content from one table to anotherstatic void
vacuum(GeoPackageCoreConnection db)
Rebuild the GeoPackage, repacking it into a minimal amount of disk space
-
-
-
Field Detail
-
ROWID_COLUMN
public static final String ROWID_COLUMN
SQLite table rowid column name- Since:
- 6.6.3
- See Also:
- Constant Field Values
-
-
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 arraycolumnsAs
- columns as values- Returns:
- columns with as values
- Since:
- 2.0.0
-
createTableSQL
public static <TColumn extends UserColumn> String 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 valuedataType
- 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
- connectiontableName
- table namecolumn
- 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
- connectionon
- 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
- connectiontableName
- 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
- connectiontableName
- 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
- connectionviewName
- 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
- connectiontableMapping
- 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
- connectiontableName
- table namecolumnName
- column namenewColumnValue
- new column value for new rowscurrentColumnValue
- 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
- connectiontableName
- table namecolumnName
- column namenewColumnValue
- new column value for new rowscurrentColumnValue
- column value for rows to insert as new rowsidColumnName
- 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
- connectionprefix
- name prefixbaseName
- 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 namesql
- SQL statementtableMapping
- 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 detectionname
- statement namesql
- SQL statementtableMapping
- 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 statementtableMapping
- 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 statementname
- namereplacement
- 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 namereplace
- value to replacereplacement
- 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 detectionname
- current namereplace
- value to replacereplacement
- 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
-
-