2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.jdbc.internal.db;
15 import java.util.List;
16 import java.util.Properties;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.knowm.yank.Yank;
20 import org.knowm.yank.exceptions.YankSQLException;
21 import org.openhab.persistence.jdbc.internal.dto.ItemVO;
22 import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
23 import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
24 import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Extended Database Configuration class. Class represents the extended database-specific configuration. Overrides and
30 * supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
32 * @author Riccardo Nimser-Joseph - Initial contribution
33 * @author Dan Cunningham - Fixes and refactoring
36 public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
37 private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
39 private final String sqlCreateHypertable = "SELECT created FROM create_hypertable('\"#tableName#\"', 'time')";
40 private final String sqlGetItemTables = "SELECT hypertable_name as table_name FROM timescaledb_information.hypertables WHERE hypertable_name != '\"#itemsManageTable#\"'";
43 public Properties getConnectionProperties() {
44 Properties properties = (Properties) this.databaseProps.clone();
45 // Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
46 if (properties.containsKey("jdbcUrl")) {
47 properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("jdbc:timescaledb", "jdbc:postgresql"));
57 public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
58 super.doCreateItemTable(vo);
59 String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
60 new String[] { vo.getTableName() });
61 this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
63 Yank.queryScalar(sql, Boolean.class, null);
64 } catch (YankSQLException e) {
65 throw new JdbcSQLException(e);
70 public List<ItemsVO> doGetItemTables(ItemsVO vo) throws JdbcSQLException {
71 String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemTables, new String[] { "#itemsManageTable#" },
72 new String[] { vo.getItemsManageTable() });
73 this.logger.debug("JDBC::doGetItemTables sql={}", sql);
75 return Yank.queryBeanList(sql, ItemsVO.class, null);
76 } catch (YankSQLException e) {
77 throw new JdbcSQLException(e);