2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.jdbc.internal.utils;
15 import java.sql.DatabaseMetaData;
16 import java.sql.SQLException;
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;
24 import com.zaxxer.hikari.HikariDataSource;
29 * @author Helmut Lehmeyer - Initial contribution
32 public class DbMetaData {
34 private final Logger logger = LoggerFactory.getLogger(DbMetaData.class);
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;
44 HikariDataSource h = Yank.getDefaultConnectionPool();
46 DatabaseMetaData meta;
48 meta = h.getConnection().getMetaData();
50 // Oracle (and some other vendors) do not support
51 // some the following methods; therefore, we need
52 // to use try-catch block.
54 dbMajorVersion = meta.getDatabaseMajorVersion();
55 logger.debug("dbMajorVersion = '{}'", dbMajorVersion);
56 } catch (Exception e) {
57 logger.error("Asking for 'dbMajorVersion' is unsupported: '{}'", e.getMessage());
61 dbMinorVersion = meta.getDatabaseMinorVersion();
62 logger.debug("dbMinorVersion = '{}'", dbMinorVersion);
63 } catch (Exception e) {
64 logger.error("Asking for 'dbMinorVersion' is unsupported: '{}'", e.getMessage());
67 driverMajorVersion = meta.getDriverMajorVersion();
68 logger.debug("driverMajorVersion = '{}'", driverMajorVersion);
70 driverMinorVersion = meta.getDriverMinorVersion();
71 logger.debug("driverMinorVersion = '{}'", driverMinorVersion);
73 dbProductName = meta.getDatabaseProductName();
74 logger.debug("dbProductName = '{}'", dbProductName);
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());
83 public int getDbMajorVersion() {
84 return dbMajorVersion;
87 public int getDbMinorVersion() {
88 return dbMinorVersion;
91 public boolean isDbVersionGreater(int major, int minor) {
92 if (dbMajorVersion > major) {
94 } else if (dbMajorVersion == major) {
95 if (dbMinorVersion > minor) {
102 public int getDriverMajorVersion() {
103 return driverMajorVersion;
106 public int getDriverMinorVersion() {
107 return driverMinorVersion;
110 public boolean isDriverVersionGreater(int major, int minor) {
111 if (major > driverMajorVersion) {
113 } else if (major == driverMajorVersion) {
114 if (minor > driverMinorVersion) {
121 public @Nullable String getDbProductName() {
122 return dbProductName;
125 public @Nullable String getDbProductVersion() {
126 return dbProductVersion;