summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/functions.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-12-05 03:07:21 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-12-05 03:07:21 +0000
commit238c2c8dbe3ca5b92d298b39e96f81eb416d1413 (patch)
treeb123393efbbb06a1e0ebc84385f5964efa98f0b1 /lib/sqlalchemy/sql/functions.py
parentc6bda7dcc89ae5f7842f0e900d3917024a74eb29 (diff)
downloadsqlalchemy-238c2c8dbe3ca5b92d298b39e96f81eb416d1413.tar.gz
- basic framework for generic functions, [ticket:615]
- changed the various "literal" generation functions to use an anonymous bind parameter. not much changes here except their labels now look like ":param_1", ":param_2" instead of ":literal" - from_obj keyword argument to select() can be a scalar or a list.
Diffstat (limited to 'lib/sqlalchemy/sql/functions.py')
-rw-r--r--lib/sqlalchemy/sql/functions.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py
new file mode 100644
index 000000000..d39032b91
--- /dev/null
+++ b/lib/sqlalchemy/sql/functions.py
@@ -0,0 +1,78 @@
+from sqlalchemy import types as sqltypes
+from sqlalchemy.sql.expression import _Function, _literal_as_binds, ClauseList, _FigureVisitName
+from sqlalchemy.sql import operators
+
+class _GenericMeta(_FigureVisitName):
+ def __init__(cls, clsname, bases, dict):
+ cls.__visit_name__ = 'function'
+ type.__init__(cls, clsname, bases, dict)
+
+ def __call__(self, *args, **kwargs):
+ args = [_literal_as_binds(c) for c in args]
+ return type.__call__(self, *args, **kwargs)
+
+class GenericFunction(_Function):
+ __metaclass__ = _GenericMeta
+
+ def __init__(self, type_=None, group=True, args=(), **kwargs):
+ self.packagenames = []
+ self.oid_column = None
+ self.name = self.__class__.__name__
+ self._bind = kwargs.get('bind', None)
+ if group:
+ self.clause_expr = ClauseList(operator=operators.comma_op, group_contents=True, *args).self_group()
+ else:
+ self.clause_expr = ClauseList(operator=operators.comma_op, group_contents=True, *args)
+ self.type = sqltypes.to_instance(type_ or getattr(self, '__return_type__', None))
+
+class AnsiFunction(GenericFunction):
+ def __init__(self, **kwargs):
+ GenericFunction.__init__(self, **kwargs)
+
+
+class coalesce(GenericFunction):
+ def __init__(self, *args, **kwargs):
+ kwargs.setdefault('type_', _type_from_args(args))
+ GenericFunction.__init__(self, args=args, **kwargs)
+
+class concat(GenericFunction):
+ __return_type__ = sqltypes.String
+ def __init__(self, *args, **kwargs):
+ GenericFunction.__init__(self, args=args, **kwargs)
+
+class char_length(GenericFunction):
+ __return_type__ = sqltypes.Integer
+
+ def __init__(self, arg, **kwargs):
+ GenericFunction.__init__(self, args=[arg], **kwargs)
+
+class current_date(AnsiFunction):
+ __return_type__ = sqltypes.Date
+
+class current_time(AnsiFunction):
+ __return_type__ = sqltypes.Time
+
+class current_timestamp(AnsiFunction):
+ __return_type__ = sqltypes.DateTime
+
+class current_user(AnsiFunction):
+ __return_type__ = sqltypes.String
+
+class localtime(AnsiFunction):
+ __return_type__ = sqltypes.DateTime
+
+class localtimestamp(AnsiFunction):
+ __return_type__ = sqltypes.DateTime
+
+class session_user(AnsiFunction):
+ __return_type__ = sqltypes.String
+
+class user(AnsiFunction):
+ __return_type__ = sqltypes.String
+
+def _type_from_args(args):
+ for a in args:
+ if not isinstance(a.type, sqltypes.NullType):
+ return a.type
+ else:
+ return sqltypes.NullType \ No newline at end of file