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

refactor LazyPst? to also handle resultset and connection

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java

    r308 r316  
    4444        private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version 
    4545         
    46         private final SimpleGdb.LazyPst pstDatasources = new SimpleGdb.LazyPst( 
     46        private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 
    4747                        "SELECT codeRight FROM link GROUP BY codeRight" 
    4848                ); 
    49         private final SimpleGdb.LazyPst pstInfo = new SimpleGdb.LazyPst( 
     49        private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 
    5050                        "SELECT * FROM info" 
    5151                ); 
    52         private final SimpleGdb.LazyPst pstXrefExists = new SimpleGdb.LazyPst( 
     52        private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 
    5353                        "SELECT id FROM " + "datanode" + " WHERE " + 
    5454                        "id = ? AND code = ?" 
    5555                ); 
    56         private final SimpleGdb.LazyPst pstBackpage = new SimpleGdb.LazyPst( 
     56        private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle( 
    5757                        "SELECT backpageText FROM datanode " + 
    5858                        " WHERE id = ? AND code = ?" 
    5959                ); 
    60         private final SimpleGdb.LazyPst pstAttribute = new SimpleGdb.LazyPst( 
     60        private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 
    6161                        "SELECT attrvalue FROM attribute " + 
    6262                        " WHERE id = ? AND code = ? AND attrname = ?" 
    6363                ); 
    64         private final SimpleGdb.LazyPst pstAllAttributes = new SimpleGdb.LazyPst( 
     64        private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 
    6565                        "SELECT attrname, attrvalue FROM attribute " + 
    6666                        " WHERE id = ? AND code = ?" 
    6767                ); 
    68         private final SimpleGdb.LazyPst pstAttributesSet = new SimpleGdb.LazyPst( 
     68        private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 
    6969                        "SELECT attrname FROM attribute GROUP BY attrname" 
    7070                ); 
    71         private final SimpleGdb.LazyPst pstCrossRefs = new SimpleGdb.LazyPst ( 
     71        private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 
    7272                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    7373                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    7474                        "WHERE src.idRight = ? AND src.codeRight = ?" 
    7575                ); 
    76         private final SimpleGdb.LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst ( 
     76        private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 
    7777                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    7878                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    7979                        "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 
    8080                ); 
    81         private final SimpleGdb.LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst ( 
     81        private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 
    8282                        "SELECT datanode.id, datanode.code FROM datanode " + 
    8383                        " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 
    8484                        "WHERE attrName = ? AND attrValue = ?" 
    8585                ); 
    86         private final SimpleGdb.LazyPst pstFreeSearch = new SimpleGdb.LazyPst ( 
     86        private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 
    8787                        "SELECT id, code FROM datanode WHERE " + 
    8888                        "LOWER(ID) LIKE ?" 
    8989                ); 
    90         private final SimpleGdb.LazyPst pstAttributeSearch = new SimpleGdb.LazyPst ( 
     90        private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 
    9191                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    9292                        "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 
    9393                ); 
    94         private final SimpleGdb.LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst ( 
     94        private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 
    9595                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    9696                        "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 
    9797                ); 
    98          
     98 
    9999        /** {@inheritDoc} */ 
    100100        public boolean xrefExists(Xref xref) throws IDMapperException  
    101101        { 
     102                final QueryLifeCycle pst = qXrefExists; 
    102103                try  
    103104                { 
    104                         PreparedStatement pst = pstXrefExists.getPreparedStatement(); 
     105                        pst.init(); 
    105106                        pst.setString(1, xref.getId()); 
    106107                        pst.setString(2, xref.getDataSource().getSystemCode()); 
     
    116117                        throw new IDMapperException (e); 
    117118                } 
     119                finally {pst.cleanup(); } 
    118120                return false; 
    119121        } 
     
    126128        private Map<String, String> getInfo() throws IDMapperException 
    127129        { 
     130                final QueryLifeCycle pst = qInfo; 
    128131                Map<String, String> result = new HashMap<String, String>(); 
    129132                try 
    130133                { 
    131                         PreparedStatement pst = pstInfo.getPreparedStatement(); 
     134                        pst.init(); 
    132135                        ResultSet rs = pst.executeQuery(); 
    133136                         
     
    162165        private String getBpInfo(Xref ref) throws IDMapperException  
    163166        { 
     167                final QueryLifeCycle pst = qBackpage; 
    164168                try { 
    165                         PreparedStatement pst = pstBackpage.getPreparedStatement(); 
     169                        pst.init(); 
    166170                        pst.setString (1, ref.getId()); 
    167171                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    174178                        return result; 
    175179                } catch (SQLException e) { throw new IDMapperException (e); } //Gene not found 
     180                finally {pst.cleanup(); } 
    176181        } 
    177182 
     
    179184        public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException 
    180185        { 
     186                final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode; 
    181187                Set<Xref> refs = new HashSet<Xref>(); 
    182188                 
     
    184190                try 
    185191                { 
    186                         PreparedStatement pst; 
    187                         if (resultDs.length != 1) 
    188                         { 
    189                                 pst = pstCrossRefs.getPreparedStatement(); 
    190                         } 
    191                         else 
    192                         { 
    193                                 pst = pstCrossRefsWithCode.getPreparedStatement(); 
    194                                 pst.setString(3, resultDs[0].getSystemCode()); 
    195                         } 
    196                          
     192                        pst.init(); 
    197193                        pst.setString(1, idc.getId()); 
    198194                        pst.setString(2, idc.getDataSource().getSystemCode()); 
     195                        if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode());                         
    199196                         
    200197                        Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
     
    214211                        throw new IDMapperException (e); 
    215212                } 
     213                finally {pst.cleanup(); } 
    216214                 
    217215                return refs; 
     
    222220//              Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue); 
    223221                List<Xref> refs = new ArrayList<Xref>(); 
    224  
     222                final QueryLifeCycle pst = qRefsByAttribute; 
    225223                try { 
    226                         PreparedStatement pst = pstRefsByAttribute.getPreparedStatement(); 
     224                        pst.init(); 
    227225                        pst.setString(1, attrName); 
    228226                        pst.setString(2, attrValue); 
     
    235233                        throw new IDMapperException (e); 
    236234                } 
     235                finally {pst.cleanup(); } 
    237236//              Logger.log.trace("End fetching cross references by attribute"); 
    238237                return refs; 
     
    373372        {                
    374373                Set<Xref> result = new HashSet<Xref>(); 
     374                final QueryLifeCycle pst = qFreeSearch; 
    375375                try { 
    376                         PreparedStatement ps1 = pstFreeSearch.getPreparedStatement(); 
    377                         ps1.setQueryTimeout(QUERY_TIMEOUT); 
    378                         if(limit > NO_LIMIT)  
    379                         { 
    380                                 ps1.setMaxRows(limit); 
    381                         } 
    382  
    383                         ps1.setString(1, "%" + text.toLowerCase() + "%"); 
    384                         ResultSet r = ps1.executeQuery(); 
     376                        pst.init(limit); 
     377                        pst.setString(1, "%" + text.toLowerCase() + "%"); 
     378                        ResultSet r = pst.executeQuery(); 
    385379                        while(r.next()) { 
    386380                                String id = r.getString(1); 
     
    394388                        throw new IDMapperException(e); 
    395389                } 
     390                finally {pst.cleanup(); } 
    396391                return result; 
    397392        } 
     
    530525        { 
    531526                Set<DataSource> result = new HashSet<DataSource>(); 
     527                final QueryLifeCycle pst = qDatasources; 
    532528        try 
    533529        { 
    534                 PreparedStatement pst = pstDatasources.getPreparedStatement(); 
     530                pst.init(); 
    535531                ResultSet rs = pst.executeQuery(); 
    536532                while (rs.next()) 
     
    544540                throw new IDMapperException(ignore); 
    545541        } 
     542                finally {pst.cleanup(); } 
    546543        return result; 
    547544        } 
     
    585582        { 
    586583                Set<String> result = new HashSet<String>(); 
     584                final QueryLifeCycle pst = qAttribute; 
    587585                 
    588586                if (ATTRIBUTES_FROM_BACKPAGE.containsKey(attrname)) 
     
    601599                 
    602600                try { 
    603                         PreparedStatement pst = pstAttribute.getPreparedStatement(); 
     601                        pst.init(); 
    604602                        pst.setString (1, ref.getId()); 
    605603                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    612610                        return result; 
    613611                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } // Database unavailable 
     612                finally {pst.cleanup(); } 
    614613        } 
    615614 
     
    619618        { 
    620619                Map<String, Set<String>> result = new HashMap<String, Set<String>>(); 
     620                final QueryLifeCycle pst = qAllAttributes; 
    621621                                 
    622622                String bpInfo = getBpInfo(ref); 
     
    637637                 
    638638                try { 
    639                         PreparedStatement pst = pstAllAttributes.getPreparedStatement(); 
     639                        pst.init(); 
    640640                        pst.setString (1, ref.getId()); 
    641641                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    658658                        return result; 
    659659                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
     660                finally {pst.cleanup(); } 
    660661        } 
    661662 
     
    680681        { 
    681682                Map<Xref, String> result = new HashMap<Xref, String>(); 
     683                final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?  
     684                                qIdSearchWithAttributes : qAttributeSearch; 
    682685                try { 
    683                         PreparedStatement pst = (MATCH_ID.equals (attrType)) ?  
    684                                         pstIdSearchWithAttributes.getPreparedStatement() : pstAttributeSearch.getPreparedStatement(); 
    685                         pst.setQueryTimeout(QUERY_TIMEOUT); 
    686                         if(limit > NO_LIMIT) pst.setMaxRows(limit); 
     686                        pst.init(limit); 
    687687                        pst.setString(1, "%" + query.toLowerCase() + "%"); 
    688688                        ResultSet r = pst.executeQuery(); 
     
    698698                        throw new IDMapperException (e); 
    699699                } 
     700                finally {pst.cleanup(); } 
    700701                return result;           
    701702        } 
     
    705706        { 
    706707                Set<String> result = new HashSet<String>(); 
     708                final QueryLifeCycle pst = qAttributesSet; 
    707709        try 
    708710        { 
    709                 PreparedStatement pst = pstAttributesSet.getPreparedStatement(); 
     711                pst.init(); 
    710712                ResultSet rs = pst.executeQuery(); 
    711713                while (rs.next()) 
     
    718720                throw new IDMapperException(ignore); 
    719721        } 
     722                finally {pst.cleanup(); } 
    720723        return result; 
    721724        }