From 34360bff781e92e326d928ce3499652b6741e263 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 27 Jul 2007 21:10:12 +0000 Subject: [PATCH] added distinct positional dictionary arg to query.params(), fixes [ticket:690] --- lib/sqlalchemy/orm/query.py | 23 +++++++++++++++++------ test/orm/query.py | 18 ++++++++++++++++++ test/sql/unicode.py | 1 + 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 4e53270b2d..9040655b28 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -249,10 +249,21 @@ class Query(object): q._lockmode = mode return q - def params(self, **kwargs): - """add values for bind parameters which may have been specified in filter().""" + def params(self, *args, **kwargs): + """add values for bind parameters which may have been specified in filter(). + + parameters may be specified using \**kwargs, or optionally a single dictionary + as the first positional argument. The reason for both is that \**kwargs is + convenient, however some parameter dictionaries contain unicode keys in which case + \**kwargs cannot be used. + """ q = self._clone() + if len(args) == 1: + d = args[0] + kwargs.update(d) + elif len(args) > 0: + raise exceptions.ArgumentError("params() takes zero or one positional argument, which is a dictionary.") q._params = q._params.copy() q._params.update(kwargs) return q @@ -714,7 +725,7 @@ class Query(object): if lockmode is not None: q = q.with_lockmode(lockmode) q = q.filter(self.select_mapper._get_clause) - q = q.params(**params)._select_context_options(populate_existing=reload, version_check=(lockmode is not None)) + q = q.params(params)._select_context_options(populate_existing=reload, version_check=(lockmode is not None)) return q.first() except IndexError: return None @@ -747,7 +758,7 @@ class Query(object): if whereclause is not None: q = q.filter(whereclause) if params is not None: - q = q.params(**params) + q = q.params(params) q = q._legacy_select_kwargs(**kwargs) return q._count() @@ -950,7 +961,7 @@ class Query(object): q = self.filter(whereclause)._legacy_select_kwargs(**kwargs) if params is not None: - q = q.params(**params) + q = q.params(params) return list(q) def _legacy_select_kwargs(self, **kwargs): #pragma: no cover @@ -1037,7 +1048,7 @@ class Query(object): def _select_statement(self, statement, params=None, **kwargs): #pragma: no cover q = self.from_statement(statement) if params is not None: - q = q.params(**params) + q = q.params(params) q._select_context_options(**kwargs) return list(q) diff --git a/test/orm/query.py b/test/orm/query.py index 3783e1fa0c..5d17d7d817 100644 --- a/test/orm/query.py +++ b/test/orm/query.py @@ -39,6 +39,24 @@ class QueryTest(ORMTest): }) mapper(Keyword, keywords) +class UnicodeSchemaTest(QueryTest): + keep_mappers = False + + def setup_mappers(self): + pass + + def define_tables(self, metadata): + super(UnicodeSchemaTest, self).define_tables(metadata) + global uni_meta, uni_users + uni_meta = MetaData() + uni_users = Table(u'users', uni_meta, + Column(u'id', Integer, primary_key=True), + Column(u'name', String(30), nullable=False)) + + def test_get(self): + mapper(User, uni_users) + assert User(id=7) == create_session(bind=testbase.db).query(User).get(7) + class GetTest(QueryTest): def test_get(self): s = create_session() diff --git a/test/sql/unicode.py b/test/sql/unicode.py index f882c2a5f8..34e3c19f84 100644 --- a/test/sql/unicode.py +++ b/test/sql/unicode.py @@ -101,5 +101,6 @@ class UnicodeSchemaTest(PersistTest): assert new_a1.a == a1.a assert new_a1.t2s[0].a == b1.a + if __name__ == '__main__': testbase.main() -- 2.47.3