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] Add support for TimescaleDB (#11090) #11091

Merged
merged 1 commit into from
Dec 11, 2021
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
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