changeset 130d98839af1 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=130d98839af1
description:
        Replace cursor fetchall by iteration over cursor

        issue5817
        review335831003
diffstat:

 trytond/admin.py                       |   2 +-
 trytond/backend/postgresql/database.py |   2 +-
 trytond/backend/postgresql/table.py    |  10 +++++-----
 trytond/backend/sqlite/database.py     |   2 +-
 trytond/backend/sqlite/table.py        |   7 +++----
 trytond/cache.py                       |   2 +-
 trytond/ir/lang.py                     |   2 +-
 trytond/ir/model.py                    |   8 ++++----
 trytond/ir/note.py                     |   2 +-
 trytond/ir/trigger.py                  |   6 +++---
 trytond/model/fields/many2one.py       |   2 +-
 trytond/model/modelsql.py              |  12 +++++-------
 trytond/modules/__init__.py            |  27 +++++++++++++--------------
 trytond/tools/multivalue.py            |   2 +-
 14 files changed, 41 insertions(+), 45 deletions(-)

diffs (350 lines):

diff -r c560c85b1cb3 -r 130d98839af1 trytond/admin.py
--- a/trytond/admin.py  Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/admin.py  Sun Mar 21 16:09:13 2021 +0100
@@ -44,7 +44,7 @@
                 lang = Table('ir_lang')
                 cursor.execute(*lang.select(lang.code,
                         where=lang.translatable == Literal(True)))
-                lang = set([x[0] for x in cursor.fetchall()])
+                lang = set([x[0] for x in cursor])
             lang.add(main_lang)
         else:
             lang = set()
diff -r c560c85b1cb3 -r 130d98839af1 trytond/backend/postgresql/database.py
--- a/trytond/backend/postgresql/database.py    Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/backend/postgresql/database.py    Sun Mar 21 16:09:13 2021 +0100
@@ -356,7 +356,7 @@
             try:
                 cursor.execute(
                     'SELECT hostname FROM ir_configuration')
-                hostnames = {h for h, in cursor.fetchall() if h}
+                hostnames = {h for h, in cursor if h}
                 if hostnames and hostname not in hostnames:
                     return False
             except ProgrammingError:
diff -r c560c85b1cb3 -r 130d98839af1 trytond/backend/postgresql/table.py
--- a/trytond/backend/postgresql/table.py       Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/backend/postgresql/table.py       Sun Mar 21 16:09:13 2021 +0100
@@ -139,7 +139,7 @@
                 'FROM information_schema.columns '
                 'WHERE table_name = %s AND table_schema = %s',
                 (self.table_name, self.table_schema))
-            for column, typname, nullable, size, default in cursor.fetchall():
+            for column, typname, nullable, size, default in cursor:
                 self._columns[column] = {
                     'typname': typname,
                     'notnull': True if nullable == 'NO' else False,
@@ -153,7 +153,7 @@
                 'FROM information_schema.table_constraints '
                 'WHERE table_name = %s AND table_schema = %s',
                 (self.table_name, self.table_schema))
-            self._constraints = [c for c, in cursor.fetchall()]
+            self._constraints = [c for c, in cursor]
 
             # add nonstandard exclude constraint
             cursor.execute('SELECT c.conname '
@@ -167,7 +167,7 @@
                     "AND r.relkind IN ('r', 'p') "
                     'AND r.relname = %s AND nr.nspname = %s',
                     (self.table_name, self.table_schema))
-            self._constraints.extend((c for c, in cursor.fetchall()))
+            self._constraints.extend((c for c, in cursor))
 
             cursor.execute('SELECT k.column_name, r.delete_rule '
                 'FROM information_schema.key_column_usage AS k '
@@ -176,7 +176,7 @@
                 'AND r.constraint_name = k.constraint_name '
                 'WHERE k.table_name = %s AND k.table_schema = %s',
                 (self.table_name, self.table_schema))
-            self._fk_deltypes = dict(cursor.fetchall())
+            self._fk_deltypes = dict(cursor)
 
         if indexes:
             # Fetch indexes defined for the table
@@ -187,7 +187,7 @@
                     "JOIN pg_class cl2 on (cl2.oid = ind.indexrelid) "
                 "WHERE cl.relname = %s AND n.nspname = %s",
                 (self.table_name, self.table_schema))
