1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Quoted field names in cache db backend SQL.

On Firebird, 'value' is a reserved word and must be quoted.
This commit is contained in:
Hajime Nakagami 2018-03-14 11:46:41 +09:00 committed by Tim Graham
parent acfc650f2a
commit 8f75984c26

View File

@ -51,11 +51,20 @@ class DatabaseCache(BaseDatabaseCache):
self.validate_key(key) self.validate_key(key)
db = router.db_for_read(self.cache_model_class) db = router.db_for_read(self.cache_model_class)
connection = connections[db] connection = connections[db]
table = connection.ops.quote_name(self._table) quote_name = connection.ops.quote_name
table = quote_name(self._table)
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute("SELECT cache_key, value, expires FROM %s " cursor.execute(
"WHERE cache_key = %%s" % table, [key]) 'SELECT %s, %s, %s FROM %s WHERE %s = %%s' % (
quote_name('cache_key'),
quote_name('value'),
quote_name('expires'),
table,
quote_name('cache_key'),
),
[key]
)
row = cursor.fetchone() row = cursor.fetchone()
if row is None: if row is None:
return default return default
@ -73,8 +82,13 @@ class DatabaseCache(BaseDatabaseCache):
db = router.db_for_write(self.cache_model_class) db = router.db_for_write(self.cache_model_class)
connection = connections[db] connection = connections[db]
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute("DELETE FROM %s " cursor.execute(
"WHERE cache_key = %%s" % table, [key]) 'DELETE FROM %s WHERE %s = %%s' % (
table,
quote_name('cache_key'),
),
[key]
)
return default return default
value = connection.ops.process_clob(row[1]) value = connection.ops.process_clob(row[1])
@ -94,7 +108,8 @@ class DatabaseCache(BaseDatabaseCache):
timeout = self.get_backend_timeout(timeout) timeout = self.get_backend_timeout(timeout)
db = router.db_for_write(self.cache_model_class) db = router.db_for_write(self.cache_model_class)
connection = connections[db] connection = connections[db]
table = connection.ops.quote_name(self._table) quote_name = connection.ops.quote_name
table = quote_name(self._table)
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute("SELECT COUNT(*) FROM %s" % table) cursor.execute("SELECT COUNT(*) FROM %s" % table)
@ -120,8 +135,15 @@ class DatabaseCache(BaseDatabaseCache):
# so be careful about changes here - test suite will NOT pick # so be careful about changes here - test suite will NOT pick
# regressions. # regressions.
with transaction.atomic(using=db): with transaction.atomic(using=db):
cursor.execute("SELECT cache_key, expires FROM %s " cursor.execute(
"WHERE cache_key = %%s" % table, [key]) 'SELECT %s, %s FROM %s WHERE %s = %%s' % (
quote_name('cache_key'),
quote_name('expires'),
table,
quote_name('cache_key'),
),
[key]
)
result = cursor.fetchone() result = cursor.fetchone()
if result: if result:
@ -136,13 +158,25 @@ class DatabaseCache(BaseDatabaseCache):
exp = connection.ops.adapt_datetimefield_value(exp) exp = connection.ops.adapt_datetimefield_value(exp)
if result and (mode == 'set' or (mode == 'add' and current_expires < now)): if result and (mode == 'set' or (mode == 'add' and current_expires < now)):
cursor.execute("UPDATE %s SET value = %%s, expires = %%s " cursor.execute(
"WHERE cache_key = %%s" % table, 'UPDATE %s SET %s = %%s, %s = %%s WHERE %s = %%s' % (
[b64encoded, exp, key]) table,
quote_name('value'),
quote_name('expires'),
quote_name('cache_key'),
),
[b64encoded, exp, key]
)
else: else:
cursor.execute("INSERT INTO %s (cache_key, value, expires) " cursor.execute(
"VALUES (%%s, %%s, %%s)" % table, 'INSERT INTO %s (%s, %s, %s) VALUES (%%s, %%s, %%s)' % (
[key, b64encoded, exp]) table,
quote_name('cache_key'),
quote_name('value'),
quote_name('expires'),
),
[key, b64encoded, exp]
)
except DatabaseError: except DatabaseError:
# To be threadsafe, updates/inserts are allowed to fail silently # To be threadsafe, updates/inserts are allowed to fail silently
return False return False
@ -166,7 +200,7 @@ class DatabaseCache(BaseDatabaseCache):
db = router.db_for_read(self.cache_model_class) db = router.db_for_read(self.cache_model_class)
connection = connections[db] connection = connections[db]
table = connection.ops.quote_name(self._table) quote_name = connection.ops.quote_name
if settings.USE_TZ: if settings.USE_TZ:
now = datetime.utcnow() now = datetime.utcnow()
@ -175,9 +209,14 @@ class DatabaseCache(BaseDatabaseCache):
now = now.replace(microsecond=0) now = now.replace(microsecond=0)
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute("SELECT cache_key FROM %s " cursor.execute(
"WHERE cache_key = %%s and expires > %%s" % table, 'SELECT %s FROM %s WHERE %s = %%s and expires > %%s' % (
[key, connection.ops.adapt_datetimefield_value(now)]) quote_name('cache_key'),
quote_name(self._table),
quote_name('cache_key'),
),
[key, connection.ops.adapt_datetimefield_value(now)]
)
return cursor.fetchone() is not None return cursor.fetchone() is not None
def _cull(self, db, cursor, now): def _cull(self, db, cursor, now):