/usr/lib/python2.7/dist-packages/sqlobject/converters.py is in python-sqlobject 2.1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | from array import array
import datetime
from decimal import Decimal
import sys
import time
from types import ClassType, InstanceType, NoneType
try:
from mx.DateTime import DateTimeType, DateTimeDeltaType
except ImportError:
try:
from DateTime import DateTimeType, DateTimeDeltaType
except ImportError:
DateTimeType = None
DateTimeDeltaType = None
try:
import Sybase
NumericType=Sybase.NumericType
except ImportError:
NumericType = None
########################################
## Quoting
########################################
sqlStringReplace = [
("'", "''"),
('\\', '\\\\'),
('\000', '\\0'),
('\b', '\\b'),
('\n', '\\n'),
('\r', '\\r'),
('\t', '\\t'),
]
class ConverterRegistry:
def __init__(self):
self.basic = {}
self.klass = {}
def registerConverter(self, typ, func):
if type(typ) is ClassType:
self.klass[typ] = func
else:
self.basic[typ] = func
def lookupConverter(self, value, default=None):
if type(value) is InstanceType:
# lookup on klasses dict
return self.klass.get(value.__class__, default)
return self.basic.get(type(value), default)
converters = ConverterRegistry()
registerConverter = converters.registerConverter
lookupConverter = converters.lookupConverter
def StringLikeConverter(value, db):
if isinstance(value, array):
try:
value = value.tounicode()
except ValueError:
value = value.tostring()
elif isinstance(value, buffer):
value = str(value)
if db in ('mysql', 'postgres', 'rdbhost'):
for orig, repl in sqlStringReplace:
value = value.replace(orig, repl)
elif db in ('sqlite', 'firebird', 'sybase', 'maxdb', 'mssql'):
value = value.replace("'", "''")
else:
assert 0, "Database %s unknown" % db
if db in ('postgres', 'rdbhost') and ('\\' in value):
return "E'%s'" % value
return "'%s'" % value
registerConverter(str, StringLikeConverter)
registerConverter(unicode, StringLikeConverter)
registerConverter(array, StringLikeConverter)
registerConverter(buffer, StringLikeConverter)
def IntConverter(value, db):
return repr(int(value))
registerConverter(int, IntConverter)
def LongConverter(value, db):
return str(value)
registerConverter(long, LongConverter)
if NumericType:
registerConverter(NumericType, IntConverter)
def BoolConverter(value, db):
if db in ('postgres', 'rdbhost'):
if value:
return "'t'"
else:
return "'f'"
else:
if value:
return '1'
else:
return '0'
registerConverter(bool, BoolConverter)
def FloatConverter(value, db):
return repr(value)
registerConverter(float, FloatConverter)
if DateTimeType:
def DateTimeConverter(value, db):
return "'%s'" % value.strftime("%Y-%m-%d %H:%M:%S.%s")
registerConverter(DateTimeType, DateTimeConverter)
def TimeConverter(value, db):
return "'%s'" % value.strftime("%H:%M:%S")
registerConverter(DateTimeDeltaType, TimeConverter)
def NoneConverter(value, db):
return "NULL"
registerConverter(NoneType, NoneConverter)
def SequenceConverter(value, db):
return "(%s)" % ", ".join([sqlrepr(v, db) for v in value])
registerConverter(tuple, SequenceConverter)
registerConverter(list, SequenceConverter)
registerConverter(dict, SequenceConverter)
registerConverter(set, SequenceConverter)
registerConverter(frozenset, SequenceConverter)
if hasattr(time, 'struct_time'):
def StructTimeConverter(value, db):
return time.strftime("'%Y-%m-%d %H:%M:%S'", value)
registerConverter(time.struct_time, StructTimeConverter)
def DateTimeConverter(value, db):
return "'%04d-%02d-%02d %02d:%02d:%02d.%06d'" % (
value.year, value.month, value.day,
value.hour, value.minute, value.second, value.microsecond)
registerConverter(datetime.datetime, DateTimeConverter)
def DateConverter(value, db):
return "'%04d-%02d-%02d'" % (value.year, value.month, value.day)
registerConverter(datetime.date, DateConverter)
def TimeConverter(value, db):
return "'%02d:%02d:%02d.%06d'" % (value.hour, value.minute, value.second, value.microsecond)
registerConverter(datetime.time, TimeConverter)
def DecimalConverter(value, db):
return value.to_eng_string()
registerConverter(Decimal, DecimalConverter)
def TimedeltaConverter(value, db):
return """INTERVAL '%d days %d seconds'""" % \
(value.days, value.seconds)
registerConverter(datetime.timedelta, TimedeltaConverter)
def sqlrepr(obj, db=None):
try:
reprFunc = obj.__sqlrepr__
except AttributeError:
converter = lookupConverter(obj)
if converter is None:
raise ValueError, "Unknown SQL builtin type: %s for %s" % \
(type(obj), repr(obj))
return converter(obj, db)
else:
return reprFunc(db)
def quote_str(s, db):
if db in ('postgres', 'rdbhost') and ('\\' in s):
return "E'%s'" % s
return "'%s'" % s
def unquote_str(s):
if s[:2].upper().startswith("E'") and s.endswith("'"):
return s[2:-1]
elif s.startswith("'") and s.endswith("'"):
return s[1:-1]
else:
return s
|