-            self._indexes = [l[0] for l in cursor.fetchall()]
+            self._indexes = [l[0] for l in cursor]
 
     @property
     def _field2module(self):
diff -r c560c85b1cb3 -r 130d98839af1 trytond/backend/sqlite/database.py
--- a/trytond/backend/sqlite/database.py        Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/backend/sqlite/database.py        Sun Mar 21 16:09:13 2021 +0100
@@ -526,7 +526,7 @@
                             configuration.hostname))
                 except Exception:
                     return False
-                hostnames = {h for h, in cursor.fetchall() if h}
+                hostnames = {h for h, in cursor if h}
                 if hostnames and hostname not in hostnames:
                     return False
         return True
diff -r c560c85b1cb3 -r 130d98839af1 trytond/backend/sqlite/table.py
--- a/trytond/backend/sqlite/table.py   Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/backend/sqlite/table.py   Sun Mar 21 16:09:13 2021 +0100
@@ -54,8 +54,7 @@
             temp_sql = sql.replace(table_name, '_temp_%s' % table_name)
             cursor.execute(temp_sql)
             cursor.execute('PRAGMA table_info("' + table_name + '")')
-            columns = ['"%s"' % column for _, column, _, _, _, _
-                    in cursor.fetchall()]
+            columns = ['"%s"' % column for _, column, _, _, _, _ in cursor]
             cursor.execute(('INSERT INTO "_temp_%s" '
                     '(' + ','.join(columns) + ') '
                     'SELECT ' + ','.join(columns)
@@ -131,7 +130,7 @@
         if columns:
             cursor.execute('PRAGMA table_info("' + self.table_name + '")')
             self._columns = {}
-            for _, column, type_, notnull, hasdef, _ in cursor.fetchall():
+            for _, column, type_, notnull, hasdef, _ in cursor:
                 column = re.sub(r'^\"|\"$', '', column)
                 match = re.match(r'(\w+)(\((.*?)\))?', type_)
                 if match:
@@ -153,7 +152,7 @@
                 cursor.execute('PRAGMA index_list("' + self.table_name + '")')
             except IndexError:  # There is sometimes IndexError
                 cursor.execute('PRAGMA index_list("' + self.table_name + '")')
-            self._indexes = [l[1] for l in cursor.fetchall()]
+            self._indexes = [l[1] for l in cursor]
 
     @property
     def _field2module(self):
diff -r c560c85b1cb3 -r 130d98839af1 trytond/cache.py
--- a/trytond/cache.py  Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/cache.py  Sun Mar 21 16:09:13 2021 +0100
@@ -205,7 +205,7 @@
                 cursor.execute(*table.select(
                         _cast(table.timestamp), table.name))
                 timestamps = {}
-                for timestamp, name in cursor.fetchall():
+                for timestamp, name in cursor:
                     timestamps[name] = timestamp
                 modules = _get_modules(cursor)
         finally:
diff -r c560c85b1cb3 -r 130d98839af1 trytond/ir/lang.py
--- a/trytond/ir/lang.py        Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/ir/lang.py        Sun Mar 21 16:09:13 2021 +0100
@@ -589,7 +589,7 @@
         cursor = Transaction().connection.cursor()
         lang = Table('ir_lang')
         cursor.execute(*lang.select(lang.code, lang.parent))
-        _parents.update(cursor.fetchall())
+        _parents.update(cursor)
     if _parents.get(code):
         return _parents[code]
     for sep in ['@', '_']:
diff -r c560c85b1cb3 -r 130d98839af1 trytond/ir/model.py
--- a/trytond/ir/model.py       Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/ir/model.py       Sun Mar 21 16:09:13 2021 +0100
@@ -420,7 +420,7 @@
             model = Model.__table__()
             cursor.execute(*model.select(model.id, model.model,
                     where=model.id.in_(model_ids)))
-            id2model = dict(cursor.fetchall())
+            id2model = dict(cursor)
 
             trans_args = []
             for rec in res:
@@ -578,7 +578,7 @@
                 group_by=ir_model.model))
         raw_access = {
             m: {'read': r, 'write': w, 'create': c, 'delete': d}
-            for m, r, w, c, d in cursor.fetchall()}
+            for m, r, w, c, d in cursor}
 
         for model in models:
             access[model] = {
@@ -774,7 +774,7 @@
                         & (group.active == Literal(True)))
                     | (field_access.group == Null)),
                 group_by=[ir_model.model, model_field.name]))
