]> git.basschouten.com Git - openhab-addons.git/blob
1a2c32a2d2398f9aa8184da78bc2ccaaa89727da
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.jdbc.db;
14
15 import java.util.Properties;
16
17 import org.knowm.yank.Yank;
18 import org.openhab.persistence.jdbc.dto.ItemVO;
19 import org.openhab.persistence.jdbc.utils.StringUtilsExt;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Extended Database Configuration class. Class represents the extended database-specific configuration. Overrides and
25  * supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
26  *
27  * @author Riccardo Nimser-Joseph - Initial contribution
28  * @author Dan Cunningham - Fixes and refactoring
29  */
30 public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
31     private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
32
33     private final String sqlCreateHypertable = "SELECT created from create_hypertable('#tableName#', 'time')";
34
35     @Override
36     public Properties getConnectionProperties() {
37         Properties properties = (Properties) this.databaseProps.clone();
38         // Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
39         if (properties.containsKey("jdbcUrl")) {
40             properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql"));
41         }
42         return properties;
43     }
44
45     @Override
46     public void doCreateItemTable(ItemVO vo) {
47         super.doCreateItemTable(vo);
48         String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
49                 new String[] { vo.getTableName() });
50         this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
51         Yank.queryScalar(sql, Boolean.class, null);
52     }
53 }