]> git.basschouten.com Git - openhab-addons.git/blob
b15e5b724c5f23b4e6c90472669f983b4e852dad
[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.utils;
14
15 import java.sql.DatabaseMetaData;
16 import java.sql.SQLException;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.knowm.yank.Yank;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.zaxxer.hikari.HikariDataSource;
25
26 /**
27  * Meta data class
28  *
29  * @author Helmut Lehmeyer - Initial contribution
30  */
31 @NonNullByDefault
32 public class DbMetaData {
33
34     private final Logger logger = LoggerFactory.getLogger(DbMetaData.class);
35
36     private int dbMajorVersion;
37     private int dbMinorVersion;
38     private int driverMajorVersion;
39     private int driverMinorVersion;
40     private @Nullable String dbProductName;
41     private @Nullable String dbProductVersion;
42
43     public DbMetaData() {
44         HikariDataSource h = Yank.getDefaultConnectionPool();
45         // HikariDataSource h = Yank.getDataSource();
46
47         DatabaseMetaData meta;
48         try {
49             meta = h.getConnection().getMetaData();
50
51             // Oracle (and some other vendors) do not support
52             // some the following methods; therefore, we need
53             // to use try-catch block.
54             try {
55                 dbMajorVersion = meta.getDatabaseMajorVersion();
56                 logger.debug("dbMajorVersion = '{}'", dbMajorVersion);
57             } catch (Exception e) {
58                 logger.error("Asking for 'dbMajorVersion' is unsupported: '{}'", e.getMessage());
59             }
60
61             try {
62                 dbMinorVersion = meta.getDatabaseMinorVersion();
63                 logger.debug("dbMinorVersion = '{}'", dbMinorVersion);
64             } catch (Exception e) {
65                 logger.error("Asking for 'dbMinorVersion' is unsupported: '{}'", e.getMessage());
66             }
67
68             driverMajorVersion = meta.getDriverMajorVersion();
69             logger.debug("driverMajorVersion = '{}'", driverMajorVersion);
70
71             driverMinorVersion = meta.getDriverMinorVersion();
72             logger.debug("driverMinorVersion = '{}'", driverMinorVersion);
73
74             dbProductName = meta.getDatabaseProductName();
75             logger.debug("dbProductName = '{}'", dbProductName);
76
77             dbProductVersion = meta.getDatabaseProductVersion();
78             logger.debug("dbProductVersion = '{}'", dbProductVersion);
79         } catch (SQLException e1) {
80             logger.error("Asking for 'dbMajorVersion' seems to be unsupported: '{}'", e1.getMessage());
81         }
82     }
83
84     public int getDbMajorVersion() {
85         return dbMajorVersion;
86     }
87
88     public int getDbMinorVersion() {
89         return dbMinorVersion;
90     }
91
92     public boolean isDbVersionGreater(int major, int minor) {
93         if (dbMajorVersion > major) {
94             return true;
95         } else if (dbMajorVersion == major) {
96             if (dbMinorVersion > minor) {
97                 return true;
98             }
99         }
100         return false;
101     }
102
103     public int getDriverMajorVersion() {
104         return driverMajorVersion;
105     }
106
107     public int getDriverMinorVersion() {
108         return driverMinorVersion;
109     }
110
111     public boolean isDriverVersionGreater(int major, int minor) {
112         if (major > driverMajorVersion) {
113             return true;
114         } else if (major == driverMajorVersion) {
115             if (minor > driverMinorVersion) {
116                 return true;
117             }
118         }
119         return false;
120     }
121
122     public @Nullable String getDbProductName() {
123         return dbProductName;
124     }
125
126     public @Nullable String getDbProductVersion() {
127         return dbProductVersion;
128     }
129 }