added sql_restart_sequence to dbhelper classes (closes #65317)

authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
changeset0d3439a238b1
branchdefault
phasepublic
hiddenno
parent revision#2b46b2376eb2 backport stable
child revision#f4b70e749f70 implement CAST function
files modified by this revision
__init__.py
postgres.py
# HG changeset patch
# User Alexandre Fayolle <alexandre.fayolle@logilab.fr>
# Date 1303307414 -7200
# Wed Apr 20 15:50:14 2011 +0200
# Node ID 0d3439a238b1417945a540c4ee8d14c056a573b1
# Parent 2b46b2376eb236beb99c7eb8434e4a29e3b174fb
added sql_restart_sequence to dbhelper classes (closes #65317)

diff --git a/__init__.py b/__init__.py
@@ -375,14 +375,14 @@
1              self.supported_backends += (backend,)
2 
3      def check_nbargs(cls, nbargs):
4          if cls.minargs is not None and \
5                 nbargs < cls.minargs:
6 -            raise BadQuery('not enough argument for function %s' % cls.name)
7 +            raise BadQuery('not enough argument for function %s' % cls.__name__)
8          if cls.maxargs is not None and \
9                 nbargs < cls.maxargs:
10 -            raise BadQuery('too many arguments for function %s' % cls.name)
11 +            raise BadQuery('too many arguments for function %s' % cls.__name__)
12      check_nbargs = classmethod(check_nbargs)
13 
14      def as_sql(self, backend, args):
15          try:
16              return getattr(self, 'as_sql_%s' % backend)(args)
@@ -700,10 +700,16 @@
17 
18      def sql_create_sequence(self, seq_name):
19          return '''CREATE TABLE %s (last INTEGER);
20  INSERT INTO %s VALUES (0);''' % (seq_name, seq_name)
21 
22 +    def sql_restart_sequence(self, seq_name, initial_value=1):
23 +        return 'UPDATE %s SET last=%s;' % (seq_name, initial_value-1)
24 +
25 +    def sql_sequence_current_state(self, seq_name):
26 +        return 'SELECT last FROM %s;' % seq_name
27 +
28      def sql_drop_sequence(self, seq_name):
29          return 'DROP TABLE %s;' % seq_name
30 
31      def sqls_increment_sequence(self, seq_name):
32          return ('UPDATE %s SET last=last+1;' % seq_name,
diff --git a/postgres.py b/postgres.py
@@ -217,10 +217,16 @@
33          return cmds
34 
35      def sql_create_sequence(self, seq_name):
36          return 'CREATE SEQUENCE %s;' % seq_name
37 
38 +    def sql_restart_sequence(self, seq_name, initial_value=1):
39 +        return 'ALTER SEQUENCE %s RESTART WITH %s;' % (seq_name, initial_value)
40 +
41 +    def sql_sequence_current_state(self, seq_name):
42 +        return 'SELECT last_value FROM %s;' % seq_name
43 +
44      def sql_drop_sequence(self, seq_name):
45          return 'DROP SEQUENCE %s;' % seq_name
46 
47      def sqls_increment_sequence(self, seq_name):
48          return ("SELECT nextval('%s');" % seq_name,)