Skip to content

Commit

Permalink
[jdbc] Add support for TimescaleDB (openhab#11090) (openhab#11091)
Browse files Browse the repository at this point in the history
Signed-off-by: Riccardo Nimser-Joseph <[email protected]>

Co-authored-by: Riccardo Nimser-Joseph <[email protected]>
  • Loading branch information
2 people authored and andan67 committed Nov 5, 2022
1 parent 5b7a823 commit 3717e8a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions bundles/org.openhab.persistence.jdbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ public void initAfterFirstDbConnection() {
dbMeta = new DbMetaData();// get DB information
}

public Properties getConnectionProperties() {
return new Properties(this.databaseProps);
}

/**************
* ITEMS DAOs *
**************/
Expand Down
Original file line number Diff line number Diff line change
@@ -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')";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private boolean updateConfig(Map<Object, Object> config) {

// 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);
Expand Down Expand Up @@ -322,7 +322,7 @@ private void testJDBCDriver(String driver) {
}

public Properties getHikariConfiguration() {
return dBDAO.databaseProps;
return dBDAO.getConnectionProperties();
}

public String getName() {
Expand Down

0 comments on commit 3717e8a

Please sign in to comment.