Commit ca51120c authored by Jim Fulton's avatar Jim Fulton

added new sql tags

parent abb0f0e0
'''Inserting equality comparisons with 'sqltest'
The 'sqltest' tag is used to insert SQL source to test whether
an SQL column is equal to a value given in a DTML variable.
The 'sqltest' 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.
column -- The name of the SQL column, if different than 'name'.
multiple -- A flag indicating whether multiple values may be
provided.
optional -- A flag indicating if the test is optional.
If the test is optinal and no value is provided for
a variable, or the value provided is an invalid
empty string, then no text is inserted.
For example, given the tag::
<!--#sqltest color column=color_name type=nb multiple-->
If the value of the color variable is '"red"', then the following
test is inserted::
column_name = 'red'
If a list of values is givem, such as: '"red"', '"pink"', and
'"purple"', then an SQL 'in' test is inserted:
column_name = in ('red', 'pink', 'purple')
'''
############################################################################
# Copyright
#
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved.
#
############################################################################
__rcs_id__='$Id: sqlgroup.py,v 1.1 1998/03/17 19:31:22 jim Exp $'
__version__='$Revision: 1.1 $'[11:-2]
from DocumentTemplate.DT_Util import *
from string import strip, join
import sys
class SQLGroup:
blockContinuations='and','or'
name='sqlgroup'
def __init__(self, blocks):
self.blocks=blocks
tname, args, section = blocks[0]
self.__name__=args or tname
def render(self,md):
r=[]
for tname, args, section in self.blocks:
__traceback_info__=tname
s=strip(section(None, md))
if s:
if r: r.append(tname)
r.append("%s\n" % s)
if r:
if len(r) > 1: return "(%s)\n" % join(r,' ')
return r[0]
return ''
__call__=render
##########################################################################
#
# $Log: sqlgroup.py,v $
# Revision 1.1 1998/03/17 19:31:22 jim
# added new sql tags
#
#
#!/usr/local/bin/python
# $What$
'''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::
<!--#sqlgroup-->
<!--#sqlgroup-->
<!--#sqltest name column=nick_name type=nb multiple optional-->
<!--#or-->
<!--#sqltest name column=first_name type=nb multiple optional-->
<!--#/sqlgroup-->
<!--#and-->
<!--#sqltest home_town type=nb optional-->
<!--#and-->
<!--#if minimum_age-->
age >= <!--#sqlvar minimum_age type=int-->
<!--#/if-->
<!--#and-->
<!--#if maximum_age-->
age <= <!--#sqlvar maximum_age type=int-->
<!--#/if-->
<!--#/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.
'''
__rcs_id__='$Id: sqltest.py,v 1.1 1998/03/17 19:31:22 jim Exp $'
############################################################################
# Copyright
#
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved.
#
############################################################################
__version__='$Revision: 1.1 $'[11:-2]
from DocumentTemplate.DT_Util import *
from string import find, split, join, atoi, atof
from types import ListType, TupleType, StringType
class SQLTest:
name='sqltest'
optional=multiple=None
def __init__(self, args):
args = parse_params(args, name='', type=None, column=None,
multiple=1, optional=1)
self.__name__ = name_param(args,'sqlvar')
has_key=args.has_key
if not has_key('type'):
raise ParseError, ('the type attribute is required', 'dtvar')
self.type=t=args['type']
if not valid_type(t):
raise ParseError, ('invalid type, %s' % t, 'dtvar')
if has_key('optional'): self.optional=args['optional']
if has_key('multiple'): self.multiple=args['multiple']
if has_key('column'): self.column=args['column']
else: self.column=self.__name__
def render(self, md):
name=self.__name__
t=self.type
v = md[name]
if type(v) in (ListType, TupleType):
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 type(v) is StringType and t != 'string': continue
if t=='int':
try:
if type(v) is StringType: atoi(v)
else: v=str(int(v))
except:
raise ValueError, (
'Invalid integer value for <em>%s</em>' % name)
elif t=='float':
if not v and type(v) is StringType: continue
try:
if type(v) is StringType: atof(v)
else: v=str(float(v))
except:
raise ValueError, (
'Invalid floating-point value for <em>%s</em>' % name)
else:
v=str(v)
if find(v,"\'") >= 0: v=join(split(v,"\'"),"''")
v="'%s'" % v
vs.append(v)
if not vs:
if self.optional: return ''
raise 'Missing Input', (
'No input was provided for <em>%s</em>' % name)
if len(vs) > 1: return "%s in (%s)" % (self.column,`vs`[1:-1])
return "%s=%s" % (self.column,vs[0])
__call__=render
valid_type={'int':1, 'float':1, 'string':1, 'nb': 1}.has_key
############################################################################
# $Log: sqltest.py,v $
# Revision 1.1 1998/03/17 19:31:22 jim
# added new sql tags
#
#
#!/usr/local/bin/python
# $What$
'''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::
<!--#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'.
'''
__rcs_id__='$Id: sqlvar.py,v 1.1 1998/03/17 19:31:22 jim Exp $'
############################################################################
# Copyright
#
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne
# Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
# rights reserved.
#
############################################################################
__version__='$Revision: 1.1 $'[11:-2]
from DocumentTemplate.DT_Util import *
from string import find, split, join, atoi, atof
StringType=type('')
class SQLVar:
name='sqlvar'
def __init__(self, args):
args = parse_params(args, name='', type=None, optional=1)
self.__name__ = name_param(args,'sqlvar')
self.args=args
if not args.has_key('type'):
raise ParseError, ('the type attribute is required', 'dtvar')
t=args['type']
if not valid_type(t):
raise ParseError, ('invalid type, %s' % t, 'dtvar')
def render(self, md):
name=self.__name__
args=self.args
t=args['type']
try: v = md[name]
except:
if args.has_key('optional') and args['optional']: return 'null'
raise 'Missing Input', 'Missing input variable, <em>%s</em>' % name
if t=='int':
try:
if type(v) is StringType: atoi(v)
else: v=str(int(v))
except:
if not v and args.has_key('optional') and args['optional']:
return 'null'
raise ValueError, (
'Invalid integer value for <em>%s</em>' % name)
elif t=='float':
try:
if type(v) is StringType: atof(v)
else: v=str(float(v))
except:
if not v and args.has_key('optional') and args['optional']:
return 'null'
raise ValueError, (
'Invalid floating-point value for <em>%s</em>' % name)
else:
v=str(v)
if not v and t=='nb':
raise ValueError, (
'Invalid empty string value for <em>%s</em>' % name)
if find(v,"\'") >= 0: v=join(split(v,"\'"),"''")
v="'%s'" % v
return v
__call__=render
valid_type={'int':1, 'float':1, 'string':1, 'nb': 1}.has_key
############################################################################
# $Log: sqlvar.py,v $
# Revision 1.1 1998/03/17 19:31:22 jim
# added new sql tags
#
# Revision 1.9 1998/01/12 16:47:34 jim
# Changed a number of custom formats to modifiers, since they can
# be applies cumulatively.
# Updated documentation.
#
# Revision 1.8 1998/01/08 20:57:34 jim
# *** empty log message ***
#
# Revision 1.7 1998/01/05 21:23:01 jim
# Added support for fmt="" to allow vars with side effects.
#
# Revision 1.6 1997/12/12 16:19:06 jim
# Added additional special formats, structured-text and sql-quote.
# Also changed the way formats are handled. This has (and will)
# reveal now hidden fmt=invalid-thing errors.
#
# Revision 1.5 1997/10/27 17:39:27 jim
# removed a comment burp.
#
# Revision 1.4 1997/10/23 14:27:47 jim
# Added truncation support via size and etc attributes.
#
# Revision 1.3 1997/10/23 13:30:16 jim
# Added comma-numeric format.
#
# Revision 1.2 1997/09/22 14:42:51 jim
# added expr
#
# Revision 1.1 1997/08/27 18:55:44 jim
# initial
#
#
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment