Package org.h2gis.utilities
Class JDBCUtilities
- java.lang.Object
-
- org.h2gis.utilities.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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJDBCUtilities.FUNCTION_TYPEstatic classJDBCUtilities.TABLE_TYPE
-
Field Summary
Fields Modifier and Type Field Description static StringH2_DRIVER_NAMEstatic StringH2_DRIVER_PACKAGE_NAMEstatic intPOSTGRES_MAX_VARCHAR
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static PropertyChangeListenerattachCancelResultSet(Statement st, org.h2gis.api.ProgressVisitor progressVisitor)static voidcreateEmptyTable(Connection connection, String tableReference)A method to create an empty table (no columns)static voidcreateEmptyTable(Connection connection, TableLocation table)A method to create an empty table (no columns)static booleancreateIndex(Connection connection, String table, String columnName)Create an index on the given column of the given table on the given connection.static booleancreateIndex(Connection connection, TableLocation table, String columnName)Create an index on the given column of the given table on the given connection.static booleancreateSpatialIndex(Connection connection, String table, String columnName)Create a spatial index on the given column of the given table on the given connection.static booleancreateSpatialIndex(Connection connection, TableLocation table, String columnName)Create a spatial index on the given column of the given table on the given connection.static StringcreateTableDDL(Connection connection, TableLocation location)A simple method to generate a DDL create table command from a table name Takes into account only data typesstatic StringcreateTableDDL(Connection connection, TableLocation sourceTable, TableLocation targetTable)A simple method to generate a DDL create table command from a table name Takes into account only data typesstatic StringcreateTableDDL(ResultSetMetaData metadata, String outputTableName)A simple method to generate a DDL create table command from ResultSetMetaData Takes into account only data typesstatic StringcreateTableDDL(ResultSet resultSet, String outputTableName)A simple method to generate a DDL create table command from a query Takes into account only data typesstatic voiddropIndex(Connection connection, String table)Drop the all the indexes of the given table on yhe given connection.static voiddropIndex(Connection connection, String table, String columnName)Drop the index of the given column of the given table on yhe given connection.static voiddropIndex(Connection connection, TableLocation table)Drop the all the indexes of the given table on the given connection.static voiddropIndex(Connection connection, TableLocation table, String columnName)Drop the index of the given column of the given table on yhe given connection.static StringgetColumnName(Connection connection, String tableName, int columnIndex)static StringgetColumnName(Connection connection, TableLocation table, int columnIndex)static StringgetColumnName(ResultSetMetaData resultSetMetaData, Integer columnIndex)Check column name from its indexstatic List<String>getColumnNames(Connection connection, String tableName)Returns the list of all the column names of a table.static List<String>getColumnNames(Connection connection, TableLocation table)Returns the list of all the column names of a table.static List<String>getColumnNames(ResultSetMetaData resultSetMetaData)Fetch the name of columnsstatic List<Tuple<String,Integer>>getColumnNamesAndIndexes(Connection connection, String tableName)Returns the list of all the column names and indexes of a table.static List<Tuple<String,Integer>>getColumnNamesAndIndexes(Connection connection, TableLocation table)Returns the list of all the column names and indexes of a table.static intgetFieldIndex(ResultSetMetaData resultSetMetaData, String fieldName)Fetch the metadata, and check field namestatic Map<String,String>getIndexNames(Connection connection, String table)Return the name of all indexes for a given tablestatic List<String>getIndexNames(Connection connection, String table, String columnName)Return the name of indexes for a given table and columnstatic Map<String,String>getIndexNames(Connection connection, TableLocation table)Return the name of all indexes for a given tablestatic List<String>getIndexNames(Connection connection, TableLocation table, String columnName)Return the name of indexes for a given table and columnstatic intgetIntegerPrimaryKey(Connection connection, TableLocation tableLocation)static Tuple<String,Integer>getIntegerPrimaryKeyNameAndIndex(Connection connection, TableLocation tableLocation)Method to fetch an integer primary key (name + index).static intgetRowCount(Connection connection, String tableName)Fetch the row count of a table.static intgetRowCount(Connection connection, TableLocation table)Fetch the row count of a table.static List<String>getTableNames(Connection connection, String catalog, String schemaPattern, String tableNamePattern, String[] types)Returns the list of table names.static List<String>getTableNames(Connection connection, TableLocation tableLocation, String[] types)Returns the list of table names.static JDBCUtilities.TABLE_TYPEgetTableType(Connection connection, TableLocation location)Return the type of the table using an Enumstatic List<String>getUniqueFieldValues(Connection connection, String tableName, String fieldName)Returns the list of distinct values contained by a field from a table from the databasestatic List<String>getUniqueFieldValues(Connection connection, TableLocation table, String fieldName)Returns the list of distinct values contained by a field from a table from the databasestatic booleanhasField(Connection connection, String tableName, String fieldName)Return true if table tableName contains field fieldName.static booleanhasField(Connection connection, TableLocation table, String fieldName)Return true if table table contains field fieldName.static booleanisH2DataBase(Connection connection)static booleanisIndexed(Connection connection, String tableName, String columnName)Returns true if the given column name from the given table is indexed, return false otherwise.static booleanisIndexed(Connection connection, TableLocation table, String columnName)Returns true if the given column name from the given table has an indexed, return false otherwise.static booleanisLinkedTable(Connection connection, String tableReference)Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a linked table.static booleanisLinkedTable(Connection connection, TableLocation table)Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a linked table.static booleanisSpatialIndexed(Connection connection, String tableName, String columnName)Returns true if the given column name from the given table is indexed, return false otherwise.static booleanisSpatialIndexed(Connection connection, TableLocation table, String columnName)Returns true if the given column name from the given table is indexed, return false otherwise.static booleanisTemporaryTable(Connection connection, TableLocation tableLocation)Read INFORMATION_SCHEMA.TABLES in order to see if the provided table reference is a temporary table.static booleantableExists(Connection connection, String tableName)Return true if the table exists.static booleantableExists(Connection connection, TableLocation tableLocation)Return true if the table exists.static ConnectionwrapConnection(Connection connection)Use this only if DataSource is not available.static DataSourcewrapSpatialDataSource(DataSource dataSource)In order to be able to useWrapper.unwrap(Class)andWrapper.unwrap(Class)to getSpatialResultSetandSpatialResultSetMetaDatathis method wrap the provided dataSource.
-
-
-
Field Detail
-
H2_DRIVER_PACKAGE_NAME
public static final String H2_DRIVER_PACKAGE_NAME
- See Also:
- Constant Field Values
-
POSTGRES_MAX_VARCHAR
public static final int POSTGRES_MAX_VARCHAR
- See Also:
- Constant Field Values
-
H2_DRIVER_NAME
public static final String H2_DRIVER_NAME
- See Also:
- Constant Field Values
-
-
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- Connectiontable- a TableLocationfieldName- 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- ConnectiontableName- a table name in the form CATALOG.SCHEMA.TABLEfieldName- 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 databasetable- a TableLocationcolumnIndex- 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 databasetableName- a table name in the form CATALOG.SCHEMA.TABLEcolumnIndex- 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 databasetable- 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 databasetableName- 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 databasetable- 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 databasetableName- 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 methodtableLocation- 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 methodtable- 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 methodtableReference- 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- ConnectiontableLocation- 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- ConnectiontableLocation- 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- ConnectiontableLocation- 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- ConnectiontableName- 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 databasetableLocation- Table nametypes- 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 databasecatalog- 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 searchschemaPattern- 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 searchtableNamePattern- A table name pattern. Must match the table name as it is stored in the databasetypes- 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- Connectiontable- 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- ConnectiontableName- 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- Connectiontable- 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- ConnectiontableReference- 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
-
wrapSpatialDataSource
public static DataSource wrapSpatialDataSource(DataSource dataSource)
In order to be able to useWrapper.unwrap(Class)andWrapper.unwrap(Class)to getSpatialResultSetandSpatialResultSetMetaDatathis method wrap the provided dataSource.- Parameters:
dataSource- H2 or PostGIS DataSource- Returns:
- Wrapped DataSource, with spatial methods
-
wrapConnection
public static Connection wrapConnection(Connection connection)
Use this only if DataSource is not available. In order to be able to useWrapper.unwrap(Class)andWrapper.unwrap(Class)to getSpatialResultSetandSpatialResultSetMetaDatathis method wrap the provided connection.- Parameters:
connection- H2 or PostGIS Connection- Returns:
- Wrapped DataSource, with spatial methods
-
attachCancelResultSet
public static PropertyChangeListener attachCancelResultSet(Statement st, org.h2gis.api.ProgressVisitor progressVisitor)
- Parameters:
st- Statement to cancelprogressVisitor- Progress to link with- Returns:
- call
ProgressVisitor.removePropertyChangeListener(java.beans.PropertyChangeListener)with this object as argument
-
getTableType
public static JDBCUtilities.TABLE_TYPE getTableType(Connection connection, TableLocation location) throws SQLException
Return the type of the table using an Enum- Parameters:
connection-location-- Returns:
- Throws:
SQLException
-
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-Connectioncontaining 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-Connectioncontaining the table to check.table-TableLocationof 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-Connectioncontaining 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-Connectioncontaining the table to check.table-TableLocationof 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.
-
-