[Zope] general queries

Thomas Weiner gertz@tu-harburg.de
Wed, 21 Jul 1999 13:16:06 +0200


Marco Guidi schrieb:

[...]

> 2. Depending on how the user fills the form, the where clause may
>    require some different operators (>, <, and or, and so on). is there
>    a way to generalize this (may be the creation of a dictionary
>    in the REQUEST with the where operators needed by the specific form ?).

Well .. I did this using properties for the SQL operators.

In my search folder I defined some token and text properties:

search_fields a list containing the names of the fields of my SQL table
I want to let show up in the search form (all are string - fields).

Critops a list with the operators: like like like = < >

Critbez a list with translations for the operators for showing up in a
select box in the search form:
contains begins_with ends_with is_eqal less_then greater_then

start_op a _text_-property containing wildcards for the sql method (note
that the empty lines are necessary):
%

%



end_op the same as start_op but a little different ;):
%
%




the search_form is a DTML Method where I define a select box for the
fieldnames <!--#in search_fields-->, a second select box with critbez,
an input field for text and an other select box with NONE / AND / OR
values for further search - requests (I put this in a loop collecting
the values in lists)

the SQL - method looks this way:

Arguments: Crit1_Fieldname:list Crit1_search:int:list
Crit1_FieldValue:list Crit1_and_or:list 

select *
from adr
<!--#if Crit1_Fieldname-->
where
                 
 <!--#in "loop_range(max=5)"-->
  <!--#unless "Crit1_Fieldname[_['sequence-index']]=='NONE'"-->
     <!--#var "Crit1_Fieldname[_['sequence-index']]"--> 
     <!--#var "Critops[(Crit1_search[_['sequence-index']])]"--> 
     '<!--#var"start_op[(Crit1_search[_['sequence-index']])]"--><!--#var
"Crit1_FieldValue[_['sequence-index']]"--><!--#var"end_op[(Crit1_search[_['sequence-index']])]"-->'
     <!--#if "Crit1_and_or[_['sequence-index']]<>'NONE'"-->
       <!--#var "Crit1_and_or[_['sequence-index']]"-->
     <!--#/if "Crit1_and_or[_['sequence-index']]<>'NONE'"--> 
  <!--#/unless "Crit1_Fieldname[_['sequence-index']]=='NONE'"-->
 <!--#/in "loop_range(max=5)"-->  
<!--#/if Crit1_Fieldname--> 

loop_range is an external method for a simple loop.

Note that the code in
'<!--#var"start_op[(Crit1_search[_['sequence-index']])]"-->...
<!--#var"end_op[(Crit1_search[_['sequence-index']])]"-->' must be
written in one single line without any blanks in it.

I don't know whether this is really elegant, but for me it works fine.

thomas
-- 
fon: ++49 (0)40 42878 3781
fax: ++49 (0)40 42878 2728