work around cursor.rowcount not returning anything useful before 1st fetch with mssql (closes #1910869) oldstable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Fri, 26 Aug 2011 17:14:37 +0200
brancholdstable
changeset 7753 681ef2a664dd
parent 7711 458cb2edf63a
child 7754 e3da9e7fc584
child 7857 074c848a3712
work around cursor.rowcount not returning anything useful before 1st fetch with mssql (closes #1910869) use select count(*) from table to get the number of rows
server/sources/native.py
--- a/server/sources/native.py	Wed Jul 27 16:43:07 2011 +0200
+++ b/server/sources/native.py	Fri Aug 26 17:14:37 2011 +0200
@@ -1686,21 +1686,24 @@
         archive.writestr('sequences/%s' % seq, serialized)
 
     def write_table(self, archive, table):
+        nb_lines_sql = 'SELECT COUNT(*) FROM %s' % table
+        self.cursor.execute(nb_lines_sql)
+        rowcount = self.cursor.fetchone()[0]
         sql = 'SELECT * FROM %s' % table
         columns, rows_iterator = self._get_cols_and_rows(sql)
-        self.logger.info('number of rows: %d', self.cursor.rowcount)
+        self.logger.info('number of rows: %d', rowcount)
         if table.startswith('cw_'): # entities
             blocksize = 2000
         else: # relations and metadata
             blocksize = 10000
-        if self.cursor.rowcount > 0:
-            for i, start in enumerate(xrange(0, self.cursor.rowcount, blocksize)):
+        if rowcount > 0:
+            for i, start in enumerate(xrange(0, rowcount, blocksize)):
                 rows = list(itertools.islice(rows_iterator, blocksize))
                 serialized = self._serialize(table, columns, rows)
                 archive.writestr('tables/%s.%04d' % (table, i), serialized)
                 self.logger.debug('wrote rows %d to %d (out of %d) to %s.%04d',
                                   start, start+len(rows)-1,
-                                  self.cursor.rowcount,
+                                  rowcount,
                                   table, i)
         else:
             rows = []