From f12969a4d8dd8a4f91a9da4cc9d855457a5a0060 Mon Sep 17 00:00:00 2001 From: Jason Kirtland Date: Wed, 2 Apr 2008 11:39:26 +0000 Subject: - Revamped the Connection memoize decorator a bit, moved to engine - MySQL character set caching is more aggressive but will invalidate the cache if a SET is issued. - MySQL connection memos are namespaced: info[('mysql', 'server_variable')] --- lib/sqlalchemy/engine/base.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/engine/base.py') diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 3d7e65198..cd662ac92 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -12,7 +12,7 @@ higher-level statement-construction, connection-management, execution and result contexts. """ -import StringIO, sys +import inspect, StringIO, sys from sqlalchemy import exceptions, schema, util, types, logging from sqlalchemy.sql import expression @@ -1864,3 +1864,27 @@ class DefaultRunner(schema.SchemaVisitor): return default.arg(self.context) else: return default.arg + + +def connection_memoize(key): + """Decorator, memoize a function in a connection.info stash. + + Only applicable to functions which take no arguments other than a + connection. The memo will be stored in ``connection.info[key]``. + + """ + def decorate(fn): + spec = inspect.getargspec(fn) + assert len(spec[0]) == 2 + assert spec[0][1] == 'connection' + assert spec[1:3] == (None, None) + + def decorated(self, connection): + try: + return connection.info[key] + except KeyError: + connection.info[key] = val = fn(self, connection) + return val + + return util.function_named(decorated, fn.__name__) + return decorate -- cgit v1.2.1