web/lib/django/core/cache/backends/db.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
--- a/web/lib/django/core/cache/backends/db.py	Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/core/cache/backends/db.py	Tue May 25 02:43:45 2010 +0200
@@ -12,7 +12,7 @@
 class CacheClass(BaseCache):
     def __init__(self, table, params):
         BaseCache.__init__(self, params)
-        self._table = table
+        self._table = connection.ops.quote_name(table)
         max_entries = params.get('max_entries', 300)
         try:
             self._max_entries = int(max_entries)
@@ -60,12 +60,14 @@
             result = cursor.fetchone()
             if result and (mode == 'set' or
                     (mode == 'add' and result[1] < now)):
-                cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
+                cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table,
+                               [encoded, connection.ops.value_to_db_datetime(exp), key])
             else:
-                cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
+                cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table,
+                               [key, encoded, connection.ops.value_to_db_datetime(exp)])
         except DatabaseError:
             # To be threadsafe, updates/inserts are allowed to fail silently
-            transaction.rollback()
+            transaction.rollback_unless_managed()
             return False
         else:
             transaction.commit_unless_managed()
@@ -79,16 +81,22 @@
     def has_key(self, key):
         now = datetime.now().replace(microsecond=0)
         cursor = connection.cursor()
-        cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s and expires > %%s" % self._table, [key, now])
+        cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s and expires > %%s" % self._table,
+                       [key, connection.ops.value_to_db_datetime(now)])
         return cursor.fetchone() is not None
 
     def _cull(self, cursor, now):
         if self._cull_frequency == 0:
-            cursor.execute("DELETE FROM %s" % self._table)
+            self.clear()
         else:
-            cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table, [str(now)])
+            cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table,
+                           [connection.ops.value_to_db_datetime(now)])
             cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
             num = cursor.fetchone()[0]
             if num > self._max_entries:
                 cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency])
                 cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]])
+
+    def clear(self):
+        cursor = connection.cursor()
+        cursor.execute('DELETE FROM %s' % self._table)