jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lu...@apache.org
Subject cvs commit: jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql QueryTagSupport.java
Date Tue, 23 Apr 2002 20:51:23 GMT
luehe       02/04/23 13:51:22

  Modified:    standard/src/javax/servlet/jsp/jstl/sql ResultImpl.java
               standard/src/org/apache/taglibs/standard/tag/common/sql
                        QueryTagSupport.java
  Log:
  Fixed 8338: isLimitedByMaxRows() returns true even when it's not (patch by Hans Bergsten)
  
  Revision  Changes    Path
  1.7       +47 -52    jakarta-taglibs/standard/src/javax/servlet/jsp/jstl/sql/ResultImpl.java
  
  Index: ResultImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/src/javax/servlet/jsp/jstl/sql/ResultImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ResultImpl.java	13 Apr 2002 23:38:13 -0000	1.6
  +++ ResultImpl.java	23 Apr 2002 20:51:22 -0000	1.7
  @@ -59,9 +59,9 @@
   import java.util.*;
   
   /**
  - * <p>This class creates a cached version of a <code>ResultSet</code>.
  - * It's represented as a <code>Result</code> implementation, capable of 
  - * returing an array of <code>Row</code> objects containing a <code>Column</code>

  + * <p>This class creates a cached version of a <tt>ResultSet</tt>.
  + * It's represented as a <tt>Result</tt> implementation, capable of 
  + * returing an array of <tt>Row</tt> objects containing a <tt>Column</tt>

    * instance for each column in the row. 
    *
    * @author Hans Bergsten
  @@ -78,60 +78,55 @@
        * This constructor reads the ResultSet and saves a cached
        * copy.
        *
  -     * @param rs an open <code>ResultSet</code>, positioned before the 
  -     *   first row
  +     * @param rs an open <tt>ResultSet</tt>, positioned before the first
  +     * row
        * @param startRow, beginning row to be cached
        * @param maxRows, query maximum rows limit
        * @exception if a database error occurs
        */
  -    public ResultImpl(ResultSet rs, int startRow, int maxRows) throws SQLException {
  -	rowMap = new ArrayList();
  -	rowByIndex = new ArrayList();
  -
  -	ResultSetMetaData rsmd = rs.getMetaData();
  -	int noOfColumns = rsmd.getColumnCount();
  -        int beginRow = 0;
  +    public ResultImpl(ResultSet rs, int startRow, int maxRows)
  +        throws SQLException 
  +    {
  +        rowMap = new ArrayList();
  +        rowByIndex = new ArrayList();
   
  +        ResultSetMetaData rsmd = rs.getMetaData();
  +        int noOfColumns = rsmd.getColumnCount();
  +
  +        // Create the column name array
           columnNames = new String[noOfColumns];
  +        for (int i = 1; i <= noOfColumns; i++) {
  +            columnNames[i-1] = rsmd.getColumnName(i);
  +        }
  +
  +        // Throw away all rows upto startRow
  +        for (int i = 0; i < startRow; i++) {
  +            rs.next();
  +        }
   
  -        /*
  -         * Shift maximum rows depending on starting point
  -         */
  -        if ((maxRows > 0) && (startRow > 0)) {
  -            maxRows = maxRows + startRow;
  -        }
  -
  -	while (rs.next()) {
  -            if ((maxRows < 0) || (beginRow < maxRows)) {
  -                if (beginRow >= startRow) {
  -                    Object[] columns = new Object[noOfColumns];
  -                    SortedMap columnMap = new TreeMap(
  -                        String.CASE_INSENSITIVE_ORDER);
  -
  -	            // JDBC uses 1 as the lowest index!
  -	            for (int i = 1; i <= noOfColumns; i++) {
  -		        Object value =  rs.getObject(i);
  -		        if (rs.wasNull()) {
  -		            value = null;
  -		        }
  -                        // 0-based indexing to be consistent w/JSTL 
  -                        if (columnNames[i-1] == null) {
  -                            columnNames[i-1] = rsmd.getColumnName(i);
  -                        }
  -                        columns[i-1] = value;
  -                        columnMap.put(columnNames[i-1], value);
  -	            }
  -                rowMap.add(columnMap);
  -                rowByIndex.add(columns);
  +        // Process the remaining rows upto maxRows
  +        int processedRows = 0;
  +        while (rs.next()) {
  +            if ((maxRows != -1) && (processedRows == maxRows)) {
  +                isLimited = true; 
  +                break;
  +            }
  +            Object[] columns = new Object[noOfColumns];
  +            SortedMap columnMap = 
  +                new TreeMap(String.CASE_INSENSITIVE_ORDER);
  +
  +            // JDBC uses 1 as the lowest index!
  +            for (int i = 1; i <= noOfColumns; i++) {
  +                Object value =  rs.getObject(i);
  +                if (rs.wasNull()) {
  +                    value = null;
                   }
  -            beginRow++;
  +                columns[i-1] = value;
  +                columnMap.put(columnNames[i-1], value);
               }
  -	}
  -
  -        if (maxRows > 0) { 
  -            isLimited = true; 
  -        } else { 
  -            isLimited = false; 
  +            rowMap.add(columnMap);
  +            rowByIndex.add(columns);
  +            processedRows++;
           }
       }
   
  @@ -188,10 +183,10 @@
        *    not be initialized due to SQLExceptions
        */
       public int getRowCount() {
  -	if (rowMap == null) {
  -	    return -1;
  -	}
  -	return rowMap.size();
  +        if (rowMap == null) {
  +            return -1;
  +        }
  +        return rowMap.size();
       }
   
       /**
  
  
  
  1.25      +7 -3      jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/QueryTagSupport.java
  
  Index: QueryTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/sql/QueryTagSupport.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- QueryTagSupport.java	22 Apr 2002 20:03:42 -0000	1.24
  +++ QueryTagSupport.java	23 Apr 2002 20:51:22 -0000	1.25
  @@ -234,11 +234,15 @@
           }
   
   	Result result = null;
  +	/* 
  +	 * Note! We must not use the setMaxRows() method on the
  +	 * the statement to limit the number of rows, since the
  +	 * Result factory must be able to figure out the correct
  +	 * value for isLimitedByMaxRows(); there's no way to check
  +	 * if it was from the ResultSet.
  +          */
   	try {
   	    PreparedStatement ps = conn.prepareStatement(sqlStatement);
  -            if (maxRows > 0 ) {
  -	        ps.setMaxRows(startRow + maxRows);
  -            }
   	    setParameters(ps, parameters);
   	    ResultSet rs = ps.executeQuery();
   	    result = new ResultImpl(rs, startRow, maxRows);
  
  
  

--
To unsubscribe, e-mail:   <mailto:taglibs-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-dev-help@jakarta.apache.org>


Mime
View raw message