From dd1a46728df75986e64fba93aa2dc30bad590eb0 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Thu, 14 May 2020 22:57:56 +0200 Subject: [PATCH] Introduce :class:`.IdentityOptions` to store common parameters for sequences and identity columns. References: #5324 Change-Id: I72f7fc1a003456206b004d3d26306940f9c36414 (cherry picked from commit e1d8b15b636793a84f0d2baa7d6213ce7dec8080) --- .../unreleased_13/5324_identity_options.rst | 6 ++ doc/build/core/defaults.rst | 3 + lib/sqlalchemy/__init__.py | 1 + lib/sqlalchemy/schema.py | 1 + lib/sqlalchemy/sql/schema.py | 90 +++++++++++++++---- 5 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 doc/build/changelog/unreleased_13/5324_identity_options.rst diff --git a/doc/build/changelog/unreleased_13/5324_identity_options.rst b/doc/build/changelog/unreleased_13/5324_identity_options.rst new file mode 100644 index 0000000000..44d78e06a4 --- /dev/null +++ b/doc/build/changelog/unreleased_13/5324_identity_options.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: sql, schema + :tickets: 5324 + + Introduce :class:`.IdentityOptions` to store common parameters for + sequences and identity columns. diff --git a/doc/build/core/defaults.rst b/doc/build/core/defaults.rst index ad86ab2398..a691655cce 100644 --- a/doc/build/core/defaults.rst +++ b/doc/build/core/defaults.rst @@ -652,3 +652,6 @@ Default Objects API .. autoclass:: Sequence :members: + + +.. autoclass:: IdentityOptions diff --git a/lib/sqlalchemy/__init__.py b/lib/sqlalchemy/__init__.py index a4d045bfc5..0cee28fee6 100644 --- a/lib/sqlalchemy/__init__.py +++ b/lib/sqlalchemy/__init__.py @@ -18,6 +18,7 @@ from .schema import DefaultClause # noqa from .schema import FetchedValue # noqa from .schema import ForeignKey # noqa from .schema import ForeignKeyConstraint # noqa +from .schema import IdentityOptions # noqa from .schema import Index # noqa from .schema import MetaData # noqa from .schema import PassiveDefault # noqa diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 581b4c1ad1..cbc3716a93 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -49,6 +49,7 @@ from .sql.schema import FetchedValue # noqa from .sql.schema import ForeignKey # noqa from .sql.schema import ForeignKeyConstraint # noqa from .sql.schema import Index # noqa +from .sql.schema import IdentityOptions # noqa from .sql.schema import MetaData # noqa from .sql.schema import PassiveDefault # noqa from .sql.schema import PrimaryKeyConstraint # noqa diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index f4fa19eedc..f1b9045b23 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2379,7 +2379,60 @@ class ColumnDefault(DefaultGenerator): return "ColumnDefault(%r)" % (self.arg,) -class Sequence(DefaultGenerator): +class IdentityOptions(object): + """Defines options for a named database sequence or an identity column. + + .. versionadded:: 1.3.18 + + .. seealso:: + + :class:`.Sequence` + + """ + + def __init__( + self, + start=None, + increment=None, + minvalue=None, + maxvalue=None, + nominvalue=None, + nomaxvalue=None, + cycle=None, + cache=None, + order=None, + ): + """Construct a :class:`.IdentityOptions` object. + + See the :class:`.Sequence` documentation for a complete description + of the parameters + + :param start: the starting index of the sequence. + :param increment: the increment value of the sequence. + :param minvalue: the minimum value of the sequence. + :param maxvalue: the maximum value of the sequence. + :param nominvalue: no minimum value of the sequence. + :param nomaxvalue: no maximum value of the sequence. + :param cycle: allows the sequence to wrap around when the maxvalue + or minvalue has been reached. + :param cache: optional integer value; number of future values in the + sequence which are calculated in advance. + :param order: optional boolean value; if true, renders the + ORDER keyword. + name. + """ + self.start = start + self.increment = increment + self.minvalue = minvalue + self.maxvalue = maxvalue + self.nominvalue = nominvalue + self.nomaxvalue = nomaxvalue + self.cycle = cycle + self.cache = cache + self.order = order + + +class Sequence(IdentityOptions, DefaultGenerator): """Represents a named database sequence. The :class:`.Sequence` object represents the name and configurational @@ -2435,7 +2488,7 @@ class Sequence(DefaultGenerator): ): """Construct a :class:`.Sequence` object. - :param name: The name of the sequence. + :param name: the name of the sequence. :param start: the starting index of the sequence. This value is used when the CREATE SEQUENCE command is emitted to the database as the value of the "START WITH" clause. If ``None``, the @@ -2493,7 +2546,7 @@ class Sequence(DefaultGenerator): .. versionadded:: 1.0.7 - :param schema: Optional schema name for the sequence, if located + :param schema: optional schema name for the sequence, if located in a schema other than the default. The rules for selecting the schema name when a :class:`_schema.MetaData` is also present are the same @@ -2519,10 +2572,10 @@ class Sequence(DefaultGenerator): this sequence on the PostgreSQL backend, where the SERIAL keyword creates a sequence for us automatically". :param quote: boolean value, when ``True`` or ``False``, explicitly - forces quoting of the schema name on or off. When left at its - default of ``None``, normal quoting rules based on casing and - reserved words take place. - :param quote_schema: set the quoting preferences for the ``schema`` + forces quoting of the :paramref:`_schema.Sequence.name` on or off. + When left at its default of ``None``, normal quoting rules based + on casing and reserved words take place. + :param quote_schema: Set the quoting preferences for the ``schema`` name. :param metadata: optional :class:`_schema.MetaData` object which this @@ -2564,17 +2617,20 @@ class Sequence(DefaultGenerator): no value is otherwise present for that column in the statement. """ - super(Sequence, self).__init__(for_update=for_update) + DefaultGenerator.__init__(self, for_update=for_update) + IdentityOptions.__init__( + self, + start=start, + increment=increment, + minvalue=minvalue, + maxvalue=maxvalue, + nominvalue=nominvalue, + nomaxvalue=nomaxvalue, + cycle=cycle, + cache=cache, + order=order, + ) self.name = quoted_name(name, quote) - self.start = start - self.increment = increment - self.minvalue = minvalue - self.maxvalue = maxvalue - self.nominvalue = nominvalue - self.nomaxvalue = nomaxvalue - self.cycle = cycle - self.cache = cache - self.order = order self.optional = optional if schema is BLANK_SCHEMA: self.schema = schema = None -- 2.39.5