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