]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jdbc] Add support for TimescaleDB (#11090) (#11091)
authornimric <55734294+nimric@users.noreply.github.com>
Sat, 11 Dec 2021 16:50:40 +0000 (17:50 +0100)
committerGitHub <noreply@github.com>
Sat, 11 Dec 2021 16:50:40 +0000 (17:50 +0100)
Signed-off-by: Riccardo Nimser-Joseph <github@nimric.de>
Co-authored-by: Riccardo Nimser-Joseph <github@nimric.de>
bundles/org.openhab.persistence.jdbc/README.md
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcTimescaledbDAO.java [new file with mode: 0644]
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcConfiguration.java

index c6d2da2d3dd1a978c9ab776ee79c936cdfcdf662..45b1c6110b88cea067d700f17e91154d49d67cf2 100644 (file)
@@ -16,6 +16,7 @@ The following databases are currently supported and tested:
 | [MySQL](https://www.mysql.com/)              | [mysql-connector-java-5.1.39.jar](https://mvnrepository.com/artifact/mysql/mysql-connector-java) |
 | [PostgreSQL](https://www.postgresql.org/)    | [postgresql-9.4.1209.jre7.jar](https://mvnrepository.com/artifact/org.postgresql/postgresql) |
 | [SQLite](https://www.sqlite.org/)            | [sqlite-jdbc-3.16.1.jar](https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc) |
+| [TimescaleDB](https://www.timescale.com/)    | [postgresql-9.4.1209.jre7.jar](https://mvnrepository.com/artifact/org.postgresql/postgresql) |
 
 ## Table of Contents
 
index 72f8407b72dde80087b6af2ceb5bcd160720ac02..3025eef1b7b8fe7dbda6785b5903d6fb8ab69470 100644 (file)
@@ -248,6 +248,10 @@ public class JdbcBaseDAO {
         dbMeta = new DbMetaData();// get DB information
     }
 
+    public Properties getConnectionProperties() {
+        return new Properties(this.databaseProps);
+    }
+
     /**************
      * ITEMS DAOs *
      **************/
diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcTimescaledbDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcTimescaledbDAO.java
new file mode 100644 (file)
index 0000000..b6e157f
--- /dev/null
@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2010-2021 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.persistence.jdbc.db;
+
+import java.util.Properties;
+
+import org.knowm.yank.Yank;
+import org.openhab.persistence.jdbc.dto.ItemVO;
+import org.openhab.persistence.jdbc.utils.StringUtilsExt;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Extended Database Configuration class. Class represents the extended database-specific configuration. Overrides and
+ * supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
+ *
+ * @author Riccardo Nimser-Joseph - Initial contribution
+ */
+public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
+    private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
+
+    protected String sqlCreateHypertable;
+
+    public JdbcTimescaledbDAO() {
+        super();
+
+        initSqlQueries();
+    }
+
+    public Properties getDatabaseProperties() {
+        Properties properties = new Properties(this.databaseProps);
+
+        // Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
+        if (properties.containsKey("jdbcUrl")) {
+            properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql"));
+        }
+
+        return properties;
+    }
+
+    public void doCreateItemTable(ItemVO vo) {
+        String sql;
+
+        sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateItemTable,
+                new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
+                new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
+        this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
+        Yank.execute(sql, null);
+
+        sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
+                new String[] { vo.getTableName() });
+        this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
+        Yank.execute(sql, null);
+    }
+
+    private void initSqlQueries() {
+        this.logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
+
+        this.sqlCreateHypertable = "SELECT create_hypertable('#tableName#', 'time')";
+    }
+}
index 2e6f878949027262ebb6392729fbb51a1f7b2a2c..8e0fb1355c04b035e2cc83bdcc472bf7e0a9212f 100644 (file)
@@ -122,7 +122,7 @@ public class JdbcConfiguration {
 
         // set database type and database type class
         setDBDAOClass(parsedURL.getProperty("dbShortcut")); // derby, h2, hsqldb, mariadb, mysql, postgresql,
-                                                            // sqlite
+                                                            // sqlite, timescaledb
         // set user
         if (user != null && !user.isBlank()) {
             dBDAO.databaseProps.setProperty("dataSource.user", user);
@@ -322,7 +322,7 @@ public class JdbcConfiguration {
     }
 
     public Properties getHikariConfiguration() {
-        return dBDAO.databaseProps;
+        return dBDAO.getConnectionProperties();
     }
 
     public String getName() {