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