Coverage Report - org.apache.tapestry.contrib.jdbc.ParameterizedStatement
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterizedStatement
0%
0/33
0%
0/8
1.571
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.contrib.jdbc;
 16  
 
 17  
 import java.sql.Connection;
 18  
 import java.sql.PreparedStatement;
 19  
 import java.sql.ResultSet;
 20  
 import java.sql.SQLException;
 21  
 import java.sql.Statement;
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 /**
 28  
  *  A wrapper around {@link PreparedStatement}.
 29  
  *
 30  
  *  @author Howard Lewis Ship
 31  
  * 
 32  
  **/
 33  
 
 34  
 public class ParameterizedStatement implements IStatement
 35  
 {
 36  0
     private static final Log LOG = LogFactory.getLog(ParameterizedStatement.class);
 37  
 
 38  
     private String _sql;
 39  
     private PreparedStatement _statement;
 40  
     private IParameter[] _parameters;
 41  
 
 42  
     /**
 43  
      *  Create a new instance; the parameters list is copied.
 44  
      * 
 45  
      *  @param SQL the SQL to execute (see {@link Connection#prepareStatement(java.lang.String)})
 46  
      *  @param connection the JDBC connection to use
 47  
      *  @param parameters list of {@link IParameter}
 48  
      * 
 49  
      **/
 50  
     
 51  
     public ParameterizedStatement(String SQL, Connection connection, List parameters) throws SQLException
 52  0
     {
 53  0
         _sql = SQL;
 54  
 
 55  0
         _statement = connection.prepareStatement(SQL);
 56  
 
 57  0
         _parameters = (IParameter[]) parameters.toArray(new IParameter[parameters.size()]);
 58  
 
 59  0
         for (int i = 0; i < _parameters.length; i++)
 60  
         {
 61  
             // JDBC numbers things from 1, not 0.
 62  
 
 63  0
             _parameters[i].set(_statement, i + 1);
 64  
         }
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Returns the SQL associated with this statement.
 69  
      *
 70  
      **/
 71  
 
 72  
     public String getSQL()
 73  
     {
 74  0
         return _sql;
 75  
     }
 76  
 
 77  
     /**
 78  
      *  Returns the underlying or {@link PreparedStatement}.
 79  
      *
 80  
      **/
 81  
 
 82  
     public Statement getStatement()
 83  
     {
 84  0
         return _statement;
 85  
     }
 86  
 
 87  
     /**
 88  
      *  Closes the underlying statement, and nulls the reference to it.
 89  
      *
 90  
      **/
 91  
 
 92  
     public void close() throws SQLException
 93  
     {
 94  0
         _statement.close();
 95  
 
 96  0
         _statement = null;
 97  0
         _sql = null;
 98  0
     }
 99  
 
 100  
     /**
 101  
      *  Executes the statement as a query, returning a {@link ResultSet}.
 102  
      *
 103  
      **/
 104  
 
 105  
     public ResultSet executeQuery() throws SQLException
 106  
     {
 107  0
         if (LOG.isDebugEnabled())
 108  0
             LOG.debug("Executing query: " + this);
 109  
 
 110  0
         return _statement.executeQuery();
 111  
     }
 112  
 
 113  
     /**
 114  
      *  Executes the statement as an update, returning the number of rows
 115  
      *  affected.
 116  
      *
 117  
      **/
 118  
 
 119  
     public int executeUpdate() throws SQLException
 120  
     {
 121  0
         if (LOG.isDebugEnabled())
 122  0
             LOG.debug("Executing update: " + this);
 123  
 
 124  0
         return _statement.executeUpdate();
 125  
     }
 126  
 
 127  
     public String toString()
 128  
     {
 129  0
         StringBuffer buffer = new StringBuffer("ParameterizedStatement@");
 130  0
         buffer.append(Integer.toHexString(hashCode()));
 131  0
         buffer.append("[SQL=\n<");
 132  0
         buffer.append(_sql);
 133  0
         buffer.append("\n>");
 134  
 
 135  0
         for (int i = 0; i < _parameters.length; i++)
 136  
         {
 137  0
             IParameter parameter = _parameters[i];
 138  
 
 139  0
             buffer.append(" ?");
 140  0
             buffer.append(i + 1);
 141  0
             buffer.append('=');
 142  
 
 143  0
             buffer.append(parameter);
 144  
         }
 145  
 
 146  0
         buffer.append(']');
 147  
 
 148  0
         return buffer.toString();
 149  
     }
 150  
 
 151  
 }