From 2696de664b097c8dceb4332e9896417835e77178 Mon Sep 17 00:00:00 2001 From: Rudrangi Anupriya Date: Tue, 30 May 2023 17:20:20 +0530 Subject: Add <,> operators support to cps-path Issue-ID: CPS-1273 Change-Id: I5d562463b9a49abfe0436047a637857d10596fff Signed-off-by: Rudrangi Anupriya --- cps-ri/pom.xml | 2 +- .../cps/spi/repository/FragmentQueryBuilder.java | 49 ++++++++++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) (limited to 'cps-ri') diff --git a/cps-ri/pom.xml b/cps-ri/pom.xml index 25dc91c6fd..aa86a7fc06 100644 --- a/cps-ri/pom.xml +++ b/cps-ri/pom.xml @@ -33,7 +33,7 @@ cps-ri - 0.54 + 0.53 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 queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType(); - final Queue 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 booleanOperatorsQueue = new LinkedList<>(cpsPathQuery.getBooleanOperators()); + final Queue 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 queryParameters) { -- cgit 1.2.3-korg