Class FeatureAdapter
java.lang.Object
org.apache.sis.internal.sql.feature.FeatureAdapter
Converter of
ResultSet rows to Feature instances.
Each FeatureAdapter instance is specific to the set of rows given by a SQL query,
ignoring DISTINCT, ORDER BY and filter conditions in the WHERE clause.
This class does not hold JDBC resources; ResultSet must be provided by the caller.
This object can be prepared once and reused every time the query needs to be executed.
Multi-threading
This class is immutable (except for the cache) and safe for concurrent use by many threads. The content of arrays in this class shall not be modified in order to preserve immutability.- Since:
- 1.1
- Version:
- 1.2
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) final String[]Name of the properties where are stored associations in feature instances.private final Column[]Attributes in feature instances, excluding operations and associations to other tables.(package private) final StringName of the property where to store the association that we cannot handle with otherdependencies.(package private) final FeatureAdapter[]The feature sets referenced through foreigner keys, orEMPTYif none.private static final FeatureAdapter[]An empty array of adapters, used when there is no dependency.private final DefaultFeatureTypeThe type of features to create.private final int[][]One-based indices of the columns to query for eachdependenciesentry.(package private) final intNumber of entries independenciesforRelation.Direction.IMPORT.(package private) final WeakValueHashMap<?,Object> Feature instances already created, ornullif the features created by this iterator are not cached.private final Class<?>The component class of the keys in theinstancesmap, ornullif the keys are not array.(package private) final StringThe SQL statement to execute for creating features, withoutDISTINCTorORDER BYclauses. -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)FeatureAdapter(Table table, DatabaseMetaData metadata) Creates a new adapter for features in the given table.privateFeatureAdapter(Table table, DatabaseMetaData metadata, List<Relation> following, Relation noFollow) Creates a new adapter for features in the given table. -
Method Summary
Modifier and TypeMethodDescriptionprivate static intappendColumn(SQLBuilder sql, String column, Map<String, Integer> columnIndices) Appends a columns in the given builder and remember the column indices.(package private) final AbstractFeaturecreateFeature(InfoStatements stmts, ResultSet result) Creates a feature with attribute values initialized to values fetched from the given result set.(package private) final ObjectgetCacheKey(ResultSet result, int dependency) Returns the key to use for caching the feature of a dependency.private static int[]getColumnIndices(SQLBuilder sql, Relation dependency, Map<String, Integer> columnIndices) Computes the 1-based indices of columns of foreigner keys of given dependency.(package private) final voidsetForeignerKeys(ResultSet source, PreparedStatement target, int dependency) Sets the statement parameters for searching a dependency.
-
Field Details
-
EMPTY
An empty array of adapters, used when there is no dependency. -
featureType
The type of features to create.- See Also:
-
attributes
Attributes in feature instances, excluding operations and associations to other tables. Elements are in the order of columns declared in theSELECT <columns>statement. This array is a shared instance and shall not be modified.- See Also:
-
associationNames
Name of the properties where are stored associations in feature instances. The length of this array shall be equal to thedependenciesarray length. Imported or exported features read bydependencies[i]will be stored in the association namedassociationNames[i]. -
deferredAssociation
Name of the property where to store the association that we cannot handle with otherdependencies. This deferred association may exist because of circular dependency. -
dependencies
The feature sets referenced through foreigner keys, orEMPTYif none. This includes the associations inferred from both the imported and exported keys. The firstimportCountiterators are for imported keys, and the remaining iterators are for the exported keys. -
importCount
final int importCountNumber of entries independenciesforRelation.Direction.IMPORT. The entries immediately following the firstimportCountentries are forRelation.Direction.EXPORT. -
foreignerKeyIndices
private final int[][] foreignerKeyIndicesOne-based indices of the columns to query for eachdependenciesentry. -
instances
Feature instances already created, ornullif the features created by this iterator are not cached. This map is used when requesting a feature by identifier, not when iterating over all features (note: we could perform an opportunistic check in a future SIS version). The same map may be shared by all iterators on the sameTable, butWeakValueHashMapalready provides the required synchronizations.The
FeatureIteratorclass does not require the identifiers to be built from primary key columns. However if this map has been provided byTable.instanceForPrimaryKeys(), then the identifiers need to be primary keys with columns in the exact same order for allowing the same map to be shared. -
keyComponentClass
The component class of the keys in theinstancesmap, ornullif the keys are not array. For example if a primary key is made of two columns of typeString, then this field may be set toString. -
sql
The SQL statement to execute for creating features, withoutDISTINCTorORDER BYclauses. May contain aWHEREclause for fetching a dependency, but not for user-specified filtering.
-
-
Constructor Details
-
FeatureAdapter
FeatureAdapter(Table table, DatabaseMetaData metadata) throws SQLException, InternalDataStoreException Creates a new adapter for features in the given table.- Parameters:
table- the table for which we are creating an adapter.metadata- metadata about the database.- Throws:
SQLExceptionInternalDataStoreException
-
FeatureAdapter
private FeatureAdapter(Table table, DatabaseMetaData metadata, List<Relation> following, Relation noFollow) throws SQLException, InternalDataStoreException Creates a new adapter for features in the given table. This constructor may be invoked recursively for creating adapters for dependencies.- Parameters:
table- the table for which we are creating an adapter.metadata- metadata about the database.following- the relations that we are following. Used for avoiding never ending loop.noFollow- relation to not follow, ornullif none.- Throws:
SQLExceptionInternalDataStoreException
-
-
Method Details
-
appendColumn
private static int appendColumn(SQLBuilder sql, String column, Map<String, Integer> columnIndices) throws InternalDataStoreExceptionAppends a columns in the given builder and remember the column indices. An exception is thrown if the column has already been added (should never happen).- Parameters:
sql- the SQL statement where to add column identifiers after theSELECTclause.column- name of the column to add.columnIndices- map where to add the mapping from column name to 1-based column index.- Throws:
InternalDataStoreException
-
getColumnIndices
private static int[] getColumnIndices(SQLBuilder sql, Relation dependency, Map<String, Integer> columnIndices) throws InternalDataStoreExceptionComputes the 1-based indices of columns of foreigner keys of given dependency. This method also ensure that the SQL statement contains all required columns, adding missing columns in the given SQL builder if necessary.- Parameters:
sql- the SQL statement to complete if there is missing columns.dependency- the dependency for which to get column indices of foreigner keys.columnIndices- the map containing existing column indices, or where to add missing column indices.- Returns:
- indices of columns of foreigner keys of given dependency. Numbering starts at 1.
- Throws:
InternalDataStoreException
-
createFeature
Creates a feature with attribute values initialized to values fetched from the given result set. This method does not follow associations.- Parameters:
stmts- prepared statements for fetching CRS from SRID, ornullif none.result- the result set from which to get attribute values.- Returns:
- the feature with attribute values initialized.
- Throws:
Exception- if an error occurred while reading the database or converting values.
-
getCacheKey
Returns the key to use for caching the feature of a dependency. If the foreigner key uses only one column, we will use the foreigner key value without creating array. But if the foreigner key uses more than one column, then we need to create an array holding all values.- Parameters:
result- the result set over rows expected by this feature adapter.dependency- index of the dependency for which to create a cache key.- Returns:
- key to use for accesses in the
instancesmap, ornullif any component of the key is null. - Throws:
SQLException
-
setForeignerKeys
final void setForeignerKeys(ResultSet source, PreparedStatement target, int dependency) throws SQLException Sets the statement parameters for searching a dependency.- Parameters:
result- the result set over rows expected by this feature adapter.target- the statement on which to set parameters.dependency- index of the dependency for which to set the parameters.- Throws:
SQLException
-