Browse Source

Merge branch 'master' of https://cloud.finedevelop.com/scm/~zhaojunzhe/finekit

* 'master' of https://cloud.finedevelop.com/scm/~zhaojunzhe/finekit:
  参数工具类

# Conflicts:
#	src/test/java/com/fanruan/api/plugin/PluginKitTest.java
pull/19/head
zjz1993 5 years ago
parent
commit
a938cd9f81
  1. 37
      src/main/java/com/fanruan/api/cal/ParameterKit.java
  2. 15
      src/test/java/com/fanruan/api/cal/ParameterKitTest.java
  3. 13
      src/test/java/com/fanruan/api/plugin/PluginKitTest.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;
}
}

15
src/test/java/com/fanruan/api/cal/ParameterKitTest.java

@ -114,4 +114,19 @@ public class ParameterKitTest extends Prepare {
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());
}
}
}
}

13
src/test/java/com/fanruan/api/plugin/PluginKitTest.java

@ -0,0 +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() {
Assert.assertFalse(PluginKit.isCurrentPluginAvailable());
}
}
Loading…
Cancel
Save