Changeset 317

Show
Ignore:
Timestamp:
02/25/10 20:57:40 (2 years ago)
Author:
martijn
Message:

Refactoring dead code:
Removed all methods pertaining to constructing Derby databases
to a separate package (org.bridgedb.rdb.construct). Also
factored out common code between SimpleGdbImpl?2/3 into
SimpleGdbImplCommon?

Location:
trunk
Files:
8 added
9 modified
7 moved

Legend:

Unmodified
Added
Removed
  • trunk/benchmarking/test/org/bridgedb/benchmarking/TestAll.java

    r255 r317  
    1010import org.bridgedb.Xref; 
    1111import org.bridgedb.bio.BioDataSource; 
    12 import org.bridgedb.rdb.DBConnectorDerbyServer; 
     12import org.bridgedb.rdb.construct.DBConnectorDerbyServer; 
    1313 
    1414import buildsystem.Measure; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DBConnector.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import java.sql.Connection; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DBConnectorDerbyServer.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import java.io.File; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataDerby.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import java.io.File; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataDerbyDirectory.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import java.sql.DriverManager; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/DataHsqldb.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import java.io.File; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/FileUtilsGdb.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import java.io.File; 
  • trunk/org.bridgedb.rdb.construct/src/org/bridgedb/rdb/construct/GdbConstruct.java

    r308 r317  
    1515// limitations under the License. 
    1616// 
    17 package org.bridgedb.rdb; 
     17package org.bridgedb.rdb.construct; 
    1818 
    1919import org.bridgedb.IDMapperException; 
     
    5858         */ 
    5959        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 
    6075} 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/IDMapperRdb.java

    r308 r317  
    1717package org.bridgedb.rdb; 
    1818 
     19import java.io.File; 
     20import java.io.IOException; 
    1921import java.sql.Connection; 
    2022import java.sql.DriverManager; 
     
    2224import java.util.Collection; 
    2325import java.util.Map; 
     26import java.util.Properties; 
    2427import java.util.Set; 
    2528 
     
    5356                public IDMapper connect(String location) throws IDMapperException  
    5457                { 
    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                        } 
    5668                } 
    5769        } 
     
    6779                        try 
    6880                        { 
    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); 
    7284                        } 
    7385                        catch (SQLException ex) 
     
    8698                public IDMapper connect(String location) throws IDMapperException  
    8799                { 
    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                        } 
    91132                } 
    92133        } 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdb.java

    r316 r317  
    1717package org.bridgedb.rdb; 
    1818 
    19 import java.io.PrintWriter; 
    2019import java.sql.Connection; 
    2120import java.sql.PreparedStatement; 
     
    5150 * to create or connect to one or more pgdb's of any type. 
    5251 */ 
    53 public abstract class SimpleGdb extends IDMapperRdb implements GdbConstruct 
     52public abstract class SimpleGdb extends IDMapperRdb 
    5453{ 
    5554        /** 
     
    176175         */ 
    177176        protected Connection con = null; 
    178         // dbConnector, helper class for dealing with RDBMS specifcs. 
    179         private DBConnector dbConnector; 
    180177 
    181178        /** {@inheritDoc} */ 
     
    191188        { 
    192189                if (con == null) throw new IDMapperException("Database connection already closed"); 
    193                 if (dbConnector != null) dbConnector.closeConnection(con); 
    194190                try 
    195191                { 
     
    202198                con = null; 
    203199        } 
    204  
    205         /** 
    206          * Excecutes several SQL statements to create the tables and indexes in the database the given 
    207          * connection is connected to 
    208          * 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(); 
    212200         
    213201        public static final int NO_LIMIT = 0; 
    214202        public static final int NO_TIMEOUT = 0; 
    215203        public static final int QUERY_TIMEOUT = 5; //seconds 
    216  
    217         /** 
    218            prepare for inserting genes and/or links. 
    219            @throws IDMapperException on failure 
    220          */ 
    221         abstract public void preInsert() throws IDMapperException; 
    222  
    223         /** 
    224            commit inserted data. 
    225            @throws IDMapperException on failure 
    226          */ 
    227         final public void commit() throws IDMapperException 
    228         { 
    229                 try 
    230                 { 
    231                         con.commit(); 
    232                 } 
    233                 catch (SQLException e) 
    234                 { 
    235                         throw new IDMapperException (e); 
    236                 } 
    237         } 
    238204 
    239205        /** 
     
    281247        } 
    282248         
    283         /** 
    284            compact the database. 
    285            @throws IDMapperException on failure 
    286          */ 
    287         final public void compact() throws IDMapperException 
    288         { 
    289                 dbConnector.compact(con); 
    290         } 
    291          
    292         /** 
    293            finalize the database. 
    294            @throws IDMapperException on failure 
    295          */ 
    296         final public void finalize() throws IDMapperException 
    297         { 
    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          
    306249} 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbFactory.java

    r308 r317  
    3434        private SimpleGdbFactory() {}; 
    3535         
    36         static final int LATEST_SCHEMA_VERSION = 3; 
    37  
    38         /** 
    39          * Older method to open a connection to a Gene database 
    40          * using a DBConnector to handle differences 
    41          * 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 Gdb 
    49          * @throws IDMapperException on failure 
    50         */ 
    51         public static SimpleGdb createInstance(String dbName, DBConnector newDbConnector, int props) throws IDMapperException 
    52         { 
    53                 DBConnector dbConnector; 
    54                 Connection con; 
    55  
    56                 try 
    57                 { 
    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  
    7436        /** 
    7537         * Opens a connection to the Gene Database located in the given file. 
     
    8244         * @throws IDMapperException on failure 
    8345        */ 
    84         public static SimpleGdb createInstance(String dbName, Connection con, int props) throws IDMapperException 
     46        public static SimpleGdb createInstance(String dbName, Connection con) throws IDMapperException 
    8547        { 
    8648                if(dbName == null) throw new NullPointerException();     
    8749 
    8850                int version = 0; 
    89                 if ((props & DBConnector.PROP_RECREATE) == 0) 
     51                try  
    9052                { 
    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)  
    10457                { 
    105                         version = LATEST_SCHEMA_VERSION; 
    106                 } 
     58                        //Ignore, older db's don't even have schema version 
     59                }                        
    10760                 
    10861                switch (version) 
    10962                { 
    11063                case 2: 
    111                         return new SimpleGdbImpl2(dbName, con, props); 
     64                        return new SimpleGdbImpl2(dbName, con); 
    11265                case 3: 
    113                         return new SimpleGdbImpl3(dbName, con, props); 
     66                        return new SimpleGdbImpl3(dbName, con); 
    11467                //NB add future schema versions here 
    11568                default: 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java

    r316 r317  
    1818 
    1919import java.sql.Connection; 
    20 import java.sql.PreparedStatement; 
    2120import java.sql.ResultSet; 
    22 import java.sql.ResultSetMetaData; 
    2321import java.sql.SQLException; 
    24 import java.sql.Statement; 
    25 import java.util.ArrayList; 
    26 import java.util.Arrays; 
    2722import java.util.HashMap; 
    2823import java.util.HashSet; 
    29 import java.util.List; 
    3024import java.util.Map; 
    3125import java.util.Set; 
     
    3327import java.util.regex.Pattern; 
    3428 
    35 import org.bridgedb.AbstractIDMapperCapabilities; 
    36 import org.bridgedb.DataSource; 
    37 import org.bridgedb.IDMapperCapabilities; 
    3829import org.bridgedb.IDMapperException; 
    3930import org.bridgedb.Xref; 
    4031 
    4132/** {@inheritDoc} */ 
    42 class SimpleGdbImpl2 extends SimpleGdb 
     33class SimpleGdbImpl2 extends SimpleGdbImplCommon 
    4334{                
    4435        private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version 
    4536         
    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                 ); 
    5637        private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle( 
    5738                        "SELECT backpageText FROM datanode " + 
    5839                        " WHERE id = ? AND code = ?" 
    5940                ); 
    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 IDMapperException  
    101         { 
    102                 final QueryLifeCycle pst = qXrefExists; 
    103                 try  
    104                 { 
    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 unavailable 
    127          */ 
    128         private Map<String, String> getInfo() throws IDMapperException 
    129         { 
    130                 final QueryLifeCycle pst = qInfo; 
    131                 Map<String, String> result = new HashMap<String, String>(); 
    132                 try 
    133                 { 
    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          
    15641 
    15742        /**  
     
    18166        } 
    18267 
    183         /** {@inheritDoc} */ 
    184         public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException 
    185         { 
    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                 try 
    191                 { 
    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  
    24068        /** 
    24169         * Opens a connection to the Gene Database located in the given file. 
     
    24775         * @throws IDMapperException when the database could not be created or connected to 
    24876         */ 
    249         public SimpleGdbImpl2(String dbName, Connection con, int props) throws IDMapperException 
     77        public SimpleGdbImpl2(String dbName, Connection con) throws IDMapperException 
    25078        { 
    25179                super (con); 
     
    25482                this.dbName = dbName; 
    25583                 
    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(); 
    27093        } 
    27194         
     
    290113                        throw new IDMapperException ("Implementation and schema version mismatch"); 
    291114                } 
    292         } 
    293  
    294         /** 
    295          * Excecutes several SQL statements to create the tables and indexes in the database the given 
    296          * connection is connected to 
    297          * 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                 try  
    304                 { 
    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                 try 
    317                 { 
    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 GDB 
    326                                         "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; //seconds 
    369  
    370         /** {@inheritDoc} */ 
    371         public Set<Xref> freeSearch (String text, int limit) throws IDMapperException  
    372         {                
    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                 try  
    403                 { 
    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         try  
    438         { 
    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 database 
    455            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 failure 
    458          */ 
    459         public void createGdbIndices() throws IDMapperException  
    460         { 
    461                 try 
    462                 { 
    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 failure 
    490          */ 
    491         public void preInsert() throws IDMapperException 
    492         { 
    493                 try 
    494                 { 
    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 unavailable 
    523          */ 
    524         private Set<DataSource> getDataSources() throws IDMapperException 
    525         { 
    526                 Set<DataSource> result = new HashSet<DataSource>(); 
    527                 final QueryLifeCycle pst = qDatasources; 
    528         try 
    529         { 
    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 AbstractIDMapperCapabilities 
    549         { 
    550                 /** default constructor. 
    551                  * @throws IDMapperException when database is not available */ 
    552                 public SimpleGdbCapabilities() throws IDMapperException  
    553                 { 
    554                         super (SimpleGdbImpl2.this.getDataSources(), true,  
    555                                         SimpleGdbImpl2.this.getInfo()); 
    556                 } 
    557         } 
    558          
    559         /** 
    560          * @return the capabilities of this gene database 
    561          */ 
    562         public IDMapperCapabilities getCapabilities()  
    563         { 
    564                 return caps; 
    565115        } 
    566116         
     
    660210                finally {pst.cleanup(); } 
    661211        } 
    662  
    663         /** 
    664          * 
    665          * @return true 
    666          */ 
    667         public boolean isFreeAttributeSearchSupported() 
    668         { 
    669                 return true; 
    670         } 
    671  
    672         /** 
    673          * free text search for matching symbols. 
    674          * @return references that match the query 
    675          * @param query The text to search for 
    676          * @param attrType the attribute to look for, e.g. 'Symbol' or 'Description'. 
    677          * @param limit The number of results to limit the search to 
    678          * @throws IDMapperException if the mapping service is (temporarily) unavailable  
    679          */ 
    680         public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException 
    681         { 
    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 IDMapperException  
    706         { 
    707                 Set<String> result = new HashSet<String>(); 
    708                 final QueryLifeCycle pst = qAttributesSet; 
    709         try 
    710         { 
    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         } 
    725212} 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl3.java

    r316 r317  
    1818 
    1919import java.sql.Connection; 
    20 import java.sql.PreparedStatement; 
    2120import java.sql.ResultSet; 
    22 import java.sql.ResultSetMetaData; 
    2321import java.sql.SQLException; 
    24 import java.sql.Statement; 
    25 import java.util.ArrayList; 
    26 import java.util.Arrays; 
    2722import java.util.HashMap; 
    2823import java.util.HashSet; 
    29 import java.util.List; 
    3024import java.util.Map; 
    3125import java.util.Set; 
    3226 
    33 import org.bridgedb.AbstractIDMapperCapabilities; 
    34 import org.bridgedb.DataSource; 
    35 import org.bridgedb.IDMapperCapabilities; 
    3627import org.bridgedb.IDMapperException; 
    3728import org.bridgedb.Xref; 
    3829 
    3930/** {@inheritDoc} */ 
    40 class SimpleGdbImpl3 extends SimpleGdb 
     31class SimpleGdbImpl3 extends SimpleGdbImplCommon 
    4132{                
    4233        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 IDMapperException  
    95         { 
    96                 final QueryLifeCycle pst = qXrefExists; 
    97                 try  
    98                 { 
    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 IDMapperException 
    119         { 
    120                 Set<Xref> refs = new HashSet<Xref>(); 
    121                 final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode;       
    122                 try 
    123                 { 
    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         } 
    17534 
    17635        /** 
     
    18342         * @throws IDMapperException when the database could not be created or connected to 
    18443         */ 
    185         public SimpleGdbImpl3(String dbName, Connection con, int props) throws IDMapperException 
     44        public SimpleGdbImpl3(String dbName, Connection con) throws IDMapperException 
    18645        { 
    18746                super(con); 
     
    19049                this.dbName = dbName; 
    19150 
    192                 if ((props & DBConnector.PROP_RECREATE) == 0) 
     51                try 
    19352                { 
    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); 
    20354                } 
    204                  
    205                 caps = new SimpleGdbCapabilities(); 
     55                catch (SQLException e) 
     56                { 
     57                        throw new IDMapperException (e); 
     58                } 
     59                checkSchemaVersion(); 
    20660        } 
    20761         
     
    22680                        throw new IDMapperException ("Implementation and schema version mismatch"); 
    22781                } 
    228         } 
    229  
    230         /** 
    231          * Excecutes several SQL statements to create the tables and indexes in the database the given 
    232          * connection is connected to 
    233          * 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                 try  
    240                 { 
    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                 try 
    253                 { 
    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 GDB 
    262                                         "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; //seconds 
    304  
    305         /** {@inheritDoc} */ 
    306         public Set<Xref> freeSearch (String text, int limit) throws IDMapperException  
    307         {                
    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 unused 
    337         if (pstGene == null) throw new NullPointerException(); 
    338                 try  
    339                 { 
    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         try  
    373         { 
    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 database 
    390            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 failure 
    393          */ 
    394         public void createGdbIndices() throws IDMapperException  
    395         { 
    396                 try 
    397                 { 
    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 failure 
    425          */ 
    426         public void preInsert() throws IDMapperException 
    427         { 
    428                 try 
    429                 { 
    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 unavailable 
    458          */ 
    459         private Map<String, String> getInfo() throws IDMapperException 
    460         { 
    461                 Map<String, String> result = new HashMap<String, String>(); 
    462                 final QueryLifeCycle pst = qInfo; 
    463                 try 
    464                 { 
    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 unavailable 
    490          */ 
    491         private Set<DataSource> getDataSources() throws IDMapperException 
    492         { 
    493                 Set<DataSource> result = new HashSet<DataSource>(); 
    494                 final QueryLifeCycle pst = qDatasources; 
    495         try 
    496         { 
    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 AbstractIDMapperCapabilities 
    515         { 
    516                 /** default constructor. 
    517                  * @throws IDMapperException when database is not available */ 
    518                 public SimpleGdbCapabilities() throws IDMapperException  
    519                 { 
    520                         super (SimpleGdbImpl3.this.getDataSources(), true,  
    521                                 SimpleGdbImpl3.this.getInfo()); 
    522                 } 
    523         } 
    524  
    525         /** 
    526          * @return the capabilities of this gene database 
    527          */ 
    528         public IDMapperCapabilities getCapabilities()  
    529         { 
    530                 return caps; 
    53182        } 
    53283 
     
    582133                finally {pst.cleanup(); } 
    583134        } 
    584  
    585         /** 
    586          * 
    587          * @return true 
    588          */ 
    589         public boolean isFreeAttributeSearchSupported() 
    590         { 
    591                 return true; 
    592         } 
    593          
    594         /** 
    595          * free text search for matching symbols. 
    596          * @return references that match the query 
    597          * @param query The text to search for 
    598          * @param attrType the attribute to look for, e.g. 'Symbol' or 'Description'. 
    599          * @param limit The number of results to limit the search to 
    600          * @throws IDMapperException if the mapping service is (temporarily) unavailable  
    601          */ 
    602         public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException 
    603         { 
    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 IDMapperException  
    628         { 
    629                 Set<String> result = new HashSet<String>(); 
    630                 final QueryLifeCycle pst = qAttributesSet; 
    631         try 
    632         { 
    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  
    648135} 
  • trunk/org.bridgedb.rdb/test/org/bridgedb/rdb/Test.java

    r308 r317  
    4949                start = System.currentTimeMillis(); 
    5050                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"); 
    5455                end = System.currentTimeMillis(); 
    5556                delta = end - start; 
  • trunk/org.bridgedb.rdb/test/org/bridgedb/rdb/Test2.java

    r308 r317  
    2525import junit.framework.TestCase; 
    2626 
     27import org.bridgedb.AttributeMapper; 
    2728import org.bridgedb.BridgeDb; 
    2829import org.bridgedb.DataSource; 
     
    3031import org.bridgedb.IDMapperException; 
    3132import org.bridgedb.Xref; 
    32 import org.bridgedb.rdb.DataDerby; 
    3333import org.bridgedb.rdb.SimpleGdb; 
    3434import org.bridgedb.rdb.SimpleGdbFactory; 
     
    8181                // test special attributes that are grabbed from backpage 
    8282                // 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; 
    8485                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")); 
    8990                 
    9091                Set<String> allExpectedAttributes = new HashSet<String>(); 
  • trunk/org.bridgedb.rdb/test/org/bridgedb/rdb/TestJdbc.java

    r308 r317  
    1414                Class.forName ("com.mysql.jdbc.Driver"); 
    1515                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"; 
    1717                 
    1818                IDMapper mapper = BridgeDb.connect(connectString);