It is sometimes useful to make inputs to an SQL statement optional. Doing so can be difficult, because not only must the test be inserted conditionally, but SQL boolean operators may or may not be needed 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. It can have any number of and and or continuation tags. The attributes of the sqlgroup tag are shown in table See Attributes of the sqlgroup tag.
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.
Suppose we want to find people with a given first or nick name, city or minimum and maximum age. Suppose we want all inputs to be optional, but want to require some input. We can use DTML source like the following:
<!--#sqlgroup required where-->
<!--#sqltest name column=nick_name type=nb multiple optional-->
<!--#sqltest name column=first_name type=nb multiple optional-->
<!--#sqltest home_town type=nb optional-->
age >= <!--#sqlvar minimum_age type=int-->
age <= <!--#sqlvar maximum_age type=int-->
If we evaluated this template with values set for home town and name, we would get an SQL query like the following:
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.
Inserting equality comparisons with sqltest 28
Inserting optional tests with sqlgroup 28
1. A number of Zope-based products provide searchable objects. Other searchable objects that are available for Zope include Tabula Collections, Confera Topics, and Zope Network Clients.
2.
Parameters can be passed explicitly using an
expr
attribute in the
in
tag. Parameters are passed using Python "keyword" parameter syntax. For example, to call the lookup_product method with a part_number of `2', the following
in
tag would be used:
<!--#in expr="lookup_product(part_number='2')"
size=50 start=query_start-->
3. See See Batch Processing in the Document Template Markup Language User's Guide.
6. Python is a very high-level object-oriented programming language. For more information on Python, visit http://www.python.org .
7. For security reasons, the Python file cannot be uploaded via the Web. You must have access to the file system of the server that is running Zope.
Previous Chapter | Up | Contents