Commits

Mike Bayer committed b66b74f

- added array slice support to row[<index>] [ticket:686]

Comments (0)

Files changed (3)

     within expressions
   - added select().with_prefix('foo') allowing any set of keywords to be
     placed before the columns clause of the SELECT [ticket:504]
+  - added array slice support to row[<index>] [ticket:686]
   - result sets make a better attempt at matching the DBAPI types present
     in cursor.description to the TypeEngine objects defined by the dialect,
     which are then used for result-processing. Note this only takes effect 

lib/sqlalchemy/engine/base.py

         return self.__parent._has_key(self.__row, key)
 
     def __getitem__(self, key):
-        return self.__parent._get_col(self.__row, key)
+        if isinstance(key, slice):
+            indices = key.indices(len(self))
+            return tuple([self.__parent._get_col(self.__row, i) for i in range(*indices)])
+        else:
+            return self.__parent._get_col(self.__row, key)
 
     def __getattr__(self, name):
         try:
     def test_column_accessor(self):
         users.insert().execute(user_id=1, user_name='john')
         users.insert().execute(user_id=2, user_name='jack')
+        addresses.insert().execute(address_id=1, user_id=2, address='foo@bar.com')
+        
         r = users.select(users.c.user_id==2).execute().fetchone()
         self.assert_(r.user_id == r['user_id'] == r[users.c.user_id] == 2)
         self.assert_(r.user_name == r['user_name'] == r[users.c.user_name] == 'jack')
         self.assert_(r.user_id == r['user_id'] == r[users.c.user_id] == 2)
         self.assert_(r.user_name == r['user_name'] == r[users.c.user_name] == 'jack')
     
+        # test slices
+        r = text("select * from query_addresses", bind=testbase.db).execute().fetchone()
+        self.assert_(r[0:1] == (1,))
+        self.assert_(r[1:] == (2, 'foo@bar.com'))
+        self.assert_(r[:-1] == (1, 2))
+        
     def test_ambiguous_column(self):
         users.insert().execute(user_id=1, user_name='john')
         r = users.outerjoin(addresses).select().execute().fetchone()