From d3348327a9d7f02976b6ad83d35ee0d58e64049c Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 19 Nov 2022 20:57:51 +0100 Subject: [PATCH] Fix PostgreSQL query for storing timestamps (#13745) Fixes #13121 Signed-off-by: Jacob Laursen --- .../jdbc/internal/db/JdbcPostgresqlDAO.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcPostgresqlDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcPostgresqlDAO.java index 7a8ab72e8..e12f0b91a 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcPostgresqlDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcPostgresqlDAO.java @@ -13,6 +13,7 @@ package org.openhab.persistence.jdbc.internal.db; import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -167,6 +168,22 @@ public class JdbcPostgresqlDAO extends JdbcBaseDAO { } } + @Override + public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTime date) throws JdbcSQLException { + ItemVO storedVO = storeItemValueProvider(item, itemState, vo); + String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, + new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" }, + new String[] { storedVO.getTableName(), storedVO.getDbType(), "?" }); + java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli()); + Object[] params = { timestamp, storedVO.getValue() }; + logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue()); + try { + Yank.execute(sql, params); + } catch (YankSQLException e) { + throw new JdbcSQLException(e); + } + } + /**************************** * SQL generation Providers * ****************************/