Skip to content

Commit

Permalink
[jdbc] Fixed NPE when SQL query failed (openhab#11954)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored and moesterheld committed Jan 18, 2022
1 parent ba83431 commit 32b7e0c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria fil
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql);
List<Object[]> m = Yank.queryObjectArrays(sql, null);
if (m == null) {
logger.debug("JDBC::doGetHistItemFilterQuery Query failed. Returning an empty list.");
return List.of();
}
// we already retrieve the unit here once as it is a very costly operation
String itemName = item.getName();
Unit<? extends Quantity<?>> unit = item instanceof NumberItem ? ((NumberItem) item).getUnit() : null;
return m.stream().map(o -> new JdbcHistoricItem(itemName, getState(item, unit, o[1]), objectAsDate(o[0])))
return m.stream().map(o -> new JdbcHistoricItem(itemName, objectAsState(item, unit, o[1]), objectAsDate(o[0])))
.collect(Collectors.<HistoricItem> toList());
}

Expand Down Expand Up @@ -489,7 +493,7 @@ protected ItemVO storeItemValueProvider(Item item, ItemVO vo) {
/*****************
* H E L P E R S *
*****************/
protected State getState(Item item, @Nullable Unit<? extends Quantity<?>> unit, Object v) {
protected State objectAsState(Item item, @Nullable Unit<? extends Quantity<?>> unit, Object v) {
logger.debug(
"JDBC::ItemResultHandler::handleResult getState value = '{}', unit = '{}', getClass = '{}', clazz = '{}'",
v, unit, v.getClass(), v.getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria fil
Unit<? extends Quantity<?>> unit = item instanceof NumberItem ? ((NumberItem) item).getUnit() : null;
return m.stream().map(o -> {
logger.debug("JDBC::doGetHistItemFilterQuery 0='{}' 1='{}'", o[0], o[1]);
return new JdbcHistoricItem(itemName, getState(item, unit, o[1]), objectAsDate(o[0]));
return new JdbcHistoricItem(itemName, objectAsState(item, unit, o[1]), objectAsDate(o[0]));
}).collect(Collectors.<HistoricItem> toList());
}

Expand Down

0 comments on commit 32b7e0c

Please sign in to comment.