Search in sources :

Example 26 with MapSqlParameterSource

use of org.springframework.jdbc.core.namedparam.MapSqlParameterSource in project Gargoyle by callakrsos.

the class VelocityTest method test.

@Test
public void test() throws SQLException, Exception {
    String sql = "SELECT table_name FROM information_schema.tables WHERE 1=1    AND  table_name in (:tableName)   limit 50";
    MapSqlParameterSource source = new MapSqlParameterSource();
    List<String> asList = Arrays.asList("tables", "columns");
    source.addValue("tableName", asList);
    List<HashMap<String, Object>> select = DbUtil.select(sql, source, (rs, r) -> {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("table_name", rs.getString("table_name"));
        return hashMap;
    });
    System.out.println(select);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 27 with MapSqlParameterSource

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

the class SimpleJdbcCallTests method testAddInvoiceFuncWithMetaDataUsingMapParamSource.

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

Example 28 with MapSqlParameterSource

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

the class MembersManagerImpl method getMembersByUsers.

public List<Member> getMembersByUsers(PerunSession sess, List<User> users, Vo vo) throws InternalErrorException {
    // If usersIds is empty, we can immediatelly return empty results
    if (users.size() == 0) {
        return new ArrayList<Member>();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    Set<Integer> usersIds = new HashSet<Integer>();
    for (User user : users) {
        usersIds.add(user.getId());
    }
    parameters.addValue("ids", usersIds);
    parameters.addValue("vo", vo.getId());
    try {
        return this.namedParameterJdbcTemplate.query("select " + memberMappingSelectQuery + " from members where members.user_id in ( :ids ) and members.vo_id=:vo", parameters, MEMBER_MAPPER);
    } catch (EmptyResultDataAccessException ex) {
        return new ArrayList<Member>();
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) User(cz.metacentrum.perun.core.api.User) ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet)

Example 29 with MapSqlParameterSource

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

the class GroupsManagerImpl method getGroupMembers.

public List<Member> getGroupMembers(PerunSession sess, Group group, List<Status> statuses, boolean excludeStatus) throws InternalErrorException {
    try {
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        List<Integer> statusesCodes = new ArrayList<Integer>();
        for (Status status : statuses) {
            statusesCodes.add(status.getCode());
        }
        parameters.addValue("statuses", statusesCodes);
        parameters.addValue("group_id", group.getId());
        if (excludeStatus) {
            // Exclude members with one of the status
            return this.namedParameterJdbcTemplate.query("select " + MembersManagerImpl.groupsMembersMappingSelectQuery + " from groups_members join members on members.id=groups_members.member_id " + "where groups_members.group_id=:group_id and members.status" + Compatibility.castToInteger() + " not in (:statuses)", parameters, MembersManagerImpl.MEMBER_MAPPER);
        } else {
            // Include members with one of the status
            return this.namedParameterJdbcTemplate.query("select " + MembersManagerImpl.groupsMembersMappingSelectQuery + " from groups_members join members on members.id=groups_members.member_id " + "where groups_members.group_id=:group_id and members.status" + Compatibility.castToInteger() + " in (:statuses)", parameters, MembersManagerImpl.MEMBER_MAPPER);
        }
    } catch (EmptyResultDataAccessException e) {
        return new ArrayList<Member>();
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : Status(cz.metacentrum.perun.core.api.Status) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ArrayList(java.util.ArrayList) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Member(cz.metacentrum.perun.core.api.Member)

Example 30 with MapSqlParameterSource

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

the class ResourcesManagerImpl method getResourcesByIds.

public List<Resource> getResourcesByIds(PerunSession sess, List<Integer> resourcesIds) throws InternalErrorException {
    if (resourcesIds.size() == 0) {
        return new ArrayList<Resource>();
    }
    MapSqlParameterSource parameters = new MapSqlParameterSource();
    parameters.addValue("ids", resourcesIds);
    try {
        return this.namedParameterJdbcTemplate.query("select " + resourceMappingSelectQuery + "  from resources where resources.id in ( :ids )", parameters, RESOURCE_MAPPER);
    } catch (RuntimeException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)42 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)17 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)15 Attribute (cz.metacentrum.perun.core.api.Attribute)10 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)9 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)9 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 SimpleJdbcInsert (org.springframework.jdbc.core.simple.SimpleJdbcInsert)4 Member (cz.metacentrum.perun.core.api.Member)3 SqlOutParameter (org.springframework.jdbc.core.SqlOutParameter)3 SqlParameter (org.springframework.jdbc.core.SqlParameter)3 ParsedSql (org.springframework.jdbc.core.namedparam.ParsedSql)3 User (cz.metacentrum.perun.core.api.User)2 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)1