Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jdbc] Consider local timezone to create time filter #9445

Merged
merged 4 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ public void doStoreItemValue(Item item, ItemVO vo) {
}

public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount,
String table, String name) {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name);
String table, String name, ZoneId timeZone) {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql);
List<Object[]> m = Yank.queryObjectArrays(sql, null);

Expand All @@ -348,19 +348,21 @@ public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria fil
static final DateTimeFormatter JDBC_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

private String histItemFilterQueryProvider(FilterCriteria filter, int numberDecimalcount, String table,
String simpleName) {
String simpleName, ZoneId timeZone) {
logger.debug(
"JDBC::getHistItemFilterQueryProvider filter = {}, numberDecimalcount = {}, table = {}, simpleName = {}",
filter.toString(), numberDecimalcount, table, simpleName);

String filterString = "";
if (filter.getBeginDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate()) + "'";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate().withZoneSameInstant(timeZone))
+ "'";
}
if (filter.getEndDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate()) + "'";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate().withZoneSameInstant(timeZone))
+ "'";
}
filterString += (filter.getOrdering() == Ordering.ASCENDING) ? " ORDER BY time ASC" : " ORDER BY time DESC ";
if (filter.getPageSize() != 0x7fffffff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.persistence.jdbc.db;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -156,8 +157,8 @@ public void doStoreItemValue(Item item, ItemVO vo) {

@Override
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount,
String table, String name) {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name);
String table, String name, ZoneId timeZone) {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
List<Object[]> m = Yank.queryObjectArrays(sql, null);

logger.debug("JDBC::doGetHistItemFilterQuery got Array length={}", m.size());
Expand All @@ -175,26 +176,22 @@ public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria fil
****************************/
static final DateTimeFormatter JDBC_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

/**
* @param filter
* @param numberDecimalcount
* @param table
* @return
*/
private String histItemFilterQueryProvider(FilterCriteria filter, int numberDecimalcount, String table,
String simpleName) {
String simpleName, ZoneId timeZone) {
logger.debug(
"JDBC::getHistItemFilterQueryProvider filter = {}, numberDecimalcount = {}, table = {}, simpleName = {}",
StringUtilsExt.filterToString(filter), numberDecimalcount, table, simpleName);

String filterString = "";
if (filter.getBeginDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate()) + "'";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate().withZoneSameInstant(timeZone))
+ "'";
}
if (filter.getEndDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate()) + "'";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate().withZoneSameInstant(timeZone))
+ "'";
}
filterString += (filter.getOrdering() == Ordering.ASCENDING) ? " ORDER BY time ASC" : " ORDER BY time DESC";
if (filter.getPageSize() != 0x7fffffff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.persistence.jdbc.db;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -146,8 +147,8 @@ public void doStoreItemValue(Item item, ItemVO vo) {

@Override
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount,
String table, String name) {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name);
String table, String name, ZoneId timeZone) {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql);
List<Object[]> m = Yank.queryObjectArrays(sql, null);

Expand All @@ -163,26 +164,22 @@ public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria fil
****************************/
static final DateTimeFormatter JDBC_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

/**
* @param filter
* @param numberDecimalcount
* @param table
* @return
*/
private String histItemFilterQueryProvider(FilterCriteria filter, int numberDecimalcount, String table,
String simpleName) {
String simpleName, ZoneId timeZone) {
logger.debug(
"JDBC::getHistItemFilterQueryProvider filter = {}, numberDecimalcount = {}, table = {}, simpleName = {}",
filter.toString(), numberDecimalcount, table, simpleName);

String filterString = "";
if (filter.getBeginDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate()) + "'";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate().withZoneSameInstant(timeZone))
+ "'";
}
if (filter.getEndDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate()) + "'";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate().withZoneSameInstant(timeZone))
+ "'";
}
filterString += (filter.getOrdering() == Ordering.ASCENDING) ? " ORDER BY time ASC" : " ORDER BY time DESC";
if (filter.getPageSize() != 0x7fffffff) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.stream.Collectors;

import org.knowm.yank.Yank;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.Item;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.HistoricItem;
Expand All @@ -38,6 +39,8 @@
public class JdbcMapper {
private final Logger logger = LoggerFactory.getLogger(JdbcMapper.class);

private final TimeZoneProvider timeZoneProvider;

// Error counter - used to reconnect to database on error
protected int errCnt;
protected boolean initialized = false;
Expand All @@ -47,6 +50,10 @@ public class JdbcMapper {
private long afterAccessMax = 0;
private static final String ITEM_NAME_PATTERN = "[^a-zA-Z_0-9\\-]";

public JdbcMapper(TimeZoneProvider timeZoneProvider) {
this.timeZoneProvider = timeZoneProvider;
}

/*****************
* MAPPER ITEMS *
*****************/
Expand Down Expand Up @@ -160,7 +167,7 @@ public List<HistoricItem> getHistItemFilterQuery(FilterCriteria filter, int numb
if (table != null) {
long timerStart = System.currentTimeMillis();
List<HistoricItem> r = conf.getDBDAO().doGetHistItemFilterQuery(item, filter, numberDecimalcount, table,
item.getName());
item.getName(), timeZoneProvider.getTimeZone());
logTime("insertItemValue", timerStart, System.currentTimeMillis());
return r;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.ConfigurableService;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
Expand Down Expand Up @@ -60,7 +61,9 @@ public class JdbcPersistenceService extends JdbcMapper implements QueryablePersi
private final ItemRegistry itemRegistry;

@Activate
public JdbcPersistenceService(final @Reference ItemRegistry itemRegistry) {
public JdbcPersistenceService(final @Reference ItemRegistry itemRegistry,
final @Reference TimeZoneProvider timeZoneProvider) {
super(timeZoneProvider);
this.itemRegistry = itemRegistry;
}

Expand Down