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