Table of Contents

Module: sqltest Zope-2.2.1-src/lib/python/Shared/DC/ZRDB/sqltest.py

Inserting optional tests with sqlgroup

It is sometimes useful to make inputs to an SQL statement optinal. Doing so can be difficult, because not only must the test be inserted conditionally, but SQL boolean operators may or may not need to be inserted depending on whether other, possibly optional, comparisons have been done. The sqlgroup tag automates the conditional insertion of boolean operators.

The sqlgroup tag is a block tag that has no attributes. It can have any number of and and or continuation tags.

Suppose we want to find all people with a given first or nick name and optionally constrain the search by city and minimum and maximum age. Suppose we want all inputs to be optional. We can use DTML source like the following:

      <dtml-sqlgroup>
        <dtml-sqlgroup>
          <dtml-sqltest name column=nick_name type=nb multiple optional>
        <dtml-or>
          <dtml-sqltest name column=first_name type=nb multiple optional>
        </dtml-sqlgroup>
      <dtml-and>
        <dtml-sqltest home_town type=nb optional>
      <dtml-and>
        <dtml-if minimum_age>
           age >= <dtml-sqlvar minimum_age type=int>
        </dtml-if>
      <dtml-and>
        <dtml-if maximum_age>
           age <= <dtml-sqlvar maximum_age type=int>
        </dtml-if>
      </dtml-sqlgroup>

This example illustrates how groups can be nested to control boolean evaluation order. It also illustrates that the grouping facility can also be used with other DTML tags like if tags.

The sqlgroup tag checks to see if text to be inserted contains other than whitespace characters. If it does, then it is inserted with the appropriate boolean operator, as indicated by use of an and or or tag, otherwise, no text is inserted.

Imported modules   
from DocumentTemplate.DT_Util import ParseError, parse_params, name_param
from string import find, split, join, atoi, atof
import sys
from types import ListType, TupleType, StringType
Classes   
SQLTest

Table of Contents

This document was automatically generated on Mon Sep 4 07:33:06 2000 by HappyDoc version r0_6