insertions via notesdb dans tables sans ids

This commit is contained in:
Emmanuel Viennet 2021-08-15 22:51:04 +02:00
parent e1dad774be
commit 357b6f1a7f

View File

@ -82,7 +82,7 @@ def SimpleDictFetch(query, args, cursor=None):
return cursor.dictfetchall() return cursor.dictfetchall()
def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1): def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1, return_id=True):
"""insert into table values in dict 'vals' """insert into table values in dict 'vals'
Return: id de l'object créé Return: id de l'object créé
""" """
@ -104,8 +104,11 @@ def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1):
) )
else: else:
cursor.execute("insert into %s default values" % table) cursor.execute("insert into %s default values" % table)
cursor.execute(f"SELECT CURRVAL('{table}_id_seq')") # id créé if return_id:
oid = cursor.fetchone()[0] cursor.execute(f"SELECT CURRVAL('{table}_id_seq')") # id créé
oid = cursor.fetchone()[0]
else:
oid = None
except: except:
log("DBInsertDict: EXCEPTION !") log("DBInsertDict: EXCEPTION !")
log("DBInsertDict: table=%s, vals=%s" % (str(table), str(vals))) log("DBInsertDict: table=%s, vals=%s" % (str(table), str(vals)))
@ -305,7 +308,13 @@ class EditableTable(object):
if title in self.input_formators: if title in self.input_formators:
vals[title] = self.input_formators[title](vals[title]) vals[title] = self.input_formators[title](vals[title])
# insert # insert
new_id = DBInsertDict(cnx, self.table_name, vals, commit=True) new_id = DBInsertDict(
cnx,
self.table_name,
vals,
commit=True,
return_id=(self.id_name is not None),
)
return new_id return new_id
def delete(self, cnx, oid, commit=True): def delete(self, cnx, oid, commit=True):