`0001from array import array
0002import datetime
0003from decimal import Decimal
0004import sys
0005import time
0006from types import ClassType, InstanceType, NoneType
0007
0008
0009try:
0010 from mx.DateTime import DateTimeType, DateTimeDeltaType
0011except ImportError:
0012 try:
0013 from DateTime import DateTimeType, DateTimeDeltaType
0014 except ImportError:
0015 DateTimeType = None
0016 DateTimeDeltaType = None
0017
0018try:
0019 import Sybase
0020 NumericType=Sybase.NumericType
0021except ImportError:
0022 NumericType = None
0023
0024
0025########################################
0026## Quoting
0027########################################
0028
0029sqlStringReplace = [
0030 ("'", "''"),
0031 ('\\', '\\\\'),
0032 ('\000', '\\0'),
0033 ('\b', '\\b'),
0034 ('\n', '\\n'),
0035 ('\r', '\\r'),
0036 ('\t', '\\t'),
0037]
0038
0039class ConverterRegistry:
0040
0041 def __init__(self):
0042 self.basic = {}
0043 self.klass = {}
0044
0045 def registerConverter(self, typ, func):
0046 if type(typ) is ClassType:
0047 self.klass[typ] = func
0048 else:
0049 self.basic[typ] = func
0050
0051 def lookupConverter(self, value, default=None):
0052 if type(value) is InstanceType:
0053 # lookup on klasses dict
0054 return self.klass.get(value.__class__, default)
0055 return self.basic.get(type(value), default)
0056
0057converters = ConverterRegistry()
0058registerConverter = converters.registerConverter
0059lookupConverter = converters.lookupConverter
0060
0061def StringLikeConverter(value, db):
0062 if isinstance(value, array):
0063 try:
0064 value = value.tounicode()
0065 except ValueError:
0066 value = value.tostring()
0067 elif isinstance(value, buffer):
0068 value = str(value)
0069
0070 if db in ('mysql', 'postgres', 'rdbhost'):
0071 for orig, repl in sqlStringReplace:
0072 value = value.replace(orig, repl)
0073 elif db in ('sqlite', 'firebird', 'sybase', 'maxdb', 'mssql'):
0074 value = value.replace("'", "''")
0075 else:
0076 assert 0, "Database %s unknown" % db
0077 if db in ('postgres', 'rdbhost') and ('\\' in value):
0078 return "E'%s'" % value
0079 return "'%s'" % value
0080
0081registerConverter(str, StringLikeConverter)
0082registerConverter(unicode, StringLikeConverter)
0083registerConverter(array, StringLikeConverter)
0084registerConverter(buffer, StringLikeConverter)
0085
0086def IntConverter(value, db):
0087 return repr(int(value))
0088
0089registerConverter(int, IntConverter)
0090
0091def LongConverter(value, db):
0092 return str(value)
0093
0094registerConverter(long, LongConverter)
0095
0096if NumericType:
0097 registerConverter(NumericType, IntConverter)
0098
0099def BoolConverter(value, db):
0100 if db in ('postgres', 'rdbhost'):
0101 if value:
0102 return "'t'"
0103 else:
0104 return "'f'"
0105 else:
0106 if value:
0107 return '1'
0108 else:
0109 return '0'
0110
0111registerConverter(bool, BoolConverter)
0112
0113def FloatConverter(value, db):
0114 return repr(value)
0115
0116registerConverter(float, FloatConverter)
0117
0118if DateTimeType:
0119 def mxDateTimeConverter(value, db):
0120 return "'%s'" % value.strftime("%Y-%m-%d %H:%M:%S")
0121
0122 registerConverter(DateTimeType, mxDateTimeConverter)
0123
0124 def mxTimeConverter(value, db):
0125 return "'%s'" % value.strftime("%H:%M:%S")
0126
0127 registerConverter(DateTimeDeltaType, mxTimeConverter)
0128
0129def NoneConverter(value, db):
0130 return "NULL"
0131
0132registerConverter(NoneType, NoneConverter)
0133
0134def SequenceConverter(value, db):
0135 return "(%s)" % ", ".join([sqlrepr(v, db) for v in value])
0136
0137registerConverter(tuple, SequenceConverter)
0138registerConverter(list, SequenceConverter)
0139registerConverter(dict, SequenceConverter)
0140registerConverter(set, SequenceConverter)
0141registerConverter(frozenset, SequenceConverter)
0142
0143if hasattr(time, 'struct_time'):
0144 def StructTimeConverter(value, db):
0145 return time.strftime("'%Y-%m-%d %H:%M:%S'", value)
0146
0147 registerConverter(time.struct_time, StructTimeConverter)
0148
0149def DateTimeConverter(value, db):
0150 return "'%04d-%02d-%02d %02d:%02d:%02d'" % (
0151 value.year, value.month, value.day,
0152 value.hour, value.minute, value.second)
0153
0154def DateTimeConverterMS(value, db):
0155 return "'%04d-%02d-%02d %02d:%02d:%02d.%06d'" % (
0156 value.year, value.month, value.day,
0157 value.hour, value.minute, value.second, value.microsecond)
0158
0159registerConverter(datetime.datetime, DateTimeConverterMS)
0160
0161def DateConverter(value, db):
0162 return "'%04d-%02d-%02d'" % (value.year, value.month, value.day)
0163
0164registerConverter(datetime.date, DateConverter)
0165
0166def TimeConverter(value, db):
0167 return "'%02d:%02d:%02d'" % (value.hour, value.minute, value.second)
0168
0169def TimeConverterMS(value, db):
0170 return "'%02d:%02d:%02d.%06d'" % (value.hour, value.minute, value.second, value.microsecond)
0171
0172registerConverter(datetime.time, TimeConverterMS)
0173
0174def DecimalConverter(value, db):
0175 return value.to_eng_string()
0176
0177registerConverter(Decimal, DecimalConverter)
0178
0179def TimedeltaConverter(value, db):
0180
0181 return """INTERVAL '%d days %d seconds'""" % (value.days, value.seconds)
0183
0184registerConverter(datetime.timedelta, TimedeltaConverter)
0185
0186
0187def sqlrepr(obj, db=None):
0188 try:
0189 reprFunc = obj.__sqlrepr__
0190 except AttributeError:
0191 converter = lookupConverter(obj)
0192 if converter is None:
0193 raise ValueError, "Unknown SQL builtin type: %s for %s" % (type(obj), repr(obj))
0195 return converter(obj, db)
0196 else:
0197 return reprFunc(db)
0198
0199
0200def quote_str(s, db):
0201 if db in ('postgres', 'rdbhost') and ('\\' in s):
0202 return "E'%s'" % s
0203 return "'%s'" % s
0204
0205def unquote_str(s):
0206 if s[:2].upper().startswith("E'") and s.endswith("'"):
0207 return s[2:-1]
0208 elif s.startswith("'") and s.endswith("'"):
0209 return s[1:-1]
0210 else:
0211 return s`