4 class CursorDebugWrapper(object):
5 def __init__(self
, cursor
, db
):
9 def execute(self
, sql
, params
=()):
12 return self
.cursor
.execute(sql
, params
)
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({
21 'time': "%.3f" % (stop
- start
),
24 def executemany(self
, sql
, param_list
):
27 return self
.cursor
.executemany(sql
, param_list
)
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
]
39 return getattr(self
.cursor
, attr
)
41 ###############################################
42 # Converters from database (string) to Python #
43 ###############################################
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
50 hour
, minutes
, seconds
= s
.split(':')
51 if '.' in seconds
: # check whether seconds have a fractional part
52 seconds
, microseconds
= seconds
.split('.')
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"
61 if not ' ' in s
: return typecast_date(s
)
63 # Extract timezone information, if it exists. Currently we just throw
64 # it away, but in the future we may make use of it.
66 t
, tz
= t
.split('-', 1)
69 t
, tz
= t
.split('+', 1)
76 if '.' in seconds
: # check whether seconds have a fractional part
77 seconds
, microseconds
= seconds
.split('.')
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()
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
)