From 871ffbe7ee30fae603cbde984ac5975ad5516aa1 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 26 Sep 2005 03:14:37 +0000 Subject: [PATCH] Fixed #515 (again) - renamed "key" field in SQL cache to "cache_key" because MySQL reserves "key". git-svn-id: http://code.djangoproject.com/svn/django/trunk@695 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/cache.py | 18 +++++++++--------- django/core/management.py | 3 ++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/django/core/cache.py b/django/core/cache.py index 6437766c2c..51a22ed2d8 100644 --- a/django/core/cache.py +++ b/django/core/cache.py @@ -377,13 +377,13 @@ class _DBCache(_Cache): def get(self, key, default=None): cursor = db.cursor() - cursor.execute("SELECT key, value, expires FROM %s WHERE key = %%s" % self._table, [key]) + cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key]) row = cursor.fetchone() if row is None: return default now = datetime.now() if row[2] < now: - cursor.execute("DELETE FROM %s WHERE key = %%s" % self._table, [key]) + cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) db.commit() return default return pickle.loads(base64.decodestring(row[1])) @@ -399,21 +399,21 @@ class _DBCache(_Cache): if num > self._max_entries: self._cull(cursor, now) encoded = base64.encodestring(pickle.dumps(value, 2)).strip() - cursor.execute("SELECT key FROM %s WHERE key = %%s" % self._table, [key]) + cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) if cursor.fetchone(): - cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE key = %%s" % self._table, [encoded, str(exp), key]) + cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key]) else: - cursor.execute("INSERT INTO %s (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, str(exp)]) db.commit() def delete(self, key): cursor = db.cursor() - cursor.execute("DELETE FROM %s WHERE key = %%s" % self._table, [key]) + cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) db.commit() def has_key(self, key): cursor = db.cursor() - cursor.execute("SELECT key FROM %s WHERE key = %%s" % self._table, [key]) + cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) return cursor.fetchone() is not None def _cull(self, cursor, now): @@ -424,8 +424,8 @@ class _DBCache(_Cache): cursor.execute("SELECT COUNT(*) FROM %s" % self._table) num = cursor.fetchone()[0] if num > self._max_entries: - cursor.execute("SELECT key FROM %s ORDER BY key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency]) - cursor.execute("DELETE FROM %s WHERE key < %%s" % self._table, [cursor.fetchone()[0]]) + 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]]) ########################################## # Read settings and load a cache backend # diff --git a/django/core/management.py b/django/core/management.py index b02914b549..ae0b6683b5 100644 --- a/django/core/management.py +++ b/django/core/management.py @@ -626,7 +626,8 @@ def createcachetable(tablename): "Creates the table needed to use the SQL cache backend" from django.core import db, meta fields = ( - meta.CharField(name='key', maxlength=255, unique=True, primary_key=True), + # "key" is a reserved word in MySQL, so use "cache_key" instead. + meta.CharField(name='cache_key', maxlength=255, unique=True, primary_key=True), meta.TextField(name='value'), meta.DateTimeField(name='expires', db_index=True), )