Class InfoStatements

java.lang.Object
org.apache.sis.internal.sql.feature.InfoStatements
All Implemented Interfaces:
AutoCloseable, Localized
Direct Known Subclasses:
ExtendedInfo

public class InfoStatements extends Object implements Localized, AutoCloseable
A set of prepared statements to create when first needed and to reuse as long as the connection is in scope. The prepared statement tasks include:
  • Searching for geometric information using SQL queries specialized for Simple Feature table.
  • Fetching a Coordinate Reference System (CRS) from a SRID.
  • Finding a SRID from a Coordinate Reference System (CRS).
This class is not thread-safe. Each instance should be used in a single thread. Instances are created by Database.createInfoStatements(Connection).
Since:
1.2
Version:
1.1
See Also:
  • Field Details

    • SPATIAL_REF_SYS

      static final String SPATIAL_REF_SYS
      The table containing CRS definitions, as specified by ISO 19125 / OGC Simple feature access part 2. Note that the standard specifies table names in upper-case letters, which is also the default case specified by the SQL standard. However, some databases use lower cases instead. This table name can be used unquoted for letting the database engine converts the case.
      See Also:
    • GEOMETRY_COLUMNS

      static final String GEOMETRY_COLUMNS
      The table containing the list of geometry columns, as specified by ISO 19125 / OGC Simple feature access part 2.
      See Also:
    • database

      private final Database<?> database
      The database that created this set of cached statements. This object includes the cache of CRS created from SRID codes and the listeners where to send warnings. A Database object does not contain live JDBC Connection.
    • connection

      private final Connection connection
      Connection to use for creating the prepared statements. This connection will not be closed by this class.
    • geometryColumns

      protected PreparedStatement geometryColumns
      A statement for fetching geometric information for a specific column.
    • wktFromSrid

      private PreparedStatement wktFromSrid
      The statement for fetching CRS Well-Known Text (WKT) from a SRID code.
      See Also:
    • sridFromCRS

      private PreparedStatement sridFromCRS
      The statement for fetching a SRID from a CRS and its set of authority codes.
    • wktReader

      private WKTFormat wktReader
      The object to use for parsing Well-Known Text (WKT), created when first needed.
  • Constructor Details

    • InfoStatements

      protected InfoStatements(Database<?> database, Connection connection)
      Creates an initially empty CachedStatements which will use the given connection for creating PreparedStatements.
      Parameters:
      database - information about the spatial database.
      connection - connection to use for creating the prepared statements.
  • Method Details

    • getLocale

      public final Locale getLocale()
      Returns the locale used for warnings and error messages.
      Specified by:
      getLocale in interface Localized
      Returns:
      the locale, or null if not explicitly defined.
    • getComponentMapping

      public final ValueGetter<?> getComponentMapping(Array array) throws SQLException
      Returns a function for getting values of components in the given array. If no match is found, then this method returns null.
      Parameters:
      array - the array from which to get the mapping of component values.
      Returns:
      converter to the corresponding java type, or null if this class cannot find a mapping.
      Throws:
      SQLException - if the mapping cannot be obtained.
    • appendFrom

      private void appendFrom(SQLBuilder sql, String table)
      Appends a " FROM <table> WHERE " text to the given builder. The table name will be prefixed by catalog and schema name if applicable.
    • appendCondition

      private static SQLBuilder appendCondition(SQLBuilder sql, char prefix, String column)
      Appends a statement after "WHERE" such as ""F_TABLE_NAME = ?".
      Parameters:
      sql - the builder where to add the SQL statement.
      prefix - the column name prefix: 'F' for features or 'R' for rasters.
      column - the column name (e.g. "TABLE_NAME".
      Returns:
      the given SQL builder.
    • prepareIntrospectionStatement

      protected final PreparedStatement prepareIntrospectionStatement(String table, char prefix, String column, String otherColumn) throws SQLException
      Prepares the statement for fetching information about all geometry or raster columns in a specified table. This method is for completeIntrospection(TableReference, Map) implementations.
      Parameters:
      table - name of the geometry table. Standard value is "GEOMETRY_COLUMNS".
      prefix - column name prefix: 'F' for features or 'R' for rasters.
      column - name of the geometry column without prefix. Standard value is "GEOMETRY_COLUMN".
      otherColumn - additional columns or null if none. Standard value is "GEOMETRY_TYPE".
      Returns:
      the prepared statement for querying the geometry table.
      Throws:
      SQLException - if the statement cannot be created.
    • completeIntrospection

      public void completeIntrospection(TableReference source, Map<String,Column> columns) throws Exception
      Gets all geometry and raster columns for the given table and sets information on the corresponding columns. Column instances in the columns map are modified in-place (the map itself is not modified). This method should be invoked before the Column.valueGetter field is set.
      Parameters:
      source - the table for which to get all geometry columns.
      columns - all columns for the specified table. Keys are column names.
      Throws:
      DataStoreContentException - if a logical error occurred in processing data.
      ParseException - if the WKT cannot be parsed.
      SQLException - if a SQL error occurred.
      Exception
    • configureSpatialColumns

      protected final void configureSpatialColumns(PreparedStatement columnQuery, TableReference source, Map<String,Column> columns, InfoStatements.GeometryTypeEncoding typeValueKind) throws Exception
      Implementation of completeIntrospection(TableReference, Map) for geometries, as a separated methods for allowing sub-classes to override above-cited method. May also be used for non-geometric columns such as rasters, in which case the typeValueKind argument shall be null.
      Parameters:
      columnQuery - a statement prepared by prepareIntrospectionStatement(String, char, String, String).
      source - the table for which to get all geometry columns.
      columns - all columns for the specified table. Keys are column names.
      typeValueKind - NUMERIC, TEXTUAL or null if none.
      Throws:
      DataStoreContentException - if a logical error occurred in processing data.
      ParseException - if the WKT cannot be parsed.
      SQLException - if a SQL error occurred.
      Exception
    • fetchCRS

      public final org.opengis.referencing.crs.CoordinateReferenceSystem fetchCRS(int srid) throws Exception
      Gets a Coordinate Reference System for to given SRID. If the given SRID is zero or negative, then this method returns null. Otherwise the CRS is decoded from the database "SPATIAL_REF_SYS" table.
      Parameters:
      srid - the Spatial Reference Identifier (SRID) to resolve as a CRS object.
      Returns:
      the CRS associated to the given SRID, or null if the SRID is zero.
      Throws:
      DataStoreContentException - if the CRS cannot be fetched. Possible reasons are: no entry found in the "SPATIAL_REF_SYS" table, or more than one entry is found, or a single entry exists but has no WKT definition and its authority code is unsupported by SIS.
      ParseException - if the WKT cannot be parsed.
      SQLException - if a SQL error occurred.
      Exception
    • parseCRS

      private org.opengis.referencing.crs.CoordinateReferenceSystem parseCRS(int srid) throws Exception
      Invoked when the requested CRS is not in the cache. This method gets the entry from the "SPATIAL_REF_SYS" table then gets the CRS from its authority code if possible, or fallback on the WKT otherwise.
      Parameters:
      srid - the Spatial Reference Identifier (SRID) of the CRS to create from the database content.
      Returns:
      the CRS created from database content.
      Throws:
      Exception - if an SQL error, parsing error or other error occurred.
    • invalidSRID

      private DataStoreContentException invalidSRID(short message, Object complement, int srid, org.opengis.referencing.NoSuchAuthorityCodeException suppressed)
      Creates the exception to throw for an invalid SRID. The message is expected to have two arguments, complement and srid if that order, where the "complement" can be a table name or a class name depending on the message.
      Parameters:
      message - key of the message to create.
      complement - first argument in message formatting.
      srid - second argument in message formatting.
      suppressed - exception to add as a suppressed exception.
      Returns:
      the exception to throw.
    • findSRID

      public final int findSRID(org.opengis.referencing.crs.CoordinateReferenceSystem crs) throws Exception
      Finds a SRID code from the spatial reference systems table for the given CRS.
      Parameters:
      crs - the CRS for which to find a SRID, or null.
      Returns:
      SRID for the given CRS, or 0 if the given CRS was null.
      Throws:
      Exception - if an SQL error, parsing error or other error occurred.
    • wktReader

      private WKTFormat wktReader()
      Returns the object to use for parsing Well Known Text (CRS). The parser is created when first needed.
    • close

      public void close() throws SQLException
      Closes all prepared statements. This method does not close the connection.
      Specified by:
      close in interface AutoCloseable
      Throws:
      SQLException - if an error occurred while closing a connection.