]> git.basschouten.com Git - openhab-addons.git/blob
db2b3cabeb670d471f77367c630ed461a212bcd2
[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.persistence.jdbc.utils.DbMetaData;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Extended Database Configuration class. Class represents
22  * the extended database-specific configuration. Overrides and supplements the
23  * default settings from JdbcBaseDAO. Enter only the differences to JdbcBaseDAO here.
24  *
25  * since driver version >= 6.0 sometimes timezone conversation is needed: ?serverTimezone=UTC
26  * example: dbProps.setProperty("jdbcUrl", "jdbc:mysql://192.168.0.181:3306/ItemTypeTest3?serverTimezone=UTC");//mysql
27  * 5.7
28  *
29  * @author Helmut Lehmeyer - Initial contribution
30  */
31 public class JdbcMysqlDAO extends JdbcBaseDAO {
32     private final Logger logger = LoggerFactory.getLogger(JdbcMysqlDAO.class);
33
34     /********
35      * INIT *
36      ********/
37     public JdbcMysqlDAO() {
38         super();
39         initSqlTypes();
40         initDbProps();
41         initSqlQueries();
42     }
43
44     private void initSqlQueries() {
45         logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
46     }
47
48     /**
49      * INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
50      */
51     private void initSqlTypes() {
52         logger.debug("JDBC::initSqlTypes: Initialize the type array");
53         sqlTypes.put("STRINGITEM", "VARCHAR(21717)");// mysql using utf-8 max 65535/3 = 21845, using 21845-128 = 21717
54     }
55
56     /**
57      * INFO: https://github.com/brettwooldridge/HikariCP
58      */
59     private void initDbProps() {
60         // Performancetuning
61         databaseProps.setProperty("dataSource.cachePrepStmts", "true");
62         databaseProps.setProperty("dataSource.prepStmtCacheSize", "250");
63         databaseProps.setProperty("dataSource.prepStmtCacheSqlLimit", "2048");
64         databaseProps.setProperty("dataSource.jdbcCompliantTruncation", "false");// jdbc standard max varchar max length
65                                                                                  // of 21845
66
67         // Properties for HikariCP
68         // Use driverClassName
69         databaseProps.setProperty("driverClassName", "com.mysql.jdbc.Driver");
70         // OR dataSourceClassName
71         // databaseProps.setProperty("dataSourceClassName", "com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
72         databaseProps.setProperty("maximumPoolSize", "3");
73         databaseProps.setProperty("minimumIdle", "2");
74     }
75
76     @Override
77     public void initAfterFirstDbConnection() {
78         logger.debug("JDBC::initAfterFirstDbConnection: Initializing step, after db is connected.");
79         dbMeta = new DbMetaData();
80         // Initialize sqlTypes, depending on DB version for example
81         if (dbMeta.isDbVersionGreater(5, 5)) {
82             sqlTypes.put("DATETIMEITEM", "TIMESTAMP(3)");
83             sqlTypes.put("tablePrimaryKey", "TIMESTAMP(3)");
84             sqlTypes.put("tablePrimaryValue", "NOW(3)");
85         }
86     }
87
88     /**************
89      * ITEMS DAOs *
90      **************/
91     @Override
92     public Integer doPingDB() {
93         return Yank.queryScalar(sqlPingDB, Long.class, null).intValue();
94     }
95
96     /*************
97      * ITEM DAOs *
98      *************/
99
100     /****************************
101      * SQL generation Providers *
102      ****************************/
103
104     /*****************
105      * H E L P E R S *
106      *****************/
107
108     /******************************
109      * public Getters and Setters *
110      ******************************/
111 }