1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from __future__ import (
19
from django.db import (
25
BIGINT_MAX = 2 ** 63 - 1
33
def __init__(self, name, incr=1, minvalue=1, maxvalue=BIGINT_MAX):
36
self.minvalue = minvalue
37
self.maxvalue = maxvalue
40
"""Create this sequence in the database."""
41
cursor = connection.cursor()
42
query = "CREATE SEQUENCE %s" % self.name
44
query + " INCREMENT BY %s MINVALUE %s MAXVALUE %s CYCLE",
45
[self.incr, self.minvalue, self.maxvalue])
46
transaction.commit_unless_managed()
49
"""Return the next value of this sequence.
51
:return: The sequence value.
54
cursor = connection.cursor()
56
"SELECT nextval(%s)", [self.name])
57
return cursor.fetchone()[0]
60
"""Drop this sequence from the database."""
61
cursor = connection.cursor()
63
"DROP SEQUENCE %s" % self.name)
64
transaction.commit_unless_managed()