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.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();
45 // HikariDataSource h = Yank.getDataSource();
47 DatabaseMetaData meta;
49 meta = h.getConnection().getMetaData();
51 // Oracle (and some other vendors) do not support
52 // some the following methods; therefore, we need
53 // to use try-catch block.
55 dbMajorVersion = meta.getDatabaseMajorVersion();
56 logger.debug("dbMajorVersion = '{}'", dbMajorVersion);
57 } catch (Exception e) {
58 logger.error("Asking for 'dbMajorVersion' is unsupported: '{}'", e.getMessage());
62 dbMinorVersion = meta.getDatabaseMinorVersion();
63 logger.debug("dbMinorVersion = '{}'", dbMinorVersion);
64 } catch (Exception e) {
65 logger.error("Asking for 'dbMinorVersion' is unsupported: '{}'", e.getMessage());
68 driverMajorVersion = meta.getDriverMajorVersion();
69 logger.debug("driverMajorVersion = '{}'", driverMajorVersion);
71 driverMinorVersion = meta.getDriverMinorVersion();
72 logger.debug("driverMinorVersion = '{}'", driverMinorVersion);
74 dbProductName = meta.getDatabaseProductName();
75 logger.debug("dbProductName = '{}'", dbProductName);
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());
84 public int getDbMajorVersion() {
85 return dbMajorVersion;
88 public int getDbMinorVersion() {
89 return dbMinorVersion;
92 public boolean isDbVersionGreater(int major, int minor) {
93 if (dbMajorVersion > major) {
95 } else if (dbMajorVersion == major) {
96 if (dbMinorVersion > minor) {
103 public int getDriverMajorVersion() {
104 return driverMajorVersion;
107 public int getDriverMinorVersion() {
108 return driverMinorVersion;
111 public boolean isDriverVersionGreater(int major, int minor) {
112 if (major > driverMajorVersion) {
114 } else if (major == driverMajorVersion) {
115 if (minor > driverMinorVersion) {
122 public @Nullable String getDbProductName() {
123 return dbProductName;
126 public @Nullable String getDbProductVersion() {
127 return dbProductVersion;