001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 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.jpa.search.builder.QueryStack;
023import ca.uhn.fhir.jpa.search.builder.predicate.CoordsPredicateBuilder;
024import ca.uhn.fhir.jpa.search.builder.predicate.DatePredicateBuilder;
025import ca.uhn.fhir.jpa.search.builder.predicate.ICanMakeMissingParamPredicate;
026import ca.uhn.fhir.jpa.search.builder.predicate.NumberPredicateBuilder;
027import ca.uhn.fhir.jpa.search.builder.predicate.QuantityPredicateBuilder;
028import ca.uhn.fhir.jpa.search.builder.predicate.ResourceLinkPredicateBuilder;
029import ca.uhn.fhir.jpa.search.builder.predicate.StringPredicateBuilder;
030import ca.uhn.fhir.jpa.search.builder.predicate.TokenPredicateBuilder;
031import ca.uhn.fhir.jpa.search.builder.predicate.UriPredicateBuilder;
032import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036public class PredicateBuilderFactory {
037
038        private static final Logger ourLog = LoggerFactory.getLogger(PredicateBuilderFactory.class);
039
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                                // we don't expect to see this
063                                ourLog.error("Invalid param type " + theParamType.name());
064                                return null;
065                }
066        }
067
068        private static StringPredicateBuilder createStringPredicateBuilder(SearchQueryBuilder theBuilder) {
069                StringPredicateBuilder sp = theBuilder.getSqlBuilderFactory().stringIndexTable(theBuilder);
070                return sp;
071        }
072
073        private static NumberPredicateBuilder createNumberPredicateBuilder(SearchQueryBuilder theBuilder) {
074                NumberPredicateBuilder np = theBuilder.getSqlBuilderFactory().numberIndexTable(theBuilder);
075                return np;
076        }
077
078        private static QuantityPredicateBuilder createQuantityPredicateBuilder(SearchQueryBuilder theBuilder) {
079                QuantityPredicateBuilder qp = theBuilder.getSqlBuilderFactory().quantityIndexTable(theBuilder);
080                return qp;
081        }
082
083        private static CoordsPredicateBuilder createCoordsPredicateBuilder(SearchQueryBuilder theBuilder) {
084                CoordsPredicateBuilder cp = theBuilder.getSqlBuilderFactory().coordsPredicateBuilder(theBuilder);
085                return cp;
086        }
087
088        private static TokenPredicateBuilder createTokenPredicateBuilder(SearchQueryBuilder theBuilder) {
089                TokenPredicateBuilder tp = theBuilder.getSqlBuilderFactory().tokenIndexTable(theBuilder);
090                return tp;
091        }
092
093        private static DatePredicateBuilder createDatePredicateBuilder(SearchQueryBuilder theBuilder) {
094                DatePredicateBuilder dp = theBuilder.getSqlBuilderFactory().dateIndexTable(theBuilder);
095                return dp;
096        }
097
098        private static UriPredicateBuilder createUriPredicateBuilder(SearchQueryBuilder theBuilder) {
099                UriPredicateBuilder up = theBuilder.getSqlBuilderFactory().uriIndexTable(theBuilder);
100                return up;
101        }
102
103        private static ResourceLinkPredicateBuilder createReferencePredicateBuilder(
104                        QueryStack theQueryStack, SearchQueryBuilder theBuilder) {
105                ResourceLinkPredicateBuilder retVal =
106                                theBuilder.getSqlBuilderFactory().referenceIndexTable(theQueryStack, theBuilder, false);
107                return retVal;
108        }
109}