From a64da28c498ccc446af75d3085793ca5973b5ff4 Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 5 Sep 2019 14:34:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/cal/ParameterKit.java | 37 ++++++++++++++++--- .../com/fanruan/api/cal/ParameterKitTest.java | 17 ++++++++- .../com/fanruan/api/plugin/PluginKitTest.java | 8 ++-- .../fanruan/api/plugin/TestExtraClass.java | 12 ------ 4 files changed, 52 insertions(+), 22 deletions(-) delete mode 100644 src/test/java/com/fanruan/api/plugin/TestExtraClass.java diff --git a/src/main/java/com/fanruan/api/cal/ParameterKit.java b/src/main/java/com/fanruan/api/cal/ParameterKit.java index afb080d..471d9fe 100644 --- a/src/main/java/com/fanruan/api/cal/ParameterKit.java +++ b/src/main/java/com/fanruan/api/cal/ParameterKit.java @@ -1,8 +1,11 @@ package com.fanruan.api.cal; +import com.fanruan.api.util.ArrayKit; +import com.fanruan.api.util.AssistKit; import com.fr.base.Parameter; import com.fr.base.ParameterHelper; import com.fr.base.ParameterMapNameSpace; +import com.fr.stable.ArrayUtils; import com.fr.stable.ParameterProvider; import com.fr.stable.script.NameSpace; import org.jetbrains.annotations.NotNull; @@ -39,6 +42,7 @@ public class ParameterKit { /** * 根据参数名创建新的参数对象 + * * @param name 参数名 * @return 参数对象 */ @@ -98,15 +102,38 @@ public class ParameterKit { public static @NotNull NameSpace createParameterMapNameSpace(ParameterProvider[] ps) { return ParameterMapNameSpace.create(ps); } + /** - * 获得新的参数集合 + * 获得新的参数集数组,新的参数结合中参数的值是通过设定的同名参数传递进去的 * * @param paramTexts sql语句 - * @param ps 旧的参数集合 - * @return 新参数集合 + * @param ps 用于传值的参数数组 + * @return 新参数素组 */ - public static ParameterProvider[] analyzeAndUnionSameParameters(String[] paramTexts, Parameter[] ps){ - return ParameterHelper.analyzeAndUnionSameParameters(paramTexts, ps); + public static ParameterProvider[] analyzeAndUnionSameParameters(String[] paramTexts, ParameterProvider[] ps) { + + ParameterProvider[] parameters = ParameterKit.analyze4Parameters(paramTexts, false); + + if (ArrayUtils.isEmpty(parameters)) { + return parameters; + } + + if (ArrayKit.isNotEmpty(ps)) { + for (ParameterProvider newParameter : parameters) { + ParameterProvider existParameter = null; + for (ParameterProvider p : ps) { + if (AssistKit.equals(p.getName(), newParameter.getName())) { + existParameter = p; + break; + } + } + if (existParameter != null) { + newParameter.setValue(existParameter.getValue()); + } + } + } + + return parameters; } } diff --git a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java index 4093687..623650f 100644 --- a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java +++ b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java @@ -111,7 +111,22 @@ public class ParameterKitTest extends Prepare { @Test public void newParameter2() { ParameterProvider provider = ParameterKit.newParameter("p1"); - Assert.assertEquals(provider.getName(),"p1"); + Assert.assertEquals(provider.getName(), "p1"); Assert.assertEquals("", provider.getValue()); } + + @Test + public void analyzeAndUnionSameParameters() { + ParameterProvider p1 = ParameterKit.newParameter("p1", 100); + ParameterProvider p2 = ParameterKit.newParameter("p2", 200); + + ParameterProvider[] providers = ParameterKit.analyzeAndUnionSameParameters( + new String[]{"select * from td where name = ${p1} and age = ${p3}"}, + new ParameterProvider[]{p1, p2}); + for (ParameterProvider provider : providers) { + if ("p1".equals(provider.getName())) { + Assert.assertEquals(100, provider.getValue()); + } + } + } } \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/plugin/PluginKitTest.java b/src/test/java/com/fanruan/api/plugin/PluginKitTest.java index 4c8a72f..7d5f9a5 100644 --- a/src/test/java/com/fanruan/api/plugin/PluginKitTest.java +++ b/src/test/java/com/fanruan/api/plugin/PluginKitTest.java @@ -1,13 +1,13 @@ package com.fanruan.api.plugin; +import com.fanruan.api.runtime.PluginKit; import org.junit.Assert; import org.junit.Test; public class PluginKitTest { @Test - public void getArray(){ - PluginKit p1 = new PluginKit(); - TestExtraClass p2 = new TestExtraClass(); - Assert.assertEquals(p1.getArray("abcd"), p2.getArray("abcd")); + public void getArray() { + Assert.assertFalse(PluginKit.isCurrentPluginAvailable()); + } } diff --git a/src/test/java/com/fanruan/api/plugin/TestExtraClass.java b/src/test/java/com/fanruan/api/plugin/TestExtraClass.java deleted file mode 100644 index f81ed57..0000000 --- a/src/test/java/com/fanruan/api/plugin/TestExtraClass.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.fanruan.api.plugin; -import com.fr.plugin.injectable.PluginSingleInjection; - -public class TestExtraClass extends com.fr.plugin.AbstractExtraClassManager{ - protected boolean mountSpecific(PluginSingleInjection injection){ - return true; - } - - protected boolean demountSpecific(PluginSingleInjection injection){ - return true; - } -}