From: Mike Bayer Date: Wed, 24 May 2006 16:35:30 +0000 (+0000) Subject: added explicit check for "==null()" to produce IS NULL, documnted "==None", "==null... X-Git-Tag: rel_0_2_0~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fc3a0648699c2b441251ba4e1d37a9107bd1986;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added explicit check for "==null()" to produce IS NULL, documnted "==None", "==null()", [ticket:187] --- diff --git a/doc/build/content/sqlconstruction.txt b/doc/build/content/sqlconstruction.txt index 3e2bf6cf5f..aa97687671 100644 --- a/doc/build/content/sqlconstruction.txt +++ b/doc/build/content/sqlconstruction.txt @@ -274,6 +274,18 @@ Supported column operators so far are all the numerical comparison operators, i. # any custom operator select([users.c.user_name.op('||')('_category')]) + # "null" comparison via == (converts to IS) + {sql}users.select(users.c.user_name==None).execute() + SELECT users.user_id, users.user_name, users.password + FROM users + WHERE users.user_name IS NULL + + # or via explicit null() construct + {sql}users.select(users.c.user_name==null()).execute() + SELECT users.user_id, users.user_name, users.password + FROM users + WHERE users.user_name IS NULL + #### Specifying the Engine {@name=engine} For queries that don't contain any tables, the SQLEngine can be specified to any constructed statement via the `engine` keyword parameter: diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index fc0346b855..38866184f8 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -549,17 +549,16 @@ class CompareMixin(object): def _bind_param(self, obj): return BindParamClause('literal', obj, shortname=None, type=self.type) def _compare(self, operator, obj): - if _is_literal(obj): - if obj is None: - if operator == '=': - return BooleanExpression(self._compare_self(), null(), 'IS') - elif operator == '!=': - return BooleanExpression(self._compare_self(), null(), 'IS NOT') - else: - raise exceptions.ArgumentError("Only '='/'!=' operators can be used with NULL") + if obj is None or isinstance(obj, Null): + if operator == '=': + return BooleanExpression(self._compare_self(), null(), 'IS') + elif operator == '!=': + return BooleanExpression(self._compare_self(), null(), 'IS NOT') return BooleanExpression(self._compare_self(), null(), 'IS') else: - obj = self._bind_param(obj) + raise exceptions.ArgumentError("Only '='/'!=' operators can be used with NULL") + elif _is_literal(obj): + obj = self._bind_param(obj) return BooleanExpression(self._compare_self(), obj, operator, type=self._compare_type(obj)) def _operate(self, operator, obj): if _is_literal(obj):