Commits

Mike Bayer committed 3d04904

- precompiled regexp for anonymous labels
- has_key()->__contains__()

  • Participants
  • Parent commits 0c5d329

Comments (0)

Files changed (2)

lib/sqlalchemy/ansisql.py

 
 BIND_PARAMS = re.compile(r'(?<![:\w\$\x5c]):([\w\$]+)(?![:\w\$])', re.UNICODE)
 BIND_PARAMS_ESC = re.compile(r'\x5c(:[\w\$]+)(?![:\w\$])', re.UNICODE)
+ANONYMOUS_LABEL = re.compile(r'{ANON (-?\d+) (.*)}')
 
 OPERATORS =  {
     operator.and_ : 'AND',
     def _truncated_identifier(self, ident_class, name):
         if (ident_class, name) in self.generated_ids:
             return self.generated_ids[(ident_class, name)]
-            
-        anonname = self._anonymize(name)
+        
+        anonname = ANONYMOUS_LABEL.sub(self._process_anon, name)
+
         if len(anonname) > self.dialect.max_identifier_length():
             counter = self.generated_ids.get(ident_class, 1)
             truncname = name[0:self.dialect.max_identifier_length() - 6] + "_" + hex(counter)[2:]
             truncname = anonname
         self.generated_ids[(ident_class, name)] = truncname
         return truncname
+
+    def _process_anon(self, match):
+        (ident, derived) = match.group(1,2)
+        if ('anonymous', ident) in self.generated_ids:
+            return self.generated_ids[('anonymous', ident)]
+        else:
+            anonymous_counter = self.generated_ids.get('anonymous', 1)
+            newname = derived + "_" + str(anonymous_counter)
+            self.generated_ids['anonymous'] = anonymous_counter + 1
+            self.generated_ids[('anonymous', ident)] = newname
+            return newname
     
     def _anonymize(self, name):
-        def anon(match):
-            (ident, derived) = match.group(1,2)
-            if ('anonymous', ident) in self.generated_ids:
-                return self.generated_ids[('anonymous', ident)]
-            else:
-                anonymous_counter = self.generated_ids.get('anonymous', 1)
-                newname = derived + "_" + str(anonymous_counter)
-                self.generated_ids['anonymous'] = anonymous_counter + 1
-                self.generated_ids[('anonymous', ident)] = newname
-                return newname
-        return re.sub(r'{ANON (-?\d+) (.*)}', anon, name)
+        return ANONYMOUS_LABEL.sub(self._process_anon, name)
             
     def bindparam_string(self, name):
         return self.bindtemplate % name

lib/sqlalchemy/sql.py

             alias = '{ANON %d %s}' % (id(self), alias or 'anon')
         self.name = alias
         self.encodedname = alias.encode('ascii', 'backslashreplace')
-
+        
     def is_derived_from(self, fromclause):
         x = self.selectable
         while True:
             if self.table is not None and self.table.named_with_column():
                 self.__label = self.table.name + "_" + self.name
                 counter = 1
-                while self.table.c.has_key(self.__label):
+                while self.__label in self.table.c:
                     self.__label = self.__label + "_%d" % counter
                     counter += 1
             else:
         self.encodedname = self.name.encode('ascii', 'backslashreplace')
         self._oid_column = _ColumnClause('oid', self, _is_oid=True)
         self._export_columns(columns)
-
+        
     def _clone(self):
         # TableClause is immutable
         return self