]> git.basschouten.com Git - openhab-addons.git/blob
2bbdcf7ebfcce8e3491e69f43c84365c2762299b
[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.internal.db;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.knowm.yank.Yank;
17 import org.knowm.yank.exceptions.YankSQLException;
18 import org.openhab.core.items.Item;
19 import org.openhab.core.types.State;
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
28  * the extended database-specific configuration. Overrides and supplements the
29  * default settings from JdbcBaseDAO. Enter only the differences to JdbcBaseDAO here.
30  *
31  * @author Helmut Lehmeyer - Initial contribution
32  */
33 @NonNullByDefault
34 public class JdbcH2DAO extends JdbcBaseDAO {
35     private static final String DRIVER_CLASS_NAME = org.h2.Driver.class.getName();
36     @SuppressWarnings("unused")
37     private static final String DATA_SOURCE_CLASS_NAME = org.h2.jdbcx.JdbcDataSource.class.getName();
38
39     private final Logger logger = LoggerFactory.getLogger(JdbcH2DAO.class);
40
41     /********
42      * INIT *
43      ********/
44     public JdbcH2DAO() {
45         initSqlQueries();
46         initSqlTypes();
47         initDbProps();
48     }
49
50     private void initSqlQueries() {
51         logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
52         sqlIfTableExists = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='#searchTable#'";
53         // SQL_INSERT_ITEM_VALUE = "INSERT INTO #tableName# (TIME, VALUE) VALUES( NOW(), CAST( ? as #dbType#) )";
54         // http://stackoverflow.com/questions/19768051/h2-sql-database-insert-if-the-record-does-not-exist
55         sqlInsertItemValue = "MERGE INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
56     }
57
58     /**
59      * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
60      */
61     private void initSqlTypes() {
62     }
63
64     /**
65      * INFO: https://github.com/brettwooldridge/HikariCP
66      */
67     private void initDbProps() {
68         // Properties for HikariCP
69         databaseProps.setProperty("driverClassName", DRIVER_CLASS_NAME);
70         // driverClassName OR BETTER USE dataSourceClassName
71         // databaseProps.setProperty("dataSourceClassName", DATA_SOURCE_CLASS_NAME);
72     }
73
74     /**************
75      * ITEMS DAOs *
76      **************/
77
78     /*************
79      * ITEM DAOs *
80      *************/
81     @Override
82     public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
83         ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
84         String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
85                 new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
86                 new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
87         Object[] params = { storedVO.getValue() };
88         logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
89         try {
90             Yank.execute(sql, params);
91         } catch (YankSQLException e) {
92             throw new JdbcSQLException(e);
93         }
94     }
95
96     /****************************
97      * SQL generation Providers *
98      ****************************/
99
100     /*****************
101      * H E L P E R S *
102      *****************/
103
104     /******************************
105      * public Getters and Setters *
106      ******************************/
107 }