Skip to main content
summaryrefslogtreecommitdiffstats
blob: 97b12dfa09c98ad4616a3b64c65549dd67a9516f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.database.core;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;

public enum SupportedDatabase {
   h2,
   oracle,
   foxpro,
   mysql,
   postgresql,
   hsql;

   public static String getDatabaseName(DatabaseMetaData metaData) throws OseeCoreException {
      String name = "";
      try {
         name = metaData.getDatabaseProductName();
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return name;
   }

   public static SupportedDatabase getDatabaseTypeAllowNull(DatabaseMetaData metaData) throws OseeCoreException {
      String dbName = getDatabaseName(metaData);
      return getDatabaseTypeAllowNull(dbName);
   }

   private static SupportedDatabase getDatabaseTypeAllowNull(String dbName) {
      SupportedDatabase toReturn = null;
      String lowerCaseName = dbName.toLowerCase();
      if (lowerCaseName.contains(h2.toString())) {
         toReturn = h2;
      } else if (lowerCaseName.contains(oracle.toString())) {
         toReturn = oracle;
      } else if (lowerCaseName.contains(foxpro.toString())) {
         toReturn = foxpro;
      } else if (lowerCaseName.contains(mysql.toString())) {
         toReturn = mysql;
      } else if (lowerCaseName.contains(postgresql.toString())) {
         toReturn = postgresql;
      } else if (lowerCaseName.contains(hsql.toString())) {
         toReturn = hsql;
      }
      return toReturn;
   }

   public static SupportedDatabase getDatabaseType(DatabaseMetaData metaData) throws OseeCoreException {
      String dbName = getDatabaseName(metaData);
      SupportedDatabase toReturn = getDatabaseTypeAllowNull(dbName);
      if (toReturn == null) {
         throw new OseeDataStoreException("Unsupported database type [%s] ", dbName);
      }
      return toReturn;
   }

   public static boolean isDatabaseType(DatabaseMetaData metaData, SupportedDatabase... dbTypes) throws OseeCoreException {
      boolean result = false;
      SupportedDatabase supportedType = getDatabaseTypeAllowNull(metaData);
      for (SupportedDatabase dbType : dbTypes) {
         if (dbType == supportedType) {
            result = true;
            break;
         }
      }
      return result;
   }

   public static boolean areHintsSupported(DatabaseMetaData metaData) throws OseeCoreException {
      try {
         if (SupportedDatabase.isDatabaseType(metaData, oracle)) {
            return metaData.getDatabaseMajorVersion() > 10;
         }
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return false;
   }

   public static String getComplementSql(DatabaseMetaData metaData) throws OseeCoreException {
      return isDatabaseType(metaData, oracle) ? "MINUS" : "EXCEPT";
   }

   public static String getValidationSql(DatabaseMetaData metaData) throws OseeCoreException {
      String validation;
      if (isDatabaseType(metaData, oracle, h2)) {
         validation = "select 1 from dual";
      } else {
         validation = "select 1";
      }
      return validation;

   }
}

Back to the top