-        for m, f, r, w, c, d in cursor.fetchall():
+        for m, f, r, w, c, d in cursor:
             accesses[m][f] = {'read': r, 'write': w, 'create': c, 'delete': d}
         for model, maccesses in accesses.items():
             cls._get_access_cache.set((user, model), maccesses)
@@ -1257,7 +1257,7 @@
             cursor = Transaction().connection.cursor()
 
             cursor.execute(*table.select(table.model, group_by=[table.model]))
-            models = [m[0] for m in cursor.fetchall()]
+            models = [m for m, in cursor]
             cls._has_model_cache.set(None, models)
         return model in models
 
diff -r c560c85b1cb3 -r 130d98839af1 trytond/ir/note.py
--- a/trytond/ir/note.py        Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/ir/note.py        Sun Mar 21 16:09:13 2021 +0100
@@ -60,7 +60,7 @@
                     Case((read.user != Null, False), else_=True),
                     where=where)
             cursor.execute(*query)
-            unread.update(cursor.fetchall())
+            unread.update(cursor)
         return unread
 
     @classmethod
diff -r c560c85b1cb3 -r 130d98839af1 trytond/ir/trigger.py
--- a/trytond/ir/trigger.py     Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/ir/trigger.py     Sun Mar 21 16:09:13 2021 +0100
@@ -85,7 +85,7 @@
             cursor.execute(*sql_table.select(
                     sql_table.id, sql_table.minimum_delay,
                     where=sql_table.minimum_delay != Null))
-            for id_, delay in cursor.fetchall():
+            for id_, delay in cursor:
                 delay = datetime.timedelta(hours=delay)
                 cursor.execute(*sql_table.update(
                         [sql_table.minimum_time_delay],
@@ -218,7 +218,7 @@
                         trigger_log.record_id, Count(Literal(1)),
                         where=red_sql & (trigger_log.trigger == self.id),
                         group_by=trigger_log.record_id))
-                number = dict(cursor.fetchall())
+                number = dict(cursor)
                 for record_id in sub_ids:
                     if record_id not in number:
                         new_ids.append(record_id)
@@ -256,7 +256,7 @@
                         trigger_log.record_id, Max(trigger_log.create_date),
                         where=(red_sql & (trigger_log.trigger == self.id)),
                         group_by=trigger_log.record_id))
-                delay = dict(cursor.fetchall())
+                delay = dict(cursor)
                 for record_id in sub_ids:
                     if record_id not in delay:
                         new_ids.append(record_id)
diff -r c560c85b1cb3 -r 130d98839af1 trytond/model/fields/many2one.py
--- a/trytond/model/fields/many2one.py  Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/model/fields/many2one.py  Sun Mar 21 16:09:13 2021 +0100
@@ -125,7 +125,7 @@
         right = getattr(Target, self.right).sql_column(table)
         cursor.execute(*table.select(left, right, where=red_sql))
         where = Or()
-        for l, r in cursor.fetchall():
+        for l, r in cursor:
             if operator.endswith('child_of'):
                 where.append((left >= l) & (right <= r))
             else:
diff -r c560c85b1cb3 -r 130d98839af1 trytond/model/modelsql.py
--- a/trytond/model/modelsql.py Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/model/modelsql.py Sun Mar 21 16:09:13 2021 +0100
@@ -1082,7 +1082,7 @@
             for sub_ids in grouped_slice(ids):
                 where = reduce_ids(field.sql_column(table), sub_ids)
                 cursor.execute(*table.select(table.id, where=where))
-                tree_ids[fname] += [x[0] for x in cursor.fetchall()]
+                tree_ids[fname] += [x[0] for x in cursor]
 
         has_translation = any(
             getattr(f, 'translate', False) and not hasattr(f, 'set')
@@ -1119,7 +1119,7 @@
                     Column(foreign_table, field_name), sub_ids)
                 cursor.execute(*foreign_table.select(foreign_table.id,
                         where=foreign_red_sql))
