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