From c8fb5454f39065e792d934728193d65af967b5b8 Mon Sep 17 00:00:00 2001 From: lucian Date: Thu, 4 Feb 2021 16:53:20 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-39459=20json=E6=95=B0=E6=8D=AE=E9=9B=86-?= =?UTF-8?q?=E4=BC=A0=E5=8F=82=E6=96=B9=E5=BC=8F=E4=BB=8Eget=E6=94=B9?= =?UTF-8?q?=E4=B8=BApost-raw=EF=BC=8C=E5=8F=82=E6=95=B0=E7=9A=84=E5=80=BC?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E4=BF=9D=E7=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/cal/ParameterKit.java | 32 ++++++++++++ .../com/fanruan/api/cal/ParameterKitTest.java | 49 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/main/java/com/fanruan/api/cal/ParameterKit.java b/src/main/java/com/fanruan/api/cal/ParameterKit.java index e2fc4a8..fcb5e09 100644 --- a/src/main/java/com/fanruan/api/cal/ParameterKit.java +++ b/src/main/java/com/fanruan/api/cal/ParameterKit.java @@ -21,6 +21,7 @@ import com.fr.stable.script.NameSpace; import org.jetbrains.annotations.NotNull; import java.util.Date; +import java.util.List; import java.util.Map; /** @@ -147,6 +148,37 @@ public class ParameterKit { return parameters; } + /** + * 获得参数并集,将新的参数拼接在后面 + * + * @param paramTexts sql语句 + * @param ps 旧的参数集合 + * @return 新参数集合 + */ + public static ParameterProvider[] analyzeAndMergeParameters(String[] paramTexts, ParameterProvider[] ps) { + ParameterProvider[] parameters = ParameterKit.analyze4Parameters(paramTexts, false); + + if (ArrayKit.isEmpty(parameters) || ArrayKit.isEmpty(ps)) { + return ArrayUtils.addAll(ps, parameters); + } + + List psList = ArrayKit.toList(ps); + + // 检查ps中是否有parameter,如果没有就加进去 + for (ParameterProvider parameter : parameters) { + for (int i = 0; i < ps.length; i ++) { + if (AssistKit.equals(ps[i].getName(), parameter.getName())) { + break; + } + else if (i >= ps.length - 1) { + psList.add(parameter); + } + } + } + + return psList.toArray(new ParameterProvider[0]); + } + /** * 增加处理数据集间联系,例如:select * from t where time=${today()} * diff --git a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java index 8498e15..8e8dded 100644 --- a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java +++ b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java @@ -150,4 +150,53 @@ public class ParameterKitTest extends Prepare { "[{\"name\":\"p1\",\"value\":1,\"type\":\"Integer\"},{\"name\":\"p2\",\"value\":\"abc\",\"type\":\"String\"},{\"name\":\"p3\",\"value\":\"=sum(1, 2, 4)\",\"type\":\"String\"}]", ParameterKit.parameters2Json(providers).toString()); } + + @Test + public void testAnalyzeAndMergeParameters() { + ParameterProvider param1 = ParameterKit.newParameter("test1", "1"); + ParameterProvider param2 = ParameterKit.newParameter("test2", "2"); + + String text1 = "${a}"; + String text2 = "${test1}"; + + ParameterProvider[] parameters1 = ParameterKit.analyzeAndMergeParameters( + new String[]{text1, text2}, + new ParameterProvider[] {param1, param2} + ); + ParameterProvider[] parameters2 = ParameterKit.analyzeAndMergeParameters(null, null); + ParameterProvider[] parameters3 = ParameterKit.analyzeAndMergeParameters( + null, + new ParameterProvider[] {param1, param2} + ); + ParameterProvider[] parameters4 = ParameterKit.analyzeAndMergeParameters( + new String[] {}, + new ParameterProvider[] {param1, param2} + ); + ParameterProvider[] parameters5 = ParameterKit.analyzeAndMergeParameters( + new String[]{text1, text2}, + new ParameterProvider[] {} + ); + + Assert.assertEquals(parameters1.length, 3); + Assert.assertEquals(parameters1[0], param1); + Assert.assertEquals(parameters1[1], param2); + Assert.assertEquals(parameters1[2].getName(), "a"); + Assert.assertEquals(parameters1[2].getValue(), ""); + + Assert.assertEquals(parameters2.length, 0); + + Assert.assertEquals(parameters3.length, 2); + Assert.assertEquals(parameters3[0], param1); + Assert.assertEquals(parameters3[1], param2); + + Assert.assertEquals(parameters4.length, 2); + Assert.assertEquals(parameters4[0], param1); + Assert.assertEquals(parameters4[1], param2); + + Assert.assertEquals(parameters5.length, 2); + Assert.assertEquals(parameters5[0].getName(), "a"); + Assert.assertEquals(parameters5[0].getValue(), ""); + Assert.assertEquals(parameters5[1].getName(), "test1"); + Assert.assertEquals(parameters5[1].getValue(), ""); + } } \ No newline at end of file