Class SpatialDatabasesDSMetadata

java.lang.Object
com.vividsolutions.jump.datastore.spatialdatabases.SpatialDatabasesDSMetadata
All Implemented Interfaces:
DataStoreMetadata
Direct Known Subclasses:
H2DSMetadata, MariadbDSMetadata, OracleDSMetadata, PostgisDSMetadata, SpatialiteDSMetadata

public class SpatialDatabasesDSMetadata extends Object implements DataStoreMetadata
A base class for Spatial Databases DataStore Metadata: DB accessed through JDBC driver, implementing if possible OGC SFSQL. Uses postgis default values =< already implements mechanism for postgis databases

Ex: Postgis, Oracle Spatial, SpatiaLite, SQL Server.

For each spatial db support, a child class should be added based on this class implementing methods as needed. PostGIS was the Spatial DB used for first impl.

TODO: sql injection
Author:
Nicolas Ribot
  • Field Details

    • reader

      protected final org.locationtech.jts.io.WKBReader reader
      OGC WKB reader if needed: TODO: keep only in needed classes ?
    • txtReader

      protected final org.locationtech.jts.io.WKTReader txtReader
      OGC WKB reader if needed: TODO: keep only in needed classes ?
    • conn

      protected DataStoreConnection conn
      The dataStoreConnection to get MD from
    • sridMap

      protected Map<String, SpatialReferenceSystemID> sridMap
      The map of SRIDs found for these MD
    • datasetNameQuery

      protected String datasetNameQuery
      query to get list of spatial tables from the connection. Must return following columns: distinct table_schema, table_name (if several geo columns exist, only one entry should be returned)
    • defaultSchemaName

      protected String defaultSchemaName
      the name of the default schema
    • spatialDbName

      protected String spatialDbName
      The name of this SpatialDatabase
    • spatialExtentQuery1

      protected String spatialExtentQuery1
      The SQL query to get spatial extent. Must return following columns: a geometric column representing the extent
    • spatialExtentQuery2

      protected String spatialExtentQuery2
      The alternate SQL query to get spatial extent (for instance for postgis)
    • geoColumnsQuery

      protected String geoColumnsQuery
      The SQL query to get list of geo columns. Must return column name (String), srid(int) and type (string) (if spatial database does not store type in metadata, can force the base DB geo type in a string: ex: select ... 'SDO_GEOMETRY' as type from all_sdo_geom_metadata
    • sridQuery

      protected String sridQuery
      The SQL query to get a SRID for a given schema name, table name and geometry column
    • coordDimQuery

      protected String coordDimQuery
      The SQL query to get the coordinate dimension for a given schema name, table name and geometry column
    • datasetInfoQuery

      protected String datasetInfoQuery
      The SQL query to get all dataset OGC information
    • dataStoreLayers

      protected ArrayList<DataStoreLayer> dataStoreLayers
      The list of dataStoreLayer for this ds metadata. Built when list of dataset names is requested: avoids to call getGeometryAttributes for each dataset, which takes too much time on big DB. Instead, all dataStoreLayer are built once and filtered out to get dataset names
  • Constructor Details

    • SpatialDatabasesDSMetadata

      public SpatialDatabasesDSMetadata()
    • SpatialDatabasesDSMetadata

      public SpatialDatabasesDSMetadata(DataStoreConnection conn)
  • Method Details

    • getDatasetNameQuery

      public String getDatasetNameQuery()
    • getDefaultSchemaName

      public String getDefaultSchemaName()
    • getSpatialDbName

      public String getSpatialDbName()
    • getSpatialExtentQuery1

      public String getSpatialExtentQuery1(String schema, String table, String attributeName)
    • getSpatialExtentQuery2

      public String getSpatialExtentQuery2(String schema, String table, String attributeName)
    • getGeoColumnsQuery

      public String getGeoColumnsQuery(String datasetName)
    • getSridQuery

      public String getSridQuery(String schemaName, String tableName, String colName)
    • getCoordinateDimensionQuery

      public String getCoordinateDimensionQuery(String schemaName, String tableName, String colName)
    • getDatasetInfoQuery

      public String getDatasetInfoQuery()
    • getDataStoreLayers

      public ArrayList<DataStoreLayer> getDataStoreLayers()
    • getSchemaName

      protected String getSchemaName(String tableName)
      Returns the schema name based on the given tableName: string before . if exists, else returns schemaName
      Parameters:
      tableName - the table name, eventually qualified
      Returns:
      the name of the schema
    • getTableName

      protected String getTableName(String tableName)
      Returns the table name based on the given tableName: string after "." if exists, else returns tableName
      Parameters:
      tableName - the table name, eventually qualified
      Returns:
      the table part of the eventually qualified table name
    • missingGeoException

      protected boolean missingGeoException(Exception e)
      Returns true if the given Exception concerns a missing geometric metadata table
      Parameters:
      e - an exception
      Returns:
      true if the exception is about missing geometry column
    • getDatasetNames

      public String[] getDatasetNames()
      Nico Ribot: 2018-08-07: new method using a query to get all information for datasets in a structure, to avoid querying too much the server
      Specified by:
      getDatasetNames in interface DataStoreMetadata
      Returns:
      dataset (table) names from this spatial Database
    • getExtents

      public org.locationtech.jts.geom.Envelope getExtents(String datasetName, String attributeName)
      Specified by:
      getExtents in interface DataStoreMetadata
      Parameters:
      datasetName - table name (optionally prefixed by a schema name)
      attributeName - column containing the Geometry
      Returns:
      May be null if the extents cannot be determined
    • getGeometryAttributes

      public List<GeometryColumn> getGeometryAttributes(String datasetName)
      Retrieves list of geometric tables from a custom DB Query: Should use OGC metadata geoemtry_columns or equivalent mechanism according to target DB.
      Specified by:
      getGeometryAttributes in interface DataStoreMetadata
      Parameters:
      datasetName - name of a dataset
      Returns:
      list of GeometryColumn's referenced in this dataset
    • getGeometryAttributes

      protected List<GeometryColumn> getGeometryAttributes(String sql, String datasetName)
    • getPrimaryKeyColumns

      public List<PrimaryKeyColumn> getPrimaryKeyColumns(String datasetName) throws SQLException
      Returns PRIMARY KEY columns of dataset names. Uses JDBC database Metadata to get this information, instead of custom SQL queries
      Specified by:
      getPrimaryKeyColumns in interface DataStoreMetadata
      Parameters:
      datasetName - name of the table (optionally prefixed by the schema name)
      Returns:
      the list of columns involved in the Primary Key (generally, a single column)
      Throws:
      SQLException - if the server throws an Exception while trying to get PrimaryKey columns
    • getColumnNames

      public String[] getColumnNames(String datasetName)
      gets the list of columns for the given dataset. TODO: factorize MD retrieval in an Util class ?
      Specified by:
      getColumnNames in interface DataStoreMetadata
      Parameters:
      datasetName - the dataset (table) name
      Returns:
      an array of column names
    • isIndexed

      public boolean isIndexed(String dsName, String column) throws SQLException
      Returns whether column is used by a spatial index (Gist) or not.
      Parameters:
      dsName - a dataset (table) name
      column - a column name
      Returns:
      true if the column is indexed
      Throws:
      SQLException - if an exception occurs during metadata querying
    • getSRID

      public SpatialReferenceSystemID getSRID(String datasetName, String colName) throws SQLException
      Description copied from interface: DataStoreMetadata
      Get the SRID of a table's (geometry) column
      Specified by:
      getSRID in interface DataStoreMetadata
      Parameters:
      datasetName - the dataset name
      colName - the column name
      Returns:
      the SpatialReferenceSystemID for this column
      Throws:
      SQLException - if the server throws an Exception during SRID retrieval
    • querySRID

      protected String querySRID(String datasetName, String colName)
    • getCoordinateDimension

      public int getCoordinateDimension(String datasetName, String colName)
    • getDataStoreConnection

      public DataStoreConnection getDataStoreConnection()
      Description copied from interface: DataStoreMetadata
      DataSoreConnection used by these metadata
      Specified by:
      getDataStoreConnection in interface DataStoreMetadata
      Returns:
      a DataStoreConnection
    • getCreateTableStatement

      public String getCreateTableStatement(FeatureSchema fSchema, String schemaName, String tableName, boolean normalizeColumnNames)
      Returns the CREATE TABLE statement corresponding to this feature schema. The statement includes column names and data types, but neither geometry column nor primary key.
      Parameters:
      fSchema - client feature schema
      schemaName - unquoted schema name or null to use default schema
      tableName - unquoted table name
      normalizeColumnNames - whether column names must be normalized (lowercased and without special characters) or not
      Returns:
      the sql string to create the table
    • createColumnList

      public String createColumnList(FeatureSchema schema, boolean includeSQLDataType, boolean includeGeometry, boolean includeExternalPK, boolean includeReadOnly, boolean normalizeColumnNames)
      Returns a comma-separated list of attributes included in schema.
      Parameters:
      schema - the FeatureSchema
      includeSQLDataType - if true, each attribute name is immediately followed by its corresponding sql DataType
      includeGeometry - if true, the geometry attribute is included
      includeExternalPK - if true, the external primary key is included
      includeReadOnly - if true, readOnly attributes are included
      normalizeColumnNames - whether feature attribute names must be normalized (lower case without spacial characters) to specify table column names.
      Returns:
      the sql string containing the list of columns for this schema
    • getAddSpatialIndexStatement

      public String getAddSpatialIndexStatement(String schemaName, String tableName, String geometryColumn)
      Create statement to add a spatial index on the specified geometry column. The geometry column name must have its final form. Attribute name normalization is the responsability of the calling method.
      Parameters:
      schemaName - unquoted schema name or null if default schema is used
      tableName - unquoted table name
      geometryColumn - unquoted geometry column name
      Returns:
      a sql string to add a spatial index
    • getAddGeometryColumnStatement

      public String getAddGeometryColumnStatement(String schemaName, String tableName, String geometryColumn, int srid, String geometryType, int dim)
      Creates the query String to add a GeometryColumn.

      Note 1 : In PostGIS 2.x, srid=-1 is automatically converted to srid=0 by AddGeometryColumn function.

      Note 2 : To stay compatible with PostGIS 1.x, last argument of AddGeometryColumn is omitted. As a consequence, geometry type uses type modifier rather than constraints (new default behaviour in 2.x)

      The geometry column name must have its final form. Attribute name normalization is the responsability of the calling method.

      Parameters:
      schemaName - the name of the Schema
      tableName - the name of the Table
      geometryColumn - the name of the geometry column
      srid - the SRID for the geometry column
      geometryType - the (SQL) geometry type as a String
      dim - the coordinate dimension (2 or 3)
      Returns:
      a SQL query string to add a geometry column to a table
    • getDbTypeName

      protected String getDbTypeName(AttributeType type)
      Return standard SQL data type for OpenJUMP AttributeType. This method must be overloaded by specific database oj2dbType
      Parameters:
      type - OpenJUMP attribute type
      Returns:
      the database datatype