本文整理汇总了Python中transaction.TransactionManager.unpatch方法的典型用法代码示例。如果您正苦于以下问题:Python TransactionManager.unpatch方法的具体用法?Python TransactionManager.unpatch怎么用?Python TransactionManager.unpatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transaction.TransactionManager
的用法示例。
在下文中一共展示了TransactionManager.unpatch方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: QueryCacheBackend
# 需要导入模块: from transaction import TransactionManager [as 别名]
# 或者: from transaction.TransactionManager import unpatch [as 别名]
#.........这里部分代码省略.........
@wraps(original, assigned=available_attrs(original))
def newfun(cls, *args, **kwargs):
db = getattr(cls, 'using', 'default')
from django.db.models.sql import compiler
# we have to do this before we check the tables, since the tables
# are actually being set in the original function
ret = original(cls, *args, **kwargs)
if isinstance(cls, compiler.SQLInsertCompiler):
#Inserts are a special case where cls.tables
#are not populated.
tables = [cls.query.model._meta.db_table]
else:
#if cls.query.tables != list(cls.query.table_map):
# pass
#tables = list(cls.query.table_map)
tables = cls.query.tables
for table in tables:
if not disallowed_table(table):
self.keyhandler.invalidate_table(table, db)
return ret
return newfun
def patch(self):
"""
monkey patches django.db.models.sql.compiler.SQL*Compiler series
"""
from django.db.models.sql import compiler
self._read_compilers = (
compiler.SQLCompiler,
compiler.SQLAggregateCompiler,
compiler.SQLDateCompiler,
)
self._write_compilers = (
compiler.SQLInsertCompiler,
compiler.SQLDeleteCompiler,
compiler.SQLUpdateCompiler,
)
if not self._patched:
self._original = {}
for reader in self._read_compilers:
self._original[reader] = reader.execute_sql
reader.execute_sql = self._monkey_select(reader.execute_sql)
for updater in self._write_compilers:
self._original[updater] = updater.execute_sql
updater.execute_sql = self._monkey_write(updater.execute_sql)
self._patched = True
self.cache_backend.patch()
self._handle_signals()
def unpatch(self):
"""un-applies this patch."""
if not self._patched:
return
for func in self._read_compilers + self._write_compilers:
func.execute_sql = self._original[func]
self.cache_backend.unpatch()
self._patched = False
def invalidate_m2m(self, instance, **kwargs):
if self._patched:
table = resolve_table(instance)
if not disallowed_table(table):
self.keyhandler.invalidate_table(instance)
def invalidate(self, instance, **kwargs):
if self._patched:
table = resolve_table(instance)
if not disallowed_table(table):
self.keyhandler.invalidate_table(table)
tables = set()
tables.add(table)
try:
instance._meta._related_objects_cache
except AttributeError:
instance._meta._fill_related_objects_cache()
for obj in instance._meta._related_objects_cache.keys():
obj_table = obj.model._meta.db_table
if obj_table not in tables:
tables.add(obj_table)
if not disallowed_table(obj_table):
self.keyhandler.invalidate_table(obj_table)
def _handle_signals(self):
post_save.connect(self.invalidate, sender=None)
post_delete.connect(self.invalidate, sender=None)
# FIXME: only needed in 1.1?
signals.qc_m2m_change.connect(self.invalidate_m2m, sender=None)
def flush_query_cache(self):
from django.db import connection
tables = connection.introspection.table_names()
#seen_models = connection.introspection.installed_models(tables)
for table in tables:
# we want this to just work, so invalidate even things in blacklist
self.keyhandler.invalidate_table(table)