diff options
author | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2023-04-13 22:49:06 +0530 |
---|---|---|
committer | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2023-04-14 18:11:50 +0530 |
commit | c4621cbcb1f9aad97ba3389468546444d1e9f3dc (patch) | |
tree | 3d65556788ac1be9257ffe6d266f0f258ee9b6d1 /cps-ri/src | |
parent | 0bc4f677b59b616ebc5eb5e4fd9529ca2a497cf6 (diff) |
Add OR operator to cps-path
Issue-ID: CPS-1215
Change-Id: I91fdf5bddcc4fc12a8cf9dbce75f77c832c55871
Signed-off-by: Rudrangi Anupriya <ra00745022@techmahindra.com>
Diffstat (limited to 'cps-ri/src')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java index c231595931..ca6e18bad3 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java @@ -22,12 +22,16 @@ package org.onap.cps.spi.repository; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.Queue; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.onap.cps.cpspath.parser.CpsPathPrefixType; import org.onap.cps.cpspath.parser.CpsPathQuery; import org.onap.cps.spi.entities.FragmentEntity; @@ -60,18 +64,7 @@ public class FragmentQueryBuilder { final Map<String, Object> queryParameters = new HashMap<>(); queryParameters.put("anchorId", anchorId); sqlStringBuilder.append(" AND xpath ~ :xpathRegex"); - final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false); - queryParameters.put("xpathRegex", xpathRegex); - if (cpsPathQuery.hasLeafConditions()) { - sqlStringBuilder.append(" AND attributes @> :leafDataAsJson\\:\\:jsonb"); - queryParameters.put("leafDataAsJson", jsonObjectMapper.asJsonString( - cpsPathQuery.getLeavesData())); - } - - addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters); - final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); - setQueryParameters(query, queryParameters); - return query; + return getQuery(cpsPathQuery, sqlStringBuilder, queryParameters); } /** @@ -83,14 +76,27 @@ public class FragmentQueryBuilder { public Query getQueryForCpsPath(final CpsPathQuery cpsPathQuery) { final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE xpath ~ :xpathRegex"); final Map<String, Object> queryParameters = new HashMap<>(); + return getQuery(cpsPathQuery, sqlStringBuilder, queryParameters); + } + + private Query getQuery(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder, + final Map<String, Object> queryParameters) { final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false); queryParameters.put("xpathRegex", xpathRegex); + final List<String> queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType(); if (cpsPathQuery.hasLeafConditions()) { - sqlStringBuilder.append(" AND attributes @> :leafDataAsJson\\:\\:jsonb"); - queryParameters.put("leafDataAsJson", jsonObjectMapper.asJsonString( - cpsPathQuery.getLeavesData())); + sqlStringBuilder.append(" AND ("); + final Queue<String> booleanOperatorsQueue = (queryBooleanOperatorsType == null) ? null : new LinkedList<>( + queryBooleanOperatorsType); + cpsPathQuery.getLeavesData().entrySet().forEach(entry -> { + sqlStringBuilder.append(" attributes @> "); + sqlStringBuilder.append("'" + jsonObjectMapper.asJsonString(entry) + "'"); + if (!CollectionUtils.isEmpty(booleanOperatorsQueue)) { + sqlStringBuilder.append(" " + booleanOperatorsQueue.poll() + " "); + } + }); + sqlStringBuilder.append(")"); } - addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters); final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); setQueryParameters(query, queryParameters); |