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 java.time.ZonedDateTime;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.knowm.yank.Yank;
20 import org.knowm.yank.exceptions.YankSQLException;
21 import org.openhab.core.items.Item;
22 import org.openhab.core.types.State;
23 import org.openhab.persistence.jdbc.internal.dto.ItemVO;
24 import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
25 import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
26 import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Extended Database Configuration class. Class represents
32 * the extended database-specific configuration. Overrides and supplements the
33 * default settings from JdbcBaseDAO. Enter only the differences to JdbcBaseDAO here.
35 * @author Helmut Lehmeyer - Initial contribution
38 public class JdbcSqliteDAO extends JdbcBaseDAO {
39 private static final String DRIVER_CLASS_NAME = org.sqlite.JDBC.class.getName();
40 @SuppressWarnings("unused")
41 private static final String DATA_SOURCE_CLASS_NAME = org.sqlite.SQLiteDataSource.class.getName();
43 private final Logger logger = LoggerFactory.getLogger(JdbcSqliteDAO.class);
48 public JdbcSqliteDAO() {
54 private void initSqlQueries() {
55 logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
56 sqlGetDB = "PRAGMA DATABASE_LIST"; // "SELECT SQLITE_VERSION()"; // "PRAGMA DATABASE_LIST"->db Path/Name
57 // "PRAGMA SCHEMA_VERSION";
58 sqlIfTableExists = "SELECT name FROM sqlite_master WHERE type='table' AND name='#searchTable#'";
59 sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (ItemId INTEGER PRIMARY KEY AUTOINCREMENT, #colname# #coltype# NOT NULL)";
60 sqlInsertItemValue = "INSERT OR IGNORE INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
64 * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
66 private void initSqlTypes() {
67 logger.debug("JDBC::initSqlTypes: Initialize the type array");
68 sqlTypes.put("tablePrimaryValue", "strftime('%Y-%m-%d %H:%M:%f' , 'now' , 'localtime')");
72 * INFO: https://github.com/brettwooldridge/HikariCP
74 private void initDbProps() {
75 // Properties for HikariCP
76 databaseProps.setProperty("driverClassName", DRIVER_CLASS_NAME);
77 // driverClassName OR BETTER USE dataSourceClassName
78 // databaseProps.setProperty("dataSourceClassName", DATA_SOURCE_CLASS_NAME);
86 public @Nullable String doGetDB() throws JdbcSQLException {
88 return Yank.queryColumn(sqlGetDB, "file", String.class, null).get(0);
89 } catch (YankSQLException e) {
90 throw new JdbcSQLException(e);
95 public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
96 String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
97 new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
98 new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
99 logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
101 Yank.execute(sql, null);
102 } catch (YankSQLException e) {
103 throw new JdbcSQLException(e);
112 public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
113 ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
114 String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
115 new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
116 new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
117 Object[] params = { storedVO.getValue() };
118 logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
120 Yank.execute(sql, params);
121 } catch (YankSQLException e) {
122 throw new JdbcSQLException(e);
127 public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTime date) throws JdbcSQLException {
128 ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
129 String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
130 new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
131 new String[] { storedVO.getTableName(), storedVO.getDbType(), "?" });
132 java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
133 Object[] params = { timestamp, storedVO.getValue() };
134 logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
136 Yank.execute(sql, params);
137 } catch (YankSQLException e) {
138 throw new JdbcSQLException(e);
142 /****************************
143 * SQL generation Providers *
144 ****************************/
150 /******************************
151 * public Getters and Setters *
152 ******************************/