| 32 | | { |
| 33 | | private static final int GDB_COMPAT_VERSION = 3; //Preferred schema version |
| 34 | | |
| 35 | | /** |
| 36 | | * Opens a connection to the Gene Database located in the given file. |
| 37 | | * A new instance of this class is created automatically. |
| 38 | | * @param dbName The file containing the Gene Database. |
| 39 | | * @param data_.con An existing java SQL connection |
| 40 | | * @param props PROP_RECREATE if you want to create a new database (possibly overwriting an existing one) |
| 41 | | * or PROP_NONE if you want to connect read-only |
| 42 | | * @throws IDMapperException when the database could not be created or connected to |
| 43 | | */ |
| 44 | | public SimpleGdbImpl3(String dbName, String connectionString) throws IDMapperException |
| 45 | | { |
| 46 | | super(dbName, connectionString); |
| 47 | | checkSchemaVersion(); |
| 48 | | } |
| 49 | | |
| 50 | | /** |
| 51 | | * look at the info table of the current database to determine the schema version. |
| 52 | | * @throws IDMapperException when looking up the schema version failed |
| 53 | | */ |
| 54 | | private void checkSchemaVersion() throws IDMapperException |
| 55 | | { |
| 56 | | int version = 0; |
| 57 | | try |
| 58 | | { |
| 59 | | ResultSet r = getConnection().createStatement().executeQuery("SELECT schemaversion FROM info"); |
| 60 | | if(r.next()) version = r.getInt(1); |
| 61 | | } |
| 62 | | catch (SQLException e) |
| 63 | | { |
| 64 | | //Ignore, older db's don't even have schema version |
| 65 | | } |
| 66 | | if(version != GDB_COMPAT_VERSION) |
| 67 | | { |
| 68 | | throw new IDMapperException ("Implementation and schema version mismatch"); |
| 69 | | } |
| 70 | | } |
| 71 | | |
| 72 | | /** {@inheritDoc} */ |
| 73 | | public Set<String> getAttributes(Xref ref, String attrname) |
| 74 | | throws IDMapperException |
| 75 | | { |
| 76 | | Set<String> result = new HashSet<String>(); |
| 77 | | final QueryLifeCycle pst = qAttribute; |
| 78 | | synchronized (pst) |
| 79 | | { |
| 80 | | try { |
| 81 | | pst.init(); |
| 82 | | pst.setString (1, ref.getId()); |
| 83 | | pst.setString (2, ref.getDataSource().getSystemCode()); |
| 84 | | pst.setString (3, attrname); |
| 85 | | ResultSet r = pst.executeQuery(); |
| 86 | | if (r.next()) |
| 87 | | { |
| 88 | | result.add (r.getString(1)); |
| 89 | | } |
| 90 | | return result; |
| 91 | | } catch (SQLException e) { throw new IDMapperException (e); } // Database unavailable |
| 92 | | finally {pst.cleanup(); } |
| 93 | | } |
| 94 | | } |
| 95 | | |
| 96 | | /** {@inheritDoc} */ |
| 97 | | public Map<String, Set<String>> getAttributes(Xref ref) |
| 98 | | throws IDMapperException |
| 99 | | { |
| 100 | | Map<String, Set<String>> result = new HashMap<String, Set<String>>(); |
| 101 | | final QueryLifeCycle pst = qAllAttributes; |
| 102 | | synchronized (pst) |
| 103 | | { |
| 104 | | try { |
| 105 | | pst.init(); |
| 106 | | pst.setString (1, ref.getId()); |
| 107 | | pst.setString (2, ref.getDataSource().getSystemCode()); |
| 108 | | ResultSet r = pst.executeQuery(); |
| 109 | | while (r.next()) |
| 110 | | { |
| 111 | | String key = r.getString(1); |
| 112 | | String value = r.getString(2); |
| 113 | | if (result.containsKey (key)) |
| 114 | | { |
| 115 | | result.get(key).add (value); |
| 116 | | } |
| 117 | | else |
| 118 | | { |
| 119 | | Set<String> valueSet = new HashSet<String>(); |
| 120 | | valueSet.add (value); |
| 121 | | result.put (key, valueSet); |
| 122 | | } |
| 123 | | } |
| 124 | | return result; |
| 125 | | } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable |
| 126 | | finally {pst.cleanup(); } |
| 127 | | } |
| 128 | | } |
| | 34 | { |
| | 35 | private static final int GDB_COMPAT_VERSION = 3; // Preferred schema version |
| | 36 | |
| | 37 | /** |
| | 38 | * Opens a connection to the Gene Database located in the given file. A new |
| | 39 | * instance of this class is created automatically. |
| | 40 | * |
| | 41 | * @param dbName |
| | 42 | * The file containing the Gene Database. |
| | 43 | * @param data_ |
| | 44 | * .con An existing java SQL connection |
| | 45 | * @param props |
| | 46 | * PROP_RECREATE if you want to create a new database (possibly |
| | 47 | * overwriting an existing one) or PROP_NONE if you want to |
| | 48 | * connect read-only |
| | 49 | * @throws IDMapperException |
| | 50 | * when the database could not be created or connected to |
| | 51 | */ |
| | 52 | public SimpleGdbImpl3(String dbName, |
| | 53 | String connectionString) throws IDMapperException |
| | 54 | { |
| | 55 | super(dbName, connectionString); |
| | 56 | checkSchemaVersion(); |
| | 57 | } |
| | 58 | |
| | 59 | /** |
| | 60 | * look at the info table of the current database to determine the schema |
| | 61 | * version. |
| | 62 | * |
| | 63 | * @throws IDMapperException |
| | 64 | * when looking up the schema version failed |
| | 65 | */ |
| | 66 | private void checkSchemaVersion() throws IDMapperException |
| | 67 | { |
| | 68 | int version = 0; |
| | 69 | Connection c = null; |
| | 70 | Statement s = null; |
| | 71 | ResultSet r = null; |
| | 72 | try |
| | 73 | { |
| | 74 | c = getConnection(); |
| | 75 | s = c.createStatement(); |
| | 76 | |
| | 77 | r = s.executeQuery("SELECT schemaversion FROM info"); |
| | 78 | if (r.next()) |
| | 79 | version = r.getInt(1); |
| | 80 | } |
| | 81 | catch (SQLException e) |
| | 82 | { |
| | 83 | // Ignore, older db's don't even have schema version |
| | 84 | } |
| | 85 | finally |
| | 86 | { |
| | 87 | closeSilently(r, s, c); |
| | 88 | } |
| | 89 | if (version != GDB_COMPAT_VERSION) |
| | 90 | { |
| | 91 | throw new IDMapperException("Implementation and schema version mismatch"); |
| | 92 | } |
| | 93 | } |
| | 94 | |
| | 95 | /** {@inheritDoc} */ |
| | 96 | public Set<String> getAttributes(Xref ref, |
| | 97 | String attrname) throws IDMapperException |
| | 98 | { |
| | 99 | Set<String> result = new HashSet<String>(); |
| | 100 | |
| | 101 | Connection c = null; |
| | 102 | PreparedStatement pst = null; |
| | 103 | ResultSet r = null; |
| | 104 | try |
| | 105 | { |
| | 106 | c = getConnection(); |
| | 107 | pst = c.prepareStatement(qAttribute); |
| | 108 | pst.setString(1, ref.getId()); |
| | 109 | pst.setString(2, ref.getDataSource().getSystemCode()); |
| | 110 | pst.setString(3, attrname); |
| | 111 | r = pst.executeQuery(); |
| | 112 | if (r.next()) |
| | 113 | { |
| | 114 | result.add(r.getString(1)); |
| | 115 | } |
| | 116 | |
| | 117 | } |
| | 118 | catch (SQLException e) |
| | 119 | { |
| | 120 | throw new IDMapperException(e); |
| | 121 | } // Database unavailable |
| | 122 | finally |
| | 123 | { |
| | 124 | closeSilently(r, pst, c); |
| | 125 | } |
| | 126 | return result; |
| | 127 | } |
| | 128 | |
| | 129 | /** {@inheritDoc} */ |
| | 130 | public Map<String, Set<String>> getAttributes(Xref ref) throws IDMapperException |
| | 131 | { |
| | 132 | Map<String, Set<String>> result = new HashMap<String, Set<String>>(); |
| | 133 | |
| | 134 | Connection c = null; |
| | 135 | PreparedStatement pst = null; |
| | 136 | ResultSet r = null; |
| | 137 | try |
| | 138 | { |
| | 139 | c = getConnection(); |
| | 140 | pst = c.prepareStatement(qAllAttributes); |
| | 141 | pst.setString(1, ref.getId()); |
| | 142 | pst.setString(2, ref.getDataSource().getSystemCode()); |
| | 143 | r = pst.executeQuery(); |
| | 144 | while (r.next()) |
| | 145 | { |
| | 146 | String key = r.getString(1); |
| | 147 | String value = r.getString(2); |
| | 148 | if (result.containsKey(key)) |
| | 149 | { |
| | 150 | result.get(key).add(value); |
| | 151 | } |
| | 152 | else |
| | 153 | { |
| | 154 | Set<String> valueSet = new HashSet<String>(); |
| | 155 | valueSet.add(value); |
| | 156 | result.put(key, valueSet); |
| | 157 | } |
| | 158 | } |
| | 159 | |
| | 160 | } |
| | 161 | catch (SQLException e) |
| | 162 | { |
| | 163 | throw new IDMapperException("Xref:" + ref, e); |
| | 164 | } // Database unavailable |
| | 165 | finally |
| | 166 | { |
| | 167 | closeSilently(r, pst, c); |
| | 168 | } |
| | 169 | return result; |
| | 170 | } |