88import java .util .LinkedHashMap ;
99import java .util .stream .Collectors ;
1010
11+ /** Lets you build a list of parameters for a CallableStatement
12+ * <br>
13+ * Create it with the Builder (DynamicParameterList.builder())
14+ *
15+ * @author pesse
16+ */
1117public class DynamicParameterList {
1218
1319 private LinkedHashMap <String , DynamicParameter > params ;
@@ -16,82 +22,55 @@ interface DynamicParameter {
1622 void setParam ( CallableStatement statement , int index ) throws SQLException ;
1723 }
1824
19- static class DynamicStringParameter implements DynamicParameter {
20- private final String value ;
21-
22- DynamicStringParameter ( String value ) {
23- this .value = value ;
24- }
25-
26- @ Override
27- public void setParam (CallableStatement statement , int index ) throws SQLException {
28- if ( value == null ) {
29- statement .setNull (index , Types .VARCHAR );
30- } else {
31- statement .setString (index , value );
32- }
33- }
34- }
35- static class DynamicIntegerParameter implements DynamicParameter {
36- private final Integer value ;
37-
38- DynamicIntegerParameter ( Integer value ) {
39- this .value = value ;
40- }
41-
42- @ Override
43- public void setParam (CallableStatement statement , int index ) throws SQLException {
44- if ( value == null ) {
45- statement .setNull (index , Types .INTEGER );
46- } else {
47- statement .setInt (index , value );
48- }
49- }
50- }
51- static class DynamicArrayParameter implements DynamicParameter {
52- private final Object [] value ;
53- private final String customTypeName ;
54- private final OracleConnection oraConnection ;
55-
56- DynamicArrayParameter ( Object [] value , String customTypeName , OracleConnection oraConnection ) {
57- this .value = value ;
58- this .customTypeName = customTypeName ;
59- this .oraConnection = oraConnection ;
60- }
61-
62- @ Override
63- public void setParam (CallableStatement statement , int index ) throws SQLException {
64- if ( value == null ) {
65- statement .setNull (index , Types .ARRAY , customTypeName );
66- } else {
67- statement .setArray (
68- index , oraConnection .createOracleArray (customTypeName , value )
69- );
70- }
71- }
72- }
73-
74- DynamicParameterList (LinkedHashMap <String , DynamicParameter > params ) {
25+ private DynamicParameterList (LinkedHashMap <String , DynamicParameter > params ) {
7526 this .params = params ;
7627 }
7728
29+ /** Returns the SQL of this ParameterList as comma-separated list of the parameter identifiers:<br>
30+ *
31+ * e.g. "a_parameter1 => ?, a_parameter2 => ?"
32+ *
33+ * @return comma-separated list of parameter identifiers
34+ */
7835 public String getSql () {
7936 return params .keySet ().stream ()
8037 .map (e -> e + " => ?" )
8138 .collect (Collectors .joining (", " ));
8239 }
8340
41+ /** Sets the contained parameters in the order they were added to the given statement by index, starting with the given one
42+ *
43+ * @param statement The statement to set the parameters to
44+ * @param startIndex The index to start with
45+ * @throws SQLException SQLException of the underlying statement.setX methods
46+ */
8447 public void setParamsStartWithIndex (CallableStatement statement , int startIndex ) throws SQLException {
8548 int index = startIndex ;
8649 for ( DynamicParameter param : params .values () ) {
8750 param .setParam (statement , index ++);
8851 }
8952 }
9053
54+ /** Returns a builder to create a DynamicParameterList
55+ *
56+ * @return Builder
57+ */
9158 public static DynamicParameterListBuilder builder () {
9259 return new DynamicParameterListBuilder ();
9360 }
9461
62+ /** Builder-class for DynamicParameterList
63+ * <br>
64+ * Usage:
65+ * <pre>
66+ * DynamicParameterList.builder()
67+ * .add("parameter1", "StringParameter")
68+ * .add("parameter2", 123)
69+ * .build();
70+ * </pre>
71+ *
72+ * @author pesse
73+ */
9574 public static class DynamicParameterListBuilder {
9675
9776 private LinkedHashMap <String , DynamicParameterList .DynamicParameter > params = new LinkedHashMap <>();
@@ -130,4 +109,62 @@ public DynamicParameterList build() {
130109 }
131110 }
132111
112+ /* Implementations of DynamicStringParameter */
113+ private static class DynamicStringParameter implements DynamicParameter {
114+ private final String value ;
115+
116+ DynamicStringParameter ( String value ) {
117+ this .value = value ;
118+ }
119+
120+ @ Override
121+ public void setParam (CallableStatement statement , int index ) throws SQLException {
122+ if ( value == null ) {
123+ statement .setNull (index , Types .VARCHAR );
124+ } else {
125+ statement .setString (index , value );
126+ }
127+ }
128+ }
129+
130+ private static class DynamicIntegerParameter implements DynamicParameter {
131+ private final Integer value ;
132+
133+ DynamicIntegerParameter ( Integer value ) {
134+ this .value = value ;
135+ }
136+
137+ @ Override
138+ public void setParam (CallableStatement statement , int index ) throws SQLException {
139+ if ( value == null ) {
140+ statement .setNull (index , Types .INTEGER );
141+ } else {
142+ statement .setInt (index , value );
143+ }
144+ }
145+ }
146+
147+ private static class DynamicArrayParameter implements DynamicParameter {
148+ private final Object [] value ;
149+ private final String customTypeName ;
150+ private final OracleConnection oraConnection ;
151+
152+ DynamicArrayParameter ( Object [] value , String customTypeName , OracleConnection oraConnection ) {
153+ this .value = value ;
154+ this .customTypeName = customTypeName ;
155+ this .oraConnection = oraConnection ;
156+ }
157+
158+ @ Override
159+ public void setParam (CallableStatement statement , int index ) throws SQLException {
160+ if ( value == null ) {
161+ statement .setNull (index , Types .ARRAY , customTypeName );
162+ } else {
163+ statement .setArray (
164+ index , oraConnection .createOracleArray (customTypeName , value )
165+ );
166+ }
167+ }
168+ }
169+
133170}
0 commit comments