ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Larry Meadors <larry.mead...@gmail.com>
Subject Re: dynamic query template?
Date Thu, 23 Jun 2005 11:41:55 GMT
Why not just do this:

<sql id="myNiftySql">
     FROM user_tbl [...] WHERE category="admin" AND ...
</sql>

<select id="countUsers" parameterClass="java.lang.String">
    SELECT COUNT(*)
    <include id="myNiftySql">
</select>

<select id="getUsers" parameterClass="java.lang.String">
    SELECT *
    <include id="myNiftySql">
</select>

Larry


On 6/23/05, Fabien Le Floc'h <logos01@gmail.com> wrote:
> We could imagine a query template:
>  
>  <template id="getUsersTemplate">
>  SELECT $templateProperty$ FROM user_tbl [...] WHERE category="admin" AND
> ...
>  </select>
>  
>  <select id="countUsers">
>  <define property="templateProperty">COUNT(*)</define>
>  <include template="getUsersTemplate"/>
>  </select>
>  
>  <select id="getUsers">
>  <define property="templateProperty">*</define>
>  <include template="getUsersTemplate"/>
>  </select>
> 
>  It is not that much better, but it avoids to have a query that has 2
> meanings.
>  
>  Fabien.
> 
>  
> On 6/23/05, Brandon Goodin <brandon.goodin@gmail.com> wrote:
> > I'm curious what _would_ be considered elegant? Anyway, the solution
> > you provided should be fine. Be sure to use remapResults="true". The
> > other route is simply to duplicate the two queries so that they
> > present cleaner and you don't need ot place a count boolean into your
> > parameter object.
> > 
> > Brandon
> > 
> > On 6/22/05, Fabien Le Floc'h <logos01@gmail.com> wrote:
> > > Hi, 
> > >
> > >  I was wondering what would be the best way to write 2 queries that
> share
> > > almost everything with iBatis.
> > >
> > >  For example if I want to do
> > >  SELECT * FROM user_tbl [...] WHERE category="admin" AND ... LIMIT 50 
> > >  SELECT COUNT(*) FROM user_tbl [...] WHERE category="admin" AND ...
> > >
> > >  ideally one would have 2 names, because it is a quite different result,
> but
> > > you want to perform both queries on the same set. 
> > >
> > >  The way I found is to use a dynamic query with a parameter that will
> act as
> > > a switch.
> > >  <select id="getUsers" parameterClass="java.lang.String">
> > >      SELECT 
> > >      <isEqual property="value" compareValue="count">
> > >           COUNT(*)
> > >      </isEqual>
> > >      <isNotEqual property="value" compareValue="count"> 
> > >           *
> > >      </isNotEqual>
> > >       FROM user_tbl [...] WHERE category="admin" AND ...
> > >  </select>
> > >
> > >  As you can see, this is not very elegant. I could generate the xml, but
> > > should this kind of case be handled by iBatis?
> > >
> > >  The paginate queryForPaginatedList does not seem to support the total
> > > number of items available.
> > >
> > >  So if you have a more elegant solution I'll be very happy to hear from
> you. 
> > >
> > >  Thanks for the great work on ibatis,
> > >
> > >  Fabien.
> > >
> > 
> 
>

Mime
View raw message