Search in sources :

Example 1 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project perun by CESNET.

the class UsersManagerImpl method getUsersByIds.

public List<User> getUsersByIds(PerunSession sess, List<Integer> usersIds) throws InternalErrorException {
    // If usersIds is empty, we can immediatelly return empty results
    if (usersIds.size() == 0) {
        return new ArrayList<User>();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("ids", usersIds);
    try {
        return namedParameterJdbcTemplate.query("select " + userMappingSelectQuery + "  from users where users.id in ( :ids )", parameters, USER_MAPPER);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<User>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 2 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project perun by CESNET.

the class GroupsManagerImpl method getGroupsByIds.

public List<Group> getGroupsByIds(PerunSession sess, List<Integer> groupsIds) throws InternalErrorException {
    // If groupsIds are empty, we can immediately return empty result
    if (groupsIds.size() == 0) {
        return new ArrayList<Group>();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("ids", groupsIds);
    try {
        return this.namedParameterJdbcTemplate.query("select " + groupMappingSelectQuery + " from groups where groups.id in ( :ids )", parameters, GROUP_MAPPER);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Group>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 3 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project perun by CESNET.

the class TaskResultDaoJdbc method getTaskResultsForDestinations.

public List<TaskResult> getTaskResultsForDestinations(List<String> destinationsNames) throws InternalErrorException {
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("destinations", destinationsNames);
    try {
        return getNamedParameterJdbcTemplate().query("select " + taskResultMappingSelectQuery + ", " + ServicesManagerImpl.destinationMappingSelectQuery + ", " + ServicesManagerImpl.serviceMappingSelectQuery + " from tasks_results left join destinations on tasks_results.destination_id = destinations.id" + " left join tasks on tasks.id = tasks_results.task_id " + " left join exec_services on exec_services.id = tasks.exec_service_id" + " left join services on services.id = exec_services.service_id where destinations.destination in ( :destinations )", parameters, TASKRESULT_ROWMAPPER);
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 4 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project spring-framework by spring-projects.

the class SimpleJdbcCallTests method testAddInvoiceProcWithoutMetaDataUsingMapParamSource.

@Test
public void testAddInvoiceProcWithoutMetaDataUsingMapParamSource() throws Exception {
    initializeAddInvoiceWithoutMetaData(false);
    SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
    adder.declareParameters(new SqlParameter("amount", Types.INTEGER), new SqlParameter("custid", Types.INTEGER), new SqlOutParameter("newid", Types.INTEGER));
    Number newId = adder.executeObject(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
    assertEquals(4, newId.intValue());
    verifyAddInvoiceWithoutMetaData(false);
    verify(connection, atLeastOnce()).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SqlOutParameter(org.springframework.jdbc.core.SqlOutParameter) Test(org.junit.Test)

Example 5 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project spring-framework by spring-projects.

the class SimpleJdbcCallTests method testAddInvoiceProcWithMetaDataUsingMapParamSource.

@Test
public void testAddInvoiceProcWithMetaDataUsingMapParamSource() throws Exception {
    initializeAddInvoiceWithMetaData(false);
    SimpleJdbcCall adder = new SimpleJdbcCall(dataSource).withProcedureName("add_invoice");
    Number newId = adder.executeObject(Number.class, new MapSqlParameterSource().addValue("amount", 1103).addValue("custid", 3));
    assertEquals(4, newId.intValue());
    verifyAddInvoiceWithMetaData(false);
    verify(connection, atLeastOnce()).close();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Test(org.junit.Test)

Aggregations

MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)494 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)82 SqlParameterSource (org.springframework.jdbc.core.namedparam.SqlParameterSource)62 Test (org.junit.jupiter.api.Test)48 ArrayList (java.util.ArrayList)35 ResultSet (java.sql.ResultSet)33 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)32 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)32 List (java.util.List)30 HashMap (java.util.HashMap)29 SQLException (java.sql.SQLException)25 Guid (org.ovirt.engine.core.compat.Guid)25 Collectors (java.util.stream.Collectors)16 CustomMapSqlParameterSource (org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource)16 Transactional (org.springframework.transaction.annotation.Transactional)14 java.util (java.util)13 Component (org.springframework.stereotype.Component)13 Map (java.util.Map)12 RowMapper (org.springframework.jdbc.core.RowMapper)11 Named (javax.inject.Named)10