2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.jdbc.internal.db;
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;
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.
31 * @author Helmut Lehmeyer - Initial contribution
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();
39 private final Logger logger = LoggerFactory.getLogger(JdbcH2DAO.class);
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#) )";
59 * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
61 private void initSqlTypes() {
65 * INFO: https://github.com/brettwooldridge/HikariCP
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);
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());
90 Yank.execute(sql, params);
91 } catch (YankSQLException e) {
92 throw new JdbcSQLException(e);
96 /****************************
97 * SQL generation Providers *
98 ****************************/
104 /******************************
105 * public Getters and Setters *
106 ******************************/