|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.oracle.adbaoverjdbc; |
| 17 | + |
| 18 | +import java.lang.reflect.Array; |
| 19 | +import jdk.incubator.sql2.SqlException; |
| 20 | +import jdk.incubator.sql2.SqlType; |
| 21 | +import java.sql.PreparedStatement; |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CompletionStage; |
| 26 | +import java.util.concurrent.Executor; |
| 27 | +import java.util.function.Consumer; |
| 28 | +import java.util.logging.Level; |
| 29 | +import java.util.stream.Collector; |
| 30 | +import jdk.incubator.sql2.ArrayRowCountOperation; |
| 31 | +import jdk.incubator.sql2.Result; |
| 32 | + |
| 33 | +/** |
| 34 | + * |
| 35 | + * @param <T> |
| 36 | + */ |
| 37 | +class ArrayCountOperation<T> extends ParameterizedOperation<T> |
| 38 | + implements ArrayRowCountOperation<T> { |
| 39 | + |
| 40 | + private static final Collector DEFAULT_COLLECTOR = Collector.of( |
| 41 | + () -> null, |
| 42 | + (a, v) -> {}, |
| 43 | + (a, b) -> null, |
| 44 | + a -> null); |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Factory method to create ArrayCountOperations. |
| 49 | + * |
| 50 | + * @param <S> the type of the value of the ArrayCountOperation |
| 51 | + * @param session the Session the ArrayCountOperation belongs to |
| 52 | + * @param grp the GroupOperation the ArrayCountOperation is a member of |
| 53 | + * @param sql the SQL string to execute. Must return a count. |
| 54 | + * @return a new ArrayCountOperation that will execute sql. |
| 55 | + */ |
| 56 | + static <S> ArrayCountOperation<S> newArrayCountOperation(Session session, OperationGroup grp, String sql) { |
| 57 | + return new ArrayCountOperation<>(session, grp, sql); |
| 58 | + } |
| 59 | + |
| 60 | + // attributes |
| 61 | + private final String sqlString; |
| 62 | + private Collector<? super Result.RowCount, Object , ? extends T> countCollector; |
| 63 | + |
| 64 | + PreparedStatement jdbcStatement; |
| 65 | + |
| 66 | + ArrayCountOperation(Session session, OperationGroup operationGroup, String sql) { |
| 67 | + super(session, operationGroup); |
| 68 | + countCollector = DEFAULT_COLLECTOR; |
| 69 | + sqlString = sql; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public <A, S extends T> ArrayRowCountOperation<T> collect(Collector<? super Result.RowCount, A, S> c) { |
| 74 | + if (isImmutable() || countCollector != DEFAULT_COLLECTOR) throw new IllegalStateException("TODO"); |
| 75 | + if (c == null) throw new IllegalArgumentException("TODO"); |
| 76 | + countCollector = (Collector<? super Result.RowCount, Object , ? extends T>) c; |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + CompletionStage<T> follows(CompletionStage<?> predecessor, Executor executor) { |
| 82 | + predecessor = attachFutureParameters(predecessor); |
| 83 | + return predecessor |
| 84 | + .thenApplyAsync(this::executeQuery, executor); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Execute the SQL query, process the returned count, and return the result of |
| 89 | + * processing the returned count. |
| 90 | + * |
| 91 | + * @param ignore not used |
| 92 | + * @return the result of processing the count |
| 93 | + */ |
| 94 | + private T executeQuery(Object ignore) { |
| 95 | + checkCanceled(); |
| 96 | + try { |
| 97 | + jdbcStatement = session.prepareStatement(sqlString); |
| 98 | + |
| 99 | + // Get parameter ids |
| 100 | + String[] keys = (String[])setParameters.keySet().toArray(new String[setParameters.size()]); |
| 101 | + |
| 102 | + // Get parameter values |
| 103 | + ParameterValue[] paramVals = (ParameterValue[]) setParameters.values().toArray(new ParameterValue[setParameters.size()]); |
| 104 | + |
| 105 | + int paramIndex; |
| 106 | + int batchSize; |
| 107 | + |
| 108 | + // Get the batch size from first parameter list/array size |
| 109 | + // TODO: Need to check all parameter value list or value array of same size |
| 110 | + Object paramVal = paramVals[0].value; |
| 111 | + if(paramVal instanceof List) |
| 112 | + batchSize = ((List)paramVal).size(); |
| 113 | + else |
| 114 | + batchSize = Array.getLength(paramVal); |
| 115 | + |
| 116 | + // Loop for each value in the list or an array |
| 117 | + for(int i = 0; i < batchSize; i++) { |
| 118 | + // Loop for each parameter to add a batch |
| 119 | + for(paramIndex = 0; paramIndex < keys.length; paramIndex++) { |
| 120 | + ParameterValue v = paramVals[paramIndex]; |
| 121 | + Object val = getParamVal(v, i); |
| 122 | + v = new ParameterValue(val, v.type); |
| 123 | + v.set(jdbcStatement, keys[paramIndex]); |
| 124 | + } |
| 125 | + |
| 126 | + // Add batch |
| 127 | + jdbcStatement.addBatch(); |
| 128 | + } |
| 129 | + |
| 130 | + // Execute batch |
| 131 | + group.logger.log(Level.FINE, () -> "executeLargeBatch(\"" + sqlString + "\")"); |
| 132 | + long[] counts = jdbcStatement.executeLargeBatch(); |
| 133 | + |
| 134 | + // Get final count using the collector |
| 135 | + Object container = countCollector.supplier().get(); |
| 136 | + for (long c : counts) |
| 137 | + countCollector.accumulator().accept(container, com.oracle.adbaoverjdbc.Result.newRowCount(c)); |
| 138 | + return countCollector.finisher().apply(container); |
| 139 | + } |
| 140 | + catch (SQLException ex) { |
| 141 | + throw new SqlException(ex.getMessage(), ex, ex.getSQLState(), ex.getErrorCode(), sqlString, -1); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Get parameter value from list or an array |
| 146 | + private Object getParamVal(ParameterValue paramVal, int index) { |
| 147 | + if(paramVal.value instanceof List) |
| 148 | + return ((List)(paramVal.value)).get(index); |
| 149 | + else |
| 150 | + return Array.get(paramVal.value, index); |
| 151 | + } |
| 152 | + |
| 153 | + // Covariant overrides |
| 154 | + |
| 155 | + @Override |
| 156 | + public ArrayCountOperation<T> set(String id, List<?> values) { |
| 157 | + return (ArrayCountOperation<T>)super.set(id, values); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public ArrayCountOperation<T> set(String id, List<?> values, SqlType type) { |
| 162 | + return (ArrayCountOperation<T>)super.set(id, values, type); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public <S> ArrayRowCountOperation<T> set(String id, S[] values, SqlType type) { |
| 167 | + return (ArrayCountOperation<T>)super.set(id, values, type); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public <S> ArrayRowCountOperation<T> set(String id, S[] values) { |
| 172 | + return (ArrayCountOperation<T>)super.set(id, values); |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + @Override |
| 177 | + public ArrayCountOperation<T> set(String id, CompletionStage<?> source) { |
| 178 | + return (ArrayCountOperation<T>)super.set(id, source); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public ArrayCountOperation<T> set(String id, CompletionStage<?> source, SqlType type) { |
| 183 | + return (ArrayCountOperation<T>)super.set(id, source, type); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public ArrayCountOperation<T> timeout(Duration minTime) { |
| 188 | + return (ArrayCountOperation<T>)super.timeout(minTime); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public ArrayCountOperation<T> onError(Consumer<Throwable> handler) { |
| 193 | + return (ArrayCountOperation<T>)super.onError(handler); |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments