App Engine Python SDK version $VERSION
[gae.git] / python / lib / django-0.96 / django / db / backends / util.py
blobd8f86fef4ffffabfb9da7480c1536356e1bbec0a
1 import datetime
2 from time import time
4 class CursorDebugWrapper(object):
5 def __init__(self, cursor, db):
6 self.cursor = cursor
7 self.db = db
9 def execute(self, sql, params=()):
10 start = time()
11 try:
12 return self.cursor.execute(sql, params)
13 finally:
14 stop = time()
15 # If params was a list, convert it to a tuple, because string
16 # formatting with '%' only works with tuples or dicts.
17 if not isinstance(params, (tuple, dict)):
18 params = tuple(params)
19 self.db.queries.append({
20 'sql': sql % params,
21 'time': "%.3f" % (stop - start),
24 def executemany(self, sql, param_list):
25 start = time()
26 try:
27 return self.cursor.executemany(sql, param_list)
28 finally:
29 stop = time()
30 self.db.queries.append({
31 'sql': 'MANY: ' + sql + ' ' + str(tuple(param_list)),
32 'time': "%.3f" % (stop - start),
35 def __getattr__(self, attr):
36 if self.__dict__.has_key(attr):
37 return self.__dict__[attr]
38 else:
39 return getattr(self.cursor, attr)
41 ###############################################
42 # Converters from database (string) to Python #
43 ###############################################
45 def typecast_date(s):
46 return s and datetime.date(*map(int, s.split('-'))) or None # returns None if s is null
48 def typecast_time(s): # does NOT store time zone information
49 if not s: return None
50 hour, minutes, seconds = s.split(':')
51 if '.' in seconds: # check whether seconds have a fractional part
52 seconds, microseconds = seconds.split('.')
53 else:
54 microseconds = '0'
55 return datetime.time(int(hour), int(minutes), int(seconds), int(float('.'+microseconds) * 1000000))
57 def typecast_timestamp(s): # does NOT store time zone information
58 # "2005-07-29 15:48:00.590358-05"
59 # "2005-07-29 09:56:00-05"
60 if not s: return None
61 if not ' ' in s: return typecast_date(s)
62 d, t = s.split()
63 # Extract timezone information, if it exists. Currently we just throw
64 # it away, but in the future we may make use of it.
65 if '-' in t:
66 t, tz = t.split('-', 1)
67 tz = '-' + tz
68 elif '+' in t:
69 t, tz = t.split('+', 1)
70 tz = '+' + tz
71 else:
72 tz = ''
73 dates = d.split('-')
74 times = t.split(':')
75 seconds = times[2]
76 if '.' in seconds: # check whether seconds have a fractional part
77 seconds, microseconds = seconds.split('.')
78 else:
79 microseconds = '0'
80 return datetime.datetime(int(dates[0]), int(dates[1]), int(dates[2]),
81 int(times[0]), int(times[1]), int(seconds), int(float('.'+microseconds) * 1000000))
83 def typecast_boolean(s):
84 if s is None: return None
85 if not s: return False
86 return str(s)[0].lower() == 't'
88 ###############################################
89 # Converters from Python to database (string) #
90 ###############################################
92 def rev_typecast_boolean(obj, d):
93 return obj and '1' or '0'
95 ##################################################################################
96 # Helper functions for dictfetch* for databases that don't natively support them #
97 ##################################################################################
99 def _dict_helper(desc, row):
100 "Returns a dictionary for the given cursor.description and result row."
101 return dict(zip([col[0] for col in desc], row))
103 def dictfetchone(cursor):
104 "Returns a row from the cursor as a dict"
105 row = cursor.fetchone()
106 if not row:
107 return None
108 return _dict_helper(cursor.description, row)
110 def dictfetchmany(cursor, number):
111 "Returns a certain number of rows from a cursor as a dict"
112 desc = cursor.description
113 for row in cursor.fetchmany(number):
114 yield _dict_helper(desc, row)
116 def dictfetchall(cursor):
117 "Returns all rows from a cursor as a dict"
118 desc = cursor.description
119 for row in cursor.fetchall():
120 yield _dict_helper(desc, row)