Skip to content

Commit 3fed2ec

Browse files
committed
Add common SqlValue implementation for JDBC Array creation
Closes gh-1850
1 parent 357d5b4 commit 3fed2ec

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.Connection;
21+
import java.sql.PreparedStatement;
22+
import java.sql.SQLException;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.mockito.BDDMockito.given;
27+
import static org.mockito.BDDMockito.mock;
28+
import static org.mockito.BDDMockito.verify;
29+
30+
/**
31+
* Tests for {@link SqlArrayValue}.
32+
*
33+
* @author Philippe Marschall
34+
* @since 6.1
35+
*/
36+
public class SqlArrayValueTests {
37+
38+
private final Connection con = mock();
39+
40+
private final PreparedStatement ps = mock();
41+
42+
private final Array arr = mock();
43+
44+
private final Object[] elements = new Object[] {1, 2, 3};
45+
46+
private final SqlArrayValue sqlArrayValue = new SqlArrayValue("smallint", elements);
47+
48+
49+
@Test
50+
public void setValue() throws SQLException {
51+
given(this.ps.getConnection()).willReturn(this.con);
52+
given(this.con.createArrayOf("smallint", elements)).willReturn(this.arr);
53+
54+
int paramIndex = 42;
55+
this.sqlArrayValue.setValue(this.ps, paramIndex);
56+
verify(ps).setArray(paramIndex, arr);
57+
58+
this.sqlArrayValue.cleanup();
59+
verify(this.arr).free();
60+
}
61+
62+
}

0 commit comments

Comments
 (0)