diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-03-20 12:49:28 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-03-20 12:49:28 -0400 |
commit | 90335a89a98df23db7a3ae1233eb4fbb5743d2e8 (patch) | |
tree | 9a4ac236f83696709bd355dcac22552aeb177694 /lib/sqlalchemy/sql/functions.py | |
parent | 75c78aa714ca55818f0ba12a67cf2f77927b68f7 (diff) | |
download | sqlalchemy-90335a89a98df23db7a3ae1233eb4fbb5743d2e8.tar.gz |
- Added new generic function "next_value()", accepts
a Sequence object as its argument and renders the
appropriate "next value" generation string on the
target platform, if supported. Also provides
".next_value()" method on Sequence itself.
[ticket:2085]
- added tests for all the conditions described
in [ticket:2085]
- postgresql dialect will exec/compile a Sequence
that has "optional=True". the optional flag is now only
checked specifically in the context of a Table primary key
evaulation.
- func.next_value() or other SQL expression can
be embedded directly into an insert() construct,
and if implicit or explicit "returning" is used
in conjunction with a primary key column,
the newly generated value will be present in
result.inserted_primary_key. [ticket:2084]
Diffstat (limited to 'lib/sqlalchemy/sql/functions.py')
-rw-r--r-- | lib/sqlalchemy/sql/functions.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index 10eaa577b..717816656 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -4,7 +4,7 @@ # This module is part of SQLAlchemy and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php -from sqlalchemy import types as sqltypes +from sqlalchemy import types as sqltypes, schema from sqlalchemy.sql.expression import ( ClauseList, Function, _literal_as_binds, text, _type_from_args ) @@ -29,6 +29,29 @@ class GenericFunction(Function): self.type = sqltypes.to_instance( type_ or getattr(self, '__return_type__', None)) + +class next_value(Function): + """Represent the 'next value', given a :class:`.Sequence` + as it's single argument. + + Compiles into the appropriate function on each backend, + or will raise NotImplementedError if used on a backend + that does not provide support for sequences. + + """ + type = sqltypes.Integer() + name = "next_value" + + def __init__(self, seq, **kw): + assert isinstance(seq, schema.Sequence), \ + "next_value() accepts a Sequence object as input." + self._bind = kw.get('bind', None) + self.sequence = seq + + @property + def _from_objects(self): + return [] + class AnsiFunction(GenericFunction): def __init__(self, **kwargs): GenericFunction.__init__(self, **kwargs) @@ -52,6 +75,7 @@ class min(ReturnTypeFromArgs): class sum(ReturnTypeFromArgs): pass + class now(GenericFunction): __return_type__ = sqltypes.DateTime |