[dataimport] allow to specify columns on which result should be sorted in lazydbtable stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Apr 2011 13:28:59 +0200
branchstable
changeset 7201 52f5831400b2
parent 7200 81fd6e40a6a8
child 7202 5b80f5ee61b5
[dataimport] allow to specify columns on which result should be sorted in lazydbtable
dataimport.py
--- a/dataimport.py	Fri Apr 08 09:02:55 2011 +0200
+++ b/dataimport.py	Mon Apr 11 13:28:59 2011 +0200
@@ -147,13 +147,16 @@
     for row in reader:
         yield dict(zip(header, row))
 
-def lazydbtable(cu, table, headers):
+def lazydbtable(cu, table, headers, orderby=None):
     """return an iterator on rows of a sql table. On each row, fetch columns
     defined in headers and return values as a dictionary.
 
     >>> data = lazydbtable(cu, 'experimentation', ('id', 'nickname', 'gps'))
     """
-    cu.execute('SELECT %s FROM %s' % (','.join(headers), table,))
+    sql = 'SELECT %s FROM %s' % (','.join(headers), table,)
+    if orderby:
+        sql += ' ORDER BY %s' % ','.join(orderby)
+    cu.execute(sql)
     while True:
         row = cu.fetchone()
         if row is None: