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