-                records = Model.browse([x[0] for x in cursor.fetchall()])
+                records = Model.browse([x[0] for x in cursor])
             else:
                 with transaction.set_context(active_test=False):
                     records = Model.search([(field_name, 'in', sub_ids)])
@@ -1375,7 +1375,7 @@
                         & (history.create_date == Null)
                         & (history.write_date
                             <= transaction.context['_datetime'])))
-                for deleted_id, delete_date in cursor.fetchall():
+                for deleted_id, delete_date in cursor:
                     history_date, _ = ids_history[deleted_id]
                     if isinstance(history_date, str):
                         strptime = datetime.datetime.strptime
@@ -1506,9 +1506,7 @@
 
         cursor.execute(*table.select(table.id,
                 where=Column(table, parent) == parent_id))
-        childs = cursor.fetchall()
-
-        for child_id, in childs:
+        for child_id, in cursor:
             right = cls._rebuild_tree(parent, child_id, right)
 
         field = cls._fields[parent]
@@ -1607,7 +1605,7 @@
                     cursor.execute(*table.select(*columns, where=where))
 
                     where = Literal(False)
-                    for row in cursor.fetchall():
+                    for row in cursor:
                         clause = table.id != row[0]
                         for column, operator, value in zip(
                                 sql.columns, sql.operators, row[1:]):
diff -r c560c85b1cb3 -r 130d98839af1 trytond/modules/__init__.py
--- a/trytond/modules/__init__.py       Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/modules/__init__.py       Sun Mar 21 16:09:13 2021 +0100
@@ -210,7 +210,7 @@
         for sub_modules in tools.grouped_slice(modules):
             cursor.execute(*ir_module.select(ir_module.name, ir_module.state,
                     where=ir_module.name.in_(list(sub_modules))))
-            module2state.update(cursor.fetchall())
+            module2state.update(cursor)
         modules = set(modules)
 
         for node in graph:
@@ -391,7 +391,7 @@
                 cursor.execute(*ir_module.select(ir_module.name,
                         where=ir_module.state.in_(('activated', 'to upgrade',
                                 'to remove'))))
-            module_list = [name for (name,) in cursor.fetchall()]
+            module_list = [name for (name,) in cursor]
             graph = None
             while graph is None:
                 module_list += update
@@ -410,23 +410,22 @@
             if update:
                 cursor.execute(*ir_module.select(ir_module.name,
                         where=(ir_module.state == 'to remove')))
-                fetchall = cursor.fetchall()
-                if fetchall:
-                    for (mod_name,) in fetchall:
-                        # TODO check if ressource not updated by the user
-                        cursor.execute(*ir_model_data.select(
+                for mod_name, in cursor:
+                    res = False
+                    # TODO check if ressource not updated by the user
+                    with transaction.connection.cursor() as cursor_delete:
+                        cursor_delete.execute(*ir_model_data.select(
                                 ir_model_data.model, ir_model_data.db_id,
                                 where=(ir_model_data.module == mod_name),
                                 order_by=ir_model_data.id.desc))
-                        for rmod, rid in cursor.fetchall():
+                        for rmod, rid in cursor_delete:
                             Model = pool.get(rmod)
                             Model.delete([Model(rid)])
-                        Transaction().connection.commit()
-                    cursor.execute(*ir_module.update([ir_module.state],
-                            ['not activated'],
-                            where=(ir_module.state == 'to remove')))
-                    Transaction().connection.commit()
-                    res = False
+                    transaction.connection.commit()
+                cursor.execute(*ir_module.update([ir_module.state],
+                        ['not activated'],
+                        where=(ir_module.state == 'to remove')))
+                transaction.connection.commit()
 
                 Module = pool.get('ir.module')
                 Module.update_list()
diff -r c560c85b1cb3 -r 130d98839af1 trytond/tools/multivalue.py
--- a/trytond/tools/multivalue.py       Sat Mar 20 12:50:28 2021 +0100
+++ b/trytond/tools/multivalue.py       Sun Mar 21 16:09:13 2021 +0100
@@ -89,7 +89,7 @@
     vcolumns = [Column(table, f) for f in fields]
     values = []
     length = len(value_names)
-    for row in cursor.fetchall():
+    for row in cursor:
         value = [c(v) for v, c in zip(row, casts)]
         if parent:
             value.append(

Reply via email to