diff options
| author | Jason Kirtland <jek@discorporate.us> | 2008-04-16 00:53:21 +0000 |
|---|---|---|
| committer | Jason Kirtland <jek@discorporate.us> | 2008-04-16 00:53:21 +0000 |
| commit | 24797113708c0f19ef0d5d81e2950b33f8c1a425 (patch) | |
| tree | 7e44582f775579cf40679112a2611e1bbab84d87 /lib/sqlalchemy/sql | |
| parent | f5126ab3a169b6f8a9171868fe32b2bd385f8b8f (diff) | |
| download | sqlalchemy-24797113708c0f19ef0d5d81e2950b33f8c1a425.tar.gz | |
- Support for COLLATE: collate(expr, col) and expr.collate(col)
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 3 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/expression.py | 20 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/operators.py | 4 |
3 files changed, 25 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 47e5ec9c5..1fe9ef062 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -90,7 +90,8 @@ OPERATORS = { operators.as_ : 'AS', operators.exists : 'EXISTS', operators.is_ : 'IS', - operators.isnot : 'IS NOT' + operators.isnot : 'IS NOT', + operators.collate : 'COLLATE', } FUNCTIONS = { diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 648e74e7e..8c439ce2c 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -40,7 +40,7 @@ __all__ = [ 'Select', 'Selectable', 'TableClause', 'Update', 'alias', 'and_', 'asc', 'between', 'bindparam', 'case', 'cast', 'column', 'delete', 'desc', 'distinct', 'except_', 'except_all', 'exists', 'extract', 'func', - 'modifier', + 'modifier', 'collate', 'insert', 'intersect', 'intersect_all', 'join', 'literal', 'literal_column', 'not_', 'null', 'or_', 'outparam', 'outerjoin', 'select', 'subquery', 'table', 'text', 'union', 'union_all', 'update', ] @@ -493,6 +493,14 @@ def extract(field, expr): expr = _BinaryExpression(text(field), expr, operators.from_) return func.extract(expr) +def collate(expression, collation): + """Return the clause ``expression COLLATE collation``.""" + + expr = _literal_as_binds(expression) + return _CalculatedClause( + expr, expr, _literal_as_text(collation), + operator=operators.collate, group=False) + def exists(*args, **kwargs): """Return an ``EXISTS`` clause as applied to a [sqlalchemy.sql.expression#Select] object. @@ -1226,6 +1234,9 @@ class ColumnOperators(Operators): def asc(self): return self.operate(operators.asc_op) + def collate(self, collation): + return self.operate(operators.collate, collation) + def __radd__(self, other): return self.reverse_operate(operators.add, other) @@ -1390,6 +1401,13 @@ class _CompareMixin(ColumnOperators): return _BinaryExpression(self, ClauseList(self._check_literal(cleft), self._check_literal(cright), operator=operators.and_, group=False), operators.between_op) + def collate(self, collation): + """Produce a COLLATE clause, i.e. ``<column> COLLATE utf8_bin``""" + name = getattr(self, 'name', None) + return _CalculatedClause( + None, self, _literal_as_text(collation), + operator=operators.collate, group=False) + def op(self, operator): """produce a generic operator function. diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py index 0047d1c73..dfd638ecb 100644 --- a/lib/sqlalchemy/sql/operators.py +++ b/lib/sqlalchemy/sql/operators.py @@ -22,6 +22,9 @@ def is_(): def isnot(): raise NotImplementedError() +def collate(): + raise NotImplementedError() + def op(a, opstring, b): return a.op(opstring)(b) @@ -105,6 +108,7 @@ _PRECEDENCE = { and_:3, or_:2, comma_op:-1, + collate: -2, as_:-1, exists:0, _smallest: -1000, |
