8
8
import java .util .LinkedHashMap ;
9
9
import java .util .stream .Collectors ;
10
10
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
+ */
11
17
public class DynamicParameterList {
12
18
13
19
private LinkedHashMap <String , DynamicParameter > params ;
@@ -16,82 +22,55 @@ interface DynamicParameter {
16
22
void setParam ( CallableStatement statement , int index ) throws SQLException ;
17
23
}
18
24
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 ) {
75
26
this .params = params ;
76
27
}
77
28
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
+ */
78
35
public String getSql () {
79
36
return params .keySet ().stream ()
80
37
.map (e -> e + " => ?" )
81
38
.collect (Collectors .joining (", " ));
82
39
}
83
40
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
+ */
84
47
public void setParamsStartWithIndex (CallableStatement statement , int startIndex ) throws SQLException {
85
48
int index = startIndex ;
86
49
for ( DynamicParameter param : params .values () ) {
87
50
param .setParam (statement , index ++);
88
51
}
89
52
}
90
53
54
+ /** Returns a builder to create a DynamicParameterList
55
+ *
56
+ * @return Builder
57
+ */
91
58
public static DynamicParameterListBuilder builder () {
92
59
return new DynamicParameterListBuilder ();
93
60
}
94
61
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
+ */
95
74
public static class DynamicParameterListBuilder {
96
75
97
76
private LinkedHashMap <String , DynamicParameterList .DynamicParameter > params = new LinkedHashMap <>();
@@ -130,4 +109,62 @@ public DynamicParameterList build() {
130
109
}
131
110
}
132
111
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
+
133
170
}
0 commit comments