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