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