[Zope3-checkins] CVS: Zope3/src/zope/app/content - sql.py:1.1.2.2

Fred L. Drake, Jr. fred@zope.com
Tue, 24 Dec 2002 02:52:33 -0500


Update of /cvs-repository/Zope3/src/zope/app/content
In directory cvs.zope.org:/tmp/cvs-serv26751

Modified Files:
      Tag: NameGeddon-branch
	sql.py 
Log Message:
reorganization


=== Zope3/src/zope/app/content/sql.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/app/content/sql.py:1.1.2.1	Mon Dec 23 14:31:29 2002
+++ Zope3/src/zope/app/content/sql.py	Tue Dec 24 02:52:32 2002
@@ -2,29 +2,184 @@
 #
 # Copyright (c) 2002 Zope Corporation and Contributors.
 # All Rights Reserved.
-# 
+#
 # This software is subject to the provisions of the Zope Public License,
 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
-# 
+#
 ##############################################################################
-"""
+"""\
+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.
+
+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. It can
+    have any number of 'and' and 'or' continuation tags.
+
+    The 'sqlgroup' tag has an optional attribure, 'required' to
+    specify groups that must include at least one test.  This is
+    useful when you want to make sure that a query is qualified, but
+    want to be very flexible about how it is qualified.
+
+    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::
+
+      <dtml-sqlgroup required>
+        <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.
+
+Inserting values with the 'sqlvar' tag
+
+    The 'sqlvar' tag is used to type-safely insert values into SQL
+    text.  The 'sqlvar' tag is similar to the 'var' tag, except that
+    it replaces text formatting parameters with SQL type information.
+
+    The sqlvar tag has the following attributes:
+
+      name -- The name of the variable to insert. As with other
+              DTML tags, the 'name=' prefix may be, and usually is,
+              ommitted.
+
+      type -- The data type of the value to be inserted.  This
+              attribute is required and may be one of 'string',
+              'int', 'float', or 'nb'.  The 'nb' data type indicates a
+              string that must have a length that is greater than 0.
+
+      optional -- A flag indicating that a value is optional.  If a
+                  value is optional and is not provided (or is blank
+                  when a non-blank value is expected), then the string
+                  'null' is inserted.
+
+    For example, given the tag::
+
+      <dtml-sqlvar x type=nb optional>
+
+    if the value of 'x' is::
+
+      Let\'s do it
+
+    then the text inserted is:
+
+      'Let''s do it'
+
+    however, if x is ommitted or an empty string, then the value
+    inserted is 'null'.
 
 $Id$
 """
 
 import re
+import sys
+
+from time import time
+from types import StringTypes
+
+from persistence import Persistent
 from persistence.dict import PersistentDict
+
+from zope.documenttemplate.dt_html import HTML
+from zope.documenttemplate.dt_util import ParseError, parse_params, name_param
 from zope.interface.common.mapping import IEnumerableMapping
 
+from zope.component import getService
+from zope.proxy.context import ContextMethod
+from zope.proxy.introspection import removeAllProxies
+
+from zope.app.cache.caching import getCacheForObj, getLocationForCache
+from zope.app.interfaces.content.file import IFileContent
+from zope.app.interfaces.content.sql import ISQLScript
+from zope.app.interfaces.annotation import IAttributeAnnotatable
+from zope.app.rdb import SQLCommand
+from zope.app.rdb import queryForResults
+from zope.app.traversing import getParent
+
+
+
 unparmre = re.compile(r'([\000- ]*([^\000- ="]+))')
 parmre = re.compile(r'([\000- ]*([^\000- ="]+)=([^\000- ="]+))')
 qparmre = re.compile(r'([\000- ]*([^\000- ="]+)="([^"]*)")')
 
-InvalidParameter = 'Invalid Parameter'
+
+class InvalidParameter(Exception):
+    pass
+
 
 class Arguments(PersistentDict):
     """Hold arguments of SQL Script"""
@@ -51,7 +206,7 @@
         length  = len(match_object.group(1))
 
     else:
-        # search for an argument having a quoted default value 
+        # search for an argument having a quoted default value
         match_object = qparmre.match(text)
 
         if match_object:
@@ -87,58 +242,7 @@
     return parseArguments(text[length:], result)
 
 
-"""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.
-
-$Id$
-"""
-import sys
-from zope.documenttemplate.dt_util import ParseError, parse_params, name_param
-from types import ListType, TupleType, StringTypes, StringType
-
-
-class SQLTest: 
+class SQLTest:
     name = 'sqltest'
     optional = multiple = None
 
@@ -186,20 +290,20 @@
             if key[0] == name and self.optional:
                 return ''
             raise KeyError, key, sys.exc_info()[2]
-            
-        if isinstance(v, (ListType, TupleType)):
+
+        if isinstance(v, (list, tuple)):
             if len(v) > 1 and not self.multiple:
                 raise 'Multiple Values', (
                     'multiple values are not allowed for <em>%s</em>'
                     % name)
         else:
             v = [v]
-        
+
         vs = []
         for v in v:
             if not v and isinstance(v, StringTypes) and t != 'string':
                 continue
-            # XXX Ahh, the code from DT_SQLVar is duplicated here!!! 
+            # XXX Ahh, the code from DT_SQLVar is duplicated here!!!
             if t == 'int':
                 try:
                     if isinstance(v, StringTypes):
@@ -211,7 +315,7 @@
                         'Invalid integer value for **%s**' % name)
 
             elif t == 'float':
-                if not v and type(v) is StringType:
+                if not v and isinstance(v, str):
                     continue
                 try:
                     if isinstance(v, StringTypes):
@@ -224,7 +328,7 @@
             else:
                 v = str(v)
                 v = self.sql_quote__(v)
-    
+
             vs.append(v)
 
         if not vs:
@@ -248,34 +352,6 @@
                          'gt': '>', 'ge': '>=', 'gte': '>=' }
 
 
-"""
-$Id$
-"""
-from types import StringTypes
-
-from persistence import Persistent
-from zope.component import getService
-from zope.proxy.context import ContextMethod
-from zope.proxy.introspection import removeAllProxies
-
-from zope.documenttemplate.dt_html import HTML
-from Zope.App.Traversing import getParent
-from zope.app.rdb import SQLCommand
-from zope.app.rdb import queryForResults
-from zope.app.interfaces.content.file import IFileContent
-from zope.app.interfaces.content.sql import ISQLScript
-
-from zope.app.interfaces.annotation import IAttributeAnnotatable
-
-from zope.app.cache.caching import getCacheForObj, getLocationForCache
-
-
-
-
-
-from time import time
-
-
 class SQLDTML(HTML):
     __name__ = 'SQLDTML'
 
@@ -410,60 +486,6 @@
                               "Connection Name for the SQL scripts.")
 
 
-
-"""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. It can
-    have any number of 'and' and 'or' continuation tags.
-
-    The 'sqlgroup' tag has an optional attribure, 'required' to
-    specify groups that must include at least one test.  This is
-    useful when you want to make sure that a query is qualified, but
-    want to be very flexible about how it is qualified. 
-
-    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::
-
-      <dtml-sqlgroup required>
-        <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.
-
-$Id$
-"""
-from zope.documenttemplate.dt_util import parse_params
-
 class SQLGroup:
     blockContinuations = 'and', 'or'
     name = 'sqlgroup'
@@ -492,7 +514,7 @@
                 if result:
                     result.append(tname)
                 result.append("%s\n" % s)
-                
+
         if result:
             if len(result) > 1:
                 result = "(%s)\n" %(' '.join(result))
@@ -510,50 +532,7 @@
     __call__ = render
 
 
-"""Inserting values with the 'sqlvar' tag
-
-    The 'sqlvar' tag is used to type-safely insert values into SQL
-    text.  The 'sqlvar' tag is similar to the 'var' tag, except that
-    it replaces text formatting parameters with SQL type information.
-
-    The sqlvar tag has the following attributes:
-
-      name -- The name of the variable to insert. As with other
-              DTML tags, the 'name=' prefix may be, and usually is,
-              ommitted.
-
-      type -- The data type of the value to be inserted.  This
-              attribute is required and may be one of 'string',
-              'int', 'float', or 'nb'.  The 'nb' data type indicates a
-              string that must have a length that is greater than 0.
-
-      optional -- A flag indicating that a value is optional.  If a
-                  value is optional and is not provided (or is blank
-                  when a non-blank value is expected), then the string
-                  'null' is inserted.
-
-    For example, given the tag::
-
-      <dtml-sqlvar x type=nb optional>
-
-    if the value of 'x' is::
- 
-      Let\'s do it
-
-    then the text inserted is:
-
-      'Let''s do it'
-
-    however, if x is ommitted or an empty string, then the value
-    inserted is 'null'.
-
-$Id$
-"""
-from zope.documenttemplate.dt_util import ParseError, parse_params, name_param
-from types import StringTypes
-
-
-class SQLVar: 
+class SQLVar:
     name = 'sqlvar'
 
     # Some defaults
@@ -637,7 +616,7 @@
                 else:
                     raise ValueError, (
                         'Invalid empty string value for **%s**' % name)
-            
+
             v = self.sql_quote__(v)
 
         return v