Class QueryBuilder
java.lang.Object
org.odpi.openmetadata.adapters.repositoryservices.postgres.repositoryconnector.database.QueryBuilder
Translates open metadata query requests into SQL fragments that can be assembled before issuing
then to the database.
-
Constructor Summary
ConstructorsConstructorDescriptionQueryBuilder(String principleTableName, String propertyTableName, OMRSRepositoryHelper repositoryHelper, String repositoryName) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionReturn the where clause built up from the query parameters supplied.getClassificationMembershipClause(String entityGUIDColumn) Return the complete SQL that restricts a query to the entities the classification search asks for.Return the where clause for each sub-select needed to express the classification search.getLocalMetadataCollectionClause(String localMetadataCollectionId) Derive a SQL fragment that restricts a count query to only the instances that the named repository is responsible for counting: those it homes itself (metadataCollectionId matches) and those it has been assigned to count on behalf of a non-cohort provenance (replicatedBy matches).getPrimaryKeysClause(String instanceGUID, long version, String classificationName) Return the where clause that identified the primary key of an entity, relationship or classification.getPropertyJoinQuery(String principleTableName, String propertiesTableName, String columnSelection) Join the principle table with its associated attributes table.getSequenceAndPaging(String principleTableName) The sequencing (order by) and paging (limit/offset) can only be added at the end and may only includebooleanReturn whether the caller asked for entities that do NOT carry the classifications named in the search.voidsetAsOfTime(Date asOfTime) Set up the database time to issue the query for - null means the latest versionvoidsetGUIDList(List<String> guidList) Set up a list of GUIDs as part of a search.voidsetLimitResultsByClassification(List<String> limitResultsByClassification) Set up the list of classifications that must be present on all returned entities.voidsetLimitResultsByStatus(List<InstanceStatus> limitResultsByStatus) Set up the list od current statuses that an instance must have to be returned.voidsetMatchProperties(InstanceProperties matchProperties, MatchCriteria matchCriteria) Set up the properties that should be matched during the query.voidsetPaging(int fromElement, int pageSize) Set up the required paging.voidsetRelationshipEndCriteria(List<String> end1EntityGUIDs, String end1EntityTypeGUID, List<String> end2EntityGUIDs, String end2EntityTypeGUID, EndMatchCriteria endMatchCriteria) Capture the criteria for matching the ends in a findRelationship search.voidsetRelationshipEndGUID(String relationshipEndGUID) Set up an entity GUID for searching for an entity's relationships.voidsetSearchClassifications(SearchClassifications matchClassifications) Set up an optional list of entity classifications to match.voidsetSearchProperties(SearchProperties searchProperties) Set up the search properties.voidsetSearchString(String searchString, boolean startsWith, boolean endsWith, boolean ignoreCase) Set up the Java regular expression used to match against any of the String property values within instances of the specified type(s).voidsetSequencingOrder(SequencingOrder sequencingOrder, String sequencingProperty) Set up the sequencing order required.voidsetTypeGUID(String typeGUID, String typeGUIDParameterName) Set up the type information for the query.voidsetTypeGUID(String typeGUID, String typeGUIDParameterName, List<String> subTypeGUIDs, boolean skipSubtypes, String subTypeGUIDsParameterName) Set up the type information for the query.voidsetTypeGUID(String typeGUID, String typeGUIDParameterName, List<String> subTypeGUIDs, String subTypeGUIDsParameterName) Set up the type information for the query.toString()toString() JSON-style
-
Constructor Details
-
QueryBuilder
public QueryBuilder(String principleTableName, String propertyTableName, OMRSRepositoryHelper repositoryHelper, String repositoryName) Constructor.- Parameters:
principleTableName- name of entity, relationship, classificationpropertyTableName- name of entity_attribute_value, classification_attribute_value, relationship_attribute_valuerepositoryHelper- helperrepositoryName- name of this repository
-
-
Method Details
-
setRelationshipEndGUID
Set up an entity GUID for searching for an entity's relationships.- Parameters:
relationshipEndGUID- entity GUID
-
getLocalMetadataCollectionClause
Derive a SQL fragment that restricts a count query to only the instances that the named repository is responsible for counting: those it homes itself (metadataCollectionId matches) and those it has been assigned to count on behalf of a non-cohort provenance (replicatedBy matches). This avoids double-counting instances that are replicated across more than one member of a cohort when a federated count sums the results from each repository.
This method must only be called when building the WHERE clause for the countEntities()/countRelationships() queries - it is deliberately kept separate from getAsOfTimeWhereClause() (used by every other query, including findEntities()/findRelationships()) so that every other query continues to return every stored matching instance, including replicas, for federation to deduplicate by GUID.- Parameters:
localMetadataCollectionId- unique identifier of the repository issuing the count query- Returns:
- fragment of SQL
-
setSearchString
public void setSearchString(String searchString, boolean startsWith, boolean endsWith, boolean ignoreCase) Set up the Java regular expression used to match against any of the String property values within instances of the specified type(s).- Parameters:
searchString- desired valuestartsWith- true if the search should be for strings that start with the search stringendsWith- true if the search should be for strings that end with the search stringignoreCase- true if the search should be case-insensitive
-
setMatchProperties
Set up the properties that should be matched during the query.- Parameters:
matchProperties- Optional list of entity properties to match (where any String property's value should be defined as a Java regular expression, even if it should be an exact match).matchCriteria- Enum defining how the match properties should be matched to the entities in the repository.
-
setRelationshipEndCriteria
public void setRelationshipEndCriteria(List<String> end1EntityGUIDs, String end1EntityTypeGUID, List<String> end2EntityGUIDs, String end2EntityTypeGUID, EndMatchCriteria endMatchCriteria) Capture the criteria for matching the ends in a findRelationship search.- Parameters:
end1EntityGUIDs- optional list of entity guids used to match end 1 of the relationships.end1EntityTypeGUID- optional unique identifier of the type that the entity at end 1 must belong to. Subtypes of the named type match too. This is independent of end1EntityGUIDs: supplying the type on its own, with the guids left null, asks for the relationships that start at any entity of that type.end2EntityGUIDs- optional list of entity guids used to match end 2 of the relationships.end2EntityTypeGUID- optional unique identifier of the type that the entity at end 2 must belong to. Subtypes of the named type match too. This is independent of end2EntityGUIDs: supplying the type on its own, with the guids left null, asks for the relationships that end at any entity of that type.endMatchCriteria- criteria for matching the ends of the relationships.
-
setSearchProperties
Set up the search properties.- Parameters:
searchProperties- Optional list of entity property conditions to match.
-
setSearchClassifications
Set up an optional list of entity classifications to match.- Parameters:
matchClassifications- match classifications
-
getClassificationSubSelectWhereClauses
Return the where clause for each sub-select needed to express the classification search.
ANY and NONE need one sub-select: the entities carrying any of the named classifications, either included or excluded. ALL needs one per classification, because an entity carries each of them on a separate row and a single row cannot satisfy two names at once - which is why asking for two classifications used to return nothing at all.- Returns:
- one where clause per sub-select; empty if there is no classification search
- Throws:
RepositoryErrorException- problem building the conditions
-
getClassificationMembershipClause
public String getClassificationMembershipClause(String entityGUIDColumn) throws RepositoryErrorException Return the complete SQL that restricts a query to the entities the classification search asks for.
The whole membership expression is built here rather than handing callers the pieces to assemble. The pieces are easy to assemble wrongly - each sub-select needs the base clauses as well as its own classification condition, ALL needs one membership test per classification while ANY and NONE need one in total, and NONE inverts the membership instead of the condition. Getting any of those wrong does not fail: the query simply stops filtering, or filters on the wrong thing, and returns a plausible answer.- Parameters:
entityGUIDColumn- the qualified entity guid column the membership applies to- Returns:
- SQL fragment, beginning with "and", or empty if there is no classification search
- Throws:
RepositoryErrorException- problem building the conditions
-
isNegatedClassificationMatch
public boolean isNegatedClassificationMatch()Return whether the caller asked for entities that do NOT carry the classifications named in the search. The sub-select this builder produces always selects the entities that DO carry them, so a NONE request is served by excluding that set rather than by selecting a different one.- Returns:
- true if the classification membership should be negated
-
setLimitResultsByClassification
Set up the list of classifications that must be present on all returned entities.- Parameters:
limitResultsByClassification- list of classification names
-
setTypeGUID
Set up the type information for the query.- Parameters:
typeGUID- unique identifier of desired type.typeGUIDParameterName- parameter name use to pass the type guid
-
setTypeGUID
public void setTypeGUID(String typeGUID, String typeGUIDParameterName, List<String> subTypeGUIDs, String subTypeGUIDsParameterName) Set up the type information for the query.- Parameters:
typeGUID- unique identifier of desired type.typeGUIDParameterName- parameter name use to pass the type guidsubTypeGUIDs- list of unique identifiers for the desired typesubTypeGUIDsParameterName- parameter name use to pass the subtype guid list
-
setTypeGUID
public void setTypeGUID(String typeGUID, String typeGUIDParameterName, List<String> subTypeGUIDs, boolean skipSubtypes, String subTypeGUIDsParameterName) Set up the type information for the query.- Parameters:
typeGUID- unique identifier of desired type.typeGUIDParameterName- parameter name use to pass the type guidsubTypeGUIDs- list of unique identifiers for the subtypes to include in (or, if skipSubtypes is true, exclude from) the query results.skipSubtypes- if true, subTypeGUIDs is treated as the list of subtypes to exclude from the query results rather than the only subtypes to include. Ignored if subTypeGUIDs is null or empty.subTypeGUIDsParameterName- parameter name use to pass the subtype guid list
-
setLimitResultsByStatus
Set up the list od current statuses that an instance must have to be returned.- Parameters:
limitResultsByStatus- list of statuses
-
setAsOfTime
Set up the database time to issue the query for - null means the latest version- Parameters:
asOfTime- date or null
-
setSequencingOrder
Set up the sequencing order required.- Parameters:
sequencingOrder- order to return resultssequencingProperty- optional property if sequencing on specific property results
-
setPaging
public void setPaging(int fromElement, int pageSize) Set up the required paging.- Parameters:
fromElement- starting from element (0 for first)pageSize- maximum elements that can be returned
-
setGUIDList
Set up a list of GUIDs as part of a search.- Parameters:
guidList- list of GUID to search for
-
getPrimaryKeysClause
Return the where clause that identified the primary key of an entity, relationship or classification.- Parameters:
instanceGUID- unique identifier of the associated entity or relationshipversion- the version of the instanceclassificationName- optional classification name- Returns:
- where clause
-
getPropertyJoinQuery
public String getPropertyJoinQuery(String principleTableName, String propertiesTableName, String columnSelection) Join the principle table with its associated attributes table.- Parameters:
principleTableName- name of main tablepropertiesTableName- name of attributes tablecolumnSelection- name of a specific column to select- Returns:
- the join part of the SQL query
-
getAsOfTimeWhereClause
Return the where clause built up from the query parameters supplied.- Returns:
- SQL command fragment
- Throws:
RepositoryErrorException
-
getSequenceAndPaging
The sequencing (order by) and paging (limit/offset) can only be added at the end and may only include- Parameters:
principleTableName- main table that the ordering will occur on- Returns:
- sql fragment
-
toString
toString() JSON-style
-