* Copyright (C) 2021-2023 Nordix Foundation.\r
* Modifications Copyright (C) 2020-2021 Bell Canada.\r
* Modifications Copyright (C) 2020-2021 Pantheon.tech.\r
+ * Modifications Copyright (C) 2023 TechMahindra Ltd.\r
* ================================================================================\r
* Licensed under the Apache License, Version 2.0 (the "License");\r
* you may not use this file except in compliance with the License.\r
import java.util.List;\r
import java.util.Optional;\r
import org.onap.cps.spi.entities.AnchorEntity;\r
+import org.onap.cps.spi.entities.DataspaceEntity;\r
import org.onap.cps.spi.entities.FragmentEntity;\r
-import org.onap.cps.spi.entities.FragmentExtract;\r
import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
+import org.onap.cps.spi.utils.EscapeUtils;\r
import org.springframework.data.jpa.repository.JpaRepository;\r
import org.springframework.data.jpa.repository.Modifying;\r
import org.springframework.data.jpa.repository.Query;\r
\r
@Repository\r
public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery,\r
- FragmentNativeRepository {\r
+ FragmentPrefetchRepository {\r
\r
Optional<FragmentEntity> findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath);\r
\r
new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));\r
}\r
\r
- boolean existsByAnchorId(int anchorId);\r
+ @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
+ nativeQuery = true)\r
+ List<FragmentEntity> findByAnchorIdAndXpathIn(@Param("anchorId") long anchorId,\r
+ @Param("xpaths") String[] xpaths);\r
\r
- @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor")\r
- List<FragmentExtract> findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity);\r
+ default List<FragmentEntity> findByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
+ final Collection<String> xpaths) {\r
+ return findByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
+ }\r
+\r
+ @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId \n"\r
+ + "AND xpath LIKE :escapedXpath||'[@%]' AND xpath NOT LIKE :escapedXpath||'[@%]/%[@%]'",\r
+ nativeQuery = true)\r
+ List<FragmentEntity> findListByAnchorIdAndEscapedXpath(@Param("anchorId") long anchorId,\r
+ @Param("escapedXpath") String escapedXpath);\r
+\r
+ default List<FragmentEntity> findListByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {\r
+ final String escapedXpath = EscapeUtils.escapeForSqlLike(xpath);\r
+ return findListByAnchorIdAndEscapedXpath(anchorEntity.getId(), escapedXpath);\r
+ }\r
+\r
+ @Query(value = "SELECT fragment.* FROM fragment JOIN anchor ON anchor.id = fragment.anchor_id "\r
+ + "WHERE dataspace_id = :dataspaceId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
+ List<FragmentEntity> findByDataspaceIdAndXpathIn(@Param("dataspaceId") int dataspaceId,\r
+ @Param("xpaths") String[] xpaths);\r
+\r
+ default List<FragmentEntity> findByDataspaceAndXpathIn(final DataspaceEntity dataspaceEntity,\r
+ final Collection<String> xpaths) {\r
+ return findByDataspaceIdAndXpathIn(dataspaceEntity.getId(), xpaths.toArray(new String[0]));\r
+ }\r
+\r
+ @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId LIMIT 1", nativeQuery = true)\r
+ Optional<FragmentEntity> findOneByAnchorId(@Param("anchorId") long anchorId);\r
+\r
+ @Modifying\r
+ @Query(value = "DELETE FROM fragment WHERE anchor_id = ANY (:anchorIds)", nativeQuery = true)\r
+ void deleteByAnchorIdIn(@Param("anchorIds") long[] anchorIds);\r
+\r
+ default void deleteByAnchorIn(final Collection<AnchorEntity> anchorEntities) {\r
+ deleteByAnchorIdIn(anchorEntities.stream().map(AnchorEntity::getId).mapToLong(id -> id).toArray());\r
+ }\r
+\r
+ @Modifying\r
+ @Query(value = "DELETE FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)", nativeQuery = true)\r
+ void deleteByAnchorIdAndXpaths(@Param("anchorId") long anchorId, @Param("xpaths") String[] xpaths);\r
+\r
+ default void deleteByAnchorIdAndXpaths(final long anchorId, final Collection<String> xpaths) {\r
+ deleteByAnchorIdAndXpaths(anchorId, xpaths.toArray(new String[0]));\r
+ }\r
\r
@Modifying\r
- @Query("DELETE FROM FragmentEntity WHERE anchor IN (:anchors)")\r
- void deleteByAnchorIn(@Param("anchors") Collection<AnchorEntity> anchorEntities);\r
-\r
- @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor"\r
- + " AND (xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%'))")\r
- List<FragmentExtract> findByAnchorAndParentXpath(@Param("anchor") AnchorEntity anchorEntity,\r
- @Param("parentXpath") String parentXpath);\r
-\r
- @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
- + " CAST(attributes AS TEXT) AS attributes"\r
- + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
- + " AND xpath ~ :xpathRegex",\r
+ @Query(value = "DELETE FROM fragment f WHERE anchor_id = :anchorId AND xpath LIKE ANY (:xpathPatterns)",\r
nativeQuery = true)\r
- List<FragmentExtract> quickFindWithDescendants(@Param("anchorId") int anchorId,\r
- @Param("xpathRegex") String xpathRegex);\r
+ void deleteByAnchorIdAndXpathLikeAny(@Param("anchorId") long anchorId,\r
+ @Param("xpathPatterns") String[] xpathPatterns);\r
\r
- @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND xpath IN :xpaths")\r
- List<String> findAllXpathByAnchorAndXpathIn(@Param("anchor") AnchorEntity anchorEntity,\r
- @Param("xpaths") Collection<String> xpaths);\r
+ default void deleteListsByAnchorIdAndXpaths(long anchorId, Collection<String> xpaths) {\r
+ deleteByAnchorIdAndXpathLikeAny(anchorId,\r
+ xpaths.stream().map(xpath -> EscapeUtils.escapeForSqlLike(xpath) + "[@%").toArray(String[]::new));\r
+ }\r
+\r
+ @Query(value = "SELECT xpath FROM fragment WHERE anchor_id = :anchorId AND xpath = ANY (:xpaths)",\r
+ nativeQuery = true)\r
+ List<String> findAllXpathByAnchorIdAndXpathIn(@Param("anchorId") long anchorId,\r
+ @Param("xpaths") String[] xpaths);\r
+\r
+ default List<String> findAllXpathByAnchorAndXpathIn(final AnchorEntity anchorEntity,\r
+ final Collection<String> xpaths) {\r
+ return findAllXpathByAnchorIdAndXpathIn(anchorEntity.getId(), xpaths.toArray(new String[0]));\r
+ }\r
\r
boolean existsByAnchorAndXpathStartsWith(AnchorEntity anchorEntity, String xpath);\r
\r
- @Query("SELECT xpath FROM FragmentEntity WHERE anchor = :anchor AND parentId IS NULL")\r
- List<String> findAllXpathByAnchorAndParentIdIsNull(@Param("anchor") AnchorEntity anchorEntity);\r
-\r
- @Query(value\r
- = "WITH RECURSIVE parent_search AS ("\r
- + " SELECT id, 0 AS depth "\r
- + " FROM fragment "\r
- + " WHERE anchor_id = :anchorId AND xpath IN :xpaths "\r
- + " UNION "\r
- + " SELECT c.id, depth + 1 "\r
- + " FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
- + " WHERE depth <= (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
- + ") "\r
- + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
- + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
- nativeQuery = true\r
- )\r
- List<FragmentExtract> findExtractsWithDescendants(@Param("anchorId") int anchorId,\r
- @Param("xpaths") Collection<String> xpaths,\r
- @Param("maxDepth") int maxDepth);\r
+ @Query(value = "SELECT * FROM fragment WHERE anchor_id = :anchorId AND parent_id IS NULL", nativeQuery = true)\r
+ List<FragmentEntity> findRootsByAnchorId(@Param("anchorId") long anchorId);\r
+\r
}\r