Class NewQueryBuilder


  • public class NewQueryBuilder
    extends java.lang.Object
    This is the JDBC implementation of a query builder for the IBM FHIR Server JDBC persistence layer schema. The builder constructs an intermediate "domain" model of the query (which isn't concerned about the intricacies of how to join two tables). This domain query is then translated in a Select statement model which can be built piece by piece. The statement is then rendered into a string which is the SQL select statement executed by the database.
    This approach improves maintainability by separating the logical structure of the query from the physical join syntax.

    Useful table reference:
     ------------------------
     PARAMETER_NAMES        reference table of parameter names
     RESOURCE_TYPES         reference table of resource type names
     COMMON_TOKEN_VALUES    normalized set of token values
     CODE_SYSTEMS           normalized set of code-system values
     LOGICAL_RESOURCES      whole-system table of resources
     xx_LOGICAL_RESOURCES   resource-specific table of logical resources
     xx_RESOURCES           resource-specific table of resource versions
     xx_STR_VALUES          string search parameters belonging to a resource
     xx_RESOURCE_TOKEN_REFS map table connecting a given resource to a set of token values
     xx_NUMBER_VALUES       number search parameters belonging to a resource
     xx_DATE_VALUES         date search parameters belonging to a resource
     xx_LATLNG_VALUES       lat/lng search parameters belonging to a resource
     xx_QUANTITY_VALUES     quantity search parameters belonging to a resource
     xx_TOKEN_VALUES_V      view hiding the join between xx_RESOURCE_TOKEN_REFS and COMMON_TOKEN_VALUES
     
    Useful column reference:
     ------------------------
     RESOURCE_TYPE_NAME     the formal name of the resource type e.g. 'Patient'
     RESOURCE_TYPE_ID       FK to the RESOURCE_TYPES table
     LOGICAL_ID             the VARCHAR holding the logical-id of the resource. Unique for a given resource type
     LOGICAL_RESOURCE_ID    the database BIGINT
     CURRENT_RESOURCE_ID    the unique BIGINT id of the latest resource version for the logical resource
     VERSION_ID             INT resource version number incrementing by 1
     IS_DELETED             CHAR(1) flag indicating the current deletion status of the resource or resource-version.
     RESOURCE_ID            the PK of the version-specific resource. Now only used as the target for CURRENT_RESOURCE_ID
     
    • Method Detail

      • buildCountQuery

        public Select buildCountQuery​(java.lang.Class<?> resourceType,
                                      FHIRSearchContext searchContext,
                                      SchemaType schemaType)
                               throws java.lang.Exception
        Builds a query that returns the count of the search results that would be found by applying the search parameters contained within the passed search context. The count query is a simpler version of the main search query because there's no need to join against the xx_RESOURCES table at the end because the DATA column is not needed. This is now possible because the IS_DELETED flag is now denormalized and stored at the xx_LOGICAL_RESOURCES level as well as per resource version (in the xx_RESOURCES table).
        Parameters:
        resourceType - - The type of resource being searched for.
        searchContext - - The search context containing the search parameters.
        schemaType - - The type of schema we are querying (PLAIN, DISTRIBUTED etc)
        Returns:
        String - A count query SQL statement
        Throws:
        java.lang.Exception
      • buildQuery

        public Select buildQuery​(java.lang.Class<?> resourceType,
                                 FHIRSearchContext searchContext,
                                 SchemaType schemaType)
                          throws java.lang.Exception
        Construct a FHIR search query
        Parameters:
        resourceType -
        searchContext -
        schemaType - - The type of schema we are querying (PLAIN, DISTRIBUTED etc)
        Returns:
        Throws:
        java.lang.Exception
      • buildIncludeQuery

        public Select buildIncludeQuery​(java.lang.Class<?> resourceType,
                                        FHIRSearchContext searchContext,
                                        InclusionParameter inclusionParm,
                                        java.util.List<java.lang.Long> logicalResourceIds,
                                        java.lang.String inclusionType,
                                        SchemaType schemaType)
                                 throws java.lang.Exception
        Builds a query that returns included resources.
        Parameters:
        resourceType - - the type of resource being searched for.
        searchContext - - the search context containing the search parameters.
        inclusionParm - - the inclusion parameter for which the query is being built.
        ids - - the list of logical resource IDs the query will run against.
        inclusionType - - either INCLUDE or REVINCLUDE.
        schemaType - - The type of schema we are querying (PLAIN, DISTRIBUTED etc)
        Returns:
        Select the query to fetch the matching list of included resources
        Throws:
        java.lang.Exception
      • buildWholeSystemDataQuery

        public Select buildWholeSystemDataQuery​(FHIRSearchContext searchContext,
                                                java.util.Map<java.lang.Integer,​java.util.List<java.lang.Long>> resourceTypeIdToLogicalResourceIdMap,
                                                SchemaType schemaType)
                                         throws java.lang.Exception
        Builds a query that returns resource data for the specified whole-system search.
        Parameters:
        searchContext - - the search context.
        resourceTypeIdToLogicalResourceIdMap - - map of resource type Ids to logical resource Ids
        schemaType - - The type of schema we are querying (PLAIN, DISTRIBUTED etc)
        Returns:
        Select the query to fetch the specified list of resources
        Throws:
        java.lang.Exception
      • getOperator

        protected java.lang.String getOperator​(QueryParameter queryParm)
      • getOperator

        protected java.lang.String getOperator​(QueryParameter queryParm,
                                               java.lang.String defaultOverride)
        Map the Modifier in the passed Parameter to a supported query operator. If the mapping results in the default operator, override the default operator with the passed operator if the passed operator is not null.
        Parameters:
        queryParm - - A valid query Parameter.
        defaultOverride - - An operator that should override the default operator.
        Returns:
        A supported operator.