|
1 import datetime |
|
2 import md5 |
|
3 from time import time |
|
4 |
|
5 try: |
|
6 import decimal |
|
7 except ImportError: |
|
8 from django.utils import _decimal as decimal # for Python 2.3 |
|
9 |
|
10 class CursorDebugWrapper(object): |
|
11 def __init__(self, cursor, db): |
|
12 self.cursor = cursor |
|
13 self.db = db # Instance of a BaseDatabaseWrapper subclass |
|
14 |
|
15 def execute(self, sql, params=()): |
|
16 start = time() |
|
17 try: |
|
18 return self.cursor.execute(sql, params) |
|
19 finally: |
|
20 stop = time() |
|
21 sql = self.db.ops.last_executed_query(self.cursor, sql, params) |
|
22 self.db.queries.append({ |
|
23 'sql': sql, |
|
24 'time': "%.3f" % (stop - start), |
|
25 }) |
|
26 |
|
27 def executemany(self, sql, param_list): |
|
28 start = time() |
|
29 try: |
|
30 return self.cursor.executemany(sql, param_list) |
|
31 finally: |
|
32 stop = time() |
|
33 self.db.queries.append({ |
|
34 'sql': '%s times: %s' % (len(param_list), sql), |
|
35 'time': "%.3f" % (stop - start), |
|
36 }) |
|
37 |
|
38 def __getattr__(self, attr): |
|
39 if attr in self.__dict__: |
|
40 return self.__dict__[attr] |
|
41 else: |
|
42 return getattr(self.cursor, attr) |
|
43 |
|
44 def __iter__(self): |
|
45 return iter(self.cursor) |
|
46 |
|
47 ############################################### |
|
48 # Converters from database (string) to Python # |
|
49 ############################################### |
|
50 |
|
51 def typecast_date(s): |
|
52 return s and datetime.date(*map(int, s.split('-'))) or None # returns None if s is null |
|
53 |
|
54 def typecast_time(s): # does NOT store time zone information |
|
55 if not s: return None |
|
56 hour, minutes, seconds = s.split(':') |
|
57 if '.' in seconds: # check whether seconds have a fractional part |
|
58 seconds, microseconds = seconds.split('.') |
|
59 else: |
|
60 microseconds = '0' |
|
61 return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000)) |
|
62 |
|
63 def typecast_timestamp(s): # does NOT store time zone information |
|
64 # "2005-07-29 15:48:00.590358-05" |
|
65 # "2005-07-29 09:56:00-05" |
|
66 if not s: return None |
|
67 if not ' ' in s: return typecast_date(s) |
|
68 d, t = s.split() |
|
69 # Extract timezone information, if it exists. Currently we just throw |
|
70 # it away, but in the future we may make use of it. |
|
71 if '-' in t: |
|
72 t, tz = t.split('-', 1) |
|
73 tz = '-' + tz |
|
74 elif '+' in t: |
|
75 t, tz = t.split('+', 1) |
|
76 tz = '+' + tz |
|
77 else: |
|
78 tz = '' |
|
79 dates = d.split('-') |
|
80 times = t.split(':') |
|
81 seconds = times[2] |
|
82 if '.' in seconds: # check whether seconds have a fractional part |
|
83 seconds, microseconds = seconds.split('.') |
|
84 else: |
|
85 microseconds = '0' |
|
86 return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]), |
|
87 int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000)) |
|
88 |
|
89 def typecast_boolean(s): |
|
90 if s is None: return None |
|
91 if not s: return False |
|
92 return str(s)[0].lower() == 't' |
|
93 |
|
94 def typecast_decimal(s): |
|
95 if s is None or s == '': |
|
96 return None |
|
97 return decimal.Decimal(s) |
|
98 |
|
99 ############################################### |
|
100 # Converters from Python to database (string) # |
|
101 ############################################### |
|
102 |
|
103 def rev_typecast_boolean(obj, d): |
|
104 return obj and '1' or '0' |
|
105 |
|
106 def rev_typecast_decimal(d): |
|
107 if d is None: |
|
108 return None |
|
109 return str(d) |
|
110 |
|
111 def truncate_name(name, length=None): |
|
112 """Shortens a string to a repeatable mangled version with the given length. |
|
113 """ |
|
114 if length is None or len(name) <= length: |
|
115 return name |
|
116 |
|
117 hash = md5.md5(name).hexdigest()[:4] |
|
118 |
|
119 return '%s%s' % (name[:length-4], hash) |