[jdbc] Add support for TimescaleDB (#11090) (#11091)

Signed-off-by: Riccardo Nimser-Joseph <github@nimric.de>

Co-authored-by: Riccardo Nimser-Joseph <github@nimric.de>
This commit is contained in:
nimric 2021-12-11 17:50:40 +01:00 committed by GitHub
parent f2996aa723
commit ba3dfe3ed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 2 deletions

View 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

View File

@ -248,6 +248,10 @@ public class JdbcBaseDAO {
dbMeta = new DbMetaData();// get DB information
}
public Properties getConnectionProperties() {
return new Properties(this.databaseProps);
}
/**************
* ITEMS DAOs *
**************/

View File

@ -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')";
}
}

View 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() {