cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ilya A. Kriveshko" <i...@kaon.com>
Subject Re: Counting rows in a resultset
Date Thu, 29 Aug 2002 19:38:50 GMT
If running two queries is not out of the question for some principal 
reasons, I'd suggest the following:

Run a query with LIMIT 1, 1. That query will return results ONLY if 
there are two or more rows that match.

Then run your second query both inside the 
<esql:no-results></esql:no-results> and inside 
<esql:results></esql:results>.

I would suggest using this only if you want to avoid coding Java by 
hand. Otherwise, using a List would be recommended.

Here's an ad hoc example. As usual, there's more than one way to 
implement anything.

<xsp:logic>
  List rows = new ArrayList();
</xsp:logic>
<xsp:execute-query>
  <xsp:query>SELECT id, name FROM names WHERE ..snip..</xsp:query>
  <xsp:results>
    <xsp:row-results>
      <xsp:logic>
        rows.add(new Object[] {new Long(<esql:get-int name="id"/>), 
<esql:get-string name="name"/>});
      </xsp:logic>
    </xsp:row-results>
  </xsp:results>
</xsp:execute-query>

<xsp:logic>
  switch(rows.size()) {
    case 0: {
        // handle empty results
      }; break;
    case 1: {
        // handle single row
        Object[] row = (Object[])rows.get(0);
        int id = ((Integer)row[0]).intValue();
        String name = (String)row[1];
        <xsp:content>
          <single-row>
            <xsp:attribute name="id"><xsp:expr>id</xsp:expr></xsp:attribute>
            <xsp:attribute 
name="name"><xsp:expr>name</xsp:expr></xsp:attribute>
          </single-row>
        </xsp:content>
      }; break;
    default: {
        // handle multiple rows
        Iterator iter = rows.iterator();
        <multi-rows>
          while(iter.hasNext()) {
            Object[] row = (Object[])iter.next();
            int id = ((Integer)row[0]).intValue();
            String name = (String)row[1];
            <row>
              <xsp:attribute 
name="id"><xsp:expr>id</xsp:expr></xsp:attribute>
              <xsp:attribute 
name="name"><xsp:expr>name</xsp:expr></xsp:attribute>
            </single>
          }
        </multi-rows>
    };
</xsp:logic>
<displaimer>This code was never compiled or run</displaimer>

--
Ilya


Christian Haul wrote:

>On 28.Aug.2002 -- 10:48 AM, Tuomo Lesonen wrote:
>
>>
>>On Wed, 28 Aug 2002, Christian Haul wrote:
>>
>>>On 28.Aug.2002 -- 10:07 AM, Tuomo Lesonen wrote:
>>>
>>>>Hi,
>>>>
>>>>How can I count the rows in my resultset? I'm using esql, but
>>>><esql:row-count/> doesn't seem to be implemented yet. SQL COUNT(*) doesn't
>>>>work in this case either. Can I do this with XSP?
>>>>
>>>Yes, have a new var declared before the execute-query and ++ it in
>>>your row-results
>>>
>>Thank you Chris, but in this case I need to know the number of rows
>>_before_ I do <esql:row-results>, in order to produce the right data.
>>
>
>Tuomo, there is nothing in the JDBC API that provides this data that
>I'm aware of. There are certainly vendor specific procedures to get
>this information.
>
>However, most of the time the output needs to be limited, in order to
>page through the data. This is supported by esql.
>
>	Chris.
>



---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail:   <cocoon-users-help@xml.apache.org>


Mime
View raw message