summaryrefslogtreecommitdiffstats
path: root/webapp/django/db/backends/util.py
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2008-10-21 07:00:00 -0700
committerThe Android Open Source Project <initial-contribution@android.com>2008-10-21 07:00:00 -0700
commit38966837f9f0b331b3cafa3dccb8b41f8c42c671 (patch)
treeff79845ba8f053cc0e57ee2020acd5957cc2022e /webapp/django/db/backends/util.py
Initial Contributionv1.0
Diffstat (limited to 'webapp/django/db/backends/util.py')
-rw-r--r--webapp/django/db/backends/util.py127
1 files changed, 127 insertions, 0 deletions
diff --git a/webapp/django/db/backends/util.py b/webapp/django/db/backends/util.py
new file mode 100644
index 0000000000..7228b4046b
--- /dev/null
+++ b/webapp/django/db/backends/util.py
@@ -0,0 +1,127 @@
+import datetime
+from time import time
+
+from django.utils.hashcompat import md5_constructor
+
+try:
+ import decimal
+except ImportError:
+ from django.utils import _decimal as decimal # for Python 2.3
+
+class CursorDebugWrapper(object):
+ def __init__(self, cursor, db):
+ self.cursor = cursor
+ self.db = db # Instance of a BaseDatabaseWrapper subclass
+
+ def execute(self, sql, params=()):
+ start = time()
+ try:
+ return self.cursor.execute(sql, params)
+ finally:
+ stop = time()
+ sql = self.db.ops.last_executed_query(self.cursor, sql, params)
+ self.db.queries.append({
+ 'sql': sql,
+ 'time': "%.3f" % (stop - start),
+ })
+
+ def executemany(self, sql, param_list):
+ start = time()
+ try:
+ return self.cursor.executemany(sql, param_list)
+ finally:
+ stop = time()
+ self.db.queries.append({
+ 'sql': '%s times: %s' % (len(param_list), sql),
+ 'time': "%.3f" % (stop - start),
+ })
+
+ def __getattr__(self, attr):
+ if attr in self.__dict__:
+ return self.__dict__[attr]
+ else:
+ return getattr(self.cursor, attr)
+
+ def __iter__(self):
+ return iter(self.cursor)
+
+###############################################
+# 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'
+
+def typecast_decimal(s):
+ if s is None or s == '':
+ return None
+ return decimal.Decimal(s)
+
+###############################################
+# Converters from Python to database (string) #
+###############################################
+
+def rev_typecast_boolean(obj, d):
+ return obj and '1' or '0'
+
+def rev_typecast_decimal(d):
+ if d is None:
+ return None
+ return str(d)
+
+def truncate_name(name, length=None):
+ """Shortens a string to a repeatable mangled version with the given length.
+ """
+ if length is None or len(name) <= length:
+ return name
+
+ hash = md5_constructor(name).hexdigest()[:4]
+
+ return '%s%s' % (name[:length-4], hash)
+
+def format_number(value, max_digits, decimal_places):
+ """
+ Formats a number into a string with the requisite number of digits and
+ decimal places.
+ """
+ return u"%.*f" % (decimal_places, value)