Commit 8e073789 authored by Julien Muchembled's avatar Julien Muchembled

Clean up sqlvar patch

parent e0388ecc
...@@ -17,100 +17,69 @@ ...@@ -17,100 +17,69 @@
from Shared.DC.ZRDB.sqlvar import SQLVar from Shared.DC.ZRDB.sqlvar import SQLVar
from Shared.DC.ZRDB import sqlvar from Shared.DC.ZRDB import sqlvar
from string import atoi,atof from string import atoi,atof
from types import StringType
from Products.ERP5Type.PsycoWrapper import psyco
from DateTime import DateTime from DateTime import DateTime
def SQLVar_render(self, md): def SQLVar_render(self, md):
name=self.__name__
args=self.args args=self.args
t=args['type'] t=args['type']
try: try:
expr=self.expr expr=self.expr
if type(expr) is type(''): v=md[expr] if type(expr) is str: v=md[expr]
else: v=expr(md) else: v=expr(md)
except: except Exception:
if args.has_key('optional') and args['optional']: if args.get('optional'):
return 'null' return 'null'
if type(expr) is not type(''): if type(expr) is not str:
raise raise
raise ValueError, 'Missing input variable, <em>%s</em>' % name raise ValueError('Missing input variable, <em>%s</em>' % self.__name__)
if v is None and args.get('optional'):
return 'null'
if t=='int': if t=='int':
try: try:
if type(v) is StringType: if type(v) is str:
if v[-1:]=='L': if v[-1:]=='L':
v=v[:-1] v=v[:-1]
atoi(v) atoi(v)
else: v=str(int(v)) return v
except: return str(int(v))
if not v and args.has_key('optional') and args['optional']: except Exception:
return 'null' t = 'integer'
raise ValueError, (
'Invalid integer value for <em>%s</em>: %r' % (name, v))
elif t=='float': elif t=='float':
try: try:
if type(v) is StringType: if type(v) is str:
if v[-1:]=='L': if v[-1:]=='L':
v=v[:-1] v=v[:-1]
atof(v) atof(v)
return v
# ERP5 patch, we use repr that have better precision than str for # ERP5 patch, we use repr that have better precision than str for
# floats # floats
else: v=repr(float(v)) return repr(float(v))
except: except Exception:
if not v and args.has_key('optional') and args['optional']: t = 'floating-point'
return 'null'
raise ValueError, (
'Invalid floating-point value for <em>%s</em>: %r' % (name, v))
# Patched by yo
elif t=='datetime': elif t=='datetime':
if v is None:
if args.has_key('optional') and args['optional']:
return 'null'
else:
raise ValueError, (
'Invalid datetime value for <em>%s</em>: %r' % (name, v))
try: try:
v = (v if isinstance(v, DateTime) else DateTime(v)).toZone('UTC') v = (v if isinstance(v, DateTime) else DateTime(v)).toZone('UTC')
# For subsecond precision in MySQL, use 'datetime(N)' type, # For subsecond precision in MySQL, use 'datetime(N)' type,
# where N is the number of digits after the decimal point. # where N is the number of digits after the decimal point.
v = "'%s.%06u'" % (v.ISO(), v.micros() % 1000000) return "'%s.%06u'" % (v.ISO(), v.micros() % 1000000)
except: except Exception:
if not v and args.has_key('optional') and args['optional']: pass
return 'null' elif t=='nb' and not v:
raise ValueError, ( t = 'empty string'
'Invalid datetime value for <em>%s</em>: %r' % (name, v))
# End of patch
else: else:
# Patched by yo v = md.getitem('sql_quote__',0)(
if v is None: v if isinstance(v, basestring) else str(v))
if args.has_key('optional') and args['optional']:
return 'null'
else:
raise ValueError, (
'Invalid string value for <em>%s</em>: %r' % (name, v))
# End of patch
if not isinstance(v, (str, unicode)):
v=str(v)
if not v and t=='nb':
if args.has_key('optional') and args['optional']:
return 'null'
else:
raise ValueError, (
'Invalid empty string value for <em>%s</em>' % name)
v=md.getitem('sql_quote__',0)(v)
#if find(v,"\'") >= 0: v=join(split(v,"\'"),"''") #if find(v,"\'") >= 0: v=join(split(v,"\'"),"''")
#v="'%s'" % v #v="'%s'" % v
return v
return v raise ValueError('Invalid %s value for <em>%s</em>: %r'
% (t, self.__name__, v))
psyco.bind(SQLVar_render)
# Patched by yo. datetime is added. # Patched by yo. datetime is added.
valid_type={'int':1, 'float':1, 'string':1, 'nb': 1, 'datetime' : 1}.has_key valid_type=('int', 'float', 'string', 'nb', 'datetime').__contains__
SQLVar.render = SQLVar_render SQLVar.render = SQLVar_render
SQLVar.__call__ = SQLVar_render SQLVar.__call__ = SQLVar_render
......
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