2006-05-02 09:31:56 +08:00
|
|
|
import datetime
|
2010-05-04 22:00:30 +08:00
|
|
|
import decimal
|
2010-10-04 23:12:39 +08:00
|
|
|
import logging
|
2006-05-02 09:31:56 +08:00
|
|
|
from time import time
|
|
|
|
|
2008-08-02 13:56:57 +08:00
|
|
|
from django.utils.hashcompat import md5_constructor
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
logger = logging.getLogger('django.db.backends')
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class CursorDebugWrapper(object):
|
2006-05-02 09:31:56 +08:00
|
|
|
def __init__(self, cursor, db):
|
|
|
|
self.cursor = cursor
|
2007-10-24 03:00:31 +08:00
|
|
|
self.db = db # Instance of a BaseDatabaseWrapper subclass
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def execute(self, sql, params=()):
|
|
|
|
start = time()
|
|
|
|
try:
|
|
|
|
return self.cursor.execute(sql, params)
|
|
|
|
finally:
|
|
|
|
stop = time()
|
2010-10-04 23:12:39 +08:00
|
|
|
duration = stop - start
|
2007-10-24 03:00:31 +08:00
|
|
|
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.db.queries.append({
|
2007-10-24 03:00:31 +08:00
|
|
|
'sql': sql,
|
2010-10-04 23:12:39 +08:00
|
|
|
'time': "%.3f" % duration,
|
2006-05-02 09:31:56 +08:00
|
|
|
})
|
2010-10-04 23:12:39 +08:00
|
|
|
logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),
|
|
|
|
extra={'duration':duration, 'sql':sql, 'params':params}
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def executemany(self, sql, param_list):
|
|
|
|
start = time()
|
|
|
|
try:
|
|
|
|
return self.cursor.executemany(sql, param_list)
|
|
|
|
finally:
|
|
|
|
stop = time()
|
2010-10-04 23:12:39 +08:00
|
|
|
duration = stop - start
|
2006-05-02 09:31:56 +08:00
|
|
|
self.db.queries.append({
|
2007-10-24 03:00:31 +08:00
|
|
|
'sql': '%s times: %s' % (len(param_list), sql),
|
2010-10-04 23:12:39 +08:00
|
|
|
'time': "%.3f" % duration,
|
2006-05-02 09:31:56 +08:00
|
|
|
})
|
2010-10-04 23:12:39 +08:00
|
|
|
logger.debug('(%.3f) %s; args=%s' % (duration, sql, param_list),
|
|
|
|
extra={'duration':duration, 'sql':sql, 'params':param_list}
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
2007-04-26 21:30:48 +08:00
|
|
|
if attr in self.__dict__:
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.__dict__[attr]
|
|
|
|
else:
|
|
|
|
return getattr(self.cursor, attr)
|
|
|
|
|
2008-03-17 22:15:15 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.cursor)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
###############################################
|
|
|
|
# Converters from database (string) to Python #
|
|
|
|
###############################################
|
|
|
|
|
|
|
|
def typecast_date(s):
|
|
|
|
return s and datetime.date(*map(int, s.split('-'))) or None # returns None if s is null
|
|
|
|
|
|
|
|
def typecast_time(s): # does NOT store time zone information
|
|
|
|
if not s: return None
|
|
|
|
hour, minutes, seconds = s.split(':')
|
|
|
|
if '.' in seconds: # check whether seconds have a fractional part
|
|
|
|
seconds, microseconds = seconds.split('.')
|
|
|
|
else:
|
|
|
|
microseconds = '0'
|
|
|
|
return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000))
|
|
|
|
|
|
|
|
def typecast_timestamp(s): # does NOT store time zone information
|
|
|
|
# "2005-07-29 15:48:00.590358-05"
|
|
|
|
# "2005-07-29 09:56:00-05"
|
|
|
|
if not s: return None
|
|
|
|
if not ' ' in s: return typecast_date(s)
|
|
|
|
d, t = s.split()
|
|
|
|
# Extract timezone information, if it exists. Currently we just throw
|
|
|
|
# it away, but in the future we may make use of it.
|
|
|
|
if '-' in t:
|
|
|
|
t, tz = t.split('-', 1)
|
|
|
|
tz = '-' + tz
|
|
|
|
elif '+' in t:
|
|
|
|
t, tz = t.split('+', 1)
|
|
|
|
tz = '+' + tz
|
|
|
|
else:
|
|
|
|
tz = ''
|
|
|
|
dates = d.split('-')
|
|
|
|
times = t.split(':')
|
|
|
|
seconds = times[2]
|
|
|
|
if '.' in seconds: # check whether seconds have a fractional part
|
|
|
|
seconds, microseconds = seconds.split('.')
|
|
|
|
else:
|
|
|
|
microseconds = '0'
|
|
|
|
return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
|
|
|
|
int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000))
|
|
|
|
|
|
|
|
def typecast_boolean(s):
|
|
|
|
if s is None: return None
|
|
|
|
if not s: return False
|
|
|
|
return str(s)[0].lower() == 't'
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
def typecast_decimal(s):
|
2007-06-10 09:52:56 +08:00
|
|
|
if s is None or s == '':
|
2007-05-21 09:29:58 +08:00
|
|
|
return None
|
|
|
|
return decimal.Decimal(s)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
###############################################
|
|
|
|
# Converters from Python to database (string) #
|
|
|
|
###############################################
|
|
|
|
|
|
|
|
def rev_typecast_boolean(obj, d):
|
|
|
|
return obj and '1' or '0'
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
def rev_typecast_decimal(d):
|
|
|
|
if d is None:
|
|
|
|
return None
|
|
|
|
return str(d)
|
|
|
|
|
2010-09-13 13:08:10 +08:00
|
|
|
def truncate_name(name, length=None, hash_len=4):
|
2007-06-23 22:16:00 +08:00
|
|
|
"""Shortens a string to a repeatable mangled version with the given length.
|
|
|
|
"""
|
|
|
|
if length is None or len(name) <= length:
|
|
|
|
return name
|
|
|
|
|
2010-09-13 13:08:10 +08:00
|
|
|
hash = md5_constructor(name).hexdigest()[:hash_len]
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2010-09-13 13:08:10 +08:00
|
|
|
return '%s%s' % (name[:length-hash_len], hash)
|
2008-07-29 13:09:29 +08:00
|
|
|
|
|
|
|
def format_number(value, max_digits, decimal_places):
|
|
|
|
"""
|
|
|
|
Formats a number into a string with the requisite number of digits and
|
|
|
|
decimal places.
|
|
|
|
"""
|
2008-11-12 08:35:24 +08:00
|
|
|
if isinstance(value, decimal.Decimal):
|
|
|
|
context = decimal.getcontext().copy()
|
|
|
|
context.prec = max_digits
|
|
|
|
return u'%s' % str(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))
|
|
|
|
else:
|
|
|
|
return u"%.*f" % (decimal_places, value)
|