/** * Convenient method to execute without parameters. * @param context the contextual information for object creation */ public List<T> execute(Map<?, ?> context) throws DataAccessException { return execute((Object[]) null, context); }
private void doTestCustomerQuery(SqlQuery<?> query, boolean namedParameters) throws SQLException { given(resultSet.next()).willReturn(true); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); given(resultSet.next()).willReturn(true, false); given(preparedStatement.executeQuery()).willReturn(resultSet); given(connection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED)).willReturn(preparedStatement); List<?> queryResults; if (namedParameters) { Map<String, Object> params = new HashMap<>(2); params.put("id", 1); params.put("country", "UK"); queryResults = query.executeByNamedParam(params); } else { Object[] params = new Object[] {1, "UK"}; queryResults = query.execute(params); } assertTrue("Customer was returned correctly", queryResults.size() == 1); Customer cust = (Customer) queryResults.get(0); assertTrue("Customer id was assigned correctly", cust.getId() == 1); assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod")); verify(resultSet).close(); verify(preparedStatement).setObject(1, 1, Types.INTEGER); verify(preparedStatement).setString(2, "UK"); verify(preparedStatement).close(); }
/** * Convenient method to execute with two int parameters. * @param p1 first int parameter * @param p2 second int parameter */ public List<T> execute(int p1, int p2) throws DataAccessException { return execute(p1, p2, null); }
/** * Convenient method to execute with a single String parameter. * @param p1 single String parameter */ public List<T> execute(String p1) throws DataAccessException { return execute(p1, null); }
/** * Convenient method to execute without context. * @param params parameters for the query. Primitive parameters must * be represented by their Object wrapper type. The ordering of parameters is * significant. */ public List<T> execute(Object... params) throws DataAccessException { return execute(params, null); }
/** * Convenient method to execute without parameters nor context. */ public List<T> execute() throws DataAccessException { return execute((Object[]) null, null); }
/** * Convenient method to execute with a single long parameter. * @param p1 single long parameter */ public List<T> execute(long p1) throws DataAccessException { return execute(p1, null); }
/** * Convenient method to execute with a single int parameter. * @param p1 single int parameter */ public List<T> execute(int p1) throws DataAccessException { return execute(p1, null); }
/** * Convenient method to execute with a single int parameter and context. * @param p1 single int parameter * @param context the contextual information for object creation */ public List<T> execute(int p1, @Nullable Map<?, ?> context) throws DataAccessException { return execute(new Object[] {p1}, context); }
/** * Convenient method to execute with a single long parameter and context. * @param p1 single long parameter * @param context the contextual information for object creation */ public List<T> execute(long p1, @Nullable Map<?, ?> context) throws DataAccessException { return execute(new Object[] {p1}, context); }
/** * Convenient method to execute with two int parameters and context. * @param p1 first int parameter * @param p2 second int parameter * @param context the contextual information for object creation */ public List<T> execute(int p1, int p2, @Nullable Map<?, ?> context) throws DataAccessException { return execute(new Object[] {p1, p2}, context); }
/** * Convenient method to execute with a single String parameter and context. * @param p1 single String parameter * @param context the contextual information for object creation */ public List<T> execute(String p1, @Nullable Map<?, ?> context) throws DataAccessException { return execute(new Object[] {p1}, context); }
/** * Generic object finder method, used by all other {@code findObject} methods. * Object finder methods are like EJB entity bean finders, in that it is * considered an error if they return more than one result. * @return the result object, or {@code null} if not found. Subclasses may * choose to treat this as an error and throw an exception. * @see org.springframework.dao.support.DataAccessUtils#singleResult */ @Nullable public T findObject(@Nullable Object[] params, @Nullable Map<?, ?> context) throws DataAccessException { List<T> results = execute(params, context); return DataAccessUtils.singleResult(results); }
@Test public void testQueryWithoutParams() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt(1)).willReturn(1); SqlQuery<Integer> query = new MappingSqlQueryWithParameters<Integer>() { @Override protected Integer mapRow(ResultSet rs, int rownum, @Nullable Object[] params, @Nullable Map<? ,?> context) throws SQLException { assertTrue("params were null", params == null); assertTrue("context was null", context == null); return rs.getInt(1); } }; query.setDataSource(dataSource); query.setSql(SELECT_ID); query.compile(); List<Integer> list = query.execute(); assertThat(list, is(equalTo(Arrays.asList(1)))); verify(connection).prepareStatement(SELECT_ID); verify(resultSet).close(); verify(preparedStatement).close(); }
/** * Convenient method to execute with a single int parameter. * @param p1 single int parameter */ public List<T> execute(int p1) throws DataAccessException { return execute(p1, null); }
/** * Convenient method to execute with a single int parameter and context. * @param p1 single int parameter * @param context the contextual information for object creation */ public List<T> execute(int p1, Map context) throws DataAccessException { return execute(new Object[] {p1}, context); }
/** * Convenient method to execute with two int parameters. * @param p1 first int parameter * @param p2 second int parameter */ public List<T> execute(int p1, int p2) throws DataAccessException { return execute(p1, p2, null); }
/** * Convenient method to execute without context. * @param params parameters for the query. Primitive parameters must * be represented by their Object wrapper type. The ordering of parameters is * significant. */ public List<T> execute(Object... params) throws DataAccessException { return execute(params, null); }
/** * Convenient method to execute with a single int parameter and context. * @param p1 single int parameter * @param context the contextual information for object creation */ public List<T> execute(int p1, @Nullable Map<?, ?> context) throws DataAccessException { return execute(new Object[] {p1}, context); }
/** * Convenient method to execute with a single String parameter and context. * @param p1 single String parameter * @param context the contextual information for object creation */ public List<T> execute(String p1, @Nullable Map<?, ?> context) throws DataAccessException { return execute(new Object[] {p1}, context); }