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.db;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.knowm.yank.Yank;
18 import org.openhab.core.items.Item;
19 import org.openhab.core.types.State;
20 import org.openhab.persistence.jdbc.dto.ItemVO;
21 import org.openhab.persistence.jdbc.dto.ItemsVO;
22 import org.openhab.persistence.jdbc.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 JdbcSqliteDAO extends JdbcBaseDAO {
35 private final Logger logger = LoggerFactory.getLogger(JdbcSqliteDAO.class);
40 public JdbcSqliteDAO() {
47 private void initSqlQueries() {
48 logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
49 sqlGetDB = "PRAGMA DATABASE_LIST"; // "SELECT SQLITE_VERSION()"; // "PRAGMA DATABASE_LIST"->db Path/Name
50 // "PRAGMA SCHEMA_VERSION";
51 sqlIfTableExists = "SELECT name FROM sqlite_master WHERE type='table' AND name='#searchTable#'";
52 sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (ItemId INTEGER PRIMARY KEY AUTOINCREMENT, #colname# #coltype# NOT NULL)";
53 sqlInsertItemValue = "INSERT OR IGNORE INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
57 * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
59 private void initSqlTypes() {
60 logger.debug("JDBC::initSqlTypes: Initialize the type array");
61 sqlTypes.put("tablePrimaryValue", "strftime('%Y-%m-%d %H:%M:%f' , 'now' , 'localtime')");
65 * INFO: https://github.com/brettwooldridge/HikariCP
67 private void initDbProps() {
68 // Properties for HikariCP
69 databaseProps.setProperty("driverClassName", "org.sqlite.JDBC");
70 // driverClassName OR BETTER USE dataSourceClassName
71 // databaseProps.setProperty("dataSourceClassName", "org.sqlite.SQLiteDataSource");
79 public @Nullable String doGetDB() {
80 return Yank.queryColumn(sqlGetDB, "file", (Class<@Nullable String>) String.class, null).get(0);
84 public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) {
85 String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
86 new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
87 new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
88 logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
89 Yank.execute(sql, null);
97 public void doStoreItemValue(Item item, State itemState, ItemVO vo) {
98 ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
99 String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
100 new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
101 new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
102 Object[] params = new Object[] { storedVO.getValue() };
103 logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
104 Yank.execute(sql, params);
107 /****************************
108 * SQL generation Providers *
109 ****************************/
115 /******************************
116 * public Getters and Setters *
117 ******************************/