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