Coverage report

  %line %branch
org.apache.commons.jelly.tags.sql.QueryTag
3% 
83% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.jelly.tags.sql;
 18  
 
 19  
 import java.sql.Connection;
 20  
 import java.sql.PreparedStatement;
 21  
 import java.sql.ResultSet;
 22  
 import java.sql.SQLException;
 23  
 import java.sql.Statement;
 24  
 
 25  
 import javax.servlet.jsp.jstl.sql.Result;
 26  
 
 27  
 import org.apache.commons.jelly.JellyTagException;
 28  
 import org.apache.commons.jelly.XMLOutput;
 29  
 import org.apache.commons.jelly.tags.Resources;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 
 33  
 
 34  
 /**
 35  
  * <p>Tag handler for &lt;Query&gt; in JSTL.
 36  
  *
 37  
  * @author Hans Bergsten
 38  
  * @author Justyna Horwat
 39  
  */
 40  
 
 41  2
 public class QueryTag extends SqlTagSupport {
 42  
 
 43  
     /** The Log to which logging calls will be made. */
 44  2
     private static final Log log = LogFactory.getLog(QueryTag.class);
 45  
 
 46  
     /*
 47  
      * The following properties take expression values, so the
 48  
      * setter methods are implemented by the expression type
 49  
      * specific subclasses.
 50  
      */
 51  0
     protected int maxRows = -1;
 52  
     protected boolean maxRowsSpecified;
 53  
     protected int startRow;
 54  
 
 55  
     /*
 56  
      * Instance variables that are not for attributes
 57  
      */
 58  
     private Connection conn;
 59  
 
 60  
     //*********************************************************************
 61  
     // Constructor and initialization
 62  
 
 63  0
     public QueryTag() {
 64  0
     }
 65  
 
 66  
     //*********************************************************************
 67  
     // Accessor methods
 68  
 
 69  
     /**
 70  
      * The index of the first row returned can be
 71  
      * specified using startRow.
 72  
      */
 73  
     public void setStartRow(int startRow) {
 74  0
         this.startRow = startRow;
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Query result can be limited by specifying
 79  
      * the maximum number of rows returned.
 80  
      */
 81  
     public void setMaxRows(int maxRows) {
 82  0
         this.maxRows = maxRows;
 83  0
         this.maxRowsSpecified = true;
 84  0
     }
 85  
 
 86  
     //*********************************************************************
 87  
     // Tag logic
 88  
 
 89  
     /**
 90  
      * <p>Execute the SQL statement, set either through the <code>sql</code>
 91  
      * attribute or as the body, and save the result as a variable
 92  
      * named by the <code>var</code> attribute in the scope specified
 93  
      * by the <code>scope</code> attribute, as an object that implements
 94  
      * the Result interface.
 95  
      *
 96  
      * <p>The connection used to execute the statement comes either
 97  
      * from the <code>DataSource</code> specified by the
 98  
      * <code>dataSource</code> attribute, provided by a parent action
 99  
      * element, or is retrieved from a JSP scope  attribute
 100  
      * named <code>javax.servlet.jstl.sql.dataSource</code>.
 101  
      */
 102  
     public void doTag(XMLOutput output) throws JellyTagException {
 103  
 
 104  0
         if (!maxRowsSpecclass="keyword">ified) {
 105  0
             Object obj = context.getVariable("org.apache.commons.jelly.sql.maxRows");
 106  0
             if (obj != null) {
 107  0
                 if (obj instanceof Integer) {
 108  0
                     maxRows = ((Integer) obj).intValue();
 109  
                 }
 110  0
                 else if (obj instanceof String) {
 111  
                     try {
 112  0
                         maxRows = Integer.parseInt((String) obj);
 113  0
                     }
 114  
                     catch (NumberFormatException nfe) {
 115  0
                         throw new JellyTagException(
 116  
                             Resources.getMessage("SQL_MAXROWS_PARSE_ERROR", (String) obj),
 117  
                             nfe);
 118  0
                     }
 119  
                 }
 120  
                 else {
 121  0
                     throw new JellyTagException(Resources.getMessage("SQL_MAXROWS_INVALID"));
 122  
                 }
 123  
             }
 124  
         }
 125  
 
 126  0
         Result result = null;
 127  0
         String sqlStatement = null;
 128  
 
 129  0
         log.debug( "About to lookup connection" );
 130  
 
 131  0
         ResultSet rs = null;
 132  0
         Statement statement = null;
 133  
         try {
 134  0
             conn = getConnection();
 135  
 
 136  
             /*
 137  
              * Use the SQL statement specified by the sql attribute, if any,
 138  
              * otherwise use the body as the statement.
 139  
              */
 140  0
             if (sql != null) {
 141  0
                 sqlStatement = sql;
 142  
             }
 143  
             else {
 144  0
                 sqlStatement = getBodyText();
 145  
             }
 146  0
             if (sqlStatement == null || sqlStatement.trim().length() == 0) {
 147  0
                 throw new JellyTagException(Resources.getMessage("SQL_NO_STATEMENT"));
 148  
             }
 149  
             /*
 150  
              * We shouldn't have a negative startRow or illegal maxrows
 151  
              */
 152  0
             if ((startRow < 0) || (maxRows < -1)) {
 153  0
                 throw new JellyTagException(Resources.getMessage("PARAM_BAD_VALUE"));
 154  
             }
 155  
 
 156  
             /*
 157  
              * Note! We must not use the setMaxRows() method on the
 158  
              * the statement to limit the number of rows, since the
 159  
              * Result factory must be able to figure out the correct
 160  
              * value for isLimitedByMaxRows(); there's no way to check
 161  
              * if it was from the ResultSet.
 162  
              */
 163  0
             if ( log.isDebugEnabled() ) {
 164  0
                 log.debug( "About to execute query: " + sqlStatement );
 165  
             }
 166  
 
 167  0
             if ( hasParameters() ) {
 168  0
                 PreparedStatement ps = conn.prepareStatement(sqlStatement);
 169  0
                 statement = ps;
 170  0
                 setParameters(ps);
 171  0
                 rs = ps.executeQuery();
 172  
             }
 173  
             else {
 174  0
                 statement = conn.createStatement();
 175  0
                 rs = statement.executeQuery(sqlStatement);
 176  
             }
 177  
 
 178  0
             result = new ResultImpl(rs, startRow, maxRows);
 179  0
             context.setVariable(var, result);
 180  
 
 181  
             // always close the result set first since it may be closed by
 182  
             // JDBC 3 when closing statements
 183  
 
 184  
             // lets nullify before we close in case we get exceptions
 185  
             // while closing, we don't want to try to close again
 186  0
             ResultSet tempRs = rs;
 187  0
             rs = null;
 188  0
             tempRs.close();
 189  0
             Statement tempStatement = statement;
 190  0
             statement = null;
 191  0
             tempStatement.close();
 192  0
         }
 193  
         catch (SQLException e) {
 194  0
             throw new JellyTagException(sqlStatement + ": " + e.getMessage(), e);
 195  
         }
 196  
         finally {
 197  0
             if (rs != null) {
 198  
                 try {
 199  0
                     rs.close();
 200  0
                 }
 201  
                 catch (SQLException e) {
 202  0
                     log.error("Caught exception while closing result set: " + e, e);
 203  
                 }
 204  
             }
 205  0
             if (statement != null) {
 206  
                 try {
 207  0
                     statement.close();
 208  0
                 }
 209  
                 catch (SQLException e) {
 210  0
                     log.error("Caught exception while closing statement: " + e, e);
 211  
                 }
 212  
             }
 213  0
             if (conn != null && !isPartOfTransaction) {
 214  
                 try {
 215  0
                     conn.close();
 216  0
                 }
 217  
                 catch (SQLException e) {
 218  0
                     log.error("Caught exception while closing connection: " + e, e);
 219  
                 }
 220  0
                 conn = null;
 221  
             }
 222  0
             clearParameters();
 223  
         }
 224  0
     }
 225  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.