]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
ActiveMapper now supports autoloading of column definitions if you supply
authorJonathan LaCour <jonathan@cleverdevil.org>
Fri, 14 Jul 2006 14:54:53 +0000 (14:54 +0000)
committerJonathan LaCour <jonathan@cleverdevil.org>
Fri, 14 Jul 2006 14:54:53 +0000 (14:54 +0000)
a __autoload__ = True attribute in your inner mapping class.  It does not
currently support autoloading relationships.

CHANGES
lib/sqlalchemy/ext/activemapper.py

diff --git a/CHANGES b/CHANGES
index 0efe992629ddea46409acc406dd544c0fff578d0..28b3316fc939f70213a1dc54fead385ead7bd1f2 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -12,6 +12,10 @@ activated when activemapper is imported
 - added count()/count_by() to list of methods proxied by assignmapper;
 this also adds them to activemapper
 - connection exceptions wrapped in DBAPIError
+- ActiveMapper now supports autoloading column definitions from the
+database if you supply a __autoload__ = True attribute in your
+mapping inner-class.  Currently this does not support reflecting 
+any relationships.
 
 0.2.5
 - fixed endless loop bug in select_by(), if the traversal hit
index 957496e8e206fe3703a6ac61dc76c243cf0cf103..d5fa29a0e0c687babcb4d4045ff779ce6c5e4efc 100644 (file)
@@ -26,6 +26,7 @@ except AttributeError:
         def __getattr__(self, name):
             return getattr(self.context.current, name)
         session = property(lambda s:s.context.current)
+    
     objectstore = Objectstore(create_session)
 
 
@@ -37,6 +38,7 @@ class column(object):
                  primary_key=False, *args, **kwargs):
         if isinstance(foreign_key, basestring): 
             foreign_key = ForeignKey(foreign_key)
+        
         self.coltype     = coltype
         self.colname     = colname
         self.foreign_key = foreign_key
@@ -58,15 +60,19 @@ class relationship(object):
         self.uselist   = uselist
         self.secondary = secondary
         self.order_by  = order_by
+    
     def process(self, klass, propname, relations):
         relclass = ActiveMapperMeta.classes[self.classname]
+        
         if isinstance(self.order_by, str):
             self.order_by = [ self.order_by ]
+        
         if isinstance(self.order_by, list):
             for itemno in range(len(self.order_by)):
                 if isinstance(self.order_by[itemno], str):
                     self.order_by[itemno] = \
                         getattr(relclass.c, self.order_by[itemno])
+        
         backref = self.create_backref(klass)
         relations[propname] = relation(relclass.mapper,
                                        secondary=self.secondary,
@@ -75,37 +81,48 @@ class relationship(object):
                                        lazy=self.lazy, 
                                        uselist=self.uselist,
                                        order_by=self.order_by)
+    
     def create_backref(self, klass):
         if self.backref is None:
             return None
+        
         relclass = ActiveMapperMeta.classes[self.classname]
+        
         if klass.__name__ == self.classname:
             br_fkey = getattr(relclass.c, self.colname)
         else:
             br_fkey = None
-        return create_backref(self.backref, foreignkey=br_fkey)
         
+        return create_backref(self.backref, foreignkey=br_fkey)
+
+
 class one_to_many(relationship):
     def __init__(self, classname, colname=None, backref=None, private=False,
                  lazy=True, order_by=False):
         relationship.__init__(self, classname, colname, backref, private, 
                               lazy, uselist=True, order_by=order_by)
 
+
 class one_to_one(relationship):
     def __init__(self, classname, colname=None, backref=None, private=False,
                  lazy=True, order_by=False):
         relationship.__init__(self, classname, colname, backref, private, 
                               lazy, uselist=False, order_by=order_by)
+    
     def create_backref(self, klass):
         if self.backref is None:
             return None
+        
         relclass = ActiveMapperMeta.classes[self.classname]
+        
         if klass.__name__ == self.classname:
             br_fkey = getattr(relclass.c, self.colname)
         else:
             br_fkey = None
+        
         return create_backref(self.backref, foreignkey=br_fkey, uselist=False)
 
+
 class many_to_many(relationship):
     def __init__(self, classname, secondary, backref=None, lazy=True,
                  order_by=False):
@@ -113,6 +130,7 @@ class many_to_many(relationship):
                               uselist=True, secondary=secondary,
                               order_by=order_by)
 
+
 # 
 # SQLAlchemy metaclass and superclass that can be used to do SQLAlchemy 
 # mapping in a declarative way, along with a function to process the 
@@ -185,6 +203,7 @@ class ActiveMapperMeta(type):
         table_name = clsname.lower()
         columns    = []
         relations  = {}
+        autoload   = False
         _metadata  = getattr(sys.modules[cls.__module__], 
                              "__metadata__", metadata)
         
@@ -200,6 +219,10 @@ class ActiveMapperMeta(type):
                 if '__metadata__' == name:
                     _metadata= value
                     continue
+                
+                if '__autoload__' == name:
+                    autoload = True
+                    continue
                     
                 if name.startswith('__'): continue
                 
@@ -223,7 +246,7 @@ class ActiveMapperMeta(type):
                 if isinstance(value, relationship):
                     relations[name] = value
             
-            if not found_pk:
+            if not found_pk and not autoload:
                 col = Column('id', Integer, primary_key=True)
                 cls.mapping.id = col
                 columns.append(col)
@@ -231,8 +254,13 @@ class ActiveMapperMeta(type):
             assert _metadata is not None, "No MetaData specified"
             
             ActiveMapperMeta.metadatas.add(_metadata)
-            cls.table = Table(table_name, _metadata, *columns)
-            cls.columns = columns
+            
+            if not autoload:
+                cls.table = Table(table_name, _metadata, *columns)
+                cls.columns = columns
+            else:
+                cls.table = Table(table_name, _metadata, autoload=True)
+                cls.columns = cls.table._columns
             
             # check for inheritence
             if hasattr(bases[0], "mapping"):