Search in sources :

Example 16 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project iaf by ibissource.

the class Storage method getReport.

@Override
public Report getReport(Integer storageId) throws StorageException {
    final Report report = new Report();
    report.setTestTool(testTool);
    report.setStorage(this);
    report.setStorageId(storageId);
    report.setName("Table " + table);
    report.setStubStrategy("Never");
    final List<Checkpoint> checkpoints = new ArrayList<Checkpoint>();
    report.setCheckpoints(checkpoints);
    StringBuilder query = new StringBuilder("select " + reportColumnNames.get(0));
    for (int i = 1; i < reportColumnNames.size(); i++) {
        query.append(", " + reportColumnNames.get(i));
    }
    query.append(" from " + table + " where LOGID = ?");
    try {
        jdbcTemplate.query(query.toString(), new Object[] { storageId }, new RowMapper() {

            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                for (int i = 0; i < reportColumnNames.size(); i++) {
                    String value = getValue(rs, i + 1);
                    Checkpoint checkpoint = new Checkpoint(report, Thread.currentThread().getName(), Storage.class.getName(), "Column " + reportColumnNames.get(i), Checkpoint.TYPE_INPUTPOINT, 0);
                    checkpoint.setMessage(value);
                    checkpoints.add(checkpoint);
                }
                return null;
            }
        });
    } catch (DataAccessException e) {
        throw new StorageException("Could not read report", e);
    }
    return report;
}
Also used : Report(nl.nn.testtool.Report) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Checkpoint(nl.nn.testtool.Checkpoint) Checkpoint(nl.nn.testtool.Checkpoint) ResultSet(java.sql.ResultSet) StorageException(nl.nn.testtool.storage.StorageException) DataAccessException(org.springframework.dao.DataAccessException) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 17 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project spring-integration by spring-projects.

the class StoredProcExecutorTests method testSetReturningResultSetRowMappersWithMapContainingNullValues.

@Test
public void testSetReturningResultSetRowMappersWithMapContainingNullValues() {
    DataSource datasource = mock(DataSource.class);
    StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource);
    Map<String, RowMapper<?>> rowmappers = new HashMap<String, RowMapper<?>>();
    rowmappers.put("results", null);
    try {
        storedProcExecutor.setReturningResultSetRowMappers(rowmappers);
    } catch (IllegalArgumentException e) {
        assertEquals("The provided map cannot contain null values.", e.getMessage());
        return;
    }
    fail("Exception expected.");
}
Also used : HashMap(java.util.HashMap) DataSource(javax.sql.DataSource) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.junit.Test)

Example 18 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project ma-core-public by infiniteautomation.

the class Upgrade14 method upgrade.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.db.upgrade.DBUpgrade#upgrade()
	 */
