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();
}
});
}
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;
}
});
}
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();
}
});
}
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;
}
});
}
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();
}
});
}
});
}
Aggregations