[Checkins] SVN: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/ added optional 'table_name' parameter to Model.add()

Andreas Jung andreas at andreas-jung.com
Sat Apr 21 01:10:49 EDT 2007


Log message for revision 74277:
  added optional 'table_name' parameter to Model.add()
  

Changed:
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/CHANGES.txt
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py

-=-
Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/CHANGES.txt
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/CHANGES.txt	2007-04-21 04:54:50 UTC (rev 74276)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/CHANGES.txt	2007-04-21 05:10:48 UTC (rev 74277)
@@ -16,6 +16,11 @@
      available). E.g. when using Postgres you can reference as
      table within a different schema through '<schema>.<tablename>'.
 
+   - Model.add() accepts a new optional parameter 'table_name' that
+     can be used to specify the name of a table (including schema
+     information) when you want to use the 'name' parameter as
+     an alias for the related table/mapper.
+
  
 0.1.6 (28.03.2007)
 

Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py	2007-04-21 04:54:50 UTC (rev 74276)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py	2007-04-21 05:10:48 UTC (rev 74277)
@@ -85,11 +85,13 @@
             # if not: introspect table definition
             if table is None:
 
+                table_name = self._model.get(name, {}).get('table_name') or name
+
                 # check for 'schema.tablename'
-                if '.' in name:
-                    schema, tablename = name.split('.')
+                if '.' in table_name:
+                    schema, tablename = table_name.split('.')
                 else:
-                    tablename, schema = name, None
+                    tablename, schema = table_name, None
 
                 table = Table(tablename, 
                               self._metadata, 
@@ -140,12 +142,12 @@
             mapper =  self._mapper_factory(table, 
                                            properties=properties, 
                                            cls=mapper_class)
-            self.registerMapper(mapper, name)
+            self._registerMapper(mapper, name)
 
         return self[name]
 
 
-    def registerMapper(self, mapper, name):
+    def _registerMapper(self, mapper, name):
         """ register a mapper under a given name """
     
         self._lock.acquire()

Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py	2007-04-21 04:54:50 UTC (rev 74276)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py	2007-04-21 05:10:48 UTC (rev 74277)
@@ -35,7 +35,7 @@
             self.add(**d)
 
 
-    def add(self, name, table=None, mapper_class=None, relations=None, autodetect_relations=False):
+    def add(self, name, table=None, mapper_class=None, relations=None, autodetect_relations=False, table_name=None):
         """ 'name'  -- name of table (no schema support so far!)
 
             'table' -- a sqlalchemy.Table instance (None, for autoloading)
@@ -47,7 +47,10 @@
 
             'autodetect_relations' -- try to autodetect the relationships between tables
                            and auto-construct the relation properties of the mapper if
-                           'relations is omitted'
+                           'relations' is omitted (set to None)
+
+            'table_name' -- optional full name of a table (e.g. 'someschema.sometable') if
+                            you want to use 'name' as alias for the table.
         """
 
         if table is not None and not isinstance(table, sqlalchemy.Table):
@@ -70,7 +73,8 @@
                       'table' : table,
                       'relations' : relations,
                       'mapper_class' : mapper_class,
-                      'autodetect_relations' : autodetect_relations
+                      'autodetect_relations' : autodetect_relations,
+                      'table_name' : table_name,
                      }
 
 



More information about the Checkins mailing list