|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
22 | 22 | import java.sql.ResultSet;
|
23 | 23 | import java.sql.SQLException;
|
24 | 24 | import java.sql.Types;
|
| 25 | +import java.util.Arrays; |
25 | 26 | import java.util.Collections;
|
26 | 27 | import java.util.HashMap;
|
27 | 28 | import java.util.LinkedList;
|
|
47 | 48 | * @author Rick Evans
|
48 | 49 | * @author Juergen Hoeller
|
49 | 50 | * @author Chris Beams
|
| 51 | + * @author Nikita Khateev |
50 | 52 | */
|
51 | 53 | public class NamedParameterJdbcTemplateTests {
|
52 | 54 |
|
53 | 55 | private static final String SELECT_NAMED_PARAMETERS =
|
54 |
| - "select id, forename from custmr where id = :id and country = :country"; |
| 56 | + "select id, forename from custmr where id = :id and country = :country"; |
55 | 57 | private static final String SELECT_NAMED_PARAMETERS_PARSED =
|
56 |
| - "select id, forename from custmr where id = ? and country = ?"; |
| 58 | + "select id, forename from custmr where id = ? and country = ?"; |
57 | 59 | private static final String SELECT_NO_PARAMETERS =
|
58 | 60 | "select id, forename from custmr";
|
59 | 61 |
|
60 | 62 | private static final String UPDATE_NAMED_PARAMETERS =
|
61 |
| - "update seat_status set booking_id = null where performance_id = :perfId and price_band_id = :priceId"; |
| 63 | + "update seat_status set booking_id = null where performance_id = :perfId and price_band_id = :priceId"; |
62 | 64 | private static final String UPDATE_NAMED_PARAMETERS_PARSED =
|
63 |
| - "update seat_status set booking_id = null where performance_id = ? and price_band_id = ?"; |
| 65 | + "update seat_status set booking_id = null where performance_id = ? and price_band_id = ?"; |
| 66 | + |
| 67 | + private static final String UPDATE_ARRAY_PARAMETERS = |
| 68 | + "update customer set type = array[:typeIds] where id = :id"; |
| 69 | + private static final String UPDATE_ARRAY_PARAMETERS_PARSED = |
| 70 | + "update customer set type = array[?, ?, ?] where id = ?"; |
64 | 71 |
|
65 | 72 | private static final String[] COLUMN_NAMES = new String[] {"id", "forename"};
|
66 | 73 |
|
| 74 | + |
67 | 75 | @Rule
|
68 | 76 | public ExpectedException thrown = ExpectedException.none();
|
69 | 77 |
|
70 | 78 | private Connection connection;
|
| 79 | + |
71 | 80 | private DataSource dataSource;
|
| 81 | + |
72 | 82 | private PreparedStatement preparedStatement;
|
| 83 | + |
73 | 84 | private ResultSet resultSet;
|
| 85 | + |
74 | 86 | private DatabaseMetaData databaseMetaData;
|
| 87 | + |
75 | 88 | private Map<String, Object> params = new HashMap<>();
|
| 89 | + |
76 | 90 | private NamedParameterJdbcTemplate namedParameterTemplate;
|
77 | 91 |
|
78 | 92 |
|
@@ -131,6 +145,31 @@ public void testExecute() throws SQLException {
|
131 | 145 | verify(connection).close();
|
132 | 146 | }
|
133 | 147 |
|
| 148 | + @Test |
| 149 | + public void testExecuteArray() throws SQLException { |
| 150 | + given(preparedStatement.executeUpdate()).willReturn(1); |
| 151 | + |
| 152 | + List<Integer> typeIds = Arrays.asList(1, 2, 3); |
| 153 | + |
| 154 | + params.put("typeIds", typeIds); |
| 155 | + params.put("id", 1); |
| 156 | + Object result = namedParameterTemplate.execute(UPDATE_ARRAY_PARAMETERS, params, |
| 157 | + (PreparedStatementCallback<Object>) ps -> { |
| 158 | + assertEquals(preparedStatement, ps); |
| 159 | + ps.executeUpdate(); |
| 160 | + return "result"; |
| 161 | + }); |
| 162 | + |
| 163 | + assertEquals("result", result); |
| 164 | + verify(connection).prepareStatement(UPDATE_ARRAY_PARAMETERS_PARSED); |
| 165 | + verify(preparedStatement).setObject(1, 1); |
| 166 | + verify(preparedStatement).setObject(2, 2); |
| 167 | + verify(preparedStatement).setObject(3, 3); |
| 168 | + verify(preparedStatement).setObject(4, 1); |
| 169 | + verify(preparedStatement).close(); |
| 170 | + verify(connection).close(); |
| 171 | + } |
| 172 | + |
134 | 173 | @Test
|
135 | 174 | public void testExecuteWithTypedParameters() throws SQLException {
|
136 | 175 | given(preparedStatement.executeUpdate()).willReturn(1);
|
|
0 commit comments