Clover coverage report - Diasparsoft Toolkit - 0.22
Coverage timestamp: Tue Jun 8 2004 12:41:26 EDT
file stats: LOC: 203   Methods: 8
NCLOC: 149   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
JdbcQueryExecuter.java 40% 48.5% 50% 46.9%
coverage coverage
 1   
 package com.diasparsoftware.jdbc;
 2   
 
 3   
 import java.sql.*;
 4   
 import java.util.*;
 5   
 
 6   
 import com.diasparsoftware.java.sql.PreparedStatementData;
 7   
 import com.diasparsoftware.store.DataStoreException;
 8   
 
 9   
 public class JdbcQueryExecuter implements PreparedStatementExecuter {
 10   
     private static final RowCountRowMapper ROW_COUNT_ROW_MAPPER = new RowCountRowMapper();
 11   
 
 12   
     private Connection connection;
 13   
 
 14  44
     public JdbcQueryExecuter(Connection connection) {
 15  44
         this.connection = connection;
 16   
     }
 17   
 
 18  0
     public void executeDeleteStatement(
 19   
             PreparedStatementData preparedStatementData) throws DataStoreException {
 20   
 
 21  0
         PreparedStatement deleteStatement = null;
 22   
 
 23  0
         try {
 24  0
             deleteStatement = connection
 25   
                     .prepareStatement(preparedStatementData.sqlString);
 26   
 
 27  0
             deleteStatement.clearParameters();
 28   
 
 29  0
             int columnIndex = 1;
 30  0
             for (Iterator i = preparedStatementData.parameters.iterator(); i
 31   
                     .hasNext(); columnIndex++) {
 32   
 
 33  0
                 Object eachParameter = (Object) i.next();
 34  0
                 deleteStatement.setObject(columnIndex, eachParameter);
 35   
             }
 36   
 
 37  0
             deleteStatement.executeUpdate();
 38   
         }
 39   
         catch (SQLException rethrow) {
 40  0
             throw new DataStoreException(rethrow);
 41   
         }
 42   
         finally {
 43  0
             try {
 44  0
                 if (deleteStatement != null)
 45  0
                     deleteStatement.close();
 46   
             }
 47   
             catch (SQLException ignored) {
 48   
             }
 49   
         }
 50   
 
 51   
     }
 52   
 
 53  0
     public int executeUpdateStatement(
 54   
             PreparedStatementData preparedStatementData) {
 55  0
         PreparedStatement updateStatement = null;
 56   
 
 57  0
         try {
 58  0
             updateStatement = connection
 59   
                     .prepareStatement(preparedStatementData.sqlString);
 60   
 
 61  0
             updateStatement.clearParameters();
 62   
 
 63  0
             int columnIndex = 1;
 64  0
             for (Iterator i = preparedStatementData.parameters.iterator(); i
 65   
                     .hasNext(); columnIndex++) {
 66   
 
 67  0
                 Object eachParameter = (Object) i.next();
 68  0
                 updateStatement.setObject(columnIndex, eachParameter);
 69   
             }
 70   
 
 71  0
             return updateStatement.executeUpdate();
 72   
         }
 73   
         catch (SQLException rethrow) {
 74  0
             throw new DataStoreException(rethrow);
 75   
         }
 76   
         finally {
 77  0
             try {
 78  0
                 if (updateStatement != null)
 79  0
                     updateStatement.close();
 80   
             }
 81   
             catch (SQLException ignored) {
 82   
             }
 83   
         }
 84   
 
 85   
     }
 86   
 
 87   
     // TODO Another version of this method that accepts
 88   
     //       the expected number of rows inserted, and
 89   
     //       checks them.
 90  11
     public int executeInsertStatement(PreparedStatementData insertStatementData) {
 91  11
         PreparedStatement insertStatement = null;
 92  11
         try {
 93  11
             insertStatement = connection
 94   
                     .prepareStatement(insertStatementData.sqlString);
 95   
 
 96  11
             insertStatement.clearParameters();
 97   
 
 98  11
             int columnIndex = 1;
 99  11
             for (Iterator i = insertStatementData.parameters.iterator(); i
 100   
                     .hasNext(); ) {
 101   
 
 102  33
                 Object each = (Object) i.next();
 103  33
                 insertStatement.setObject(columnIndex, each);
 104   
 
 105  33
                 columnIndex++;
 106   
             }
 107   
 
 108  11
             return insertStatement.executeUpdate();
 109   
         }
 110   
         catch (SQLException rethrow) {
 111  0
             throw new DataStoreException(rethrow);
 112   
         }
 113   
         finally {
 114  11
             try {
 115  11
                 if (insertStatement != null)
 116  11
                     insertStatement.close();
 117   
             }
 118   
             catch (SQLException ignored) {
 119   
             }
 120   
         }
 121   
     }
 122   
 
 123  33
     public List executeSelectStatement(
 124   
             PreparedStatementData selectStatementData, JdbcRowMapper rowMapper) {
 125   
 
 126  33
         List result = new ArrayList();
 127   
 
 128  33
         PreparedStatement selectStatement = null;
 129  33
         ResultSet resultSet = null;
 130   
 
 131  33
         try {
 132  33
             selectStatement = connection
 133   
                     .prepareStatement(selectStatementData.sqlString);
 134   
 
 135  33
             selectStatement.clearParameters();
 136   
 
 137  33
             JdbcUtil.setPreparedStatementParameters(selectStatement,
 138   
                     selectStatementData.parameters);
 139   
 
 140  33
             resultSet = selectStatement.executeQuery();
 141  33
             while (resultSet.next()) {
 142  44
                 result.add(rowMapper.makeDomainObject(resultSet));
 143   
             }
 144   
         }
 145   
         catch (SQLException rethrow) {
 146  0
             throw new DataStoreException(rethrow);
 147   
         }
 148   
         finally {
 149  33
             try {
 150  33
                 if (resultSet != null)
 151  33
                     resultSet.close();
 152   
 
 153  33
                 if (selectStatement != null)
 154  33
                     selectStatement.close();
 155   
             }
 156   
             catch (SQLException ignored) {
 157   
             }
 158   
         }
 159   
 
 160  33
         return result;
 161   
     }
 162   
 
 163   
     /**
 164   
      * Invoke this only for SELECT statements that count rows. This
 165   
      * method assumes that the database returns only a single row for
 166   
      * SELECT COUNT(...) statements.
 167   
      * 
 168   
      * @param countStatementData
 169   
      *            A SELECT COUNT(...) statement.
 170   
      * @return The number of rows determined by the COUNT statement.
 171   
      */
 172  0
     public int executeCountStatement(PreparedStatementData countStatementData) {
 173  0
         List rowCountResults = executeSelectStatement(countStatementData,
 174   
                 ROW_COUNT_ROW_MAPPER);
 175   
 
 176  0
         Object rowCountAsObject = new LinkedList(rowCountResults).get(0);
 177   
 
 178  0
         return ((Integer) rowCountAsObject).intValue();
 179   
     }
 180   
 
 181  11
     public Object executeSingleRowSelectStatement(
 182   
             PreparedStatementData selectStatementData,
 183   
             JdbcRowMapper simpleMapper) {
 184   
 
 185  11
         List rows = executeSelectStatement(selectStatementData, simpleMapper);
 186  11
         if (rows.isEmpty()) {
 187  0
             return null;
 188   
         }
 189   
         else {
 190  11
             return rows.get(0);
 191   
         }
 192   
     }
 193   
 
 194  0
     public void commit() {
 195  0
         try {
 196  0
             connection.commit();
 197   
         }
 198   
         catch (SQLException wrapped) {
 199  0
             throw new DataStoreException("Unable to commit last operation",
 200   
                     wrapped);
 201   
         }
 202   
     }
 203   
 }