001package ca.uhn.fhir.jpa.dao.data;
002
003import ca.uhn.fhir.jpa.entity.Search;
004import org.springframework.data.domain.Pageable;
005import org.springframework.data.domain.Slice;
006import org.springframework.data.jpa.repository.JpaRepository;
007import org.springframework.data.jpa.repository.Modifying;
008import org.springframework.data.jpa.repository.Query;
009import org.springframework.data.repository.query.Param;
010
011import java.util.Collection;
012import java.util.Date;
013import java.util.Optional;
014
015/*
016 * #%L
017 * HAPI FHIR JPA Server
018 * %%
019 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
020 * %%
021 * Licensed under the Apache License, Version 2.0 (the "License");
022 * you may not use this file except in compliance with the License.
023 * You may obtain a copy of the License at
024 *
025 * http://www.apache.org/licenses/LICENSE-2.0
026 *
027 * Unless required by applicable law or agreed to in writing, software
028 * distributed under the License is distributed on an "AS IS" BASIS,
029 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
030 * See the License for the specific language governing permissions and
031 * limitations under the License.
032 * #L%
033 */
034
035public interface ISearchDao extends JpaRepository<Search, Long>, IHapiFhirJpaRepository {
036
037        @Query("SELECT s FROM Search s LEFT OUTER JOIN FETCH s.myIncludes WHERE s.myUuid = :uuid")
038        Optional<Search> findByUuidAndFetchIncludes(@Param("uuid") String theUuid);
039
040        @Query("SELECT s.myId FROM Search s WHERE (s.myCreated < :cutoff) AND (s.myExpiryOrNull IS NULL OR s.myExpiryOrNull < :now) AND (s.myDeleted IS NULL OR s.myDeleted = FALSE)")
041        Slice<Long> findWhereCreatedBefore(@Param("cutoff") Date theCutoff, @Param("now") Date theNow, Pageable thePage);
042
043        @Query("SELECT s.myId FROM Search s WHERE s.myDeleted = TRUE")
044        Slice<Long> findDeleted(Pageable thePage);
045
046        @Query("SELECT s FROM Search s WHERE s.myResourceType = :type AND mySearchQueryStringHash = :hash AND (s.myCreated > :cutoff) AND s.myDeleted = false AND s.myStatus <> 'FAILED'")
047        Collection<Search> findWithCutoffOrExpiry(@Param("type") String theResourceType, @Param("hash") int theHashCode, @Param("cutoff") Date theCreatedCutoff);
048
049        @Query("SELECT COUNT(s) FROM Search s WHERE s.myDeleted = TRUE")
050        int countDeleted();
051
052        @Modifying
053        @Query("UPDATE Search s SET s.myDeleted = :deleted WHERE s.myId = :pid")
054        void updateDeleted(@Param("pid") Long thePid, @Param("deleted") boolean theDeleted);
055
056        @Modifying
057        @Query("DELETE FROM Search s WHERE s.myId = :pid")
058        void deleteByPid(@Param("pid") Long theId);
059
060}