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