This is the problem with the Criteria.or/and methods:

When I say:

criteria.or(Peer.COL1,1);
criteria.or(Peer.COL2,2);

I expect:

WHERE COL1 = 1 OR COL2 = 2

But torque generates:

WHERE COL1 = 1 AND COL2 = 2

I'm supposing this because the "right way" to construct criteria is using
criterion and the "and/or" methods on the Criteria object are for
convenience.  

The javadocs for Criteria.or() touch on this issue:

"This method adds a new criterion to the list of criterias. If a criterion
for the requested column already exists, it is OR'ed to the existing
criterion."

Nevertheless, the javadocs are not explicit as to the effects of OR'ing two
different columns.

This is non-intuitive and *time-consuming* to debug the first time you run
across it.  The javadocs should amplify this issue or perhaps the
Criteria.or() methods should be deprecated entirely. 

Ryan

-----Original Message-----
From: Rafal Maczewski [mailto:[EMAIL PROTECTED]] 
Sent: Monday, October 07, 2002 2:47 PM
To: Turbine Torque Users List
Subject: Re: How to construct a WHERE clause using OR instead of AND between
Criteria?

Please try:

     Criteria crit = new Criteria();
     Criteria.Criterion a1 = crit.getNewCriterion( SchoolsPeer.NAME,
(Object) (search), Criteria.EQUAL);
     Criteria.Criterion a2 = crit.getNewCriterion(
SchoolsPeer.SCHOOL_NUMBER, (Object) search, Criteria.EQUAL);
     crit.add( a1.or( a2 ));

Rafal



> Yes, I believe so. I'm using this:
>
>         Criteria crit = new Criteria();
>         crit.add(SchoolsPeer.NAME, (Object) (search), Criteria.EQUAL);
>         crit.or(SchoolsPeer.SCHOOL_NUMBER, (Object) search,
Criteria.EQUAL);
>
> But when the Query class is used to generate the output, it uses " AND "
as
> the delimiter between criteria. Is there another way to construct these
> Criteria?
>
> > -----Original Message-----
> > From: Howard Lin [mailto:[EMAIL PROTECTED]]
> > Sent: Monday, October 07, 2002 3:26 PM
> > To: Turbine Torque Users List
> > Subject: RE: How to construct a WHERE clause using OR instead of AND
> > between Criteria?
> >
> >
> > Have you tried Criteria.or(...) method?
> >
> > Howard Lin
> >
> > > -----Original Message-----
> > > From: Daniel Seltzer [mailto:[EMAIL PROTECTED]]
> > > Sent: Monday, October 07, 2002 3:09 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: How to construct a WHERE clause using OR instead of
> > > AND between
> > > Criteria?
> > >
> > >
> > > I'm running 3.0-b4 and have a question about how to control
> > > the construction
> > > of the WHERE clause from Criteria. I've reviewed the docs,
> > > the archives and
> > > the source but suspect I'm missing something obvious...
> > >
> > > I'm trying to construct a simple query that looks like this:
> > >
> > >     select * from table1 where col1 = 'foo' or col2 = 'foo'
> > >
> > > But all expressions in a WHERE clause appear to be joined with AND by
> > > default, regardless of how the Criteria are added. In the source for
> > > org.apache.torque.util.Query, line 253, it looks like AND is
> > > being passed to
> > > the StringStack's toString() method as the delimiter for output:
> > >
> > >         if ( !whereCriteria.empty() )
> > >         {
> > >             stmt.append(WHERE)
> > >                 .append(whereCriteria.toString( AND ));
> > >         }
> > >
> > > Is there some other way to indicate that multiple criteria in
> > > a Query should
> > > be OR'd instead of AND'd?
> > >
> > > Thanks,
> > > Daniel
> > >
> > > ________________________________________
> > >
> > > Daniel Seltzer
> > > [EMAIL PROTECTED]
> > > www.h2co3.com
> > >
> > >
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
> <mailto:[EMAIL PROTECTED]>
>
>
>
> --
> To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to