|
|
|
@ -17,9 +17,7 @@ package org.thingsboard.server.dao.sqlts.timescale; |
|
|
|
|
|
|
|
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
|
|
|
import org.springframework.stereotype.Repository; |
|
|
|
import org.springframework.transaction.TransactionStatus; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult; |
|
|
|
import org.thingsboard.server.dao.model.sqlts.timescale.TimescaleTsKvEntity; |
|
|
|
import org.thingsboard.server.dao.sqlts.AbstractTimeseriesInsertRepository; |
|
|
|
import org.thingsboard.server.dao.util.PsqlDao; |
|
|
|
@ -28,7 +26,6 @@ import org.thingsboard.server.dao.util.TimescaleDBTsDao; |
|
|
|
import java.sql.PreparedStatement; |
|
|
|
import java.sql.SQLException; |
|
|
|
import java.sql.Types; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
@TimescaleDBTsDao |
|
|
|
@ -57,101 +54,46 @@ public class TimescaleInsertRepository extends AbstractTimeseriesInsertRepositor |
|
|
|
|
|
|
|
@Override |
|
|
|
public void saveOrUpdate(List<TimescaleTsKvEntity> entities) { |
|
|
|
transactionTemplate.execute(new TransactionCallbackWithoutResult() { |
|
|
|
jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() { |
|
|
|
@Override |
|
|
|
protected void doInTransactionWithoutResult(TransactionStatus status) { |
|
|
|
int[] result = jdbcTemplate.batchUpdate(BATCH_UPDATE, new BatchPreparedStatementSetter() { |
|
|
|
@Override |
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
|
|
|
|
|
if (entities.get(i).getBooleanValue() != null) { |
|
|
|
ps.setBoolean(1, entities.get(i).getBooleanValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(1, Types.BOOLEAN); |
|
|
|
} |
|
|
|
|
|
|
|
ps.setString(2, replaceNullChars(entities.get(i).getStrValue())); |
|
|
|
|
|
|
|
if (entities.get(i).getLongValue() != null) { |
|
|
|
ps.setLong(3, entities.get(i).getLongValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(3, Types.BIGINT); |
|
|
|
} |
|
|
|
|
|
|
|
if (entities.get(i).getDoubleValue() != null) { |
|
|
|
ps.setDouble(4, entities.get(i).getDoubleValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(4, Types.DOUBLE); |
|
|
|
} |
|
|
|
|
|
|
|
ps.setString(5, entities.get(i).getTenantId()); |
|
|
|
ps.setString(6, entities.get(i).getEntityId()); |
|
|
|
ps.setString(7, entities.get(i).getKey()); |
|
|
|
ps.setLong(8, entities.get(i).getTs()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int getBatchSize() { |
|
|
|
return entities.size(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
int updatedCount = 0; |
|
|
|
for (int i = 0; i < result.length; i++) { |
|
|
|
if (result[i] == 0) { |
|
|
|
updatedCount++; |
|
|
|
} |
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
|
ps.setString(1, entities.get(i).getTenantId()); |
|
|
|
ps.setString(2, entities.get(i).getEntityId()); |
|
|
|
ps.setString(3, entities.get(i).getKey()); |
|
|
|
ps.setLong(4, entities.get(i).getTs()); |
|
|
|
|
|
|
|
if (entities.get(i).getBooleanValue() != null) { |
|
|
|
ps.setBoolean(5, entities.get(i).getBooleanValue()); |
|
|
|
ps.setBoolean(9, entities.get(i).getBooleanValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(5, Types.BOOLEAN); |
|
|
|
ps.setNull(9, Types.BOOLEAN); |
|
|
|
} |
|
|
|
|
|
|
|
List<TimescaleTsKvEntity> insertEntities = new ArrayList<>(updatedCount); |
|
|
|
for (int i = 0; i < result.length; i++) { |
|
|
|
if (result[i] == 0) { |
|
|
|
insertEntities.add(entities.get(i)); |
|
|
|
} |
|
|
|
ps.setString(6, replaceNullChars(entities.get(i).getStrValue())); |
|
|
|
ps.setString(10, replaceNullChars(entities.get(i).getStrValue())); |
|
|
|
|
|
|
|
|
|
|
|
if (entities.get(i).getLongValue() != null) { |
|
|
|
ps.setLong(7, entities.get(i).getLongValue()); |
|
|
|
ps.setLong(11, entities.get(i).getLongValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(7, Types.BIGINT); |
|
|
|
ps.setNull(11, Types.BIGINT); |
|
|
|
} |
|
|
|
|
|
|
|
if (entities.get(i).getDoubleValue() != null) { |
|
|
|
ps.setDouble(8, entities.get(i).getDoubleValue()); |
|
|
|
ps.setDouble(12, entities.get(i).getDoubleValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(8, Types.DOUBLE); |
|
|
|
ps.setNull(12, Types.DOUBLE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
jdbcTemplate.batchUpdate(INSERT_OR_UPDATE, new BatchPreparedStatementSetter() { |
|
|
|
@Override |
|
|
|
public void setValues(PreparedStatement ps, int i) throws SQLException { |
|
|
|
ps.setString(1, entities.get(i).getTenantId()); |
|
|
|
ps.setString(2, entities.get(i).getEntityId()); |
|
|
|
ps.setString(3, entities.get(i).getKey()); |
|
|
|
ps.setLong(4, entities.get(i).getTs()); |
|
|
|
|
|
|
|
if (entities.get(i).getBooleanValue() != null) { |
|
|
|
ps.setBoolean(5, entities.get(i).getBooleanValue()); |
|
|
|
ps.setBoolean(9, entities.get(i).getBooleanValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(5, Types.BOOLEAN); |
|
|
|
ps.setNull(9, Types.BOOLEAN); |
|
|
|
} |
|
|
|
|
|
|
|
ps.setString(6, replaceNullChars(entities.get(i).getStrValue())); |
|
|
|
ps.setString(10, replaceNullChars(entities.get(i).getStrValue())); |
|
|
|
|
|
|
|
|
|
|
|
if (entities.get(i).getLongValue() != null) { |
|
|
|
ps.setLong(7, entities.get(i).getLongValue()); |
|
|
|
ps.setLong(11, entities.get(i).getLongValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(7, Types.BIGINT); |
|
|
|
ps.setNull(11, Types.BIGINT); |
|
|
|
} |
|
|
|
|
|
|
|
if (entities.get(i).getDoubleValue() != null) { |
|
|
|
ps.setDouble(8, entities.get(i).getDoubleValue()); |
|
|
|
ps.setDouble(12, entities.get(i).getDoubleValue()); |
|
|
|
} else { |
|
|
|
ps.setNull(8, Types.DOUBLE); |
|
|
|
ps.setNull(12, Types.DOUBLE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int getBatchSize() { |
|
|
|
return insertEntities.size(); |
|
|
|
} |
|
|
|
}); |
|
|
|
@Override |
|
|
|
public int getBatchSize() { |
|
|
|
return entities.size(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|