Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e9de71c9039690adb059b574975f281b2b60dd5 (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
/*******************************************************************************
 * 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.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.sql.Timestamp;
import java.sql.Types;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.jdk.core.type.IVariantData;
import org.eclipse.osee.framework.jdk.core.type.VariantData;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Roberto E. Escobar
 */
public class ResultSetProcessor {
   public static IVariantData parse(IOseeStatement chStmt) throws OseeDataStoreException {
      IVariantData toReturn = new VariantData();
      int numberOfColumns = chStmt.getColumnCount() + 1;
      for (int index = 1; index < numberOfColumns; index++) {
         int columnIndex = index;
         int type = chStmt.getColumnType(columnIndex);
         String name = chStmt.getColumnName(columnIndex);
         // Store name - all upper case
         String upperCasedName = name.toUpperCase();
         switch (type) {
            case Types.CLOB:
            case Types.BINARY:
               InputStream inputStream = chStmt.getAsciiStream(name);
               toReturn.put(upperCasedName, streamToByteArray(inputStream));
               break;
            case Types.BLOB:
               InputStream blobStream = chStmt.getBinaryStream(name);
               toReturn.put(upperCasedName, streamToByteArray(blobStream));
               break;
            case Types.TIMESTAMP:
               Timestamp timeStamp = chStmt.getTimestamp(name);
               if (timeStamp != null) {
                  toReturn.put(upperCasedName, timeStamp.getTime());
               }
               break;
            case Types.DATE:
               Date date = chStmt.getDate(name);
               if (date != null) {
                  toReturn.put(upperCasedName, date.getTime());
               }
               break;
            default:
               try {
                  String value = chStmt.getString(name);
                  if (Strings.isValid(value) != false) {
                     value = value.trim();
                  }
                  toReturn.put(upperCasedName, chStmt.getString(name));
               } catch (OseeDataStoreException ex) {
                  String typeName = chStmt.getColumnTypeName(columnIndex);
                  throw new OseeDataStoreException(getErrorMessage(name, typeName), ex);
               }
               break;
         }
      }
      return toReturn;
   }

   private static String getErrorMessage(String name, String typeName) {
      return String.format("Unable to convert [%s] of raw type [%s] to string.", name, typeName);
   }

   private static byte[] streamToByteArray(InputStream inputStream) throws OseeDataStoreException {
      byte[] toReturn = new byte[0];
      if (inputStream != null) {
         try {
            toReturn = Lib.inputStreamToBytes(inputStream);
         } catch (IOException ex) {
            throw new OseeDataStoreException(ex);
         }
      }
      return toReturn;
   }
}

Back to the top