@@ -0,0 +1,28 @@ | |||
| 1 | + package org.utplsql.api.db; | ||
| 2 | + | ||
| 3 | + import java.util.LinkedHashMap; | ||
| 4 | + import java.util.function.Consumer; | ||
| 5 | + import java.util.stream.Collectors; | ||
| 6 | + | ||
| 7 | + public class DynamicParameterList { | ||
| 8 | + | ||
| 9 | + LinkedHashMap<String, Consumer<Integer>> params; | ||
| 10 | + | ||
| 11 | + DynamicParameterList(LinkedHashMap<String, Consumer<Integer>> params) { | ||
| 12 | + this.params = params; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + public String getSql() { | ||
| 16 | + return params.keySet().stream() | ||
| 17 | + .map(e -> e + " = ?") | ||
| 18 | + .collect(Collectors.joining(", ")); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public void applyFromIndex( int startIndex ) { | ||
| 22 | + int index = startIndex; | ||
| 23 | + for ( Consumer<Integer> function : params.values() ) { | ||
| 24 | + function.accept(index++); | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + } | ||
@@ -0,0 +1,29 @@ | |||
| 1 | + package org.utplsql.api.db; | ||
| 2 | + | ||
| 3 | + import java.util.LinkedHashMap; | ||
| 4 | + import java.util.function.Consumer; | ||
| 5 | + | ||
| 6 | + public class DynamicParameterListBuilder { | ||
| 7 | + | ||
| 8 | + private LinkedHashMap<String, Consumer<Integer>> params = new LinkedHashMap<>(); | ||
| 9 | + | ||
| 10 | + private DynamicParameterListBuilder() { | ||
| 11 | + | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + public DynamicParameterListBuilder addParameter(String identifier, Consumer<Integer> function) { | ||
| 15 | + | ||
| 16 | + params.put(identifier, function); | ||
| 17 | + | ||
| 18 | + return this; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public DynamicParameterList build() { | ||
| 22 | + return new DynamicParameterList(params); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + public static DynamicParameterListBuilder create() { | ||
| 27 | + return new DynamicParameterListBuilder(); | ||
| 28 | + } | ||
| 29 | + } | ||
@@ -0,0 +1,29 @@ | |||
| 1 | + package org.utplsql.api.db; | ||
| 2 | + | ||
| 3 | + import org.junit.jupiter.api.Test; | ||
| 4 | + | ||
| 5 | + import java.util.ArrayList; | ||
| 6 | + | ||
| 7 | + import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 8 | + | ||
| 9 | + public class DynamicParameterListTest { | ||
| 10 | + | ||
| 11 | + @Test | ||
| 12 | + void firstTest() { | ||
| 13 | + | ||
| 14 | + final ArrayList<Object> resultArray = new ArrayList<>(); | ||
| 15 | + | ||
| 16 | + DynamicParameterList parameterList = DynamicParameterListBuilder.create() | ||
| 17 | + .addParameter("a_object_owner", i -> resultArray.add(i + ": MyOwner")) | ||
| 18 | + .addParameter("a_num_param", i -> resultArray.add( i + ": 123")) | ||
| 19 | + .build(); | ||
| 20 | + | ||
| 21 | + parameterList.applyFromIndex(5); | ||
| 22 | + assertEquals("a_object_owner = ?, a_num_param = ?", parameterList.getSql()); | ||
| 23 | + | ||
| 24 | + ArrayList<Object> expectedList = new ArrayList<>(); | ||
| 25 | + expectedList.add("5: MyOwner"); | ||
| 26 | + expectedList.add("6: 123"); | ||
| 27 | + assertEquals( expectedList, resultArray); | ||
| 28 | + } | ||
| 29 | + } | ||
0 commit comments