Replace cursor fetchall by iteration over cursor

issue5817
review335831003
This commit is contained in:
Cédric Krier
2021-03-21 16:09:13 +01:00
parent aa6037385f
commit 9c81cb6a4a
14 changed files with 41 additions and 45 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ def run(options):
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()
+1 -1
View File
@@ -356,7 +356,7 @@ class Database(DatabaseInterface):
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:
+5 -5
View File
@@ -139,7 +139,7 @@ class TableHandler(TableHandlerInterface):
'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 @@ class TableHandler(TableHandlerInterface):
'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 @@ class TableHandler(TableHandlerInterface):
"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 @@ class TableHandler(TableHandlerInterface):
'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 @@ class TableHandler(TableHandlerInterface):
"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):
+1 -1
View File
@@ -526,7 +526,7 @@ class Database(DatabaseInterface):
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
+3 -4
View File
@@ -54,8 +54,7 @@ class TableHandler(TableHandlerInterface):
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 @@ class TableHandler(TableHandlerInterface):
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 @@ class TableHandler(TableHandlerInterface):
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):
+1 -1
View File
@@ -205,7 +205,7 @@ class MemoryCache(BaseCache):
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:
+1 -1
View File
@@ -589,7 +589,7 @@ def get_parent_language(code):
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 ['@', '_']:
+4 -4
View File
@@ -420,7 +420,7 @@ class ModelField(ModelSQL, ModelView):
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 @@ class ModelAccess(DeactivableMixin, ModelSQL, ModelView):
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 @@ class ModelFieldAccess(DeactivableMixin, ModelSQL, ModelView):
& (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 @@ class ModelData(ModelSQL, ModelView):
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
+1 -1
View File
@@ -60,7 +60,7 @@ class Note(ResourceMixin, ModelSQL, ModelView):
Case((read.user != Null, False), else_=True),
where=where)
cursor.execute(*query)
unread.update(cursor.fetchall())
unread.update(cursor)
return unread
@classmethod
+3 -3
View File
@@ -85,7 +85,7 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
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 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
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 @@ class Trigger(DeactivableMixin, ModelSQL, ModelView):
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)
+1 -1
View File
@@ -125,7 +125,7 @@ class Many2One(Field):
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:
+5 -7
View File
@@ -1082,7 +1082,7 @@ class ModelSQL(ModelStorage):
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 @@ class ModelSQL(ModelStorage):
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 @@ class ModelSQL(ModelStorage):
& (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 @@ class ModelSQL(ModelStorage):
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 @@ class ModelSQL(ModelStorage):
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:]):
+13 -14
View File
@@ -210,7 +210,7 @@ def load_module_graph(graph, pool, update=None, lang=None):
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 @@ def load_modules(
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 @@ def load_modules(
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()
+1 -1
View File
@@ -89,7 +89,7 @@ def migrate_property(
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(