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