@Override
protected void upgrade() throws Exception {
    OutputStream os = createUpdateLogOutputStream();
    // Run the scripts to add the ID Column
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyIdColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlIdColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlIdColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2IdColumnScript);
    runScript(scripts, os);
    // Run the scripts to create the XID column but not restrict it to NOT NULL
    scripts = new HashMap<>();
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyXidColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlXidColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlXidColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2XidColumnScript);
    runScript(scripts, os);
    // Set XIDs on all the user comments, ugh
    AtomicInteger count = new AtomicInteger();
    final String prefix = "UC_";
    final RowMapper<Integer> mapper = new RowMapper<Integer>() {

        @Override
        public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getInt(1);
        }
    };
    final MappedRowCallback<Integer> callback = new MappedRowCallback<Integer>() {

        @Override
        public void row(Integer item, int index) {
            ejt.update("UPDATE userComments SET xid=? WHERE id=?", new Object[] { prefix + UUID.randomUUID(), item });
            count.incrementAndGet();
        }
    };
    this.ejt.query("SELECT id FROM userComments", new Object[] {}, new RowCallbackHandler() {

        private int rowNum = 0;

        public void processRow(ResultSet rs) throws SQLException {
            callback.row(mapper.mapRow(rs, rowNum), rowNum);
            rowNum++;
        }
    });
    String upgradedString = new String("Updated " + count.get() + " user comments with XIDs.\n");
    os.write(upgradedString.getBytes(Common.UTF8_CS));
    // Run the scripts to restrict the XID to NOT NULL
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyXidNotNullColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlXidNotNullColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlXidNotNullColumnScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2XidNotNullColumnScript);
    runScript(scripts, os);
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) OutputStream(java.io.OutputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MappedRowCallback(com.serotonin.db.MappedRowCallback) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 19 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project ovirt-engine by oVirt.

the class SystemStatisticsDaoImpl method getSystemStatisticsValue.

@Override
public Integer getSystemStatisticsValue(String entity, String status) {
    MapSqlParameterSource parameterSource = new CustomMapSqlParameterSource(dbEngineDialect).addValue("entity", entity).addValue("status", status);
    RowMapper<Integer> mapper = (rs, rowNum) -> rs.getInt("val");
    Map<String, Object> dbResults = dbEngineDialect.createJdbcCallForQuery(jdbcTemplate).withProcedureName("Getsystem_statistics").returningResultSet("RETURN_VALUE", mapper).execute(parameterSource);
    return (Integer) DbFacadeUtils.asSingleResult((List<?>) dbResults.get("RETURN_VALUE"));
}
Also used : Objects(java.util.Objects) Inject(javax.inject.Inject) List(java.util.List) Map(java.util.Map) RowMapper(org.springframework.jdbc.core.RowMapper) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Named(javax.inject.Named) DbEngineDialect(org.ovirt.engine.core.dal.dbbroker.DbEngineDialect) Singleton(javax.inject.Singleton) CustomMapSqlParameterSource(org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource) DbFacadeUtils(org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) CustomMapSqlParameterSource(org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource) CustomMapSqlParameterSource(org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource) List(java.util.List)

Example 20 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project ovirt-engine by oVirt.

the class TagDaoImpl method getTagTemplateByTagIdAndByTemplateId.

/**
 * In the database both Template and Vm Tags share the same tables and functions
 */
@Override
public TagsTemplateMap getTagTemplateByTagIdAndByTemplateId(Guid tagId, Guid vmId) {
    MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource().addValue("tag_id", tagId).addValue("vm_id", vmId);
    RowMapper<TagsTemplateMap> mapper = (rs, rowNum) -> {
        TagsTemplateMap entity = new TagsTemplateMap();
        entity.setTagId(getGuidDefaultEmpty(rs, "tag_id"));
        entity.setTemplateId(getGuidDefaultEmpty(rs, "vm_id"));
        entity.setDefaultDisplayType((Integer) rs.getObject("DefaultDisplayType"));
        return entity;
    };
    return getCallsHandler().executeRead("GetTagVmByTagIdAndByvmId", mapper, parameterSource);
}
Also used : TagsVdsMap(org.ovirt.engine.core.common.businessentities.TagsVdsMap) Guid(org.ovirt.engine.core.compat.Guid) TagsUserGroupMap(org.ovirt.engine.core.common.businessentities.TagsUserGroupMap) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Singleton(javax.inject.Singleton) TagsTemplateMap(org.ovirt.engine.core.common.businessentities.TagsTemplateMap) TagsType(org.ovirt.engine.core.common.businessentities.TagsType) SQLException(java.sql.SQLException) List(java.util.List) TagsUserMap(org.ovirt.engine.core.common.businessentities.TagsUserMap) ResultSet(java.sql.ResultSet) Tags(org.ovirt.engine.core.common.businessentities.Tags) TagsVmMap(org.ovirt.engine.core.common.businessentities.TagsVmMap) RowMapper(org.springframework.jdbc.core.RowMapper) Named(javax.inject.Named) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) TagsTemplateMap(org.ovirt.engine.core.common.businessentities.TagsTemplateMap)

Aggregations

RowMapper (org.springframework.jdbc.core.RowMapper)51 ResultSet (java.sql.ResultSet)27 SQLException (java.sql.SQLException)16 List (java.util.List)14 Named (javax.inject.Named)8 Singleton (javax.inject.Singleton)8 Test (org.junit.jupiter.api.Test)8 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)8 Guid (org.ovirt.engine.core.compat.Guid)7 QueryMappingConfiguration (org.springframework.data.jdbc.repository.QueryMappingConfiguration)7 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Tags (org.ovirt.engine.core.common.businessentities.Tags)5 TagsTemplateMap (org.ovirt.engine.core.common.businessentities.TagsTemplateMap)5 TagsType (org.ovirt.engine.core.common.businessentities.TagsType)5 TagsUserGroupMap (org.ovirt.engine.core.common.businessentities.TagsUserGroupMap)5 TagsUserMap (org.ovirt.engine.core.common.businessentities.TagsUserMap)5 TagsVdsMap (org.ovirt.engine.core.common.businessentities.TagsVdsMap)5 TagsVmMap (org.ovirt.engine.core.common.businessentities.TagsVmMap)5 DataAccessException (org.springframework.dao.DataAccessException)5