Anonymous committed 6738c2d

[svn] Adding lazy mapper for SQLAlchemy 0.4 and object detection of objects that have a SA 0.4 query function on them (Session.mapper).

Comments (0)

Files changed (1)


         if inspect.isclass(obj) and issubclass(obj, sqlobject.SQLObject):
             return SQLObjectLazy(, *args, **kw)
     if orms.get('sqlalchemy'):
-        if hasattr(obj, '_is_primary_mapper') or isinstance(obj, sqlalchemy.Query):
+        if hasattr(obj, '_is_primary_mapper') or \
+           (not sqlalchemy.__version__.startswith('0.4') and \
+            isinstance(obj, sqlalchemy.Query)):
             return SQLAlchemyLazyMapper(obj, *args, **kw)
         if hasattr(obj, 'mapper') and hasattr(obj, 'select') and hasattr(obj, 'count'):
             return SQLAlchemyLazyMapper(obj, *args, **kw)
         if isinstance(obj, sqlalchemy.Table):
             return SQLAlchemyLazyTable(obj, *args, **kw)
+        if hasattr(obj, 'query'):
+            return SQLAlchemy04LazyMapper(obj, *args, **kw)
     raise TypeError("You must call paginate() with either a sequence, an "
                     "SQLObject class or an SQLAlchemy query object.")
             if k != 'order_by':
                 kw[k] = v
         return self.fn.count(*self.args, **kw)
+class SQLAlchemy04LazyMapper(Partial):
+    def __getitem__(self, key):
+        if not isinstance(key, slice):
+            raise Exception, "SQLAlchemyLazy doesn't support getitem without slicing"
+        limit = key.stop - key.start
+        offset = key.start
+        fn = self.fn
+        result = fn.query
+        # Translate keyword args like 'order_by=blah' into func calls for SA 0.4
+        # such that its .order_by(blah) on the query object
+        for key, val in
+            result = getattr(result, key)(val)
+        return result.limit(limit).offset(offset).all()
+    def __len__(self):
+        kw = {}
+        fn = self.fn.query
+        for key, val in
+            fn = getattr(fn, key)(val)
+        return fn.count()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.