]> git.basschouten.com Git - openhab-addons.git/blob
cc8553bcac12ddd6c36a79bfdcba673fa5b1aa3e
[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  */
29 public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
30     private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
31
32     protected String sqlCreateHypertable;
33
34     public JdbcTimescaledbDAO() {
35         super();
36
37         initSqlQueries();
38     }
39
40     public Properties getDatabaseProperties() {
41         Properties properties = new Properties(this.databaseProps);
42
43         // Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
44         if (properties.containsKey("jdbcUrl")) {
45             properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql"));
46         }
47
48         return properties;
49     }
50
51     public void doCreateItemTable(ItemVO vo) {
52         String sql;
53
54         sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateItemTable,
55                 new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
56                 new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
57         this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
58         Yank.execute(sql, null);
59
60         sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
61                 new String[] { vo.getTableName() });
62         this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
63         Yank.execute(sql, null);
64     }
65
66     private void initSqlQueries() {
67         this.logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
68
69         this.sqlCreateHypertable = "SELECT create_hypertable('#tableName#', 'time')";
70     }
71 }