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 StringROWID_COLUMNSQLite table rowid column name
-
Constructor Summary
Constructors Constructor Description CoreSQLUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddColumn(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 StringcolumnDefaultValue(Object defaultValue, GeoPackageDataType dataType)Get the column default value as a stringstatic StringcolumnDefaultValue(UserColumn column)Get the column default value as a stringstatic StringcolumnDefinition(UserColumn column)Create the column definition SQL in the format:static StringcolumnSQL(UserColumn column)Create the column SQL in the format:static booleancontainsWhitespace(String name)Determine if the name contains whitespacestatic StringcreateName(String name, String replace, String replacement)Create a new name by replacing a case insensitive value with a new value.static StringcreateName(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 voiddropTable(GeoPackageCoreConnection db, String tableName)Drop the table if it existsstatic StringdropTableSQL(String tableName)Create the drop table if exists SQLstatic voiddropView(GeoPackageCoreConnection db, String viewName)Drop the view if it existsstatic StringdropViewSQL(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 StringforeignKeyCheckSQL()Create the foreign key check SQLstatic StringforeignKeyCheckSQL(String tableName)Create the foreign key check SQLstatic booleanforeignKeys(GeoPackageCoreConnection db)Query for the foreign keys valuestatic booleanforeignKeys(GeoPackageCoreConnection db, boolean on)Change the foreign keys statestatic StringforeignKeysSQL(boolean on)Create the foreign keys SQLstatic StringintegrityCheckSQL()Create the integrity check SQLstatic booleanisEmptyOrContainsWhitespace(String name)Determine if the name is empty or contains whitespacestatic StringmodifySQL(String name, String sql, TableMapping tableMapping)Modify the SQL with a name change and the table mapping modificationsstatic StringmodifySQL(String sql, TableMapping tableMapping)Modify the SQL with table mapping modificationsstatic StringmodifySQL(GeoPackageCoreConnection db, String name, String sql, TableMapping tableMapping)Modify the SQL with a name change and the table mapping modificationsstatic StringquickCheckSQL()Create the quick check SQLstatic StringquoteUnwrap(String name)Remove double quotes from the namestatic StringquoteWrap(String name)Wrap the name in double quotesstatic String[]quoteWrap(String[] names)Wrap the names in double quotesstatic StringreplaceName(String sql, String name, String replacement)Replace the name (table, column, etc) in the SQL with the replacement.static StringsingleQuoteWrap(String name)Wrap the name in single quotesstatic String[]singleQuoteWrap(String[] names)Wrap the names in single quotesstatic StringtempTableName(GeoPackageCoreConnection db, String prefix, String baseName)Get an available temporary table name.static voidtransferTableContent(GeoPackageCoreConnection db, String tableName, String columnName, Object newColumnValue, Object currentColumnValue)Transfer table content to itself with new rows containing a new column value.static voidtransferTableContent(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 voidtransferTableContent(GeoPackageCoreConnection db, TableMapping tableMapping)Transfer table content from one table to anotherstatic StringtransferTableContentSQL(TableMapping tableMapping)Create insert SQL to transfer table content from one table to anotherstatic voidvacuum(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
-
-