[Zope3-checkins] CVS: Zope3/src/zope/app/content - sql.py:1.1.2.4
Fred L. Drake, Jr.
fred@zope.com
Tue, 24 Dec 2002 11:16:06 -0500
Update of /cvs-repository/Zope3/src/zope/app/content
In directory cvs.zope.org:/tmp/cvs-serv2734
Modified Files:
Tag: NameGeddon-branch
sql.py
Log Message:
- re-order classes to avoid broken forward references at import time
- remove duplicate definition of valid_type()
=== Zope3/src/zope/app/content/sql.py 1.1.2.3 => 1.1.2.4 ===
--- Zope3/src/zope/app/content/sql.py:1.1.2.3 Tue Dec 24 07:51:00 2002
+++ Zope3/src/zope/app/content/sql.py Tue Dec 24 11:16:06 2002
@@ -242,6 +242,144 @@
return parseArguments(text[length:], result)
+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:
+ 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
+
+
class SQLTest:
name = 'sqltest'
optional = multiple = None
@@ -344,7 +482,6 @@
__call__ = render
-valid_type = {'int':1, 'float':1, 'string':1, 'nb': 1}.has_key
# SQL compliant comparison operators
comparison_operators = { 'eq': '=', 'ne': '<>',
@@ -361,9 +498,9 @@
commands[k]=v
# add the new tags to the DTML
- commands['sqlvar' ] = SQLVar
+ commands['sqlvar'] = SQLVar
commands['sqltest'] = SQLTest
- commands['sqlgroup' ] = SQLGroup
+ commands['sqlgroup'] = SQLGroup
class SQLScript(SQLCommand, Persistent):
@@ -484,144 +621,6 @@
"Set the SQL template source.")
connectionName = property(getConnectionName, setConnectionName, None,
"Connection Name for the SQL scripts.")
-
-
-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:
- 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