diff options
Diffstat (limited to 'lib/sqlalchemy/sql/functions.py')
-rw-r--r-- | lib/sqlalchemy/sql/functions.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index b24f8cbec..d26589bd9 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -6,7 +6,8 @@ from .. import types as sqltypes, schema from .expression import ( - ClauseList, Function, _literal_as_binds, literal_column, _type_from_args + ClauseList, Function, _literal_as_binds, literal_column, _type_from_args, + cast, extract ) from . import operators from .visitors import VisitableType @@ -14,6 +15,19 @@ from .. import util _registry = util.defaultdict(dict) +def register_function(identifier, fn, package="_default"): + """Associate a callable with a particular func. name. + + This is normally called by _GenericMeta, but is also + available by itself so that a non-Function construct + can be associated with the :data:`.func` accessor (i.e. + CAST, EXTRACT). + + """ + reg = _registry[package] + reg[identifier] = fn + + class _GenericMeta(VisitableType): def __init__(cls, clsname, bases, clsdict): cls.name = name = clsdict.get('name', clsname) @@ -22,8 +36,7 @@ class _GenericMeta(VisitableType): # legacy if '__return_type__' in clsdict: cls.type = clsdict['__return_type__'] - reg = _registry[package] - reg[identifier] = cls + register_function(identifier, cls, package) super(_GenericMeta, cls).__init__(clsname, bases, clsdict) class GenericFunction(Function): @@ -113,6 +126,9 @@ class GenericFunction(Function): kwargs.pop("type_", None) or getattr(self, 'type', None)) +register_function("cast", cast) +register_function("extract", extract) + class next_value(GenericFunction): """Represent the 'next value', given a :class:`.Sequence` as it's single argument. |