[jdbc] Removed check if item exists to allow to truncate data for clean-ups ()

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2022-01-15 17:09:54 +01:00 committed by GitHub
parent 0df790c687
commit 67b701b6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 20 deletions
bundles/org.openhab.persistence.jdbc/src
main/java/org/openhab/persistence/jdbc
test/java/org/openhab/persistence/jdbc/db

@ -362,7 +362,7 @@ public class JdbcBaseDAO {
.collect(Collectors.<HistoricItem> toList());
}
public void doDeleteItemValues(Item item, FilterCriteria filter, String table, ZoneId timeZone) {
public void doDeleteItemValues(FilterCriteria filter, String table, ZoneId timeZone) {
String sql = histItemFilterDeleteProvider(filter, table, timeZone);
logger.debug("JDBC::doDeleteItemValues sql={}", sql);
Yank.execute(sql, null);
@ -400,10 +400,8 @@ public class JdbcBaseDAO {
logger.debug("JDBC::histItemFilterDeleteProvider filter = {}, table = {}", filter, table);
String filterString = resolveTimeFilter(filter, timeZone);
String deleteString = "DELETE FROM " + table;
if (!filterString.isEmpty()) {
deleteString += filterString;
}
String deleteString = filterString.isEmpty() ? "TRUNCATE TABLE " + table
: "DELETE FROM " + table + filterString;
logger.debug("JDBC::delete deleteString = {}", deleteString);
return deleteString;
}

@ -184,12 +184,13 @@ public class JdbcMapper {
return null;
}
public boolean deleteItemValues(FilterCriteria filter, String table, Item item) {
logger.debug("JDBC::deleteItemValues filter='{}' table='{}' item='{}' itemName='{}'", (filter != null), table,
item, item.getName());
@SuppressWarnings("null")
public boolean deleteItemValues(FilterCriteria filter, String table) {
logger.debug("JDBC::deleteItemValues filter='{}' table='{}' itemName='{}'", (filter != null), table,
filter.getItemName());
if (table != null) {
long timerStart = System.currentTimeMillis();
conf.getDBDAO().doDeleteItemValues(item, filter, table, timeZoneProvider.getTimeZone());
conf.getDBDAO().doDeleteItemValues(filter, table, timeZoneProvider.getTimeZone());
logTime("deleteItemValues", timerStart, System.currentTimeMillis());
errCnt = 0;
return true;

@ -253,18 +253,11 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
// Get the item name from the filter
// Also get the Item object so we can determine the type
Item item = null;
String itemName = filter.getItemName();
logger.debug("JDBC::remove: item is {}", itemName);
if (itemName == null) {
throw new IllegalArgumentException("Item name must not be null");
}
try {
item = itemRegistry.getItem(itemName);
} catch (ItemNotFoundException e) {
logger.error("JDBC::remove: unable to get item for itemName: '{}'. Ignore and give up!", itemName);
return false;
}
String table = sqlTables.get(itemName);
if (table == null) {
@ -273,10 +266,10 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
}
long timerStart = System.currentTimeMillis();
boolean result = deleteItemValues(filter, table, item);
boolean result = deleteItemValues(filter, table);
if (logger.isDebugEnabled()) {
logger.debug("JDBC: Deleted values for item '{}' in SQL database at {} in {} ms.", item.getName(),
new Date(), System.currentTimeMillis() - timerStart);
logger.debug("JDBC: Deleted values for item '{}' in SQL database at {} in {} ms.", itemName, new Date(),
System.currentTimeMillis() - timerStart);
}
return result;

@ -83,7 +83,7 @@ public class JdbcBaseDAOTest {
@Test
public void testHistItemFilterDeleteProviderReturnsDeleteQueryWithoutWhereClause() {
String sql = jdbcBaseDAO.histItemFilterDeleteProvider(filter, DB_TABLE_NAME, UTC_ZONE_ID);
assertThat(sql, is("DELETE FROM " + DB_TABLE_NAME));
assertThat(sql, is("TRUNCATE TABLE " + DB_TABLE_NAME));
}
@Test