[Zope3-checkins] CVS: Zope3/src/zope/products/sqlscript -
README.txt:1.1.2.1 __init__.py:1.1.2.1 add.pt:1.1.2.1
browser.py:1.1.2.1 configure.zcml:1.1.2.1 dtml.py:1.1.2.1
edit.pt:1.1.2.1 interfaces.py:1.1.2.1 sqlscript.py:1.1.2.1
test.pt:1.1.2.1 testresults.pt:1.1.2.1
Philipp von Weitershausen
philikon at philikon.de
Wed Feb 11 11:29:28 EST 2004
Update of /cvs-repository/Zope3/src/zope/products/sqlscript
In directory cvs.zope.org:/tmp/cvs-serv21715/sqlscript
Added Files:
Tag: philikon-movecontent-branch
README.txt __init__.py add.pt browser.py configure.zcml
dtml.py edit.pt interfaces.py sqlscript.py test.pt
testresults.pt
Log Message:
Get rid of zope.products.content and zope.products.codecontent and move
content components in their own packages at zope.products.
See the package geddon proposal: http://dev.zope.org/Zope3/PackageGeddon
=== Added File Zope3/src/zope/products/sqlscript/README.txt ===
Using additional DTML tags in SQLScript
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'.
=== Added File Zope3/src/zope/products/sqlscript/__init__.py ===
#
# This file is necessary to make this directory a package.
=== Added File Zope3/src/zope/products/sqlscript/add.pt ===
<html metal:use-macro="context/@@standard_macros/page">
<head>
<title>SQL Script</title>
</head>
<body>
<div metal:fill-slot="body">
<div metal:use-macro="view/generated_form/macros/body">
<input type="submit" value="Add And Test"
metal:fill-slot="extra_buttons" name="add_test" />
</div>
</div>
</body>
</html>
=== Added File Zope3/src/zope/products/sqlscript/browser.py ===
##############################################################################
#
# Copyright (c) 2001, 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.
#
##############################################################################
"""SQL Script Views
$Id: browser.py,v 1.1.2.1 2004/02/11 16:29:24 philikon Exp $
"""
from zope.app.browser.form.add import AddView
from zope.app.browser.form.submit import Update
from zope.app.interfaces.rdb import DatabaseException
from zope.app.interfaces.container import IAdding
from zope.app import zapi
from zope.products.sqlscript.sqlscript import SQLScript
from zope.products.sqlscript.interfaces import ISQLScript
class SQLScriptTest(object):
"""Test the SQL inside the SQL Script
"""
__used_for__ = ISQLScript
error = None
def getArguments(self):
form = self.request.form
arguments = {}
for argname, argvalue in self.context.getArguments().items():
value = form.get(argname)
if value is None:
value = argvalue.get('default')
if value is not None:
arguments[argname.encode('UTF-8')] = value
return arguments
def getTestResults(self):
try:
return self.context(**self.getArguments())
except (DatabaseException, AttributeError, Exception), error:
self.error = error
return []
def getFormattedError(self):
error = str(self.error)
return error
def getRenderedSQL(self):
return self.context.getTemplate()(**self.getArguments())
class SQLScriptAdd(object):
"""Provide interface to add SQL Script
"""
def update(self):
"""Set the Update variable for Add and Test
>>> from zope.publisher.browser import TestRequest
>>> rqst = TestRequest()
>>> class Base(object):
... def __init__(self, request):
... self.request = request
... def update(self):
... self.updated = True
>>> class V(SQLScriptAdd, Base):
... pass
>>> dc = V(rqst)
>>> dc.update()
>>> dc.updated
True
>>> 'UPDATE_SUBMIT' in rqst
False
>>> d = {'add_test': True}
>>> rqst1 = TestRequest(form = d)
>>> dc1 = V(rqst1)
>>> dc1.update()
>>> 'UPDATE_SUBMIT' in rqst1
True
"""
if 'add_test' in self.request:
self.request.form[Update] = ''
return super(SQLScriptAdd, self).update()
def nextURL(self):
"""
>>> from zope.publisher.browser import TestRequest
>>> from zope.app.tests.placelesssetup import setUp, tearDown
>>> setUp()
>>> rqst = TestRequest()
>>> class Base(object):
... def __init__(self, request):
... self.request = request
... self.context = self
... self.contentName = 'new srcipt'
... def __getitem__(self, key):
... return None
... def nextURL(self):
... return "www.zeomega.com"
>>> class V(SQLScriptAdd, Base):
... pass
>>>
>>> rqst = TestRequest()
>>> dc = V(rqst)
>>> dc.nextURL()
'www.zeomega.com'
>>> d = {'add_test': True}
>>> rqst1 = TestRequest(form = d)
>>> dc1 = V(rqst1)
>>> dc1.nextURL()
'http://127.0.0.1/test.html'
"""
if 'add_test' in self.request:
name = self.context.contentName
container = self.context.context
obj = container[name]
url = zapi.getView(obj, 'absolute_url', self.request)()
url = '%s/test.html' % url
return url
else:
return super(SQLScriptAdd, self).nextURL()
class SQLScriptEdit(object):
"""Provide interface to Edit and Test SQL Script
"""
def update(self):
"""Set the Update variable for Change and Test
>>> from zope.publisher.browser import TestRequest
>>> rqst = TestRequest()
>>> class Base(object):
... def __init__(self, request):
... self.request = request
... self.errors = ('no errors')
... def update(self):
... self.updated = True
... return "update returned"
>>> class V(SQLScriptEdit, Base):
... pass
>>> dc = V(rqst)
>>> dc.update()
'update returned'
>>> dc.updated
True
>>> 'UPDATE_SUBMIT' in rqst
False
>>>
>>> d = {'change_test': True}
>>> rqst1 = TestRequest(form = d)
>>> dc1 = V(rqst1)
>>> dc1.errors = ()
>>> dc1.update()
'update returned'
>>> 'UPDATE_SUBMIT' in rqst1
True
>>> dc1.updated
True
>>> rqst1.response.getHeader('location')
'test.html'
>>> rqst1.response.getStatus()
302
>>> d = {'change_test': True}
>>> rqst2 = TestRequest(form = d)
>>> dc2 = V(rqst2)
>>> dc2.errors = ('errorname', 1234)
>>> dc2.update()
'update returned'
>>> 'UPDATE_SUBMIT' in rqst2
True
>>> rqst2.response.getHeader('location')
>>> rqst2.response.getStatus()
599
"""
if 'change_test' in self.request:
self.request.form[Update] = ''
super(SQLScriptEdit, self).update()
if not self.errors:
url = 'test.html'
self.request.response.redirect(url)
return super(SQLScriptEdit, self).update()
=== Added File Zope3/src/zope/products/sqlscript/configure.zcml ===
<configure
xmlns='http://namespaces.zope.org/zope'
xmlns:browser='http://namespaces.zope.org/browser'
xmlns:fssync='http://namespaces.zope.org/fssync'
i18n_domain='zope'
>
<!-- SQL Script Directives -->
<permission
id="zope.AddSQLScripts"
title="[add-sql-scripts-permission] Add SQL Scripts"
/>
<interface
interface=".interfaces.ISQLScript"
type="zope.app.interfaces.content.IContentType"
/>
<content class=".sqlscript.SQLScript">
<require
permission = "zope.ManageContent"
interface = ".interfaces.ISQLScript"
set_schema = ".interfaces.ISQLScript"
/>
<require
permission="zope.ManageContent"
interface="zope.products.file.interfaces.IFileContent"
/>
<implements
interface="zope.app.interfaces.annotation.IAttributeAnnotatable"
/>
</content>
<!-- Arguments Directives -->
<content class=".sqlscript.Arguments">
<require
permission="zope.ManageContent"
interface="zope.interface.common.mapping.IEnumerableMapping"
/>
</content>
<!-- SQL DTML Directives -->
<content class=".dtml.SQLDTML">
<require
permission="zope.ManageContent"
attributes="__call__" />
</content>
<!-- SQL Script View Directives -->
<browser:addMenuItem
title="SQLScript"
class=".sqlscript.SQLScript"
permission="zope.ManageContent"
view="zope.products.sqlscript.SQLScript" />
<browser:addform
schema=".interfaces.ISQLScript"
label="Add a SQL Script"
content_factory=".sqlscript.SQLScript"
keyword_arguments="connectionName source arguments"
name="zope.products.sqlscript.SQLScript"
permission="zope.ManageContent"
template="add.pt"
class=".browser.SQLScriptAdd"
/>
<browser:editform
schema=".interfaces.ISQLScript"
name="edit.html"
menu="zmi_views"
label="Edit an SQL script"
permission="zope.ManageContent"
template="edit.pt"
class=".browser.SQLScriptEdit"
/>
<browser:pages
for=".interfaces.ISQLScript"
permission="zope.View"
class=".browser.SQLScriptTest" >
<browser:page
name="test.html"
template="test.pt"
menu="zmi_views"
title="[test-page-title] Test"
/>
<browser:page
name="testResults.html"
template="testresults.pt"
/>
</browser:pages>
</configure>
=== Added File Zope3/src/zope/products/sqlscript/dtml.py ===
##############################################################################
#
# Copyright (c) 2004 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: dtml.py,v 1.1.2.1 2004/02/11 16:29:24 philikon Exp $
"""
import sys
from types import StringTypes
from zope.documenttemplate.dt_html import HTML
from zope.documenttemplate.dt_util import ParseError, parse_params, name_param
from interfaces import MissingInput
__metaclass__ = type
valid_type = {'int': True,
'float': True,
'string': True,
'nb': True}.has_key
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 key[0] == name and self.optional:
return ''
raise KeyError, key, sys.exc_info()[2]
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!!!
if t == 'int':
try:
if isinstance(v, StringTypes):
int(v)
else:
v = str(int(v))
except ValueError:
raise ValueError, (
'Invalid integer value for **%s**' %name)
elif t == 'float':
if not v and isinstance(v, str):
continue
try:
if isinstance(v, StringTypes):
float(v)
else:
v = str(float(v))
except ValueError:
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 MissingInput, '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
# SQL compliant comparison operators
comparison_operators = { 'eq': '=', 'ne': '<>',
'lt': '<', 'le': '<=', 'lte': '<=',
'gt': '>', 'ge': '>=', 'gte': '>=' }
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
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 (KeyError, ValueError):
if args.has_key('optional') and args['optional']:
return 'null'
if not isinstance(expr, StringTypes):
raise
raise MissingInput, '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 ValueError:
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
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
=== Added File Zope3/src/zope/products/sqlscript/edit.pt ===
<html metal:use-macro="context/@@standard_macros/page">
<head>
<title>SQL Script</title>
</head>
<body>
<div metal:fill-slot="body">
<div metal:use-macro="view/generated_form/macros/body">
<input type="submit" value="Change And Test"
metal:fill-slot="extra_buttons" name="change_test" />
</div>
</div>
</body>
</html>
=== Added File Zope3/src/zope/products/sqlscript/interfaces.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: interfaces.py,v 1.1.2.1 2004/02/11 16:29:24 philikon Exp $
"""
import zope.schema
from zope.app import zapi
from zope.app.interfaces.rdb import IZopeDatabaseAdapter, ISQLCommand
from zope.component import getService, ComponentLookupError
from zope.app.i18n import ZopeMessageIDFactory as _
class MissingInput(Exception):
pass
class SQLConnectionName(zope.schema.TextLine):
"""SQL Connection Name"""
def __allowed(self):
"""Note that this method works only if the Field is context wrapped."""
try:
connections = zapi.getUtilitiesFor(self.context,
IZopeDatabaseAdapter)
except ComponentLookupError:
return []
return [c[0] for c in connections]
allowed_values = property(__allowed)
class ISQLScript(ISQLCommand):
"""A persistent script that can execute SQL."""
connectionName = SQLConnectionName(
title=_(u"Connection Name"),
description=_(u"The Connection Name for the connection to be used."),
required=False)
arguments = zope.schema.BytesLine(
title=_(u"Arguments"),
description=_(
u"A set of attributes that can be used during the SQL command "
u"rendering process to provide dynamic data."),
required=False,
default='',
missing_value='')
source = zope.schema.ASCII(
title=_(u"Source"),
description=_(u"The SQL command to be run."),
required=False,
default='',
missing_value='')
def getArguments():
"""Returns a set of arguments. Note that this is not a string!"""
def getTemplate():
"""Get the SQL DTML Template object."""
=== Added File Zope3/src/zope/products/sqlscript/sqlscript.py ===
##############################################################################
#
# Copyright (c) 2004 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.2.1 2004/02/11 16:29:24 philikon Exp $
"""
import re
from types import StringTypes
from persistence import Persistent
from persistence.dict import PersistentDict
from zope.interface import implements
from zope.interface.common.mapping import IEnumerableMapping
from zope.app import zapi
from zope.app.rdb import queryForResults
from zope.app.container.contained import Contained
from zope.app.cache.caching import getCacheForObj, getLocationForCache
from zope.app.interfaces.rdb import IZopeDatabaseAdapter
from zope.products.file.interfaces import IFileContent
from interfaces import ISQLScript
from dtml import SQLDTML
unparmre = re.compile(r'([\000- ]*([^\000- ="]+))')
parmre = re.compile(r'([\000- ]*([^\000- ="]+)=([^\000- ="]+))')
qparmre = re.compile(r'([\000- ]*([^\000- ="]+)="([^"]*)")')
class InvalidParameter(Exception):
pass
class Arguments(PersistentDict):
"""Hold arguments of SQL Script
"""
implements(IEnumerableMapping)
class SQLScript(Persistent, Contained):
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.arguments = arguments
def setArguments(self, arguments):
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.interfaces.content.sql.ISQLScript'
return self._arguments
def getArgumentsString(self):
return self._arg_string
# See zope.app.interfaces.content.sql.ISQLScript
arguments = property(getArgumentsString, setArguments)
def setSource(self, source):
self.template.munge(source)
def getSource(self):
return self.template.read_raw()
# See zope.app.interfaces.content.sql.ISQLScript
source = property(getSource, setSource)
def getTemplate(self):
'See zope.app.interfaces.content.sql.ISQLScript'
return self.template
def _setConnectionName(self, name):
self._connectionName = name
cache = getCacheForObj(self)
location = getLocationForCache(self)
if cache and location:
cache.invalidate(location)
def _getConnectionName(self):
return self._connectionName
def getConnection(self):
name = self.connectionName
connection = zapi.getUtility(self, IZopeDatabaseAdapter, name)
return connection()
# See zope.app.interfaces.content.sql.ISQLScript
connectionName = property(_getConnectionName, _setConnectionName)
def __call__(self, **kw):
'See zope.app.interfaces.rdb'
# 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 KeyError:
# Okay, the first try failed, so let's try to find the default
arg = self._arguments[name]
try:
arg_values[name] = arg['default']
except KeyError:
# Now the argument might be optional anyways; let's check
try:
if not arg['optional']:
missing.append(name)
except KeyError:
missing.append(name)
try:
connection = self.getConnection()
except KeyError:
raise AttributeError, (
"The database connection '%s' cannot be found." % (
self.connectionName))
query = apply(self.template, (), arg_values)
cache = getCacheForObj(self)
location = getLocationForCache(self)
if cache and location:
_marker = object()
result = cache.query(location, {'query': query}, default=_marker)
if result is not _marker:
return result
result = queryForResults(connection, query)
if cache and location:
cache.set(result, location, {'query': query})
return result
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/src/zope/products/sqlscript/test.pt ===
<html metal:use-macro="views/standard_macros/page">
<body>
<div metal:fill-slot="body">
<form action="." method="post">
<pre tal:content="context/source" />
<table border="1"
tal:define="args context/getArguments"
tal:condition="args">
<tbody>
<tr>
<th i18n:translate="">Argument Name</th>
<th i18n:translate="">Type</th>
<th i18n:translate="">Value</th>
</tr>
<tr tal:repeat="arg python: args.keys()">
<td tal:content="arg"></td>
<td tal:content="python: args[arg].get('type')"> </td>
<td><input type="text" name="" size="10" value=""
tal:attributes="value python: args[arg].get('default');
name arg"/></td>
</tr>
</tbody>
</table>
<input type="submit" name="testResults.html:method" value="Test"
i18n:attributes="value test-button"/>
</form>
</div>
</body>
</html>
=== Added File Zope3/src/zope/products/sqlscript/testresults.pt ===
<html metal:use-macro="views/standard_macros/page">
<body>
<div metal:fill-slot="body">
<pre tal:content="view/getRenderedSQL" />
<table border="1" cellspacing="0" cellpadding="2"
tal:define="result view/getTestResults"
tal:condition="result">
<tbody>
<tr>
<th tal:repeat="field result/columns"
tal:content="field">Field Name</th>
</tr>
<tr tal:repeat="row result">
<td tal:repeat="field result/columns"
tal:content="python: getattr(row, field)">Value</td>
</tr>
</tbody>
</table>
<tal:block tal:condition="view/error">
<h3 i18n:translate="">An Error occurred</h3>
<pre tal:content="view/getFormattedError" />
</tal:block>
</div>
</body>
</html>
More information about the Zope3-Checkins
mailing list