]> git.basschouten.com Git - openhab-addons.git/blob
23167dbf621e23cfc3aee956c36a8e9790e86dc1
[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.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
46         DatabaseMetaData meta;
47         try {
48             meta = h.getConnection().getMetaData();
49
50             // Oracle (and some other vendors) do not support
51             // some the following methods; therefore, we need
52             // to use try-catch block.
53             try {
54                 dbMajorVersion = meta.getDatabaseMajorVersion();
55                 logger.debug("dbMajorVersion = '{}'", dbMajorVersion);
56             } catch (Exception e) {
57                 logger.error("Asking for 'dbMajorVersion' is unsupported: '{}'", e.getMessage());
58             }
59
60             try {
61                 dbMinorVersion = meta.getDatabaseMinorVersion();
62                 logger.debug("dbMinorVersion = '{}'", dbMinorVersion);
63             } catch (Exception e) {
64                 logger.error("Asking for 'dbMinorVersion' is unsupported: '{}'", e.getMessage());
65             }
66
67             driverMajorVersion = meta.getDriverMajorVersion();
68             logger.debug("driverMajorVersion = '{}'", driverMajorVersion);
69
70             driverMinorVersion = meta.getDriverMinorVersion();
71             logger.debug("driverMinorVersion = '{}'", driverMinorVersion);
72
73             dbProductName = meta.getDatabaseProductName();
74             logger.debug("dbProductName = '{}'", dbProductName);
75
76             dbProductVersion = meta.getDatabaseProductVersion();
77             logger.debug("dbProductVersion = '{}'", dbProductVersion);
78         } catch (SQLException e1) {
79             logger.error("Asking for 'dbMajorVersion' seems to be unsupported: '{}'", e1.getMessage());
80         }
81     }
82
83     public int getDbMajorVersion() {
84         return dbMajorVersion;
85     }
86
87     public int getDbMinorVersion() {
88         return dbMinorVersion;
89     }
90
91     public boolean isDbVersionGreater(int major, int minor) {
92         if (dbMajorVersion > major) {
93             return true;
94         } else if (dbMajorVersion == major) {
95             if (dbMinorVersion > minor) {
96                 return true;
97             }
98         }
99         return false;
100     }
101
102     public int getDriverMajorVersion() {
103         return driverMajorVersion;
104     }
105
106     public int getDriverMinorVersion() {
107         return driverMinorVersion;
108     }
109
110     public boolean isDriverVersionGreater(int major, int minor) {
111         if (major > driverMajorVersion) {
112             return true;
113         } else if (major == driverMajorVersion) {
114             if (minor > driverMinorVersion) {
115                 return true;
116             }
117         }
118         return false;
119     }
120
121     public @Nullable String getDbProductName() {
122         return dbProductName;
123     }
124
125     public @Nullable String getDbProductVersion() {
126         return dbProductVersion;
127     }
128 }