]> git.basschouten.com Git - openhab-addons.git/blob
c27503274e16101f4bd4190c1581b82ad597758d
[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.eclipse.jdt.annotation.NonNullByDefault;
18 import org.knowm.yank.Yank;
19 import org.openhab.persistence.jdbc.dto.ItemVO;
20 import org.openhab.persistence.jdbc.utils.StringUtilsExt;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Extended Database Configuration class. Class represents the extended database-specific configuration. Overrides and
26  * supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
27  *
28  * @author Riccardo Nimser-Joseph - Initial contribution
29  * @author Dan Cunningham - Fixes and refactoring
30  */
31 @NonNullByDefault
32 public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
33     private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
34
35     private final String sqlCreateHypertable = "SELECT created from create_hypertable('#tableName#', 'time')";
36
37     @Override
38     public Properties getConnectionProperties() {
39         Properties properties = (Properties) this.databaseProps.clone();
40         // Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
41         if (properties.containsKey("jdbcUrl")) {
42             properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql"));
43         }
44         return properties;
45     }
46
47     @Override
48     public void doCreateItemTable(ItemVO vo) {
49         super.doCreateItemTable(vo);
50         String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
51                 new String[] { vo.getTableName() });
52         this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
53         Yank.queryScalar(sql, Boolean.class, null);
54     }
55 }