Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3d5779

Browse files
committed
We can add booleans now
Responsibility to build SQL fragment now also lies in the DynamicParameter
1 parent 4aba265 commit b3d5779

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/main/java/org/utplsql/api/db/DynamicParameterList.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class DynamicParameterList {
2020

2121
interface DynamicParameter {
2222
void setParam( CallableStatement statement, int index ) throws SQLException;
23+
24+
default String getSql( String key ) {
25+
return key + " => ?";
26+
}
2327
}
2428

2529
private DynamicParameterList(LinkedHashMap<String, DynamicParameter> params) {
@@ -33,8 +37,8 @@ private DynamicParameterList(LinkedHashMap<String, DynamicParameter> params) {
3337
* @return comma-separated list of parameter identifiers
3438
*/
3539
public String getSql() {
36-
return params.keySet().stream()
37-
.map(e -> e + " => ?")
40+
return params.entrySet().stream()
41+
.map(e -> e.getValue().getSql(e.getKey()))
3842
.collect(Collectors.joining(", "));
3943
}
4044

@@ -116,7 +120,7 @@ public DynamicParameterListBuilder addIfNotEmpty(String identifier, Object[] val
116120
}
117121

118122
public DynamicParameterListBuilder add(String identifier, Boolean value) {
119-
params.put(identifier, null);
123+
params.put(identifier, new DynamicBoolParameter(value));
120124
return this;
121125
}
122126

@@ -167,6 +171,28 @@ public void setParam(CallableStatement statement, int index) throws SQLException
167171
}
168172
}
169173

174+
private static class DynamicBoolParameter implements DynamicParameter {
175+
private final Boolean value;
176+
177+
DynamicBoolParameter( Boolean value ) {
178+
this.value = value;
179+
}
180+
181+
@Override
182+
public void setParam(CallableStatement statement, int index) throws SQLException {
183+
if ( value == null ) {
184+
statement.setNull(index, Types.BOOLEAN);
185+
} else {
186+
statement.setInt(index, (value)?1:0);
187+
}
188+
}
189+
190+
@Override
191+
public String getSql(String key) {
192+
return key + " => (case ? when 1 then true else false)";
193+
}
194+
}
195+
170196
private static class DynamicArrayParameter implements DynamicParameter {
171197
private final Object[] value;
172198
private final String customTypeName;

src/test/java/org/utplsql/api/db/DynamicParameterListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void can_add_boolean() throws SQLException {
6868
.add("a_bool", true)
6969
.build();
7070

71-
assertEquals("a_param => (case ? when 1 then true else false)", paramList.getSql());
71+
assertEquals("a_bool => (case ? when 1 then true else false)", paramList.getSql());
7272

7373
paramList.setParamsStartWithIndex(stmt, 3);
7474
verify(stmt).setInt(3, 1);

0 commit comments

Comments
 (0)