From 6a1970e978bea1fc4a95b17f69ce0af65d424806 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 29 Mar 2008 16:31:31 +0000 Subject: [PATCH] turned starargs conversion to a decorator, per jek's advice. select().order_by()/group_by() already take *args. --- CHANGES | 3 ++- lib/sqlalchemy/orm/query.py | 14 ++++++++------ lib/sqlalchemy/util.py | 16 +++++++++------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/CHANGES b/CHANGES index d81c0a87b5..5dea65f02b 100644 --- a/CHANGES +++ b/CHANGES @@ -26,7 +26,8 @@ CHANGES more flexible approach. - query.order_by() and query.group_by() will accept - multiple arguments using *args. + multiple arguments using *args (like select() + already does). - Fixed/covered case when using a False/0 value as a polymorphic discriminator. diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 8c0f1335dd..1d4b24e0c5 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -529,24 +529,26 @@ class Query(object): q = self._no_statement("order_by") if self._aliases_tail: - criterion = [expression._literal_as_text(o) for o in util.starargs_as_list(*criterion)] + criterion = [expression._literal_as_text(o) for o in criterion] criterion = self._aliases_tail.adapt_list(criterion) if q._order_by is False: - q._order_by = util.starargs_as_list(*criterion) + q._order_by = criterion else: - q._order_by = q._order_by + util.starargs_as_list(*criterion) + q._order_by = q._order_by + criterion return q - + order_by = util.array_as_starargs_decorator(order_by) + def group_by(self, *criterion): """apply one or more GROUP BY criterion to the query and return the newly resulting ``Query``""" q = self._no_statement("group_by") if q._group_by is False: - q._group_by = util.starargs_as_list(*criterion) + q._group_by = criterion else: - q._group_by = q._group_by + util.starargs_as_list(*criterion) + q._group_by = q._group_by + criterion return q + group_by = util.array_as_starargs_decorator(group_by) def having(self, criterion): """apply a HAVING criterion to the query and return the newly resulting ``Query``.""" diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 6919d1557d..26b6dbe9a0 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -188,15 +188,17 @@ def to_list(x, default=None): else: return x -def starargs_as_list(*args): - """interpret the given *args as either a list of *args, - or detect if it's a single list and return that. +def array_as_starargs_decorator(func): + """Interpret a single positional array argument as + *args for the decorated method. """ - if len(args) == 1: - return to_list(args[0], []) - else: - return list(args) + def starargs_as_list(self, *args, **kwargs): + if len(args) == 1: + return func(self, *to_list(args[0], []), **kwargs) + else: + return func(self, *args, **kwargs) + return starargs_as_list def to_set(x): if x is None: -- 2.47.3