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/SimpleGdbImpl3.java

    r308 r316  
    4242        private static final int GDB_COMPAT_VERSION = 3; //Preferred schema version 
    4343         
    44         private final SimpleGdb.LazyPst pstDatasources = new SimpleGdb.LazyPst( 
     44        private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 
    4545                        "SELECT codeRight FROM link GROUP BY codeRight" 
    4646                ); 
    47         private final SimpleGdb.LazyPst pstInfo = new SimpleGdb.LazyPst( 
     47        private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 
    4848                        "SELECT * FROM info" 
    4949                ); 
    50         private final SimpleGdb.LazyPst pstXrefExists = new SimpleGdb.LazyPst( 
     50        private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 
    5151                        "SELECT id FROM " + "datanode" + " WHERE " + 
    5252                        "id = ? AND code = ?" 
    5353                );       
    54         private final SimpleGdb.LazyPst pstAttribute = new SimpleGdb.LazyPst( 
     54        private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 
    5555                        "SELECT attrvalue FROM attribute " + 
    5656                        " WHERE id = ? AND code = ? AND attrname = ?" 
    5757                ); 
    58         private final SimpleGdb.LazyPst pstAllAttributes = new SimpleGdb.LazyPst( 
     58        private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 
    5959                        "SELECT attrname, attrvalue FROM attribute " + 
    6060                        " WHERE id = ? AND code = ?" 
    6161                ); 
    62         private final SimpleGdb.LazyPst pstAttributesSet = new SimpleGdb.LazyPst( 
     62        private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 
    6363                        "SELECT attrname FROM attribute GROUP BY attrname" 
    6464                ); 
    65         private final SimpleGdb.LazyPst pstCrossRefs = new SimpleGdb.LazyPst ( 
     65        private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 
    6666                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    6767                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    6868                        "WHERE src.idRight = ? AND src.codeRight = ?" 
    6969                ); 
    70         private final SimpleGdb.LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst ( 
     70        private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 
    7171                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    7272                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    7373                        "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 
    7474                ); 
    75         private final SimpleGdb.LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst ( 
     75        private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 
    7676                        "SELECT datanode.id, datanode.code FROM datanode " + 
    7777                        " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 
    7878                        "WHERE attrName = ? AND attrValue = ?" 
    7979                ); 
    80         private final SimpleGdb.LazyPst pstFreeSearch = new SimpleGdb.LazyPst ( 
     80        private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 
    8181                        "SELECT id, code FROM datanode WHERE " + 
    8282                        "LOWER(ID) LIKE ?" 
    8383                ); 
    84         private final SimpleGdb.LazyPst pstAttributeSearch = new SimpleGdb.LazyPst ( 
     84        private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 
    8585                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    8686                        "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 
    8787                ); 
    88         private final SimpleGdb.LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst ( 
     88        private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 
    8989                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    9090                        "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 
    9191                ); 
    92          
     92 
    9393        /** {@inheritDoc} */ 
    9494        public boolean xrefExists(Xref xref) throws IDMapperException  
    9595        { 
     96                final QueryLifeCycle pst = qXrefExists; 
    9697                try  
    9798                { 
    98                         PreparedStatement pst = pstXrefExists.getPreparedStatement(); 
     99                        pst.init(); 
    99100                        pst.setString(1, xref.getId()); 
    100101                        pst.setString(2, xref.getDataSource().getSystemCode()); 
     
    110111                        throw new IDMapperException (e); 
    111112                } 
     113                finally {pst.cleanup(); } 
    112114                return false; 
    113115        } 
     
    117119        { 
    118120                Set<Xref> refs = new HashSet<Xref>(); 
    119                  
     121                final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode;       
    120122                try 
    121123                { 
    122                         PreparedStatement pst; 
    123                         if (resultDs.length != 1) 
    124                         { 
    125                                 pst = pstCrossRefs.getPreparedStatement(); 
    126                         } 
    127                         else 
    128                         { 
    129                                 pst = pstCrossRefsWithCode.getPreparedStatement(); 
    130                                 pst.setString(3, resultDs[0].getSystemCode()); 
    131                         } 
    132                          
     124                        pst.init();                      
    133125                        pst.setString(1, idc.getId()); 
    134126                        pst.setString(2, idc.getDataSource().getSystemCode()); 
     127                        if (resultDs.length == 1) 
     128                        { 
     129                                pst.setString(3, resultDs[0].getSystemCode()); 
     130                        } 
    135131 
    136132                        Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
     
    150146                        throw new IDMapperException (e); 
    151147                } 
     148                finally {pst.cleanup(); } 
    152149                 
    153150                return refs; 
     
    159156                List<Xref> refs = new ArrayList<Xref>(); 
    160157 
     158                final QueryLifeCycle pst = qRefsByAttribute; 
    161159                try { 
    162                         PreparedStatement pst = pstRefsByAttribute.getPreparedStatement(); 
     160                        pst.init(); 
    163161                        pst.setString(1, attrName); 
    164162                        pst.setString(2, attrValue); 
     
    171169                        throw new IDMapperException (e); 
    172170                } 
     171                finally {pst.cleanup(); } 
    173172//              Logger.log.trace("End fetching cross references by attribute"); 
    174173                return refs; 
     
    308307        {                
    309308                Set<Xref> result = new HashSet<Xref>(); 
     309                final QueryLifeCycle pst = qFreeSearch; 
    310310                try { 
    311                         PreparedStatement ps1 = pstFreeSearch.getPreparedStatement(); 
    312                         ps1.setQueryTimeout(QUERY_TIMEOUT); 
    313                         if(limit > NO_LIMIT)  
    314                         { 
    315                                 ps1.setMaxRows(limit); 
    316                         } 
    317  
    318                         ps1.setString(1, "%" + text.toLowerCase() + "%"); 
    319                         ResultSet r = ps1.executeQuery(); 
     311                        pst.init(limit); 
     312                        pst.setString(1, "%" + text.toLowerCase() + "%"); 
     313                        ResultSet r = pst.executeQuery(); 
    320314                        while(r.next()) { 
    321315                                String id = r.getString(1); 
     
    329323                        throw new IDMapperException(e); 
    330324                } 
     325                finally {pst.cleanup(); } 
    331326                return result; 
    332327        } 
     
    465460        { 
    466461                Map<String, String> result = new HashMap<String, String>(); 
     462                final QueryLifeCycle pst = qInfo; 
    467463                try 
    468464                { 
    469                         PreparedStatement pst = pstInfo.getPreparedStatement(); 
     465                        pst.init(); 
    470466                        ResultSet rs = pst.executeQuery(); 
    471467                         
     
    496492        { 
    497493                Set<DataSource> result = new HashSet<DataSource>(); 
     494                final QueryLifeCycle pst = qDatasources; 
    498495        try 
    499496        { 
    500                 PreparedStatement pst = pstDatasources.getPreparedStatement(); 
     497                        pst.init(); 
    501498                ResultSet rs = pst.executeQuery(); 
    502499                while (rs.next()) 
     
    539536        { 
    540537                Set<String> result = new HashSet<String>(); 
     538                final QueryLifeCycle pst = qAttribute; 
    541539                try { 
    542                         PreparedStatement pst = pstAttribute.getPreparedStatement(); 
     540                        pst.init(); 
    543541                        pst.setString (1, ref.getId()); 
    544542                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    551549                        return result; 
    552550                } catch (SQLException e) { throw new IDMapperException (e); } // Database unavailable 
     551                finally {pst.cleanup(); } 
    553552        } 
    554553 
     
    558557        { 
    559558                Map<String, Set<String>> result = new HashMap<String, Set<String>>();                            
     559                final QueryLifeCycle pst = qAllAttributes; 
    560560                try { 
    561                         PreparedStatement pst = pstAllAttributes.getPreparedStatement(); 
     561                        pst.init(); 
    562562                        pst.setString (1, ref.getId()); 
    563563                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    580580                        return result; 
    581581                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
     582                finally {pst.cleanup(); } 
    582583        } 
    583584 
     
    602603        { 
    603604                Map<Xref, String> result = new HashMap<Xref, String>(); 
     605                final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?  
     606                                qIdSearchWithAttributes : qAttributeSearch; 
    604607                try { 
    605                         PreparedStatement pst = (MATCH_ID.equals (attrType)) ?  
    606                                         pstIdSearchWithAttributes.getPreparedStatement() : pstAttributeSearch.getPreparedStatement(); 
    607                         pst.setQueryTimeout(QUERY_TIMEOUT); 
    608                         if(limit > NO_LIMIT) pst.setMaxRows(limit); 
     608                        pst.init(limit); 
    609609                        pst.setString(1, "%" + query.toLowerCase() + "%"); 
    610610                        ResultSet r = pst.executeQuery(); 
     
    620620                        throw new IDMapperException (e); 
    621621                } 
     622                finally {pst.cleanup(); } 
    622623                return result;           
    623624        } 
     
    627628        { 
    628629                Set<String> result = new HashSet<String>(); 
     630                final QueryLifeCycle pst = qAttributesSet; 
    629631        try 
    630632        { 
    631                 PreparedStatement pst = pstAttributesSet.getPreparedStatement(); 
     633                        pst.init(); 
    632634                ResultSet rs = pst.executeQuery(); 
    633635                while (rs.next()) 
     
    640642                throw new IDMapperException(ignore); 
    641643        } 
     644                finally {pst.cleanup(); } 
    642645        return result; 
    643646        }