|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 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 | + * https://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 | + |
| 17 | +package org.springframework.jdbc.support; |
| 18 | + |
| 19 | +import java.sql.Array; |
| 20 | +import java.sql.PreparedStatement; |
| 21 | +import java.sql.SQLException; |
| 22 | + |
| 23 | +import org.springframework.dao.DataAccessResourceFailureException; |
| 24 | +import org.springframework.lang.Nullable; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * Common {@link SqlValue} implementation for JDBC {@link Array} creation |
| 29 | + * based on the JDBC 4 {@link java.sql.Connection#createArrayOf} method. |
| 30 | + * |
| 31 | + * <p>Also serves as a template for custom {@link SqlValue} implementations. |
| 32 | + * |
| 33 | + * @author Juergen Hoeller |
| 34 | + * @author Philippe Marschall |
| 35 | + * @since 6.1 |
| 36 | + */ |
| 37 | +public class SqlArrayValue implements SqlValue { |
| 38 | + |
| 39 | + private final String typeName; |
| 40 | + |
| 41 | + private final Object[] elements; |
| 42 | + |
| 43 | + @Nullable |
| 44 | + private Array array; |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Create a new {@code SqlArrayValue} for the given type name and elements. |
| 49 | + * @param typeName the SQL name of the type the elements of the array map to |
| 50 | + * @param elements the elements to populate the {@code Array} object with |
| 51 | + * @see java.sql.Connection#createArrayOf |
| 52 | + */ |
| 53 | + public SqlArrayValue(String typeName, Object[] elements) { |
| 54 | + Assert.notNull(typeName, "Type name must not be null"); |
| 55 | + Assert.notNull(elements, "Elements array must not be null"); |
| 56 | + this.typeName = typeName; |
| 57 | + this.elements = elements; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setValue(PreparedStatement ps, int paramIndex) throws SQLException { |
| 63 | + this.array = ps.getConnection().createArrayOf(this.typeName, this.elements); |
| 64 | + ps.setArray(paramIndex, this.array); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void cleanup() { |
| 69 | + if (this.array != null) { |
| 70 | + try { |
| 71 | + this.array.free(); |
| 72 | + } |
| 73 | + catch (SQLException ex) { |
| 74 | + throw new DataAccessResourceFailureException("Could not free Array object", ex); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments