11package org .utplsql .api .db ;
22
3+ import oracle .jdbc .OracleConnection ;
4+
5+ import java .sql .CallableStatement ;
6+ import java .sql .SQLException ;
7+ import java .sql .Types ;
38import java .util .LinkedHashMap ;
4- import java .util .function .Consumer ;
59import java .util .stream .Collectors ;
610
711public class DynamicParameterList {
812
9- LinkedHashMap <String , Consumer <Integer >> params ;
13+ LinkedHashMap <String , DynamicParameter > params ;
14+
15+ interface DynamicParameter {
16+ void setParam ( CallableStatement statement , int index ) throws SQLException ;
17+ }
18+
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+ }
1073
11- DynamicParameterList (LinkedHashMap <String , Consumer < Integer > > params ) {
74+ DynamicParameterList (LinkedHashMap <String , DynamicParameter > params ) {
1275 this .params = params ;
1376 }
1477
@@ -18,10 +81,10 @@ public String getSql() {
1881 .collect (Collectors .joining (", " ));
1982 }
2083
21- public void applyFromIndex ( int startIndex ) {
84+ public void setParamsStartWithIndex ( CallableStatement statement , int startIndex ) throws SQLException {
2285 int index = startIndex ;
23- for ( Consumer < Integer > function : params .values () ) {
24- function . accept ( index ++);
86+ for ( DynamicParameter param : params .values () ) {
87+ param . setParam ( statement , index ++);
2588 }
2689 }
2790
0 commit comments