[numrange] add a new type of sequence that can deliver whole ranges at a time

Closes #208555

authorAurelien Campeas <aurelien.campeas@logilab.fr>
changeseta6706284f282
branchdefault
phasepublic
hiddenno
parent revision#fe7b043deb65 merge 1.11.2 into default
child revision#102dd17a1f69 [psycopg] pass extra_args down to connect(), #f297b86d24b5 [pkg] Bump to 1.12.0, #e767d3e02ff2 [psycopg] pass extra_args down to connect()
files modified by this revision
ChangeLog
__init__.py
# HG changeset patch
# User Aurelien Campeas <aurelien.campeas@logilab.fr>
# Date 1394202158 -3600
# Fri Mar 07 15:22:38 2014 +0100
# Node ID a6706284f2826a97209ef0e63c9b9847f1dcd238
# Parent fe7b043deb6518ab69dffa3d35e1b1ca94853833
[numrange] add a new type of sequence that can deliver whole ranges at a time

Closes #208555

diff --git a/ChangeLog b/ChangeLog
@@ -1,8 +1,12 @@
1  Changelog for logilab database package
2  ======================================
3 
4 +2014-03-XX  --  1.12.0
5 +    * #208555: add `numrange`, a variant on sequences that allows reserving a
6 +      range of ids
7 +
8  2014-02-20  --  1.11.2
9      * #146815 (fti): add "RIGHT SINGLE QUOTATION MARK" (U+2019) character as
10        word delimiter
11 
12  2014-02-17  --  1.11.1
diff --git a/__init__.py b/__init__.py
@@ -1,6 +1,6 @@
13 -# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
14 +# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
15  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
16  #
17  # This file is part of logilab-database.
18  #
19  # logilab-database is free software: you can redistribute it and/or modify it
@@ -908,13 +908,15 @@
20                   DeprecationWarning)
21              indexname = 'unique_%s_%s_idx' % (table, '_'.join(columns))
22          sql = 'DROP INDEX %s;' % (indexname.lower())
23          return [sql]
24 
25 +    # sequence protocol
26 +
27      def sql_create_sequence(self, seq_name):
28 -        return '''CREATE TABLE %s (last INTEGER);
29 -INSERT INTO %s VALUES (0);''' % (seq_name, seq_name)
30 +        return ('CREATE TABLE %s (last INTEGER);'
31 +                'INSERT INTO %s VALUES (0);') % (seq_name, seq_name)
32 
33      def sql_restart_sequence(self, seq_name, initial_value=1):
34          return 'UPDATE %s SET last=%s;' % (seq_name, initial_value)
35 
36      def sql_sequence_current_state(self, seq_name):
@@ -925,10 +927,24 @@
37 
38      def sqls_increment_sequence(self, seq_name):
39          return ('UPDATE %s SET last=last+1;' % seq_name,
40                  'SELECT last FROM %s;' % seq_name)
41 
42 +    # /sequence
43 +    # numrange protocol
44 +    # this is like sequence, but allows whole range allocations
45 +    sql_create_numrange = sql_create_sequence
46 +    sql_restart_numrange = sql_restart_sequence
47 +    sql_numrange_current_state = sql_sequence_current_state
48 +    sql_drop_numrange = sql_drop_sequence
49 +
50 +    def sqls_increment_numrange(self, seq_name, count=1):
51 +        return ('UPDATE %s SET last=last+%s;' % (seq_name, count),
52 +                'SELECT last FROM %s;' % seq_name)
53 +
54 +    # /numrange
55 +
56      def sql_add_limit_offset(self, sql, limit=None, offset=0, orderby=None):
57          """
58          modify the sql statement to add LIMIT and OFFSET clauses
59          (or to emulate them if the backend does not support these SQL extensions)
60