Skip to main content
summaryrefslogtreecommitdiffstats
blob: d4e8381dc187ba7b6b422b836ca773be0795bac0 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
 * 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.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.eclipse.osee.framework.core.data.IDatabaseInfo;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.internal.Activator;

/**
 * Handles connection recovery in the event of database connection being lost
 *
 * @author Jeff C. Phillips
 */
public final class ConnectionHandler {

   private static IOseeDatabaseService getDatabase() throws OseeDataStoreException {
      return Activator.getInstance().getOseeDatabaseService();
   }

   public static IOseeSequence getSequence() throws OseeDataStoreException {
      return getDatabase().getSequence();
   }

   /**
    * Avoid using if possible
    */
   public static OseeConnection getConnection() throws OseeDataStoreException {
      return getDatabase().getConnection();
   }

   /**
    * Avoid using if possible
    */
   public static OseeConnection getConnection(IDatabaseInfo info) throws OseeDataStoreException {
      return getDatabase().getConnection(info);
   }

   public static IOseeStatement getStatement() throws OseeDataStoreException {
      return getDatabase().getStatement();
   }

   public static IOseeStatement getStatement(OseeConnection connection) throws OseeDataStoreException {
      return getDatabase().getStatement(connection);
   }

   public static IOseeStatement getStatement(OseeConnection connection, boolean autoClose) throws OseeDataStoreException {
      return getDatabase().getStatement(connection, autoClose);
   }

   /**
    * This method should only be used when not contained in a DB transaction
    *
    * @param query
    * @param data
    * @return number of records updated
    * @throws OseeDataStoreException
    */
   public static <O extends Object> int runPreparedUpdate(String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedUpdate(query, data);
   }

   /**
    * This method should only be used when not contained in a DB transaction
    *
    * @param query
    * @param dataList
    * @return number of records updated
    * @throws OseeDataStoreException
    */
   public static <O extends Object> int runBatchUpdate(String query, List<O[]> dataList) throws OseeDataStoreException {
      return getDatabase().runBatchUpdate(query, dataList);
   }

   /**
    * This method should only be used when contained in a DB transaction
    *
    * @param connection
    * @param query
    * @param data
    * @return number of records updated
    * @throws OseeDataStoreException
    */
   public static <O extends Object> int runPreparedUpdate(OseeConnection connection, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedUpdate(connection, query, data);
   }

   public static <O extends Object> int runBatchUpdate(OseeConnection connection, String query, List<O[]> dataList) throws OseeDataStoreException {
      return getDatabase().runBatchUpdate(connection, query, dataList);
   }

   public static <O extends Object> int runPreparedQueryFetchInt(int defaultValue, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedQueryFetchObject(defaultValue, query, data);
   }

   public static <O extends Object> int runPreparedQueryFetchInt(OseeConnection connection, int defaultValue, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedQueryFetchObject(connection, defaultValue, query, data);
   }

   public static <O extends Object> int runCallableStatementFetchInt(String query, O... data) throws OseeDataStoreException {
      return runCallableStatementFetchInt(getStatement(), query, data);
   }

   public static <O extends Object> int runCallableStatementFetchInt(OseeConnection connection, String query, O... data) throws OseeDataStoreException {
      return runCallableStatementFetchInt(getStatement(connection), query, data);
   }

   public static <O extends Object> int runCallableStatementFetchInt(IOseeStatement chStmt, String query, O... data) throws OseeDataStoreException {
      try {
         chStmt.runCallableStatement(query, data);
         return chStmt.getCallableInt(1);
      } finally {
         chStmt.close();
      }
   }

   public static <O extends Object> double runCallableStatementFetchDouble(String query, O... data) throws OseeDataStoreException {
      return runCallableStatementFetchDouble(getStatement(), query, data);
   }

   public static <O extends Object> double runCallableStatementFetchDouble(OseeConnection connection, String query, O... data) throws OseeDataStoreException {
      return runCallableStatementFetchDouble(getStatement(connection), query, data);
   }

   private static <O extends Object> double runCallableStatementFetchDouble(IOseeStatement chStmt, String query, O... data) throws OseeDataStoreException {
      try {
         chStmt.runCallableStatement(query, data);
         return chStmt.getCallableDouble(1);
      } finally {
         chStmt.close();
      }
   }

   public static <O extends Object> long runPreparedQueryFetchLong(long defaultValue, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedQueryFetchObject(defaultValue, query, data);
   }

   public static <O extends Object> long runPreparedQueryFetchLong(OseeConnection connection, long defaultValue, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedQueryFetchObject(connection, defaultValue, query, data);
   }

   public static <O extends Object> String runPreparedQueryFetchString(String defaultValue, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedQueryFetchObject(defaultValue, query, data);
   }

   public static <O extends Object> String runPreparedQueryFetchString(OseeConnection connection, String defaultValue, String query, O... data) throws OseeDataStoreException {
      return getDatabase().runPreparedQueryFetchObject(connection, defaultValue, query, data);
   }

   /**
    * Cause constraint checking to be deferred until the end of the current transaction.
    *
    * @param connection
    * @throws OseeDataStoreException
    */
   public static void deferConstraintChecking(OseeConnection connection) throws OseeDataStoreException {
      if (SupportedDatabase.getDatabaseType(connection.getMetaData()) == SupportedDatabase.derby) {
         return;
      }
      // NOTE: this must be a PreparedStatement to play correctly with DB Transactions.
      runPreparedUpdate(connection, "SET CONSTRAINTS ALL DEFERRED");
   }

   public static DatabaseMetaData getMetaData() throws OseeDataStoreException {
      OseeConnection connection = getDatabase().getConnection();
      try {
         return connection.getMetaData();
      } finally {
         connection.close();
      }
   }

   public static boolean doesTableExist(String targetTable) {
      return doesTableExist(null, targetTable);
   }

   public static boolean doesTableExist(String targetSchema, String targetTable) {
      ResultSet resultSet = null;
      try {
         resultSet = getMetaData().getTables(null, null, null, new String[] {"TABLE"});
         if (resultSet != null) {
            while (resultSet.next()) {
               String tableName = resultSet.getString("TABLE_NAME");
               String schemaName = resultSet.getString("TABLE_SCHEM");
               if (targetTable.equalsIgnoreCase(tableName)) {
                  if (targetSchema == null || targetSchema.equalsIgnoreCase(schemaName)) {
                     return true;
                  }
               }
            }
         }
      } catch (Exception ex) {
         // Do nothing
      } finally {
         if (resultSet != null) {
            try {
               resultSet.close();
            } catch (SQLException ex) {
               // Do nothing
            }
         }
      }
      return false;
   }
}

Back to the top