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