Skip to main content
summaryrefslogtreecommitdiffstats
blob: 25b2bce1e77b2678ef67c7c4e20a5c7adee9993f (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*******************************************************************************
 * 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.internal.core;

import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.SQL3DataType;
import org.eclipse.osee.framework.database.core.SupportedDatabase;
import org.eclipse.osee.framework.database.internal.Activator;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Jeff C. Phillips
 * @author Ryan D. Brooks
 */
public final class OseeStatementImpl implements IOseeStatement {
   private ResultSet rSet;
   private PreparedStatement preparedStatement;
   private CallableStatement callableStatement;
   private OseeConnectionImpl connection;
   private final boolean autoClose;
   private final OseeConnectionPoolImpl connectionPool;
   private final int resultSetType;
   private final int resultSetConcurrency;

   public OseeStatementImpl(OseeConnectionPoolImpl connectionPool, OseeConnectionImpl connection) {
      this(connectionPool, connection, connection == null);
   }

   public OseeStatementImpl(OseeConnectionPoolImpl connectionPool, OseeConnectionImpl connection, boolean autoClose) {
      this(connectionPool, connection, autoClose, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
   }

   public OseeStatementImpl(OseeConnectionPoolImpl connectionPool) {
      this(connectionPool, null);
   }

   public OseeStatementImpl(OseeConnectionPoolImpl connectionPool, OseeConnectionImpl connection, boolean autoClose, int resultSetType, int resultSetConcurrency) {
      this.autoClose = autoClose;
      this.connection = connection;
      this.connectionPool = connectionPool;
      this.resultSetType = resultSetType;
      this.resultSetConcurrency = resultSetConcurrency;
   }

   @Override
   public void runPreparedQuery(String query, Object... data) throws OseeCoreException {
      runPreparedQuery(0, query, data);
   }

   /**
    * @param fetchSize hint as to the number of rows that should be fetched from the database at a time. will be limited
    * to 10,000
    */
   @Override
   public void runPreparedQuery(int fetchSize, String query, Object... data) throws OseeCoreException {

      try {
         allowReuse();
         preparedStatement = connection.prepareStatement(query, resultSetType, resultSetConcurrency);
         preparedStatement.setFetchSize(Math.min(fetchSize, 10000));
         StatementUtil.populateValuesForPreparedStatement(preparedStatement, data);

         rSet = preparedStatement.executeQuery();
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   /**
    * Invokes a stored procedure parameters of type SQL3DataType are registered as Out parameters and all others are set
    * as in parameters
    */
   @Override
   public void runCallableStatement(String query, Object... data) throws OseeCoreException {

      try {
         allowReuse();
         callableStatement = connection.prepareCall(query, resultSetType, resultSetConcurrency);

         for (int index = 0; index < data.length; index++) {
            if (data[index] instanceof SQL3DataType) {
               callableStatement.registerOutParameter(index + 1, ((SQL3DataType) data[index]).getSQLTypeNumber());
            }
         }
         StatementUtil.populateValuesForPreparedStatement(callableStatement, data);
         if (callableStatement.execute()) {
            rSet = callableStatement.getResultSet();
         }
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   @Override
   public boolean next() throws OseeCoreException {
      if (rSet != null) {
         try {
            return rSet.next();
         } catch (SQLException ex) {
            OseeExceptions.wrapAndThrow(ex);
         }
      }
      return false;
   }

   /**
    * The application must call close when it is done using this object; however, it is safe to use this same object
    * multiple times, for example calling runPreparedQuery() repeatedly, without any intermediate calls to close
    */
   @Override
   public void close() {
      try {
         closePreviousResources();
         if (autoClose && connection != null) {
            connection.close();
            connection = null;// this allows for multiple calls to runPreparedQuery to have an open connection
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

   /**
    * allows for multiple uses of this object to have an open connection
    */
   private void allowReuse() throws OseeCoreException {
      if (connection == null) {
         connection = connectionPool.getConnection();
      }
      closePreviousResources();
   }

   private void closePreviousResources() throws OseeCoreException {
      try {
         if (rSet != null) {
            rSet.close();
         }
         if (preparedStatement != null) {
            preparedStatement.close();
         }
         if (callableStatement != null) {
            callableStatement.close();
         }
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   @Override
   public InputStream getBinaryStream(String columnName) throws OseeCoreException {
      try {
         return rSet.getBinaryStream(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public InputStream getAsciiStream(String columnName) throws OseeCoreException {
      try {
         return rSet.getAsciiStream(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public String getString(String columnName) throws OseeCoreException {
      try {
         return rSet.getString(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public float getFloat(String columnName) throws OseeCoreException {
      try {
         return rSet.getFloat(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0.0f; // unreachable since wrapAndThrow() always throws an exceptions
      }
   }

   @Override
   public long getLong(String columnName) throws OseeCoreException {
      try {
         return rSet.getLong(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public int getInt(String columnName) throws OseeCoreException {
      try {
         return rSet.getInt(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public int getInt(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getInt(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public int getCallableInt(int columnIndex) throws OseeCoreException {
      try {
         return callableStatement.getInt(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public double getCallableDouble(int columnIndex) throws OseeCoreException {
      try {
         return callableStatement.getDouble(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   /**
    * should not be used by application code because it is less readable than using the column name
    */
   @Override
   public long getLong(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getLong(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   /**
    * should not be used by application code because it is less readable than using the column name
    */
   @Override
   public String getString(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getString(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public Timestamp getTimestamp(String columnName) throws OseeCoreException {
      try {
         return rSet.getTimestamp(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public BigDecimal getBigDecimal(String name) throws OseeCoreException {
      try {
         return rSet.getBigDecimal(name);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public Time getTime(String name) throws OseeCoreException {
      try {
         return rSet.getTime(name);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public double getDouble(String columnName) throws OseeCoreException {
      try {
         return rSet.getDouble(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public Date getDate(String columnName) throws OseeCoreException {
      try {
         return rSet.getDate(columnName);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public boolean wasNull() throws OseeCoreException {
      try {
         return rSet.wasNull();
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return false; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public int getColumnCount() throws OseeCoreException {
      try {
         return rSet.getMetaData().getColumnCount();
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public String getColumnName(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getMetaData().getColumnName(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public int getColumnType(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getMetaData().getColumnType(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public String getColumnTypeName(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getMetaData().getColumnTypeName(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public Object getObject(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getObject(columnIndex);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   /**
    * Returns the number of rows in the result set. Once this method returns the result set will be pointing to the last
    * row
    * 
    * @return the number of rows in the result set
    */
   @Override
   public int getRowCount() throws OseeCoreException {
      try {
         rSet.last();
         return rSet.getRow();
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return 0; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public boolean isNullable(int columnIndex) throws OseeCoreException {
      try {
         return rSet.getMetaData().isNullable(columnIndex) == ResultSetMetaData.columnNullable;
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return false; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   @Override
   public String getComplementSql() throws OseeCoreException {
      allowReuse();
      return SupportedDatabase.getComplementSql(connection.getMetaData());
   }

   @Override
   public boolean isDatabaseType(SupportedDatabase type) throws OseeCoreException {
      return SupportedDatabase.isDatabaseType(connection.getMetaData(), type);
   }

   @Override
   public void updateObject(String columnName, Object value) throws OseeCoreException {
      try {
         rSet.updateObject(columnName, value);
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   @Override
   public void updateRow() throws OseeCoreException {
      try {
         rSet.updateRow();
      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }
}

Back to the top