|
| 1 | +package org.utplsql.api.db; |
| 2 | + |
| 3 | +import oracle.jdbc.OracleConnection; |
| 4 | + |
| 5 | +import java.sql.CallableStatement; |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.sql.Types; |
| 8 | +import java.util.LinkedHashMap; |
| 9 | +import java.util.stream.Collectors; |
| 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 | + */ |
| 17 | +public class DynamicParameterList { |
| 18 | + |
| 19 | + private LinkedHashMap<String, DynamicParameter> params; |
| 20 | + |
| 21 | + interface DynamicParameter { |
| 22 | + void setParam( CallableStatement statement, int index ) throws SQLException; |
| 23 | + } |
| 24 | + |
| 25 | + private DynamicParameterList(LinkedHashMap<String, DynamicParameter> params) { |
| 26 | + this.params = params; |
| 27 | + } |
| 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 | + */ |
| 35 | + public String getSql() { |
| 36 | + return params.keySet().stream() |
| 37 | + .map(e -> e + " => ?") |
| 38 | + .collect(Collectors.joining(", ")); |
| 39 | + } |
| 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 | + */ |
| 47 | + public void setParamsStartWithIndex(CallableStatement statement, int startIndex ) throws SQLException { |
| 48 | + int index = startIndex; |
| 49 | + for ( DynamicParameter param : params.values() ) { |
| 50 | + param.setParam(statement, index++); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** Returns a builder to create a DynamicParameterList |
| 55 | + * |
| 56 | + * @return Builder |
| 57 | + */ |
| 58 | + public static DynamicParameterListBuilder builder() { |
| 59 | + return new DynamicParameterListBuilder(); |
| 60 | + } |
| 61 | + |
| 62 | + /** Builder-class for DynamicParameterList |
| 63 | + * <br> |
| 64 | + * Usage: |
| 65 | + * <pre> |
| 66 | + * DynamicParameterList.builder() |
| 67 | + * .add("parameter1", "StringParameter") |
| 68 | + * .addIfNotEmpty("parameter2", 123) |
| 69 | + * .build(); |
| 70 | + * </pre> |
| 71 | + * |
| 72 | + * @author pesse |
| 73 | + */ |
| 74 | + public static class DynamicParameterListBuilder { |
| 75 | + |
| 76 | + private LinkedHashMap<String, DynamicParameterList.DynamicParameter> params = new LinkedHashMap<>(); |
| 77 | + |
| 78 | + private DynamicParameterListBuilder() { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public DynamicParameterListBuilder add(String identifier, String value ) { |
| 83 | + params.put(identifier, new DynamicParameterList.DynamicStringParameter(value)); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public DynamicParameterListBuilder addIfNotEmpty(String identifier, String value ) { |
| 88 | + if ( value != null && !value.isEmpty() ) { |
| 89 | + add(identifier, value); |
| 90 | + } |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + public DynamicParameterListBuilder add(String identifier, Integer value ) { |
| 95 | + params.put(identifier, new DynamicParameterList.DynamicIntegerParameter(value)); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public DynamicParameterListBuilder addIfNotEmpty(String identifier, Integer value ) { |
| 100 | + if ( value != null) { |
| 101 | + add(identifier, value); |
| 102 | + } |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public DynamicParameterListBuilder add(String identifier, Object[] value, String customTypeName, OracleConnection oraConnection ) { |
| 107 | + params.put(identifier, new DynamicParameterList.DynamicArrayParameter(value, customTypeName, oraConnection)); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public DynamicParameterListBuilder addIfNotEmpty(String identifier, Object[] value, String customTypeName, OracleConnection oraConnection ) { |
| 112 | + if ( value != null && value.length > 0 ) { |
| 113 | + add(identifier, value, customTypeName, oraConnection); |
| 114 | + } |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public DynamicParameterList build() { |
| 119 | + return new DynamicParameterList(params); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /* Implementations of DynamicStringParameter */ |
| 124 | + private static class DynamicStringParameter implements DynamicParameter { |
| 125 | + private final String value; |
| 126 | + |
| 127 | + DynamicStringParameter( String value ) { |
| 128 | + this.value = value; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void setParam(CallableStatement statement, int index) throws SQLException { |
| 133 | + if ( value == null ) { |
| 134 | + statement.setNull(index, Types.VARCHAR); |
| 135 | + } else { |
| 136 | + statement.setString(index, value); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static class DynamicIntegerParameter implements DynamicParameter { |
| 142 | + private final Integer value; |
| 143 | + |
| 144 | + DynamicIntegerParameter( Integer value ) { |
| 145 | + this.value = value; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void setParam(CallableStatement statement, int index) throws SQLException { |
| 150 | + if ( value == null ) { |
| 151 | + statement.setNull(index, Types.INTEGER); |
| 152 | + } else { |
| 153 | + statement.setInt(index, value); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static class DynamicArrayParameter implements DynamicParameter { |
| 159 | + private final Object[] value; |
| 160 | + private final String customTypeName; |
| 161 | + private final OracleConnection oraConnection; |
| 162 | + |
| 163 | + DynamicArrayParameter( Object[] value, String customTypeName, OracleConnection oraConnection ) { |
| 164 | + this.value = value; |
| 165 | + this.customTypeName = customTypeName; |
| 166 | + this.oraConnection = oraConnection; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void setParam(CallableStatement statement, int index) throws SQLException { |
| 171 | + if ( value == null ) { |
| 172 | + statement.setNull(index, Types.ARRAY, customTypeName); |
| 173 | + } else { |
| 174 | + statement.setArray( |
| 175 | + index, oraConnection.createOracleArray(customTypeName, value) |
| 176 | + ); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments