]> git.basschouten.com Git - openhab-addons.git/blob
e5665e2c019881f2fe5a1e6304edfeb251ef29e4
[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.db;
14
15 import org.knowm.yank.Yank;
16 import org.openhab.core.items.Item;
17 import org.openhab.core.types.State;
18 import org.openhab.persistence.jdbc.dto.ItemVO;
19 import org.openhab.persistence.jdbc.dto.ItemsVO;
20 import org.openhab.persistence.jdbc.utils.StringUtilsExt;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Extended Database Configuration class. Class represents
26  * the extended database-specific configuration. Overrides and supplements the
27  * default settings from JdbcBaseDAO. Enter only the differences to JdbcBaseDAO here.
28  *
29  * @author Helmut Lehmeyer - Initial contribution
30  */
31 public class JdbcSqliteDAO extends JdbcBaseDAO {
32     private final Logger logger = LoggerFactory.getLogger(JdbcSqliteDAO.class);
33
34     /********
35      * INIT *
36      ********/
37     public JdbcSqliteDAO() {
38         super();
39         initSqlQueries();
40         initSqlTypes();
41         initDbProps();
42     }
43
44     private void initSqlQueries() {
45         logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
46         sqlGetDB = "PRAGMA DATABASE_LIST"; // "SELECT SQLITE_VERSION()"; // "PRAGMA DATABASE_LIST"->db Path/Name
47                                            // "PRAGMA SCHEMA_VERSION";
48         sqlIfTableExists = "SELECT name FROM sqlite_master WHERE type='table' AND name='#searchTable#'";
49         sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (ItemId INTEGER PRIMARY KEY AUTOINCREMENT, #colname# #coltype# NOT NULL)";
50         sqlInsertItemValue = "INSERT OR IGNORE INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
51     }
52
53     /**
54      * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
55      */
56     private void initSqlTypes() {
57         logger.debug("JDBC::initSqlTypes: Initialize the type array");
58         sqlTypes.put("tablePrimaryValue", "strftime('%Y-%m-%d %H:%M:%f' , 'now' , 'localtime')");
59     }
60
61     /**
62      * INFO: https://github.com/brettwooldridge/HikariCP
63      */
64     private void initDbProps() {
65         // Properties for HikariCP
66         databaseProps.setProperty("driverClassName", "org.sqlite.JDBC");
67         // driverClassName OR BETTER USE dataSourceClassName
68         // databaseProps.setProperty("dataSourceClassName", "org.sqlite.SQLiteDataSource");
69     }
70
71     /**************
72      * ITEMS DAOs *
73      **************/
74
75     @Override
76     public String doGetDB() {
77         return Yank.queryColumn(sqlGetDB, "file", String.class, null).get(0);
78     }
79
80     @Override
81     public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) {
82         String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
83                 new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
84                 new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
85         logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
86         Yank.execute(sql, null);
87         return vo;
88     }
89
90     /*************
91      * ITEM DAOs *
92      *************/
93     @Override
94     public void doStoreItemValue(Item item, State itemState, ItemVO vo) {
95         ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
96         String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
97                 new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
98                 new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
99         Object[] params = new Object[] { storedVO.getValue() };
100         logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
101         Yank.execute(sql, params);
102     }
103
104     /****************************
105      * SQL generation Providers *
106      ****************************/
107
108     /*****************
109      * H E L P E R S *
110      *****************/
111
112     /******************************
113      * public Getters and Setters *
114      ******************************/
115 }