Support returning more than one nextid

This commit is contained in:
Cédric Krier committed 2023-04-22 11:05:23 +02:00
1 parent 4a9e0713d6
commit b4d78247cf
3 files changed
+11 -7

No files matched your search

+2 -2
View File
@@ -65,9 +65,9 @@ Database
If ``hostname`` is set, it checks also if it has the same configured
hostname.
.. method:: Database.nextid(connection, table)
.. method:: Database.nextid(connection, table[, count])
Return the next ID for the ``table`` using the ``connection``.
Return the next ``count`` IDs for the ``table`` using the ``connection``.
.. note:: It may return ``None`` for some database.
+1 -1
View File
@@ -55,7 +55,7 @@ class DatabaseInterface(object):
'''
raise NotImplementedError
def nextid(self, connection, table):
def nextid(self, connection, table, count=1):
pass
def setnextid(self, connection, table, value):
@@ -414,12 +414,16 @@ class Database(DatabaseInterface):
pass
return True
def nextid(self, connection, table):
def nextid(self, connection, table, count=1):
cursor = connection.cursor()
cursor.execute(
"SELECT nextval(pg_get_serial_sequence(format(%s, %s), %s))",
('%I', table, 'id'))
return cursor.fetchone()[0]
"SELECT nextval(pg_get_serial_sequence(format(%s, %s), %s)) "
"FROM generate_series(1, %s)",
('%I', table, 'id', count))
if count == 1:
return cursor.fetchone()[0]
else:
return [id for id, in cursor]
def setnextid(self, connection, table, value):
cursor = connection.cursor()