[Zope-DB] How can I dynamically generate a Z Search Interface?

Jim Penny jpenny@universal-fasteners.com
Mon, 9 Sep 2002 16:58:23 -0400


On Mon, Sep 09, 2002 at 09:45:30PM +0200, Luis Gregorio Muniz Rodriguez wrote:
> On Fri, Aug 30, 2002 at 12:00:03PM -0400, zope-db-request@zope.org wrote:
> > From: "Tena Sakai" <tena.sakai.b@bayer.com>

> 
> a) If you only need to "select", then simply put:
> 	select * from mytable
> 
>    And, in the DMTL or ZPT code that iterates through the results of 
>    the search, choose what fields to show.  It should be easier and
>    safer than the next method.  And if you have been using a form
>    to select which data to retrieve, you have half the work done.
> 
> 
>    Or, if the attributes you are going to supply are muthual exclusive,
>    then you can sort _always_:
> 
> 	   <dtml-sqlgroup where>
> 	     <dtml-sqltest research type=nb optional> ORDER BY (research)
> 	   </dtml-sqlgroup>
> 	   <dtml-sqlgroup where>
> 	     <dtml-sqltest login type=nb optional> ORDER BY (login)
> 	   </dtml-sqlgroup>

Note:  not only can you use plain dtml-var, you can also use dtml-if
(or any other dtml-construct).  A slight generalization could also be 
rendered as:

    select * from mytable
    <dtml-if "research and login">
      where research=<dtml-sqlvar research type=string> and 
            login=<dtml-sqlvar login type=string> order by (research, login)
    <dtml-elif research>
      where research=<dtml-sqlvar research type=string> order by (research)
    <dtml-else>
      where login=<dtml-sqlvar login type=string> order by (login)
    </dtml-if>

This gets rid of the mutually exclusive condition.
I have no real objection to this kind of usage, and do it myself.  The
key is that, as long as the method remains simple enough that you can
explain what it does with cursory inspection, you are fine.  Typically
you will define four to six methods per table; one for select, 
one for update, one for insert, and one for delete.  Sometimes you need
a couple selects, or a couple of updates.

A more questionable usage (which I have also used) is something like
  insert into my_table values
  (
  <dtml-if research>
    research,
  <dtml-else>
    login
  </dtml-if>
  )
  values
  (
  <dtml-if research>
    <dtml-sqlvar research type=string>,
  <dtml-else>
    <dtml-sqlvar login type=string>
  </dtml-if>
  )

This is objectionable mainly because SQL has horrible syntax.  Updates
work much better with this format.

Jim Penny

Jim Penny

>    
> 
> Finally, I also agree that it is best to have a couple small, fixed 
> Z SQL methods than using a more complex solution, like the generic
> "list" method.  However, as the number of small methods increase I've 
> found this dynamic solution more elegant and less confusing.
> 
> HTH,
> --
> 	- kreator@lmunix.net
> 
> _______________________________________________
> Zope-DB mailing list
> Zope-DB@zope.org
> http://lists.zope.org/mailman/listinfo/zope-db
>