Browse Source

参数工具类

remotes/1611766341912730171/master
richie 5 years ago
parent
commit
a64da28c49
  1. 37
      src/main/java/com/fanruan/api/cal/ParameterKit.java
  2. 17
      src/test/java/com/fanruan/api/cal/ParameterKitTest.java
  3. 8
      src/test/java/com/fanruan/api/plugin/PluginKitTest.java
  4. 12
      src/test/java/com/fanruan/api/plugin/TestExtraClass.java

37
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;
}
}

17
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());
}
}
}
}

8
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());
}
}

12
src/test/java/com/fanruan/api/plugin/TestExtraClass.java

@ -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;
}
}
Loading…
Cancel
Save