diff options
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 22 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/expression.py | 95 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/operators.py | 19 |
3 files changed, 65 insertions, 71 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index b3f74ceef..d51dd625a 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -83,9 +83,7 @@ OPERATORS = { operators.add: ' + ', operators.mul: ' * ', operators.sub: ' - ', -# start Py2K -# operators.div: ' / ', -# end Py2K + operators.div: ' / ', operators.mod: ' % ', operators.truediv: ' / ', operators.neg: '-', @@ -826,12 +824,12 @@ class SQLCompiler(engine.Compiled): of the DBAPI. """ - if isinstance(value, str): + if isinstance(value, util.string_types): value = value.replace("'", "''") return "'%s'" % value elif value is None: return "NULL" - elif isinstance(value, (float, int)): + elif isinstance(value, (float, ) + util.int_types): return repr(value) elif isinstance(value, decimal.Decimal): return str(value) @@ -1214,7 +1212,7 @@ class SQLCompiler(engine.Compiled): self.positiontup = self.cte_positional + self.positiontup cte_text = self.get_cte_preamble(self.ctes_recursive) + " " cte_text += ", \n".join( - [txt for txt in list(self.ctes.values())] + [txt for txt in self.ctes.values()] ) cte_text += "\n " return cte_text @@ -1325,7 +1323,7 @@ class SQLCompiler(engine.Compiled): dialect_hints = dict([ (table, hint_text) for (table, dialect), hint_text in - list(insert_stmt._hints.items()) + insert_stmt._hints.items() if dialect in ('*', self.dialect.name) ]) if insert_stmt.table in dialect_hints: @@ -1422,7 +1420,7 @@ class SQLCompiler(engine.Compiled): dialect_hints = dict([ (table, hint_text) for (table, dialect), hint_text in - list(update_stmt._hints.items()) + update_stmt._hints.items() if dialect in ('*', self.dialect.name) ]) if update_stmt.table in dialect_hints: @@ -1559,7 +1557,7 @@ class SQLCompiler(engine.Compiled): if extra_tables and stmt_parameters: normalized_params = dict( (sql._clause_element_as_expr(c), param) - for c, param in list(stmt_parameters.items()) + for c, param in stmt_parameters.items() ) assert self.isupdate affected_tables = set() @@ -1752,7 +1750,7 @@ class SQLCompiler(engine.Compiled): dialect_hints = dict([ (table, hint_text) for (table, dialect), hint_text in - list(delete_stmt._hints.items()) + delete_stmt._hints.items() if dialect in ('*', self.dialect.name) ]) if delete_stmt.table in dialect_hints: @@ -1870,11 +1868,11 @@ class DDLCompiler(engine.Compiled): first_pk = True except exc.CompileError as ce: util.raise_from_cause( - exc.CompileError("(in table '%s', column '%s'): %s" % ( + exc.CompileError(util.u("(in table '%s', column '%s'): %s" % ( table.description, column.name, ce.args[0] - ))) + )))) const = self.create_table_constraints(table) if const: diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 1ad6364d2..aff5512d3 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -26,7 +26,7 @@ to stay the same in future releases. """ - +from __future__ import unicode_literals import itertools import re from operator import attrgetter @@ -1375,7 +1375,7 @@ func = _FunctionGenerator() modifier = _FunctionGenerator(group=False) -class _truncated_label(str): +class _truncated_label(util.text_type): """A unicode subclass used to identify symbolic " "names that may require truncation.""" @@ -1395,13 +1395,13 @@ class _anonymous_label(_truncated_label): def __add__(self, other): return _anonymous_label( - str(self) + - str(other)) + util.text_type(self) + + util.text_type(other)) def __radd__(self, other): return _anonymous_label( - str(other) + - str(self)) + util.text_type(other) + + util.text_type(self)) def apply_map(self, map_): return self % map_ @@ -1422,7 +1422,7 @@ def _as_truncated(value): def _string_or_unprintable(element): - if isinstance(element, str): + if isinstance(element, util.string_types): return element else: try: @@ -1486,7 +1486,7 @@ def _labeled(element): def _column_as_key(element): - if isinstance(element, str): + if isinstance(element, util.string_types): return element if hasattr(element, '__clause_element__'): element = element.__clause_element__() @@ -1508,8 +1508,8 @@ def _literal_as_text(element): return element elif hasattr(element, '__clause_element__'): return element.__clause_element__() - elif isinstance(element, str): - return TextClause(str(element)) + elif isinstance(element, util.string_types): + return TextClause(util.text_type(element)) elif isinstance(element, (util.NoneType, bool)): return _const_expr(element) else: @@ -1583,8 +1583,8 @@ def _interpret_as_column_or_from(element): def _interpret_as_from(element): insp = inspection.inspect(element, raiseerr=False) if insp is None: - if isinstance(element, str): - return TextClause(str(element)) + if isinstance(element, util.string_types): + return TextClause(util.text_type(element)) elif hasattr(insp, "selectable"): return insp.selectable raise exc.ArgumentError("FROM expression expected") @@ -1914,12 +1914,10 @@ class ClauseElement(Visitable): return dialect.statement_compiler(dialect, self, **kw) def __str__(self): -# start Py3K - return str(self.compile()) -# end Py3K -# start Py2K -# return unicode(self.compile()).encode('ascii', 'backslashreplace') -# end Py2K + if util.py3k: + return str(self.compile()) + else: + return unicode(self.compile()).encode('ascii', 'backslashreplace') def __and__(self, other): return and_(self, other) @@ -1933,6 +1931,8 @@ class ClauseElement(Visitable): def __bool__(self): raise TypeError("Boolean value of this clause is not defined") + __nonzero__ = __bool__ + def _negate(self): if hasattr(self, 'negation_clause'): return self.negation_clause @@ -2508,7 +2508,7 @@ class ColumnCollection(util.OrderedProperties): def update(self, value): self._data.update(value) self._all_cols.clear() - self._all_cols.update(list(self._data.values())) + self._all_cols.update(self._data.values()) def extend(self, iter): self.update((c.key, c) for c in iter) @@ -2524,13 +2524,13 @@ class ColumnCollection(util.OrderedProperties): return and_(*l) def __contains__(self, other): - if not isinstance(other, str): + if not isinstance(other, util.string_types): raise exc.ArgumentError("__contains__ requires a string argument") return util.OrderedProperties.__contains__(self, other) def __setstate__(self, state): self.__dict__['_data'] = state['_data'] - self.__dict__['_all_cols'] = util.column_set(list(self._data.values())) + self.__dict__['_all_cols'] = util.column_set(self._data.values()) def contains_column(self, col): # this has to be done via set() membership @@ -3185,13 +3185,13 @@ class TextClause(Executable, ClauseElement): _hide_froms = [] def __init__( - self, - text='', - bind=None, - bindparams=None, - typemap=None, - autocommit=None, - ): + self, + text='', + bind=None, + bindparams=None, + typemap=None, + autocommit=None): + self._bind = bind self.bindparams = {} self.typemap = typemap @@ -3201,9 +3201,9 @@ class TextClause(Executable, ClauseElement): 'e)') self._execution_options = \ self._execution_options.union( - {'autocommit': autocommit}) + {'autocommit': autocommit}) if typemap is not None: - for key in list(typemap.keys()): + for key in typemap: typemap[key] = sqltypes.to_instance(typemap[key]) def repl(m): @@ -3237,7 +3237,7 @@ class TextClause(Executable, ClauseElement): def _copy_internals(self, clone=_clone, **kw): self.bindparams = dict((b.key, clone(b, **kw)) - for b in list(self.bindparams.values())) + for b in self.bindparams.values()) def get_children(self, **kwargs): return list(self.bindparams.values()) @@ -3751,7 +3751,7 @@ class BinaryExpression(ColumnElement): negate=None, modifiers=None): # allow compatibility with libraries that # refer to BinaryExpression directly and pass strings - if isinstance(operator, str): + if isinstance(operator, util.string_types): operator = operators.custom_op(operator) self._orig = (left, right) self.left = _literal_as_text(left).self_group(against=operator) @@ -3770,6 +3770,7 @@ class BinaryExpression(ColumnElement): return self.operator(hash(self._orig[0]), hash(self._orig[1])) else: raise TypeError("Boolean value of this clause is not defined") + __nonzero__ = __bool__ @property def is_comparison(self): @@ -4058,12 +4059,10 @@ class Alias(FromClause): @property def description(self): -# start Py3K - return self.name -# end Py3K -# start Py2K -# return self.name.encode('ascii', 'backslashreplace') -# end Py2K + if util.py3k: + return self.name + else: + return self.name.encode('ascii', 'backslashreplace') def as_scalar(self): try: @@ -4473,12 +4472,10 @@ class ColumnClause(Immutable, ColumnElement): @util.memoized_property def description(self): -# start Py3K - return self.name -# end Py3K -# start Py2K -# return self.name.encode('ascii', 'backslashreplace') -# end Py2K + if util.py3k: + return self.name + else: + return self.name.encode('ascii', 'backslashreplace') @_memoized_property def _key_label(self): @@ -4605,12 +4602,10 @@ class TableClause(Immutable, FromClause): @util.memoized_property def description(self): -# start Py3K - return self.name -# end Py3K -# start Py2K -# return self.name.encode('ascii', 'backslashreplace') -# end Py2K + if util.py3k: + return self.name + else: + return self.name.encode('ascii', 'backslashreplace') def append_column(self, c): self._columns[c.key] = c diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index cf1c484d0..4afb3db48 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -9,16 +9,19 @@ """Defines operators used in SQL expressions.""" +from .. import util + + from operator import ( and_, or_, inv, add, mul, sub, mod, truediv, lt, le, ne, gt, ge, eq, neg, getitem, lshift, rshift ) -# start Py2K -#from operator import (div,) -# end Py2K +if util.py2k: + from operator import div +else: + div = truediv -from ..util import symbol class Operators(object): @@ -781,17 +784,15 @@ parenthesize (a op b). """ -_smallest = symbol('_smallest', canonical=-100) -_largest = symbol('_largest', canonical=100) +_smallest = util.symbol('_smallest', canonical=-100) +_largest = util.symbol('_largest', canonical=100) _PRECEDENCE = { from_: 15, getitem: 15, mul: 8, truediv: 8, -# start Py2K -# div: 8, -# end Py2K + div: 8, mod: 8, neg: 8, add: 7, |