Class FeatureAnalyzer

java.lang.Object
org.apache.sis.internal.sql.feature.FeatureAnalyzer
Direct Known Subclasses:
QueryAnalyzer, TableAnalyzer

abstract class FeatureAnalyzer extends Object
Defines an application schema inferred from an SQL database (query, table, etc.). This is used by Analyzer for creating Table instances. A view or a custom query is considered as a "virtual" table.

Side effects

Methods shall be invoked as below, in that order. The order is important because some methods have values computed as side-effects and which are required by a subsequent method. This is highly dependent of implementation details and may change in any future version. Methods not listed above can be invoked in any order.
Since:
1.1
Version:
1.2
  • Field Details

    • analyzer

      final Analyzer analyzer
      The parent analyzer for all tables.
    • id

      final TableReference id
      The catalog, schema and table name of the table to analyze. The catalog and schema parts are optional and can be null, but the table is mandatory.
    • primaryKeyClass

      private Class<?> primaryKeyClass
      The class of primary key values, or null if there is no primary key. If the primary key use more than one column, then is the class of an array; it may be an array of primitive type.

      This field is computed as a side-effect of

      invalid reference
      #createAttributes(FeatureTypeBuilder)
      .

      See Also:
    • primaryKeyNullable

      private boolean primaryKeyNullable
      Whether the primary key can have null values.
    • primaryKey

      final Set<String> primaryKey
      The columns that constitute the primary key, or an empty set if there is no primary key.
    • foreignerKeys

      private final Map<String,List<Relation>> foreignerKeys
      Foreigner keys that are referencing primary keys of other tables (Relation.Direction.IMPORT). Keys are column names and values are information about the relation (referenced table, etc). For each value, the list should contain exactly 1 element. But more elements are allowed because the same column could be used as a component of more than one foreigner key. The list may contain nulls.

      This map is populated as a side-effect of getForeignerKeys(Direction.IMPORT, …) call.

    • feature

      final FeatureTypeBuilder feature
      The builder builder where to append attributes and associations.
    • countLowerCaseStarts

      private int countLowerCaseStarts
      Count of the number of columns having a name starting with lower cases.
    • hasGeometry

      boolean hasGeometry
      Whether this table or view contains at least one geometry column.
      See Also:
    • hasRaster

      boolean hasRaster
      Whether this table or view contains at least one raster column.
      See Also:
  • Constructor Details

    • FeatureAnalyzer

      protected FeatureAnalyzer(Analyzer analyzer, TableReference id)
      Creates a new analyzer.
      Parameters:
      id - the catalog, schema and table name of the table to analyze.
  • Method Details

    • getForeignerKeys

      abstract Relation[] getForeignerKeys(Relation.Direction direction) throws SQLException, DataStoreException
      Returns a list of associations between the table analyzed by this method and other tables. The associations are defined by the foreigner keys referencing primary keys.

      Design note

      The table relations can be defined in both ways: the foreigner keys of the analyzed table may be referencing the primary keys of other tables (Relation.Direction.IMPORT), or the primary keys of the analyzed table may be referenced by the foreigner keys of other tables (Relation.Direction.EXPORT). However, in both cases, we will translate that into associations from the analyzed table to the other tables. We cannot rely on IMPORT versus EXPORT direction for determining the association navigability because the database designer's choice may be driven by the need to support multi-occurrences.
      Parameters:
      direction - direction of the foreigner key for which to return components.
      Returns:
      components of the foreigner key for the requested direction.
      Throws:
      SQLException - if an error occurred while fetching information from the database.
      DataStoreException - if a logical error occurred while analyzing the relations.
    • addForeignerKeys

      final void addForeignerKeys(Relation relation)
      Declares that a foreigner key is referencing the primary key of another tables (Relation.Direction.IMPORT). This is a helper method for getForeignerKeys(Relation.Direction) implementations.
    • createAttributes

      abstract Column[] createAttributes() throws Exception
      Configures the feature builder with attributes and "simple" associations inferred from the analyzed table. The ordinary attributes and the "simple" associations (inferred from foreigner keys) are handled together in order to have properties listed in the same order as the columns in the database table.

      For each column in the table that is not a foreigner key, this method creates an AttributeType of the same name. The Java type is inferred from the SQL type, and the attribute multiplicity in inferred from the SQL nullability. Attribute names column names are usually the same, except when a column is used both as a primary key and as foreigner key.

      This method handles only "ordinary" columns and Relation.Direction.IMPORT foreigner keys. The values of those properties are singletons. By contrast, the associations in EXPORT direction are multi-valued.

      Parameters:
      feature - the builder where to add attributes and associations.
      Returns:
      the columns for attribute values (not including associations).
      Throws:
      SQLException - if an error occurred while fetching information from the database.
      DataStoreException - if a logical error occurred while analyzing the relations.
      Exception - for WKB parsing error or other kinds of errors.
    • createAttribute

      final boolean createAttribute(Column column) throws Exception
      Creates a feature attribute for the given column. This is a helper class for createAttributes() implementations.
      Parameters:
      column - the column for which to create an attribute.
      Returns:
      true if the column has been added as an attribute, or false if it was an association.
      Throws:
      Exception
    • createAssociations

      final PrimaryKey createAssociations(Relation[] exportedKeys) throws Exception
      Completes the configuration of feature builder with remaining associations. This method appends the potentially multi-valued associations inferred from Relation.Direction.EXPORT foreigner keys.

      This analysis step computes the primary key (if available) as a side-effect. That information is not available before this step because of type analysis done for providing the PrimaryKey.valueClass information.

      Parameters:
      exportedKeys - value of getForeignerKeys(EXPORT).
      Returns:
      primary key columns, or null if there is no primary key.
      Throws:
      SQLException - if an error occurred while fetching information from the database.
      DataStoreException - if a logical error occurred while analyzing the relations.
      Exception - for WKB parsing error or other kinds of errors.
    • updateCaseHeuristic

      private void updateCaseHeuristic(String column)
      Heuristic rule for determining if the column names starts with lower case or upper case. Words that are all upper-case are ignored on the assumption that they are acronyms.
      Parameters:
      column - the column name.
    • toHeuristicLabel

      private String toHeuristicLabel(String propertyName)
      Rewrites the given property name in a more human-readable form using heuristic rules.
      Parameters:
      propertyName - name of the property to rewrite.
      Returns:
      proposed human-readable property name.
    • duplicatedColumn

      final DataStoreContentException duplicatedColumn(Column column)
      Returns the exception to throw if a column is duplicated.
    • getRemarks

      String getRemarks() throws SQLException
      Returns an optional description of the application schema. This information is not used by computation, but allows to give end-user global information about the schema (s)he is manipulating.
      Throws:
      SQLException
    • buildFeatureType

      final DefaultFeatureType buildFeatureType() throws DataStoreException, SQLException
      Completes the creation of the feature type. This method sets global information about the feature type:
      • The feature name, which is derived from the table name when possible.
      • An optional description of the application schema. This information is not used by computation, but allows to give end-user global information about the schema (s)he is manipulating.
      The remarks can be opportunistically fetched from id.freeText if known by the caller.
      Returns:
      the feature type.
      Throws:
      DataStoreException
      SQLException