diff options
author | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2023-05-30 17:20:20 +0530 |
---|---|---|
committer | Rudrangi Anupriya <ra00745022@techmahindra.com> | 2023-05-30 20:32:36 +0530 |
commit | 2696de664b097c8dceb4332e9896417835e77178 (patch) | |
tree | 5a455bbe60a5bf02519a39864956057747af2244 /cps-ri/src/main | |
parent | 325ac091ddea291c5659fa6e803f132a578deb14 (diff) |
Add <,> operators support to cps-path
Issue-ID: CPS-1273
Change-Id: I5d562463b9a49abfe0436047a637857d10596fff
Signed-off-by: Rudrangi Anupriya <ra00745022@techmahindra.com>
Diffstat (limited to 'cps-ri/src/main')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java | 49 |
1 files changed, 32 insertions, 17 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 72750dcc92..ba94d56b1c 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 @@ -23,7 +23,6 @@ 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; @@ -36,6 +35,7 @@ import org.onap.cps.cpspath.parser.CpsPathQuery; import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.FragmentEntity; +import org.onap.cps.spi.exceptions.CpsPathException; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.stereotype.Component; @@ -141,25 +141,40 @@ public class FragmentQueryBuilder { private void addLeafConditions(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder) { if (cpsPathQuery.hasLeafConditions()) { - sqlStringBuilder.append(" AND ("); - final List<String> queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType(); - final Queue<String> booleanOperatorsQueue = (queryBooleanOperatorsType == null) ? null : new LinkedList<>( - queryBooleanOperatorsType); - cpsPathQuery.getLeavesData().entrySet().forEach(entry -> { - sqlStringBuilder.append(" attributes @> "); - sqlStringBuilder.append("'"); - sqlStringBuilder.append(jsonObjectMapper.asJsonString(entry)); - sqlStringBuilder.append("'"); - if (!(booleanOperatorsQueue == null || booleanOperatorsQueue.isEmpty())) { - sqlStringBuilder.append(" "); - sqlStringBuilder.append(booleanOperatorsQueue.poll()); - sqlStringBuilder.append(" "); - } - }); - sqlStringBuilder.append(")"); + queryLeafConditions(cpsPathQuery, sqlStringBuilder); } } + private void queryLeafConditions(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder) { + sqlStringBuilder.append(" AND ("); + final Queue<String> booleanOperatorsQueue = new LinkedList<>(cpsPathQuery.getBooleanOperators()); + final Queue<String> comparativeOperatorQueue = new LinkedList<>(cpsPathQuery.getComparativeOperators()); + cpsPathQuery.getLeavesData().entrySet().forEach(entry -> { + final String nextComparativeOperator = comparativeOperatorQueue.poll(); + if (entry.getValue() instanceof Integer) { + sqlStringBuilder.append("(attributes ->> "); + sqlStringBuilder.append("'").append(entry.getKey()).append("')\\:\\:int"); + sqlStringBuilder.append(" ").append(nextComparativeOperator).append(" "); + sqlStringBuilder.append("'").append(jsonObjectMapper.asJsonString(entry.getValue())).append("'"); + } else { + if ("=".equals(nextComparativeOperator)) { + sqlStringBuilder.append(" attributes @> "); + sqlStringBuilder.append("'"); + sqlStringBuilder.append(jsonObjectMapper.asJsonString(entry)); + sqlStringBuilder.append("'"); + } else { + throw new CpsPathException(" can use only " + nextComparativeOperator + " with integer "); + } + } + if (!booleanOperatorsQueue.isEmpty()) { + sqlStringBuilder.append(" "); + sqlStringBuilder.append(booleanOperatorsQueue.poll()); + sqlStringBuilder.append(" "); + } + }); + sqlStringBuilder.append(")"); + } + private static void addTextFunctionCondition(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder, final Map<String, Object> queryParameters) { |