]> git.basschouten.com Git - openhab-addons.git/blob
6c6bbba7ee1aa188b39f767b50e2cc2a8428549b
[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.List;
16 import java.util.Properties;
17
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;
27
28 /**
29  * Extended Database Configuration class. Class represents the extended database-specific configuration. Overrides and
30  * supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
31  *
32  * @author Riccardo Nimser-Joseph - Initial contribution
33  * @author Dan Cunningham - Fixes and refactoring
34  */
35 @NonNullByDefault
36 public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
37     private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
38
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#\"'";
41
42     @Override
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"));
48         }
49         return properties;
50     }
51
52     /*************
53      * ITEM DAOs *
54      *************/
55
56     @Override
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);
62         try {
63             Yank.queryScalar(sql, Boolean.class, null);
64         } catch (YankSQLException e) {
65             throw new JdbcSQLException(e);
66         }
67     }
68
69     @Override
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);
74         try {
75             return Yank.queryBeanList(sql, ItemsVO.class, null);
76         } catch (YankSQLException e) {
77             throw new JdbcSQLException(e);
78         }
79     }
80 }