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.eclipse.jdt.annotation.Nullable;
17 import org.knowm.yank.Yank;
18 import org.knowm.yank.exceptions.YankSQLException;
19 import org.openhab.core.items.Item;
20 import org.openhab.core.types.State;
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;
29 * Extended Database Configuration class. Class represents
30 * the extended database-specific configuration. Overrides and supplements the
31 * default settings from JdbcBaseDAO. Enter only the differences to JdbcBaseDAO here.
33 * @author Helmut Lehmeyer - Initial contribution
36 public class JdbcSqliteDAO extends JdbcBaseDAO {
37 private static final String DRIVER_CLASS_NAME = org.sqlite.JDBC.class.getName();
38 @SuppressWarnings("unused")
39 private static final String DATA_SOURCE_CLASS_NAME = org.sqlite.SQLiteDataSource.class.getName();
41 private final Logger logger = LoggerFactory.getLogger(JdbcSqliteDAO.class);
46 public JdbcSqliteDAO() {
52 private void initSqlQueries() {
53 logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
54 sqlGetDB = "PRAGMA DATABASE_LIST"; // "SELECT SQLITE_VERSION()"; // "PRAGMA DATABASE_LIST"->db Path/Name
55 // "PRAGMA SCHEMA_VERSION";
56 sqlIfTableExists = "SELECT name FROM sqlite_master WHERE type='table' AND name='#searchTable#'";
57 sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (ItemId INTEGER PRIMARY KEY AUTOINCREMENT, #colname# #coltype# NOT NULL)";
58 sqlInsertItemValue = "INSERT OR IGNORE INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
62 * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
64 private void initSqlTypes() {
65 logger.debug("JDBC::initSqlTypes: Initialize the type array");
66 sqlTypes.put("tablePrimaryValue", "strftime('%Y-%m-%d %H:%M:%f' , 'now' , 'localtime')");
70 * INFO: https://github.com/brettwooldridge/HikariCP
72 private void initDbProps() {
73 // Properties for HikariCP
74 databaseProps.setProperty("driverClassName", DRIVER_CLASS_NAME);
75 // driverClassName OR BETTER USE dataSourceClassName
76 // databaseProps.setProperty("dataSourceClassName", DATA_SOURCE_CLASS_NAME);
84 public @Nullable String doGetDB() throws JdbcSQLException {
86 return Yank.queryColumn(sqlGetDB, "file", String.class, null).get(0);
87 } catch (YankSQLException e) {
88 throw new JdbcSQLException(e);
93 public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
94 String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
95 new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
96 new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
97 logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
99 Yank.execute(sql, null);
100 } catch (YankSQLException e) {
101 throw new JdbcSQLException(e);
110 public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
111 ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
112 String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
113 new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
114 new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
115 Object[] params = { storedVO.getValue() };
116 logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
118 Yank.execute(sql, params);
119 } catch (YankSQLException e) {
120 throw new JdbcSQLException(e);
124 /****************************
125 * SQL generation Providers *
126 ****************************/
132 /******************************
133 * public Getters and Setters *
134 ******************************/