[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/SQLScript - Arguments.py:1.1 DT_SQLGroup.py:1.1 DT_SQLTest.py:1.1 DT_SQLVar.py:1.1 ISQLScript.py:1.1 SQLScript.py:1.1 __init__.py:1.1 configure.zcml:1.1
Stephan Richter
srichter@cbu.edu
Wed, 10 Jul 2002 20:03:19 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/SQLScript
In directory cvs.zope.org:/tmp/cvs-serv9815/SQLScript
Added Files:
Arguments.py DT_SQLGroup.py DT_SQLTest.py DT_SQLVar.py
ISQLScript.py SQLScript.py __init__.py configure.zcml
Log Message:
SQL Scripts that function almost exactely like Zope 2 SQL methods.
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/Arguments.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
$Id: Arguments.py,v 1.1 2002/07/11 00:03:18 srichter Exp $
"""
import re
from Persistence import PersistentMapping
from Interface.Common.Mapping import IEnumerableMapping
unparmre = re.compile(r'([\000- ]*([^\000- ="]+))')
parmre = re.compile(r'([\000- ]*([^\000- ="]+)=([^\000- ="]+))')
qparmre = re.compile(r'([\000- ]*([^\000- ="]+)="([^"]*)")')
InvalidParameter = 'Invalid Parameter'
class Arguments(PersistentMapping):
"""Hold arguments of SQL Script"""
__implements__ = IEnumerableMapping
def parseArguments(text, result=None):
"""Parse argument string."""
# Make some initializations
if result is None:
result = {}
__traceback_info__ = text
# search for the first argument assuming a default value (unquoted) was
# given
match_object = parmre.match(text)
if match_object:
name = match_object.group(2)
value = {'default': match_object.group(3)}
length = len(match_object.group(1))
else:
# search for an argument having a quoted default value
match_object = qparmre.match(text)
if match_object:
name = match_object.group(2)
value = {'default': match_object.group(3)}
length = len(match_object.group(1))
else:
# search for an argument without a default value
match_object = unparmre.match(text)
if match_object:
name = match_object.group(2)
value = {}
length = len(match_object.group(1))
else:
# We are done parsing
if not text or not text.strip():
return Arguments(result)
raise InvalidParameter, text
# Find type of argument (int, float, string, ...)
lt = name.find(':')
if lt > 0:
if len(name) > lt+1 and name[lt+1] not in ('"', "'", '='):
value['type'] = name[lt+1:]
name = name[:lt]
else:
raise InvalidParameter, text
result[name] = value
return parseArguments(text[length:], result)
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/DT_SQLGroup.py ===
##############################################################################
#
# 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. 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: DT_SQLGroup.py,v 1.1 2002/07/11 00:03:18 srichter Exp $
"""
from Zope.DocumentTemplate.DT_Util import parse_params
class SQLGroup:
blockContinuations = 'and', 'or'
name = 'sqlgroup'
required = None
where = None
def __init__(self, blocks):
self.blocks = blocks
tname, args, section = blocks[0]
self.__name__ = "%s %s" % (tname, args)
args = parse_params(args, required=1, where=1)
if args.has_key(''):
args[args['']] = 1
if args.has_key('required'):
self.required = args['required']
if args.has_key('where'):
self.where = args['where']
def render(self, md):
result = []
for tname, args, section in self.blocks:
__traceback_info__ = tname
s = section(None, md).strip()
if s:
if result:
result.append(tname)
result.append("%s\n" % s)
if result:
if len(result) > 1:
result = "(%s)\n" %(' '.join(result))
else:
result = result[0]
if self.where:
result = "where\n" + result
return result
if self.required:
raise 'Input Error', 'Not enough input was provided!'
return ''
__call__ = render
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/DT_SQLTest.py ===
##############################################################################
#
# 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.
$Id: DT_SQLTest.py,v 1.1 2002/07/11 00:03:18 srichter Exp $
"""
import sys
from Zope.DocumentTemplate.DT_Util import ParseError, parse_params, name_param
from types import ListType, TupleType, StringTypes
class SQLTest:
name = 'sqltest'
optional = multiple = None
# Some defaults
sql_delimiter = '\0'
def sql_quote__(self, v):
if v.find("\'") >= 0:
v = "''".join(v.split("\'"))
return "'%s'" %v
def __init__(self, args):
args = parse_params(args, name='', type=None, column=None,
multiple=1, optional=1, op=None)
self.__name__ = name_param(args, 'sqlvar')
has_key=args.has_key
if not has_key('type'):
raise ParseError, ('the type attribute is required', 'sqltest')
self.type = t = args['type']
if not valid_type(t):
raise ParseError, ('invalid type, %s' % t, 'sqltest')
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__
# Deal with optional operator specification
op = '=' # Default
if has_key('op'):
op = args['op']
# Try to get it from the chart, otherwise use the one provided
op = comparison_operators.get(op, op)
self.op = op
def render(self, md):
name = self.__name__
t = self.type
try:
v = md[name]
except KeyError, key:
if str(key) == name and self.optional:
return ''
raise KeyError, key, sys.exc_info()[2]
if isinstance(v, (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 isinstance(v, StringTypes) and t != 'string':
continue
# XXX Ahh, the code from DT_SQLVar is duplicated here!!!
if t == 'int':
try:
if isinstance(v, StringTypes):
int(v)
else:
v = str(int(v))
except:
raise ValueError, (
'Invalid integer value for **%s**' % name)
elif t == 'float':
if not v and type(v) is StringType:
continue
try:
if isinstance(v, StringTypes):
float(v)
else:
v = str(float(v))
except:
raise ValueError, (
'Invalid floating-point value for **%s**' % name)
else:
v = str(v)
v = self.sql_quote__(v)
vs.append(v)
if not vs:
if self.optional:
return ''
raise 'Missing Input', (
'No input was provided for **%s**' % name)
if len(vs) > 1:
vs = ', '.join(map(str, vs))
return "%s in (%s)" % (self.column,vs)
return "%s %s %s" % (self.column, self.op, vs[0])
__call__ = render
valid_type = {'int':1, 'float':1, 'string':1, 'nb': 1}.has_key
# SQL compliant comparison operators
comparison_operators = { 'eq': '=', 'ne': '<>',
'lt': '<', 'le': '<=', 'lte': '<=',
'gt': '>', 'ge': '>=', 'gte': '>=' }
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/DT_SQLVar.py ===
##############################################################################
#
# 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 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: DT_SQLVar.py,v 1.1 2002/07/11 00:03:18 srichter Exp $
"""
from Zope.DocumentTemplate.DT_Util import ParseError, parse_params, name_param
from types import StringTypes
class SQLVar:
name = 'sqlvar'
# Some defaults
sql_delimiter = '\0'
def sql_quote__(self, v):
if v.find("\'") >= 0:
v = "''".join(v.split("\'"))
return "'%s'" %v
def __init__(self, args):
args = parse_params(args, name='', expr='', type=None, optional=1)
name, expr = name_param(args, 'sqlvar', 1)
if expr is None:
expr = name
else:
expr = expr.eval
self.__name__, self.expr = name, expr
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:
expr = self.expr
if isinstance(expr, StringTypes):
v = md[expr]
else:
v = expr(md)
except:
if args.has_key('optional') and args['optional']:
return 'null'
if not isinstance(expr, StringTypes):
raise
raise ('Missing Input',
'Missing input variable, **%s**' % name)
# XXX Shrug, should these tyoes be really hard coded? What about
# Dates and other types a DB supports; I think we should make this
# a plugin.
if t == 'int':
try:
if isinstance(v, StringTypes):
int(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 **%s**' % name)
elif t == 'float':
try:
if isinstance(v, StringTypes):
float(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 **%s**' % name)
else:
orig_v = v
v = str(v)
if (not v or orig_v is None) and t == 'nb':
if args.has_key('optional') and args['optional']:
return 'null'
else:
raise ValueError, (
'Invalid empty string value for **%s**' % name)
v = self.sql_quote__(v)
return v
__call__ = render
valid_type = {'int':1, 'float':1, 'string':1, 'nb': 1}.has_key
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/ISQLScript.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
$Id: ISQLScript.py,v 1.1 2002/07/11 00:03:18 srichter Exp $
"""
from Zope.App.RDB.ISQLCommand import ISQLCommand
from Interface.Attribute import Attribute
class ISQLScript(ISQLCommand):
""" """
arguments = Attribute('''A set of attributes that can be used during
the DTML rendering process to provide dynamic
data.''')
def setArguments(arguments):
"""Processes the arguments (which could be a dict, string or whatever)
to arguments as they are needed for the rendering process."""
def getArguments():
"""Get the arguments. A method is preferred here, since some argument
evaluation might be done."""
def getArgumentsString():
"""This method returns the arguments string."""
def setSource(source):
"""Save the source of the page template.
"""
def getSource():
"""Get the source of the page template.
"""
def getTemplate():
"""Get the SQL DTML Template object.
"""
def setConnectionName(name):
"""Save the connection name for this SQL Script.
"""
def getConnectionName():
"""Get the connection name for this SQL Script.
"""
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/SQLScript.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
$Id: SQLScript.py,v 1.1 2002/07/11 00:03:18 srichter Exp $
"""
from types import StringTypes
from Persistence import Persistent
from Zope.ComponentArchitecture import getNextService
from Zope.ContextWrapper import ContextMethod
from Zope.DocumentTemplate.DT_HTML import HTML
from Zope.App.Traversing import getParent
from Zope.App.RDB.SQLCommand import SQLCommand
from Zope.App.RDB.Util import queryForResults
from Zope.App.OFS.Content.IFileContent import IFileContent
from Zope.App.OFS.Content.SQLScript.ISQLScript import ISQLScript
from Zope.App.OFS.Content.SQLScript.Arguments import parseArguments
from DT_SQLVar import SQLVar
from DT_SQLTest import SQLTest
from DT_SQLGroup import SQLGroup
class SQLDTML(HTML):
__name__ = 'SQLDTML'
commands = {}
for k, v in HTML.commands.items():
commands[k]=v
# add the new tags to the DTML
commands['sqlvar' ] = SQLVar
commands['sqltest'] = SQLTest
commands['sqlgroup' ] = SQLGroup
class SQLScript(SQLCommand, Persistent):
__implements__ = ISQLScript, IFileContent
def __init__(self, connectionName='', source='', arguments=''):
self.template = SQLDTML(source)
self.connectionName = connectionName
# In our case arguments should be a string that is parsed
self.setArguments(arguments)
############################################################
# Implementation methods for interface
# Zope.App.OFS.Content.SQLScript.ISQLScript.
def setArguments(self, arguments):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
assert isinstance(arguments, StringTypes), \
'"arguments" argument of setArguments() must be a string'
self._arg_string = arguments
self.arguments = parseArguments(arguments)
def getArguments(self):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
return self.arguments
def getArgumentsString(self):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
return self._arg_string
def setSource(self, source):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
self.template.munge(source)
def getSource(self):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
return self.template.read_raw()
def getTemplate(self):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
return self.template
def setConnectionName(self, name):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
self.connectionName = name
def getConnectionName(self):
'See Zope.App.OFS.Content.SQLScript.ISQLScript.ISQLScript'
return self.connectionName
######################################
# from: Zope.App.RDB.ISQLCommand.ISQLCommand
def getConnection(self):
'See Zope.App.RDB.ISQLCommand.ISQLCommand'
try:
parent = getParent(self)
except:
parent = self
connection_service = getNextService(parent, "ConnectionService")
connection = connection_service.getConnection(self.connectionName)
return connection
getConnection = ContextMethod(getConnection)
def __call__(self, **kw):
'See Zope.App.RDB.ISQLCommand.ISQLCommand'
# Try to resolve arguments
arg_values = {}
missing = []
for name in self.arguments.keys():
name = name.encode('UTF-8')
try:
# Try to find argument in keywords
arg_values[name] = kw[name]
except:
# Okay, the first try failed, so let's try to find the default
arg = self.arguments[name]
try:
arg_values[name] = arg['default']
except:
# Now the argument might be optional anyways; let's check
try:
if not arg['optional']:
missing.append(name)
except:
missing.append(name)
try:
connection = self.getConnection()
except AttributeError:
raise AttributeError, (
"The database connection **%s** cannot be found." % (
self.connectionName))
if connection is None:
raise 'Database Error', (
'%s is not connected to a database' %'foo')# self.id)
query = apply(self.template, (), arg_values)
return queryForResults(connection, query)
__call__ = ContextMethod(__call__)
#
############################################################
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/__init__.py ===
=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/configure.zcml ===
<zopeConfigure xmlns="http://namespaces.zope.org/zope">
<!-- SQL Script Directives -->
<permission id="Zope.AddSQLScripts" title="Add SQL Scripts" />
<content class=".SQLScript.">
<factory
id="SQLScript"
permission="Zope.ManageContent"
title="SQL Script"
description="Dynamic SQL Script" />
<require
permission="Zope.ManageContent"
interface=".ISQLScript." />
<require
permission="Zope.ManageContent"
interface="Zope.App.OFS.Content.IFileContent." />
</content>
<adapter
factory="Zope.App.OFS.Annotation.AttributeAnnotations."
provides="Zope.App.OFS.Annotation.IAnnotations."
for=".ISQLScript." />
<!-- Arguments Directives -->
<content class=".Arguments.">
<require
permission="Zope.ManageContent"
interface="Interface.Common.Mapping.IEnumerableMapping" />
</content>
<!-- SQL DTML Directives -->
<content class=".SQLScript.SQLDTML">
<require
permission="Zope.ManageContent"
attributes="__call__" />
</content>
<!-- Further Directives -->
<include package=".Views" />
</zopeConfigure>