001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.search.builder.sql;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.jpa.search.builder.QueryStack;
024import ca.uhn.fhir.jpa.search.builder.predicate.CoordsPredicateBuilder;
025import ca.uhn.fhir.jpa.search.builder.predicate.DatePredicateBuilder;
026import ca.uhn.fhir.jpa.search.builder.predicate.ICanMakeMissingParamPredicate;
027import ca.uhn.fhir.jpa.search.builder.predicate.NumberPredicateBuilder;
028import ca.uhn.fhir.jpa.search.builder.predicate.QuantityPredicateBuilder;
029import ca.uhn.fhir.jpa.search.builder.predicate.ResourceLinkPredicateBuilder;
030import ca.uhn.fhir.jpa.search.builder.predicate.StringPredicateBuilder;
031import ca.uhn.fhir.jpa.search.builder.predicate.TokenPredicateBuilder;
032import ca.uhn.fhir.jpa.search.builder.predicate.UriPredicateBuilder;
033import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
034import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
035import jakarta.annotation.Nonnull;
036
037public class PredicateBuilderFactory {
038
039        @Nonnull
040        public static ICanMakeMissingParamPredicate createPredicateBuilderForParamType(
041                        RestSearchParameterTypeEnum theParamType, SearchQueryBuilder theBuilder, QueryStack theQueryStack) {
042                switch (theParamType) {
043                        case NUMBER:
044                                return createNumberPredicateBuilder(theBuilder);
045                        case DATE:
046                                return createDatePredicateBuilder(theBuilder);
047                        case STRING:
048                                return createStringPredicateBuilder(theBuilder);
049                        case TOKEN:
050                                return createTokenPredicateBuilder(theBuilder);
051                        case QUANTITY:
052                                return createQuantityPredicateBuilder(theBuilder);
053                        case URI:
054                                return createUriPredicateBuilder(theBuilder);
055                        case REFERENCE:
056                                return createReferencePredicateBuilder(theQueryStack, theBuilder);
057                        case HAS:
058                        case SPECIAL:
059                                return createCoordsPredicateBuilder(theBuilder);
060                        case COMPOSITE:
061                        default:
062                                throw new InternalErrorException(Msg.code(2593) + "Invalid param type " + theParamType.name());
063                }
064        }
065
066        private static StringPredicateBuilder createStringPredicateBuilder(SearchQueryBuilder theBuilder) {
067                return theBuilder.getSqlBuilderFactory().stringIndexTable(theBuilder);
068        }
069
070        private static NumberPredicateBuilder createNumberPredicateBuilder(SearchQueryBuilder theBuilder) {
071                return theBuilder.getSqlBuilderFactory().numberIndexTable(theBuilder);
072        }
073
074        private static QuantityPredicateBuilder createQuantityPredicateBuilder(SearchQueryBuilder theBuilder) {
075                return theBuilder.getSqlBuilderFactory().quantityIndexTable(theBuilder);
076        }
077
078        private static CoordsPredicateBuilder createCoordsPredicateBuilder(SearchQueryBuilder theBuilder) {
079                return theBuilder.getSqlBuilderFactory().coordsPredicateBuilder(theBuilder);
080        }
081
082        private static TokenPredicateBuilder createTokenPredicateBuilder(SearchQueryBuilder theBuilder) {
083                return theBuilder.getSqlBuilderFactory().tokenIndexTable(theBuilder);
084        }
085
086        private static DatePredicateBuilder createDatePredicateBuilder(SearchQueryBuilder theBuilder) {
087                return theBuilder.getSqlBuilderFactory().dateIndexTable(theBuilder);
088        }
089
090        private static UriPredicateBuilder createUriPredicateBuilder(SearchQueryBuilder theBuilder) {
091                return theBuilder.getSqlBuilderFactory().uriIndexTable(theBuilder);
092        }
093
094        private static ResourceLinkPredicateBuilder createReferencePredicateBuilder(
095                        QueryStack theQueryStack, SearchQueryBuilder theBuilder) {
096                return theBuilder.getSqlBuilderFactory().referenceIndexTable(theQueryStack, theBuilder, false);
097        }
098}