Search in sources :

Example 11 with BatchPreparedStatementSetter

use of org.springframework.jdbc.core.BatchPreparedStatementSetter in project SSM by Intel-bigdata.

the class FileStateDao method batchDelete.

public int[] batchDelete(final List<String> paths) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    final String sql = "DELETE FROM " + TABLE_NAME + " WHERE path = ?";
    return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setString(1, paths.get(i));
        }

        @Override
        public int getBatchSize() {
            return paths.size();
        }
    });
}
Also used : SQLException(java.sql.SQLException) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 12 with BatchPreparedStatementSetter

use of org.springframework.jdbc.core.BatchPreparedStatementSetter in project SSM by Intel-bigdata.

the class FileStateDao method batchInsertUpdate.

public int[] batchInsertUpdate(final FileState[] fileStates) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "REPLACE INTO " + TABLE_NAME + " (path, type, stage) VALUES (?,?,?)";
    return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setString(1, fileStates[i].getPath());
            ps.setInt(2, fileStates[i].getFileType().getValue());
            ps.setInt(3, fileStates[i].getFileStage().getValue());
        }

        @Override
        public int getBatchSize() {
            return fileStates.length;
        }
    });
}
Also used : SQLException(java.sql.SQLException) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 13 with BatchPreparedStatementSetter

use of org.springframework.jdbc.core.BatchPreparedStatementSetter in project SSM by Intel-bigdata.

the class SmallFileDao method batchDelete.

public int[] batchDelete(final List<String> paths) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    final String sql = "DELETE FROM small_file WHERE path = ?";
    return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setString(1, paths.get(i));
        }

        @Override
        public int getBatchSize() {
            return paths.size();
        }
    });
}
Also used : SQLException(java.sql.SQLException) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 14 with BatchPreparedStatementSetter

use of org.springframework.jdbc.core.BatchPreparedStatementSetter in project SSM by Intel-bigdata.

the class StorageDao method insertUpdateStoragesTable.

public void insertUpdateStoragesTable(final StorageCapacity[] storages) throws SQLException {
    if (storages.length == 0) {
        return;
    }
    final Long curr = System.currentTimeMillis();
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "REPLACE INTO storage (type, time_stamp, capacity, free) VALUES (?,?,?,?);";
    jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setString(1, storages[i].getType());
            if (storages[i].getTimeStamp() == null) {
                ps.setLong(2, curr);
            } else {
                ps.setLong(2, storages[i].getTimeStamp());
            }
            ps.setLong(3, storages[i].getCapacity());
            ps.setLong(4, storages[i].getFree());
        }

        public int getBatchSize() {
            return storages.length;
        }
    });
}
Also used : SQLException(java.sql.SQLException) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 15 with BatchPreparedStatementSetter

use of org.springframework.jdbc.core.BatchPreparedStatementSetter in project thingsboard by thingsboard.

the class AttributeKvInsertRepository method saveOrUpdate.

protected void saveOrUpdate(List<AttributeKvEntity> entities) {
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            int[] result = jdbcTemplate.batchUpdate(BATCH_UPDATE, new BatchPreparedStatementSetter() {

                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    AttributeKvEntity kvEntity = entities.get(i);
                    ps.setString(1, replaceNullChars(kvEntity.getStrValue()));
                    if (kvEntity.getLongValue() != null) {
                        ps.setLong(2, kvEntity.getLongValue());
                    } else {
                        ps.setNull(2, Types.BIGINT);
                    }
                    if (kvEntity.getDoubleValue() != null) {
                        ps.setDouble(3, kvEntity.getDoubleValue());
                    } else {
                        ps.setNull(3, Types.DOUBLE);
                    }
                    if (kvEntity.getBooleanValue() != null) {
                        ps.setBoolean(4, kvEntity.getBooleanValue());
                    } else {
                        ps.setNull(4, Types.BOOLEAN);
                    }
                    ps.setString(5, replaceNullChars(kvEntity.getJsonValue()));
                    ps.setLong(6, kvEntity.getLastUpdateTs());
                    ps.setString(7, kvEntity.getId().getEntityType().name());
                    ps.setObject(8, kvEntity.getId().getEntityId());
                    ps.setString(9, kvEntity.getId().getAttributeType());
                    ps.setString(10, kvEntity.getId().getAttributeKey());
                }

                @Override
                public int getBatchSize() {
                    return entities.size();
                }
            });
            int updatedCount = 0;
            for (int i = 0; i < result.length; i++) {
                if (result[i] == 0) {
                    updatedCount++;
                }
            }
            List<AttributeKvEntity> insertEntities = new ArrayList<>(updatedCount);
            for (int i = 0; i < result.length; i++) {
                if (result[i] == 0) {
                    insertEntities.add(entities.get(i));
                }
            }
            jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() {

                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    AttributeKvEntity kvEntity = insertEntities.get(i);
                    ps.setString(1, kvEntity.getId().getEntityType().name());
                    ps.setObject(2, kvEntity.getId().getEntityId());
                    ps.setString(3, kvEntity.getId().getAttributeType());
                    ps.setString(4, kvEntity.getId().getAttributeKey());
                    ps.setString(5, replaceNullChars(kvEntity.getStrValue()));
                    ps.setString(11, replaceNullChars(kvEntity.getStrValue()));
                    if (kvEntity.getLongValue() != null) {
                        ps.setLong(6, kvEntity.getLongValue());
                        ps.setLong(12, kvEntity.getLongValue());
                    } else {
                        ps.setNull(6, Types.BIGINT);
                        ps.setNull(12, Types.BIGINT);
                    }
                    if (kvEntity.getDoubleValue() != null) {
                        ps.setDouble(7, kvEntity.getDoubleValue());
                        ps.setDouble(13, kvEntity.getDoubleValue());
                    } else {
                        ps.setNull(7, Types.DOUBLE);
                        ps.setNull(13, Types.DOUBLE);
                    }
                    if (kvEntity.getBooleanValue() != null) {
                        ps.setBoolean(8, kvEntity.getBooleanValue());
                        ps.setBoolean(14, kvEntity.getBooleanValue());
                    } else {
                        ps.setNull(8, Types.BOOLEAN);
                        ps.setNull(14, Types.BOOLEAN);
                    }
                    ps.setString(9, replaceNullChars(kvEntity.getJsonValue()));
                    ps.setString(15, replaceNullChars(kvEntity.getJsonValue()));
                    ps.setLong(10, kvEntity.getLastUpdateTs());
                    ps.setLong(16, kvEntity.getLastUpdateTs());
                }

                @Override
                public int getBatchSize() {
                    return insertEntities.size();
                }
            });
        }
    });
}
Also used : AttributeKvEntity(org.thingsboard.server.dao.model.sql.AttributeKvEntity) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

PreparedStatement (java.sql.PreparedStatement)28 BatchPreparedStatementSetter (org.springframework.jdbc.core.BatchPreparedStatementSetter)28 SQLException (java.sql.SQLException)22 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)18 TransactionStatus (org.springframework.transaction.TransactionStatus)6 ArrayList (java.util.ArrayList)5 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)5 List (java.util.List)4 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)4 TransactionCallback (org.springframework.transaction.support.TransactionCallback)2 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)2 ModifyWordbook (zjp.translateit.dto.ModifyWordbook)2 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)1 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)1 DbDataMedia (com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia)1 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)1 NamedThreadFactory (com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory)1 Query (com.baidu.unbiz.common.genericdao.operator.Query)1 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)1 EmailRecipient (com.serotonin.m2m2.vo.mailingList.EmailRecipient)1