Changeset 317
- Timestamp:
- 02/25/10 20:57:40 (5 months ago)
- Location:
- trunk
- Files:
-
- 8 added
- 9 modified
- 7 moved
-
benchmarking/test/org/bridgedb/benchmarking/TestAll.java (modified) (1 diff)
-
org.bridgedb.rdb.construct (added)
-
org.bridgedb.rdb.construct/src (added)
-
org.bridgedb.rdb.construct/src/org (added)
-
org.bridgedb.rdb.construct/src/org/bridgedb (added)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb (added)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct (added)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DBConnector.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/DBConnector.java) (1 diff)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DBConnectorDerbyServer.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/DBConnectorDerbyServer.java) (1 diff)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataDerby.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/DataDerby.java) (1 diff)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataDerbyDirectory.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/DataDerbyDirectory.java) (1 diff)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataHsqldb.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/DataHsqldb.java) (1 diff)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/FileUtilsGdb.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/FileUtilsGdb.java) (1 diff)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/GdbConstruct.java (moved) (moved from trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/GdbConstruct.java) (2 diffs)
-
org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/GdbConstructImpl3.java (added)
-
org.bridgedb.rdb/src/org/bridgedb/rdb/IDMapperRdb.java (modified) (5 diffs)
-
org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdb.java (modified) (6 diffs)
-
org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbFactory.java (modified) (2 diffs)
-
org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java (modified) (7 diffs)
-
org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl3.java (modified) (5 diffs)
-
org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImplCommon.java (added)
-
org.bridgedb.rdb/test/org/bridgedb/rdb/Test.java (modified) (1 diff)
-
org.bridgedb.rdb/test/org/bridgedb/rdb/Test2.java (modified) (3 diffs)
-
org.bridgedb.rdb/test/org/bridgedb/rdb/TestJdbc.java (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/benchmarking/test/org/bridgedb/benchmarking/TestAll.java
r255 r317 10 10 import org.bridgedb.Xref; 11 11 import org.bridgedb.bio.BioDataSource; 12 import org.bridgedb.rdb. DBConnectorDerbyServer;12 import org.bridgedb.rdb.construct.DBConnectorDerbyServer; 13 13 14 14 import buildsystem.Measure; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DBConnector.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import java.sql.Connection; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DBConnectorDerbyServer.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import java.io.File; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataDerby.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import java.io.File; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataDerbyDirectory.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import java.sql.DriverManager; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataHsqldb.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import java.io.File; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/FileUtilsGdb.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import java.io.File; -
trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/GdbConstruct.java
r308 r317 15 15 // limitations under the License. 16 16 // 17 package org.bridgedb.rdb ;17 package org.bridgedb.rdb.construct; 18 18 19 19 import org.bridgedb.IDMapperException; … … 58 58 */ 59 59 public void createGdbIndices() throws IDMapperException; 60 61 /** 62 prepare for inserting genes and/or links. 63 @throws IDMapperException on failure 64 */ 65 public void preInsert() throws IDMapperException; 66 67 /** 68 * Excecutes several SQL statements to create the tables and indexes in the database the given 69 * connection is connected to 70 * Note: Official GDB's are created by Alex Pico's script, not with this code. 71 * This is just here for testing purposes. 72 */ 73 abstract public void createGdbTables(); 74 60 75 } -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/IDMapperRdb.java
r308 r317 17 17 package org.bridgedb.rdb; 18 18 19 import java.io.File; 20 import java.io.IOException; 19 21 import java.sql.Connection; 20 22 import java.sql.DriverManager; … … 22 24 import java.util.Collection; 23 25 import java.util.Map; 26 import java.util.Properties; 24 27 import java.util.Set; 25 28 … … 53 56 public IDMapper connect(String location) throws IDMapperException 54 57 { 55 return SimpleGdbFactory.createInstance(location, new DataDerby(), 0); 58 try 59 { 60 String url = "jdbc:derby:jar:(" + location + ")database"; 61 Connection con = DriverManager.getConnection(url); 62 return SimpleGdbFactory.createInstance(location, con); 63 } 64 catch (SQLException ex) 65 { 66 throw new IDMapperException(ex); 67 } 56 68 } 57 69 } … … 67 79 try 68 80 { 69 Connection con = DriverManager.getConnection("jdbc:" + location);70 71 return SimpleGdbFactory.createInstance(location, con , 0);81 String url = "jdbc:" + location; 82 Connection con = DriverManager.getConnection(url); 83 return SimpleGdbFactory.createInstance(location, con); 72 84 } 73 85 catch (SQLException ex) … … 86 98 public IDMapper connect(String location) throws IDMapperException 87 99 { 88 //TODO: make port and host configurable 89 DBConnectorDerbyServer.init ("wikipathways.org", 1527); 90 return SimpleGdbFactory.createInstance(location, new DBConnectorDerbyServer(), 0); 100 try 101 { 102 Map<String, String> args = 103 InternalUtils.parseLocation(location, "host", "port"); 104 105 if (!args.containsKey("BASE")) 106 throw new IllegalArgumentException("Expected species name in connection string: " + location); 107 108 String host = args.containsKey("host") ? args.get("host") : "wikipathways.org"; 109 String port = args.containsKey("port") ? args.get("port") : "1527"; 110 111 Class.forName("org.apache.derby.jdbc.ClientDriver"); 112 Properties sysprop = System.getProperties(); 113 sysprop.setProperty("derby.storage.tempDirectory", System.getProperty("java.io.tmpdir")); 114 sysprop.setProperty("derby.stream.error.file", File.createTempFile("derby",".log").toString()); 115 116 String url = "jdbc:derby://" + host + ":" + port + "/" + args.get("BASE"); 117 Connection con = DriverManager.getConnection(url); 118 return SimpleGdbFactory.createInstance(location, con); 119 } 120 catch (SQLException f) 121 { 122 throw new IDMapperException (f); 123 } 124 catch (IOException e) 125 { 126 throw new IDMapperException (e); 127 } 128 catch (ClassNotFoundException e) 129 { 130 throw new IDMapperException (e); 131 } 91 132 } 92 133 } -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdb.java
r316 r317 17 17 package org.bridgedb.rdb; 18 18 19 import java.io.PrintWriter;20 19 import java.sql.Connection; 21 20 import java.sql.PreparedStatement; … … 51 50 * to create or connect to one or more pgdb's of any type. 52 51 */ 53 public abstract class SimpleGdb extends IDMapperRdb implements GdbConstruct52 public abstract class SimpleGdb extends IDMapperRdb 54 53 { 55 54 /** … … 176 175 */ 177 176 protected Connection con = null; 178 // dbConnector, helper class for dealing with RDBMS specifcs.179 private DBConnector dbConnector;180 177 181 178 /** {@inheritDoc} */ … … 191 188 { 192 189 if (con == null) throw new IDMapperException("Database connection already closed"); 193 if (dbConnector != null) dbConnector.closeConnection(con);194 190 try 195 191 { … … 202 198 con = null; 203 199 } 204 205 /**206 * Excecutes several SQL statements to create the tables and indexes in the database the given207 * connection is connected to208 * Note: Official GDB's are created by Alex Pico's script, not with this code.209 * This is just here for testing purposes.210 */211 abstract public void createGdbTables();212 200 213 201 public static final int NO_LIMIT = 0; 214 202 public static final int NO_TIMEOUT = 0; 215 203 public static final int QUERY_TIMEOUT = 5; //seconds 216 217 /**218 prepare for inserting genes and/or links.219 @throws IDMapperException on failure220 */221 abstract public void preInsert() throws IDMapperException;222 223 /**224 commit inserted data.225 @throws IDMapperException on failure226 */227 final public void commit() throws IDMapperException228 {229 try230 {231 con.commit();232 }233 catch (SQLException e)234 {235 throw new IDMapperException (e);236 }237 }238 204 239 205 /** … … 281 247 } 282 248 283 /**284 compact the database.285 @throws IDMapperException on failure286 */287 final public void compact() throws IDMapperException288 {289 dbConnector.compact(con);290 }291 292 /**293 finalize the database.294 @throws IDMapperException on failure295 */296 final public void finalize() throws IDMapperException297 {298 dbConnector.compact(con);299 createGdbIndices();300 dbConnector.closeConnection(con, DBConnector.PROP_FINALIZE);301 String newDb = dbConnector.finalizeNewDatabase(dbName);302 dbName = newDb;303 }304 305 306 249 } -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbFactory.java
r308 r317 34 34 private SimpleGdbFactory() {}; 35 35 36 static final int LATEST_SCHEMA_VERSION = 3;37 38 /**39 * Older method to open a connection to a Gene database40 * using a DBConnector to handle differences41 * between different RDBMS-es. The other createInstance() is preferred.42 * <p>43 * Use this instead of constructor to create an instance of SimpleGdb that matches the schema version.44 * @param dbName The file containing the Gene Database.45 * @param newDbConnector handles the differences between types of RDBMS.46 * A new instance of DbConnector class is instantiated automatically.47 * @param props PROP_RECREATE if you want to create a new database, overwriting any existing ones. Otherwise, PROP_NONE.48 * @return a new Gdb49 * @throws IDMapperException on failure50 */51 public static SimpleGdb createInstance(String dbName, DBConnector newDbConnector, int props) throws IDMapperException52 {53 DBConnector dbConnector;54 Connection con;55 56 try57 {58 // create a fresh db connector of the correct type.59 dbConnector = newDbConnector.getClass().newInstance();60 }61 catch (InstantiationException e)62 {63 throw new IDMapperException (e);64 }65 catch (IllegalAccessException e)66 {67 throw new IDMapperException (e);68 }69 con = dbConnector.createConnection(dbName, props);70 71 return createInstance(dbName, con, props);72 }73 74 36 /** 75 37 * Opens a connection to the Gene Database located in the given file. … … 82 44 * @throws IDMapperException on failure 83 45 */ 84 public static SimpleGdb createInstance(String dbName, Connection con , int props) throws IDMapperException46 public static SimpleGdb createInstance(String dbName, Connection con) throws IDMapperException 85 47 { 86 48 if(dbName == null) throw new NullPointerException(); 87 49 88 50 int version = 0; 89 if ((props & DBConnector.PROP_RECREATE) == 0)51 try 90 52 { 91 // read database and try to determine version 92 93 try 94 { 95 ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); 96 if(r.next()) version = r.getInt(1); 97 } 98 catch (SQLException e) 99 { 100 //Ignore, older db's don't even have schema version 101 } 102 } 103 else 53 ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); 54 if(r.next()) version = r.getInt(1); 55 } 56 catch (SQLException e) 104 57 { 105 version = LATEST_SCHEMA_VERSION;106 } 58 //Ignore, older db's don't even have schema version 59 } 107 60 108 61 switch (version) 109 62 { 110 63 case 2: 111 return new SimpleGdbImpl2(dbName, con , props);64 return new SimpleGdbImpl2(dbName, con); 112 65 case 3: 113 return new SimpleGdbImpl3(dbName, con , props);66 return new SimpleGdbImpl3(dbName, con); 114 67 //NB add future schema versions here 115 68 default: -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java
r316 r317 18 18 19 19 import java.sql.Connection; 20 import java.sql.PreparedStatement;21 20 import java.sql.ResultSet; 22 import java.sql.ResultSetMetaData;23 21 import java.sql.SQLException; 24 import java.sql.Statement;25 import java.util.ArrayList;26 import java.util.Arrays;27 22 import java.util.HashMap; 28 23 import java.util.HashSet; 29 import java.util.List;30 24 import java.util.Map; 31 25 import java.util.Set; … … 33 27 import java.util.regex.Pattern; 34 28 35 import org.bridgedb.AbstractIDMapperCapabilities;36 import org.bridgedb.DataSource;37 import org.bridgedb.IDMapperCapabilities;38 29 import org.bridgedb.IDMapperException; 39 30 import org.bridgedb.Xref; 40 31 41 32 /** {@inheritDoc} */ 42 class SimpleGdbImpl2 extends SimpleGdb 33 class SimpleGdbImpl2 extends SimpleGdbImplCommon 43 34 { 44 35 private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version 45 36 46 private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle(47 "SELECT codeRight FROM link GROUP BY codeRight"48 );49 private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle(50 "SELECT * FROM info"51 );52 private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle(53 "SELECT id FROM " + "datanode" + " WHERE " +54 "id = ? AND code = ?"55 );56 37 private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle( 57 38 "SELECT backpageText FROM datanode " + 58 39 " WHERE id = ? AND code = ?" 59 40 ); 60 private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle(61 "SELECT attrvalue FROM attribute " +62 " WHERE id = ? AND code = ? AND attrname = ?"63 );64 private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle(65 "SELECT attrname, attrvalue FROM attribute " +66 " WHERE id = ? AND code = ?"67 );68 private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle(69 "SELECT attrname FROM attribute GROUP BY attrname"70 );71 private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle (72 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " +73 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " +74 "WHERE src.idRight = ? AND src.codeRight = ?"75 );76 private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle (77 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " +78 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " +79 "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?"80 );81 private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle (82 "SELECT datanode.id, datanode.code FROM datanode " +83 " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " +84 "WHERE attrName = ? AND attrValue = ?"85 );86 private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle (87 "SELECT id, code FROM datanode WHERE " +88 "LOWER(ID) LIKE ?"89 );90 private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle (91 "SELECT id, code, attrvalue FROM attribute WHERE " +92 "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?"93 );94 private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle (95 "SELECT id, code, attrvalue FROM attribute WHERE " +96 "attrname = 'Symbol' AND LOWER(ID) LIKE ?"97 );98 99 /** {@inheritDoc} */100 public boolean xrefExists(Xref xref) throws IDMapperException101 {102 final QueryLifeCycle pst = qXrefExists;103 try104 {105 pst.init();106 pst.setString(1, xref.getId());107 pst.setString(2, xref.getDataSource().getSystemCode());108 ResultSet r = pst.executeQuery();109 110 while(r.next())111 {112 return true;113 }114 }115 catch (SQLException e)116 {117 throw new IDMapperException (e);118 }119 finally {pst.cleanup(); }120 return false;121 }122 123 /**124 * Read the info table and return as properties.125 * @return a map where keys are column names and values are the fields in the first row.126 * @throws IDMapperException when the database became unavailable127 */128 private Map<String, String> getInfo() throws IDMapperException129 {130 final QueryLifeCycle pst = qInfo;131 Map<String, String> result = new HashMap<String, String>();132 try133 {134 pst.init();135 ResultSet rs = pst.executeQuery();136 137 if (rs.next())138 {139 ResultSetMetaData rsmd = rs.getMetaData();140 for (int i = 1; i <= rsmd.getColumnCount(); ++i)141 {142 String key = rsmd.getColumnName(i);143 String val = rs.getString(i);144 result.put (key, val);145 }146 }147 }148 catch (SQLException ex)149 {150 throw new IDMapperException (ex);151 }152 153 return result;154 }155 156 41 157 42 /** … … 181 66 } 182 67 183 /** {@inheritDoc} */184 public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException185 {186 final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode;187 Set<Xref> refs = new HashSet<Xref>();188 189 if (idc.getDataSource() == null) return refs;190 try191 {192 pst.init();193 pst.setString(1, idc.getId());194 pst.setString(2, idc.getDataSource().getSystemCode());195 if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode());196 197 Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs));198 199 ResultSet rs = pst.executeQuery();200 while (rs.next())201 {202 DataSource ds = DataSource.getBySystemCode(rs.getString(2));203 if (resultDs.length == 0 || dsFilter.contains(ds))204 {205 refs.add (new Xref (rs.getString(1), ds));206 }207 }208 }209 catch (SQLException e)210 {211 throw new IDMapperException (e);212 }213 finally {pst.cleanup(); }214 215 return refs;216 }217 218 /** {@inheritDoc} */219 public List<Xref> getCrossRefsByAttribute(String attrName, String attrValue) throws IDMapperException {220 // Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue);221 List<Xref> refs = new ArrayList<Xref>();222 final QueryLifeCycle pst = qRefsByAttribute;223 try {224 pst.init();225 pst.setString(1, attrName);226 pst.setString(2, attrValue);227 ResultSet r = pst.executeQuery();228 while(r.next()) {229 Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2)));230 refs.add(ref);231 }232 } catch(SQLException e) {233 throw new IDMapperException (e);234 }235 finally {pst.cleanup(); }236 // Logger.log.trace("End fetching cross references by attribute");237 return refs;238 }239 240 68 /** 241 69 * Opens a connection to the Gene Database located in the given file. … … 247 75 * @throws IDMapperException when the database could not be created or connected to 248 76 */ 249 public SimpleGdbImpl2(String dbName, Connection con , int props) throws IDMapperException77 public SimpleGdbImpl2(String dbName, Connection con) throws IDMapperException 250 78 { 251 79 super (con); … … 254 82 this.dbName = dbName; 255 83 256 if ((props & DBConnector.PROP_RECREATE) == 0) 257 { 258 try 259 { 260 con.setReadOnly(true); 261 } 262 catch (SQLException e) 263 { 264 throw new IDMapperException (e); 265 } 266 checkSchemaVersion(); 267 } 268 269 caps = new SimpleGdbCapabilities(); 84 try 85 { 86 con.setReadOnly(true); 87 } 88 catch (SQLException e) 89 { 90 throw new IDMapperException (e); 91 } 92 checkSchemaVersion(); 270 93 } 271 94 … … 290 113 throw new IDMapperException ("Implementation and schema version mismatch"); 291 114 } 292 }293 294 /**295 * Excecutes several SQL statements to create the tables and indexes in the database the given296 * connection is connected to297 * Note: Official GDB's are created by AP, not with this code.298 * This is just here for testing purposes.299 */300 public void createGdbTables()301 {302 // Logger.log.info("Info: Creating tables");303 try304 {305 Statement sh = con.createStatement();306 sh.execute("DROP TABLE info");307 sh.execute("DROP TABLE link");308 sh.execute("DROP TABLE datanode");309 sh.execute("DROP TABLE attribute");310 }311 catch(SQLException e)312 {313 // Logger.log.error("Unable to drop gdb tables (ignoring): " + e.getMessage());314 }315 316 try317 {318 Statement sh = con.createStatement();319 sh.execute(320 "CREATE TABLE " +321 " info " +322 "( schemaversion INTEGER PRIMARY KEY " +323 ")");324 // Logger.log.info("Info table created");325 sh.execute( //Add compatibility version of GDB326 "INSERT INTO info VALUES ( " + GDB_COMPAT_VERSION + ")");327 // Logger.log.info("Version stored in info");328 sh.execute(329 "CREATE TABLE " +330 " link " +331 " ( idLeft VARCHAR(50) NOT NULL, " +332 " codeLeft VARCHAR(50) NOT NULL, " +333 " idRight VARCHAR(50) NOT NULL, " +334 " codeRight VARCHAR(50) NOT NULL, " +335 " bridge VARCHAR(50), " +336 " PRIMARY KEY (idLeft, codeLeft, " +337 " idRight, codeRight) " +338 " ) ");339 // Logger.log.info("Link table created");340 sh.execute(341 "CREATE TABLE " +342 " datanode " +343 " ( id VARCHAR(50), " +344 " code VARCHAR(50), " +345 " backpageText VARCHAR(800), " +346 " PRIMARY KEY (id, code) " +347 " ) ");348 // Logger.log.info("DataNode table created");349 sh.execute(350 "CREATE TABLE " +351 " attribute " +352 " ( id VARCHAR(50), " +353 " code VARCHAR(50), " +354 " attrname VARCHAR(50), " +355 " attrvalue VARCHAR(255) " +356 " ) ");357 // Logger.log.info("Attribute table created");358 }359 catch (SQLException e)360 {361 // Logger.log.error("while creating gdb tables: " + e.getMessage(), e);362 }363 }364 365 366 public static final int NO_LIMIT = 0;367 public static final int NO_TIMEOUT = 0;368 public static final int QUERY_TIMEOUT = 20; //seconds369 370 /** {@inheritDoc} */371 public Set<Xref> freeSearch (String text, int limit) throws IDMapperException372 {373 Set<Xref> result = new HashSet<Xref>();374 final QueryLifeCycle pst = qFreeSearch;375 try {376 pst.init(limit);377 pst.setString(1, "%" + text.toLowerCase() + "%");378 ResultSet r = pst.executeQuery();379 while(r.next()) {380 String id = r.getString(1);381 DataSource ds = DataSource.getBySystemCode(r.getString(2));382 Xref ref = new Xref (id, ds);383 result.add (ref);384 }385 }386 catch (SQLException e)387 {388 throw new IDMapperException(e);389 }390 finally {pst.cleanup(); }391 return result;392 }393 394 private PreparedStatement pstGene = null;395 private PreparedStatement pstLink = null;396 private PreparedStatement pstAttr = null;397 398 /** {@inheritDoc} */399 public int addGene(Xref ref, String bpText)400 {401 if (pstGene == null) throw new NullPointerException();402 try403 {404 pstGene.setString(1, ref.getId());405 pstGene.setString(2, ref.getDataSource().getSystemCode());406 pstGene.setString(3, bpText);407 pstGene.executeUpdate();408 }409 catch (SQLException e)410 {411 // Logger.log.error("" + ref, e);412 return 1;413 }414 return 0;415 }416 417 /** {@inheritDoc} */418 public int addAttribute(Xref ref, String attr, String val)419 {420 try {421 pstAttr.setString(1, attr);422 pstAttr.setString(2, val);423 pstAttr.setString(3, ref.getId());424 pstAttr.setString(4, ref.getDataSource().getSystemCode());425 pstAttr.executeUpdate();426 } catch (SQLException e) {427 // Logger.log.error(attr + "\t" + val + "\t" + ref, e);428 return 1;429 }430 return 0;431 }432 433 /** {@inheritDoc} */434 public int addLink(Xref left, Xref right)435 {436 if (pstLink == null) throw new NullPointerException();437 try438 {439 pstLink.setString(1, left.getId());440 pstLink.setString(2, left.getDataSource().getSystemCode());441 pstLink.setString(3, right.getId());442 pstLink.setString(4, right.getDataSource().getSystemCode());443 pstLink.executeUpdate();444 }445 catch (SQLException e)446 {447 // Logger.log.error(left + "\t" + right , e);448 return 1;449 }450 return 0;451 }452 453 /**454 Create indices on the database455 You can call this at any time after creating the tables,456 but it is good to do it only after inserting all data.457 @throws IDMapperException on failure458 */459 public void createGdbIndices() throws IDMapperException460 {461 try462 {463 Statement sh = con.createStatement();464 sh.execute(465 "CREATE INDEX i_codeLeft" +466 " ON link(codeLeft)"467 );468 sh.execute(469 "CREATE INDEX i_idRight" +470 " ON link(idRight)"471 );472 sh.execute(473 "CREATE INDEX i_codeRight" +474 " ON link(codeRight)"475 );476 sh.execute(477 "CREATE INDEX i_code" +478 " ON " + "datanode" + "(code)"479 );480 }481 catch (SQLException e)482 {483 throw new IDMapperException (e);484 }485 }486 487 /**488 prepare for inserting genes and/or links.489 @throws IDMapperException on failure490 */491 public void preInsert() throws IDMapperException492 {493 try494 {495 con.setAutoCommit(false);496 pstGene = con.prepareStatement(497 "INSERT INTO datanode " +498 " (id, code," +499 " backpageText)" +500 "VALUES (?, ?, ?)"501 );502 pstLink = con.prepareStatement(503 "INSERT INTO link " +504 " (idLeft, codeLeft," +505 " idRight, codeRight)" +506 "VALUES (?, ?, ?, ?)"507 );508 pstAttr = con.prepareStatement(509 "INSERT INTO attribute " +510 " (attrname, attrvalue, id, code)" +511 "VALUES (?, ?, ?, ?)"512 );513 }514 catch (SQLException e)515 {516 throw new IDMapperException (e);517 }518 }519 520 /**521 * @return a list of data sources present in this database.522 @throws IDMapperException when the database is unavailable523 */524 private Set<DataSource> getDataSources() throws IDMapperException525 {526 Set<DataSource> result = new HashSet<DataSource>();527 final QueryLifeCycle pst = qDatasources;528 try529 {530 pst.init();531 ResultSet rs = pst.executeQuery();532 while (rs.next())533 {534 DataSource ds = DataSource.getBySystemCode(rs.getString(1));535 result.add (ds);536 }537 }538 catch (SQLException ignore)539 {540 throw new IDMapperException(ignore);541 }542 finally {pst.cleanup(); }543 return result;544 }545 546 private final IDMapperCapabilities caps;547 548 private class SimpleGdbCapabilities extends AbstractIDMapperCapabilities549 {550 /** default constructor.551 * @throws IDMapperException when database is not available */552 public SimpleGdbCapabilities() throws IDMapperException553 {554 super (SimpleGdbImpl2.this.getDataSources(), true,555 SimpleGdbImpl2.this.getInfo());556 }557 }558 559 /**560 * @return the capabilities of this gene database561 */562 public IDMapperCapabilities getCapabilities()563 {564 return caps;565 115 } 566 116 … … 660 210 finally {pst.cleanup(); } 661 211 } 662 663 /**664 *665 * @return true666 */667 public boolean isFreeAttributeSearchSupported()668 {669 return true;670 }671 672 /**673 * free text search for matching symbols.674 * @return references that match the query675 * @param query The text to search for676 * @param attrType the attribute to look for, e.g. 'Symbol' or 'Description'.677 * @param limit The number of results to limit the search to678 * @throws IDMapperException if the mapping service is (temporarily) unavailable679 */680 public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException681 {682 Map<Xref, String> result = new HashMap<Xref, String>();683 final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?684 qIdSearchWithAttributes : qAttributeSearch;685 try {686 pst.init(limit);687 pst.setString(1, "%" + query.toLowerCase() + "%");688 ResultSet r = pst.executeQuery();689 690 while(r.next())691 {692 String id = r.getString("id");693 String code = r.getString("code");694 String symbol = r.getString("attrValue");695 result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol);696 }697 } catch (SQLException e) {698 throw new IDMapperException (e);699 }700 finally {pst.cleanup(); }701 return result;702 }703 704 /** {@inheritDoc} */705 public Set<String> getAttributeSet() throws IDMapperException706 {707 Set<String> result = new HashSet<String>();708 final QueryLifeCycle pst = qAttributesSet;709 try710 {711 pst.init();712 ResultSet rs = pst.executeQuery();713 while (rs.next())714 {715 result.add (rs.getString(1));716 }717 }718 catch (SQLException ignore)719 {720 throw new IDMapperException(ignore);721 }722 finally {pst.cleanup(); }723 return result;724 }725 212 } -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl3.java
r316 r317 18 18 19 19 import java.sql.Connection; 20 import java.sql.PreparedStatement;21 20 import java.sql.ResultSet; 22 import java.sql.ResultSetMetaData;23 21 import java.sql.SQLException; 24 import java.sql.Statement;25 import java.util.ArrayList;26 import java.util.Arrays;27 22 import java.util.HashMap; 28 23 import java.util.HashSet; 29 import java.util.List;30 24 import java.util.Map; 31 25 import java.util.Set; 32 26 33 import org.bridgedb.AbstractIDMapperCapabilities;34 import org.bridgedb.DataSource;35 import org.bridgedb.IDMapperCapabilities;36 27 import org.bridgedb.IDMapperException; 37 28 import org.bridgedb.Xref; 38 29 39 30 /** {@inheritDoc} */ 40 class SimpleGdbImpl3 extends SimpleGdb 31 class SimpleGdbImpl3 extends SimpleGdbImplCommon 41 32 { 42 33 private static final int GDB_COMPAT_VERSION = 3; //Preferred schema version 43 44 private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle(45 "SELECT codeRight FROM link GROUP BY codeRight"46 );47 private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle(48 "SELECT * FROM info"49 );50 private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle(51 "SELECT id FROM " + "datanode" + " WHERE " +52 "id = ? AND code = ?"53 );54 private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle(55 "SELECT attrvalue FROM attribute " +56 " WHERE id = ? AND code = ? AND attrname = ?"57 );58 private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle(59 "SELECT attrname, attrvalue FROM attribute " +60 " WHERE id = ? AND code = ?"61 );62 private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle(63 "SELECT attrname FROM attribute GROUP BY attrname"64 );65 private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle (66 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " +67 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " +68 "WHERE src.idRight = ? AND src.codeRight = ?"69 );70 private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle (71 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " +72 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " +73 "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?"74 );75 private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle (76 "SELECT datanode.id, datanode.code FROM datanode " +77 " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " +78 "WHERE attrName = ? AND attrValue = ?"79 );80 private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle (81 "SELECT id, code FROM datanode WHERE " +82 "LOWER(ID) LIKE ?"83 );84 private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle (85 "SELECT id, code, attrvalue FROM attribute WHERE " +86 "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?"87 );88 private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle (89 "SELECT id, code, attrvalue FROM attribute WHERE " +90 "attrname = 'Symbol' AND LOWER(ID) LIKE ?"91 );92 93 /** {@inheritDoc} */94 public boolean xrefExists(Xref xref) throws IDMapperException95 {96 final QueryLifeCycle pst = qXrefExists;97 try98 {99 pst.init();100 pst.setString(1, xref.getId());101 pst.setString(2, xref.getDataSource().getSystemCode());102 ResultSet r = pst.executeQuery();103 104 while(r.next())105 {106 return true;107 }108 }109 catch (SQLException e)110 {111 throw new IDMapperException (e);112 }113 finally {pst.cleanup(); }114 return false;115 }116 117 /** {@inheritDoc} */118 public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException119 {120 Set<Xref> refs = new HashSet<Xref>();121 final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode;122 try123 {124 pst.init();125 pst.setString(1, idc.getId());126 pst.setString(2, idc.getDataSource().getSystemCode());127 if (resultDs.length == 1)128 {129 pst.setString(3, resultDs[0].getSystemCode());130 }131 132 Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs));133 134 ResultSet rs = pst.executeQuery();135 while (rs.next())136 {137 DataSource ds = DataSource.getBySystemCode(rs.getString(2));138 if (resultDs.length == 0 || dsFilter.contains(ds))139 {140 refs.add (new Xref (rs.getString(1), ds));141 }142 }143 }144 catch (SQLException e)145 {146 throw new IDMapperException (e);147 }148 finally {pst.cleanup(); }149 150 return refs;151 }152 153 /** {@inheritDoc} */154 public List<Xref> getCrossRefsByAttribute(String attrName, String attrValue) throws IDMapperException {155 // Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue);156 List<Xref> refs = new ArrayList<Xref>();157 158 final QueryLifeCycle pst = qRefsByAttribute;159 try {160 pst.init();161 pst.setString(1, attrName);162 pst.setString(2, attrValue);163 ResultSet r = pst.executeQuery();164 while(r.next()) {165 Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2)));166 refs.add(ref);167 }168 } catch(SQLException e) {169 throw new IDMapperException (e);170 }171 finally {pst.cleanup(); }172 // Logger.log.trace("End fetching cross references by attribute");173 return refs;174 }175 34 176 35 /** … … 183 42 * @throws IDMapperException when the database could not be created or connected to 184 43 */ 185 public SimpleGdbImpl3(String dbName, Connection con , int props) throws IDMapperException44 public SimpleGdbImpl3(String dbName, Connection con) throws IDMapperException 186 45 { 187 46 super(con); … … 190 49 this.dbName = dbName; 191 50 192 if ((props & DBConnector.PROP_RECREATE) == 0)51 try 193 52 { 194 try 195 { 196 con.setReadOnly(true); 197 } 198 catch (SQLException e) 199 { 200 throw new IDMapperException (e); 201 } 202 checkSchemaVersion(); 53 con.setReadOnly(true); 203 54 } 204 205 caps = new SimpleGdbCapabilities(); 55 catch (SQLException e) 56 { 57 throw new IDMapperException (e); 58 } 59 checkSchemaVersion(); 206 60 } 207 61 … … 226 80 throw new IDMapperException ("Implementation and schema version mismatch"); 227 81 } 228 }229 230 /**231 * Excecutes several SQL statements to create the tables and indexes in the database the given232 * connection is connected to233 * Note: Official GDB's are created by AP, not with this code.234 * This is just here for testing purposes.235 */236 public void createGdbTables()237 {238 // Logger.log.info("Info: Creating tables");239 try240 {241 Statement sh = con.createStatement();242 sh.execute("DROP TABLE info");243 sh.execute("DROP TABLE link");244 sh.execute("DROP TABLE datanode");245 sh.execute("DROP TABLE attribute");246 }247 catch(SQLException e)248 {249 // Logger.log.error("Unable to drop gdb tables (ignoring): " + e.getMessage());250 }251 252 try253 {254 Statement sh = con.createStatement();255 sh.execute(256 "CREATE TABLE " +257 " info " +258 "( schemaversion INTEGER PRIMARY KEY " +259 ")");260 // Logger.log.info("Info table created");261 sh.execute( //Add compatibility version of GDB262 "INSERT INTO info VALUES ( " + GDB_COMPAT_VERSION + ")");263 // Logger.log.info("Version stored in info");264 sh.execute(265 "CREATE TABLE " +266 " link " +267 " ( idLeft VARCHAR(50) NOT NULL, " +268 " codeLeft VARCHAR(50) NOT NULL, " +269 " idRight VARCHAR(50) NOT NULL, " +270 " codeRight VARCHAR(50) NOT NULL, " +271 " bridge VARCHAR(50), " +272 " PRIMARY KEY (idLeft, codeLeft, " +273 " idRight, codeRight) " +274 " ) ");275 // Logger.log.info("Link table created");276 sh.execute(277 "CREATE TABLE " +278 " datanode " +279 " ( id VARCHAR(50), " +280 " code VARCHAR(50) " +281 " PRIMARY KEY (id, code) " +282 " ) ");283 // Logger.log.info("DataNode table created");284 sh.execute(285 "CREATE TABLE " +286 " attribute " +287 " ( id VARCHAR(50), " +288 " code VARCHAR(50), " +289 " attrname VARCHAR(50), " +290 " attrvalue VARCHAR(255) " +291 " ) ");292 // Logger.log.info("Attribute table created");293 }294 catch (SQLException e)295 {296 // Logger.log.error("while creating gdb tables: " + e.getMessage(), e);297 }298 }299 300 301 public static final int NO_LIMIT = 0;302 public static final int NO_TIMEOUT = 0;303 public static final int QUERY_TIMEOUT = 20; //seconds304 305 /** {@inheritDoc} */306 public Set<Xref> freeSearch (String text, int limit) throws IDMapperException307 {308 Set<Xref> result = new HashSet<Xref>();309 final QueryLifeCycle pst = qFreeSearch;310 try {311 pst.init(limit);312 pst.setString(1, "%" + text.toLowerCase() + "%");313 ResultSet r = pst.executeQuery();314 while(r.next()) {315 String id = r.getString(1);316 DataSource ds = DataSource.getBySystemCode(r.getString(2));317 Xref ref = new Xref (id, ds);318 result.add (ref);319 }320 }321 catch (SQLException e)322 {323 throw new IDMapperException(e);324 }325 finally {pst.cleanup(); }326 return result;327 }328 329 private PreparedStatement pstGene = null;330 private PreparedStatement pstLink = null;331 private PreparedStatement pstAttr = null;332 333 /** {@inheritDoc} */334 public int addGene(Xref ref, String bpText)335 {336 //TODO: bpText is unused337 if (pstGene == null) throw new NullPointerException();338 try339 {340 pstGene.setString(1, ref.getId());341 pstGene.setString(2, ref.getDataSource().getSystemCode());342 pstGene.executeUpdate();343 }344 catch (SQLException e)345 {346 // Logger.log.error("" + ref, e);347 return 1;348 }349 return 0;350 }351 352 /** {@inheritDoc} */353 public int addAttribute(Xref ref, String attr, String val)354 {355 try {356 pstAttr.setString(1, attr);357 pstAttr.setString(2, val);358 pstAttr.setString(3, ref.getId());359 pstAttr.setString(4, ref.getDataSource().getSystemCode());360 pstAttr.executeUpdate();361 } catch (SQLException e) {362 // Logger.log.error(attr + "\t" + val + "\t" + ref, e);363 return 1;364 }365 return 0;366 }367 368 /** {@inheritDoc} */369 public int addLink(Xref left, Xref right)370 {371 if (pstLink == null) throw new NullPointerException();372 try373 {374 pstLink.setString(1, left.getId());375 pstLink.setString(2, left.getDataSource().getSystemCode());376 pstLink.setString(3, right.getId());377 pstLink.setString(4, right.getDataSource().getSystemCode());378 pstLink.executeUpdate();379 }380 catch (SQLException e)381 {382 // Logger.log.error(left + "\t" + right , e);383 return 1;384 }385 return 0;386 }387 388 /**389 Create indices on the database390 You can call this at any time after creating the tables,391 but it is good to do it only after inserting all data.392 @throws IDMapperException on failure393 */394 public void createGdbIndices() throws IDMapperException395 {396 try397 {398 Statement sh = con.createStatement();399 sh.execute(400 "CREATE INDEX i_codeLeft" +401 " ON link(codeLeft)"402 );403 sh.execute(404 "CREATE INDEX i_idRight" +405 " ON link(idRight)"406 );407 sh.execute(408 "CREATE INDEX i_codeRight" +409 " ON link(codeRight)"410 );411 sh.execute(412 "CREATE INDEX i_code" +413 " ON " + "datanode" + "(code)"414 );415 }416 catch (SQLException e)417 {418 throw new IDMapperException (e);419 }420 }421 422 /**423 prepare for inserting genes and/or links.424 @throws IDMapperException on failure425 */426 public void preInsert() throws IDMapperException427 {428 try429 {430 con.setAutoCommit(false);431 pstGene = con.prepareStatement(432 "INSERT INTO datanode " +433 " (id, code)" +434 "VALUES (?, ?)"435 );436 pstLink = con.prepareStatement(437 "INSERT INTO link " +438 " (idLeft, codeLeft," +439 " idRight, codeRight)" +440 "VALUES (?, ?, ?, ?)"441 );442 pstAttr = con.prepareStatement(443 "INSERT INTO attribute " +444 " (attrname, attrvalue, id, code)" +445 "VALUES (?, ?, ?, ?)"446 );447 }448 catch (SQLException e)449 {450 throw new IDMapperException (e);451 }452 }453 454 /**455 * Read the info table and return as properties.456 * @return a map where keys are column names and values are the fields in the first row.457 * @throws IDMapperException when the database became unavailable458 */459 private Map<String, String> getInfo() throws IDMapperException460 {461 Map<String, String> result = new HashMap<String, String>();462 final QueryLifeCycle pst = qInfo;463 try464 {465 pst.init();466 ResultSet rs = pst.executeQuery();467 468 if (rs.next())469 {470 ResultSetMetaData rsmd = rs.getMetaData();471 for (int i = 1; i <= rsmd.getColumnCount(); ++i)472 {473 String key = rsmd.getColumnName(i);474 String val = rs.getString(i);475 result.put (key, val);476 }477 }478 }479 catch (SQLException ex)480 {481 throw new IDMapperException (ex);482 }483 484 return result;485 }486 487 /**488 * @return a list of data sources present in this database.489 @throws IDMapperException when the database is unavailable490 */491 private Set<DataSource> getDataSources() throws IDMapperException492 {493 Set<DataSource> result = new HashSet<DataSource>();494 final QueryLifeCycle pst = qDatasources;495 try496 {497 pst.init();498 ResultSet rs = pst.executeQuery();499 while (rs.next())500 {501 DataSource ds = DataSource.getBySystemCode(rs.getString(1));502 result.add (ds);503 }504 }505 catch (SQLException ignore)506 {507 throw new IDMapperException(ignore);508 }509 return result;510 }511 512 private final IDMapperCapabilities caps;513 514 private class SimpleGdbCapabilities extends AbstractIDMapperCapabilities515 {516 /** default constructor.517 * @throws IDMapperException when database is not available */518 public SimpleGdbCapabilities() throws IDMapperException519 {520 super (SimpleGdbImpl3.this.getDataSources(), true,521 SimpleGdbImpl3.this.getInfo());522 }523 }524 525 /**526 * @return the capabilities of this gene database527 */528 public IDMapperCapabilities getCapabilities()529 {530 return caps;531 82 } 532 83 … … 582 133 finally {pst.cleanup(); } 583 134 } 584 585 /**586 *587 * @return true588 */589 public boolean isFreeAttributeSearchSupported()590 {591 return true;592 }593 594 /**595 * free text search for matching symbols.596 * @return references that match the query597 * @param query The text to search for598 * @param attrType the attribute to look for, e.g. 'Symbol' or 'Description'.599 * @param limit The number of results to limit the search to600 * @throws IDMapperException if the mapping service is (temporarily) unavailable601 */602 public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException603 {604 Map<Xref, String> result = new HashMap<Xref, String>();605 final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?606 qIdSearchWithAttributes : qAttributeSearch;607 try {608 pst.init(limit);609 pst.setString(1, "%" + query.toLowerCase() + "%");610 ResultSet r = pst.executeQuery();611 612 while(r.next())613 {614 String id = r.getString("id");615 String code = r.getString("code");616 String symbol = r.getString("attrValue");617 result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol);618 }619 } catch (SQLException e) {620 throw new IDMapperException (e);621 }622 finally {pst.cleanup(); }623 return result;624 }625 626 /** {@inheritDoc} */627 public Set<String> getAttributeSet() throws IDMapperException628 {629 Set<String> result = new HashSet<String>();630 final QueryLifeCycle pst = qAttributesSet;631 try632 {633 pst.init();634 ResultSet rs = pst.executeQuery();635 while (rs.next())636 {637 result.add (rs.getString(1));638 }639 }640 catch (SQLException ignore)641 {642 throw new IDMapperException(ignore);643 }644 finally {pst.cleanup(); }645 return result;646 }647 648 135 } -
trunk/org.bridgedb.rdb/test/org/bridgedb/rdb/Test.java
r308 r317 49 49 start = System.currentTimeMillis(); 50 50 Class.forName ("org.bridgedb.rdb.IDMapperRdb"); 51 DBConnectorDerbyServer.init ("wikipathways.org", 1527); 52 IDMapper mapper = BridgeDb.connect ("idmapper-derbyclient:Homo sapiens"); 53 IDMapper mapper2 = BridgeDb.connect ("idmapper-derbyclient:metabolites"); 51 Class.forName ("org.apache.derby.jdbc.ClientDriver"); 52 53 IDMapper mapper = BridgeDb.connect ("idmapper-derbyclient:Homo sapiens?host=137.120.14.24"); 54 IDMapper mapper2 = BridgeDb.connect ("idmapper-derbyclient:metabolites?host=137.120.14.24"); 54 55 end = System.currentTimeMillis(); 55 56 delta = end - start; -
trunk/org.bridgedb.rdb/test/org/bridgedb/rdb/Test2.java
r308 r317 25 25 import junit.framework.TestCase; 26 26 27 import org.bridgedb.AttributeMapper; 27 28 import org.bridgedb.BridgeDb; 28 29 import org.bridgedb.DataSource; … … 30 31 import org.bridgedb.IDMapperException; 31 32 import org.bridgedb.Xref; 32 import org.bridgedb.rdb.DataDerby;33 33 import org.bridgedb.rdb.SimpleGdb; 34 34 import org.bridgedb.rdb.SimpleGdbFactory; … … 81 81 // test special attributes that are grabbed from backpage 82 82 // since this is a Schema v2 database 83 SimpleGdb gdb = SimpleGdbFactory.createInstance (GDB_HUMAN, new DataDerby(), 0); 83 IDMapper gdb = BridgeDb.connect ("idmapper-pgdb:" + GDB_HUMAN); 84 AttributeMapper am = (AttributeMapper)gdb; 84 85 Xref ref = new Xref ("26873", DataSource.getBySystemCode("L")); 85 assertTrue ( gdb.getAttributes(ref, "Synonyms").contains ("5-Opase|DKFZP434H244|OPLA"));86 assertTrue ( gdb.getAttributes(ref, "Description").contains ("5-oxoprolinase (EC 3.5.2.9) (5-oxo-L-prolinase) (5-OPase) (Pyroglutamase) [Source:UniProtKB/Swiss-Prot.Acc:O14841]"));87 assertTrue ( gdb.getAttributes(ref, "Chromosome").contains ("8"));88 assertTrue ( gdb.getAttributes(ref, "Symbol").contains ("OPLAH"));86 assertTrue (am.getAttributes(ref, "Synonyms").contains ("5-Opase|DKFZP434H244|OPLA")); 87 assertTrue (am.getAttributes(ref, "Description").contains ("5-oxoprolinase (EC 3.5.2.9) (5-oxo-L-prolinase) (5-OPase) (Pyroglutamase) [Source:UniProtKB/Swiss-Prot.Acc:O14841]")); 88 assertTrue (am.getAttributes(ref, "Chromosome").contains ("8")); 89 assertTrue (am.getAttributes(ref, "Symbol").contains ("OPLAH")); 89 90 90 91 Set<String> allExpectedAttributes = new HashSet<String>(); -
trunk/org.bridgedb.rdb/test/org/bridgedb/rdb/TestJdbc.java
r308 r317 14 14 Class.forName ("com.mysql.jdbc.Driver"); 15 15 Class.forName ("org.bridgedb.rdb.IDMapperRdb"); 16 String connectString = "idmapper-jdbc:mysql://localhost/ Celegans?user=bridgedb&password=bridgedb";16 String connectString = "idmapper-jdbc:mysql://localhost/worm?user=bridgedb"; 17 17 18 18 IDMapper mapper = BridgeDb.connect(connectString);
