2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
import datetime
|
2010-05-04 22:00:30 +08:00
|
|
|
import decimal
|
2011-03-28 10:11:19 +08:00
|
|
|
import hashlib
|
2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
2006-05-02 09:31:56 +08:00
|
|
|
from time import time
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.conf import settings
|
2012-08-29 02:59:56 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import utc
|
2008-08-02 13:56:57 +08:00
|
|
|
|
2011-02-12 21:03:34 +08:00
|
|
|
|
2012-09-21 03:03:24 +08:00
|
|
|
logger = logging.getLogger('django.db.backends')
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2011-02-12 21:03:34 +08:00
|
|
|
|
|
|
|
class CursorWrapper(object):
|
2011-02-14 08:06:52 +08:00
|
|
|
def __init__(self, cursor, db):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.cursor = cursor
|
2011-02-14 08:06:52 +08:00
|
|
|
self.db = db
|
2011-02-12 21:03:34 +08:00
|
|
|
|
2013-09-23 04:14:17 +08:00
|
|
|
WRAP_ERROR_ATTRS = frozenset(['fetchone', 'fetchmany', 'fetchall', 'nextset'])
|
2013-09-15 15:12:16 +08:00
|
|
|
|
2012-01-13 05:55:19 +08:00
|
|
|
def __getattr__(self, attr):
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
cursor_attr = getattr(self.cursor, attr)
|
2013-09-15 15:12:16 +08:00
|
|
|
if attr in CursorWrapper.WRAP_ERROR_ATTRS:
|
|
|
|
return self.db.wrap_database_errors(cursor_attr)
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
else:
|
|
|
|
return cursor_attr
|
2011-02-12 21:03:34 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.cursor)
|
|
|
|
|
2013-09-24 08:17:59 +08:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
# Ticket #17671 - Close instead of passing thru to avoid backend
|
2014-04-24 14:41:37 +08:00
|
|
|
# specific behavior. Catch errors liberally because errors in cleanup
|
|
|
|
# code aren't useful.
|
|
|
|
try:
|
|
|
|
self.close()
|
|
|
|
except self.db.Database.Error:
|
|
|
|
pass
|
2013-09-24 08:17:59 +08:00
|
|
|
|
2013-09-23 04:14:17 +08:00
|
|
|
# The following methods cannot be implemented in __getattr__, because the
|
|
|
|
# code must run when the method is invoked, not just when it is accessed.
|
2011-02-12 21:03:34 +08:00
|
|
|
|
2013-09-23 04:14:17 +08:00
|
|
|
def callproc(self, procname, params=None):
|
|
|
|
self.db.validate_no_broken_transaction()
|
|
|
|
self.db.set_dirty()
|
|
|
|
with self.db.wrap_database_errors:
|
|
|
|
if params is None:
|
|
|
|
return self.cursor.callproc(procname)
|
|
|
|
else:
|
|
|
|
return self.cursor.callproc(procname, params)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-03-23 23:09:56 +08:00
|
|
|
def execute(self, sql, params=None):
|
2013-09-23 04:14:17 +08:00
|
|
|
self.db.validate_no_broken_transaction()
|
2013-02-20 09:11:54 +08:00
|
|
|
self.db.set_dirty()
|
2013-09-23 04:14:17 +08:00
|
|
|
with self.db.wrap_database_errors:
|
|
|
|
if params is None:
|
|
|
|
return self.cursor.execute(sql)
|
|
|
|
else:
|
|
|
|
return self.cursor.execute(sql, params)
|
|
|
|
|
|
|
|
def executemany(self, sql, param_list):
|
|
|
|
self.db.validate_no_broken_transaction()
|
|
|
|
self.db.set_dirty()
|
|
|
|
with self.db.wrap_database_errors:
|
|
|
|
return self.cursor.executemany(sql, param_list)
|
|
|
|
|
|
|
|
|
|
|
|
class CursorDebugWrapper(CursorWrapper):
|
|
|
|
|
|
|
|
# XXX callproc isn't instrumented at this time.
|
|
|
|
|
|
|
|
def execute(self, sql, params=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
start = time()
|
|
|
|
try:
|
2013-09-23 04:14:17 +08:00
|
|
|
return super(CursorDebugWrapper, self).execute(sql, params)
|
2006-05-02 09:31:56 +08:00
|
|
|
finally:
|
|
|
|
stop = time()
|
2010-10-04 23:12:39 +08:00
|
|
|
duration = stop - start
|
2011-02-14 08:06:52 +08:00
|
|
|
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
|
|
|
|
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),
|
2012-01-23 00:41:20 +08:00
|
|
|
extra={'duration': duration, 'sql': sql, 'params': params}
|
2010-10-04 23:12:39 +08:00
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def executemany(self, sql, param_list):
|
|
|
|
start = time()
|
|
|
|
try:
|
2013-09-23 04:14:17 +08:00
|
|
|
return super(CursorDebugWrapper, self).executemany(sql, param_list)
|
2006-05-02 09:31:56 +08:00
|
|
|
finally:
|
|
|
|
stop = time()
|
2010-10-04 23:12:39 +08:00
|
|
|
duration = stop - start
|
2012-01-23 00:41:20 +08:00
|
|
|
try:
|
|
|
|
times = len(param_list)
|
|
|
|
except TypeError: # param_list could be an iterator
|
|
|
|
times = '?'
|
2011-02-14 08:06:52 +08:00
|
|
|
self.db.queries.append({
|
2012-01-23 00:41:20 +08:00
|
|
|
'sql': '%s times: %s' % (times, 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),
|
2012-01-23 00:41:20 +08:00
|
|
|
extra={'duration': duration, 'sql': sql, 'params': param_list}
|
2010-10-04 23:12:39 +08:00
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-02-12 21:03:34 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
###############################################
|
|
|
|
# Converters from database (string) to Python #
|
|
|
|
###############################################
|
|
|
|
|
|
|
|
def typecast_date(s):
|
2013-11-03 05:02:56 +08:00
|
|
|
return datetime.date(*map(int, s.split('-'))) if s else None # returns None if s is null
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2013-11-03 05:02:56 +08:00
|
|
|
def typecast_time(s): # does NOT store time zone information
|
2013-07-08 08:39:54 +08:00
|
|
|
if not s:
|
|
|
|
return None
|
2006-05-02 09:31:56 +08:00
|
|
|
hour, minutes, seconds = s.split(':')
|
2013-11-03 05:02:56 +08:00
|
|
|
if '.' in seconds: # check whether seconds have a fractional part
|
2006-05-02 09:31:56 +08:00
|
|
|
seconds, microseconds = seconds.split('.')
|
|
|
|
else:
|
|
|
|
microseconds = '0'
|
2013-07-08 08:39:54 +08:00
|
|
|
return datetime.time(int(hour), int(minutes), int(seconds), int(float('.' + microseconds) * 1000000))
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-03 05:02:56 +08:00
|
|
|
def typecast_timestamp(s): # does NOT store time zone information
|
2006-05-02 09:31:56 +08:00
|
|
|
# "2005-07-29 15:48:00.590358-05"
|
|
|
|
# "2005-07-29 09:56:00-05"
|
2013-07-08 08:39:54 +08:00
|
|
|
if not s:
|
|
|
|
return None
|
2014-03-31 03:11:05 +08:00
|
|
|
if ' ' not in s:
|
2013-07-08 08:39:54 +08:00
|
|
|
return typecast_date(s)
|
2006-05-02 09:31:56 +08:00
|
|
|
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]
|
2013-11-03 05:02:56 +08:00
|
|
|
if '.' in seconds: # check whether seconds have a fractional part
|
2006-05-02 09:31:56 +08:00
|
|
|
seconds, microseconds = seconds.split('.')
|
|
|
|
else:
|
|
|
|
microseconds = '0'
|
2011-11-18 21:01:06 +08:00
|
|
|
tzinfo = utc if settings.USE_TZ else None
|
2006-05-02 09:31:56 +08:00
|
|
|
return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
|
2011-11-18 21:01:06 +08:00
|
|
|
int(times[0]), int(times[1]), int(seconds),
|
|
|
|
int((microseconds + '000000')[:6]), tzinfo)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
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)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
###############################################
|
|
|
|
# Converters from Python to database (string) #
|
|
|
|
###############################################
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
def rev_typecast_decimal(d):
|
|
|
|
if d is None:
|
|
|
|
return None
|
|
|
|
return str(d)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
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
|
|
|
|
|
2012-08-29 02:59:56 +08:00
|
|
|
hsh = hashlib.md5(force_bytes(name)).hexdigest()[:hash_len]
|
2013-07-08 08:39:54 +08:00
|
|
|
return '%s%s' % (name[:length - hash_len], hsh)
|
|
|
|
|
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
|
2013-04-01 04:29:35 +08:00
|
|
|
return "{0:f}".format(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))
|
2008-11-12 08:35:24 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
return "%.*f" % (decimal_places, value)
|