From ad89932715193275d37b5e22b830f092e350b1fe Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 14 Oct 2009 02:19:37 +0000 Subject: [PATCH] remove instanceof() in favor of memoized flags, part of [ticket:1566] --- lib/sqlalchemy/schema.py | 22 +++++++++++++++++++++- lib/sqlalchemy/sql/compiler.py | 16 +++++++--------- test/aaa_profiling/test_zoomark.py | 4 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 845459e817..7965070d1e 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -1043,6 +1043,8 @@ class DefaultGenerator(SchemaItem): __visit_name__ = 'default_generator' + is_sequence = False + def __init__(self, for_update=False): self.for_update = for_update @@ -1084,7 +1086,15 @@ class ColumnDefault(DefaultGenerator): if util.callable(arg): arg = self._maybe_wrap_callable(arg) self.arg = arg - + + @util.memoized_property + def is_callable(self): + return util.callable(self.arg) + + @util.memoized_property + def is_clause_element(self): + return isinstance(self.arg, expression.ClauseElement) + def _maybe_wrap_callable(self, fn): """Backward compat: Wrap callables that don't accept a context.""" @@ -1133,6 +1143,8 @@ class Sequence(DefaultGenerator): __visit_name__ = 'sequence' + is_sequence = True + def __init__(self, name, start=None, increment=None, schema=None, optional=False, quote=None, metadata=None, for_update=False): super(Sequence, self).__init__(for_update=for_update) @@ -1144,6 +1156,14 @@ class Sequence(DefaultGenerator): self.schema = schema self.metadata = metadata + @util.memoized_property + def is_callable(self): + return False + + @util.memoized_property + def is_clause_element(self): + return False + def __repr__(self): return "Sequence(%s)" % ', '.join( [repr(self.name)] + diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 4cf4bd8698..61c6c214f1 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -823,13 +823,12 @@ class SQLCompiler(engine.Compiled): ): if implicit_returning: - if isinstance(c.default, schema.Sequence): + if c.default is not None and c.default.is_sequence: proc = self.process(c.default) if proc is not None: values.append((c, proc)) self.returning.append(c) - elif isinstance(c.default, schema.ColumnDefault) and \ - isinstance(c.default.arg, sql.ClauseElement): + elif c.default is not None and c.default.is_clause_element: values.append((c, self.process(c.default.arg.self_group()))) self.returning.append(c) elif c.default is not None: @@ -842,21 +841,20 @@ class SQLCompiler(engine.Compiled): c.default is not None and \ ( self.dialect.supports_sequences or - not isinstance(c.default, schema.Sequence) + not c.default.is_sequence ) ) or self.dialect.preexecute_autoincrement_sequences: values.append((c, self._create_crud_bind_param(c, None))) self.prefetch.append(c) - elif isinstance(c.default, schema.Sequence): + elif c.default is not None and c.default.is_sequence: proc = self.process(c.default) if proc is not None: values.append((c, proc)) if not c.primary_key: self.postfetch.append(c) - elif isinstance(c.default, schema.ColumnDefault) and \ - isinstance(c.default.arg, sql.ClauseElement): + elif c.default is not None and c.default.is_clause_element: values.append((c, self.process(c.default.arg.self_group()))) if not c.primary_key: @@ -869,8 +867,8 @@ class SQLCompiler(engine.Compiled): if not c.primary_key: self.postfetch.append(c) elif self.isupdate: - if isinstance(c.onupdate, schema.ColumnDefault): - if isinstance(c.onupdate.arg, sql.ClauseElement): + if c.onupdate is not None and not c.onupdate.is_sequence: + if c.onupdate.is_clause_element: values.append((c, self.process(c.onupdate.arg.self_group()))) self.postfetch.append(c) else: diff --git a/test/aaa_profiling/test_zoomark.py b/test/aaa_profiling/test_zoomark.py index 282e38ab0e..75419c7a03 100644 --- a/test/aaa_profiling/test_zoomark.py +++ b/test/aaa_profiling/test_zoomark.py @@ -323,11 +323,11 @@ class ZooMarkTest(TestBase): def test_profile_1_create_tables(self): self.test_baseline_1_create_tables() - @profiling.function_call_count(5726, {'2.4': 3650}) + @profiling.function_call_count(5371, {'2.4': 3650}) def test_profile_1a_populate(self): self.test_baseline_1a_populate() - @profiling.function_call_count(282, {'2.4': 186}) + @profiling.function_call_count(259, {'2.4': 186}) def test_profile_2_insert(self): self.test_baseline_2_insert() -- 2.47.2