summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-09-04 10:44:37 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-09-04 10:44:37 -0400
commitbc9ff8fcc9e13fe50714c95e3f56c9144aba94f5 (patch)
tree7879eadd68a537ca4531ac48793222fc8d507dea
parent9e612f111b9645f4958e3ef0595d9e19bd9e5ae3 (diff)
downloadsqlalchemy-bc9ff8fcc9e13fe50714c95e3f56c9144aba94f5.tar.gz
`lshift` (<<) and `rshift` (>>) are also supported as optional operators.
-rw-r--r--CHANGES4
-rw-r--r--lib/sqlalchemy/sql/expression.py2
-rw-r--r--lib/sqlalchemy/sql/operators.py20
-rw-r--r--test/sql/test_operators.py38
4 files changed, 61 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index aa4e0bfc1..c81022aa3 100644
--- a/CHANGES
+++ b/CHANGES
@@ -421,7 +421,9 @@ underneath "0.7.xx".
for end-user definition of custom __getitem__
schemes which can be applied at the type
level as well as within ORM-level custom
- operator schemes.
+ operator schemes. `lshift` (<<)
+ and `rshift` (>>) are also supported as
+ optional operators.
Note that this change has the effect that
descriptor-based __getitem__ schemes used by
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 4edbeafe2..7ef8e5b53 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -2107,6 +2107,8 @@ class _DefaultColumnComparator(operators.ColumnOperators):
"between_op": (_between_impl, ),
"neg": (_neg_impl,),
"getitem": (_unsupported_impl,),
+ "lshift": (_unsupported_impl,),
+ "rshift": (_unsupported_impl,),
}
diff --git a/lib/sqlalchemy/sql/operators.py b/lib/sqlalchemy/sql/operators.py
index ba33d016a..38936231d 100644
--- a/lib/sqlalchemy/sql/operators.py
+++ b/lib/sqlalchemy/sql/operators.py
@@ -11,7 +11,7 @@
from operator import (
and_, or_, inv, add, mul, sub, mod, truediv, lt, le, ne, gt, ge, eq, neg,
- getitem
+ getitem, lshift, rshift
)
# Py2K
@@ -315,6 +315,24 @@ class ColumnOperators(Operators):
"""
return self.operate(getitem, index)
+ def __lshift__(self, other):
+ """implement the << operator.
+
+ Not used by SQLAlchemy core, this is provided
+ for custom operator systems which want to use
+ << as an extension point.
+ """
+ return self.operate(lshift, other)
+
+ def __rshift__(self, other):
+ """implement the >> operator.
+
+ Not used by SQLAlchemy core, this is provided
+ for custom operator systems which want to use
+ >> as an extension point.
+ """
+ return self.operate(rshift, other)
+
def concat(self, other):
"""Implement the 'concat' operator.
diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py
index 69a22172f..5b7aa67ac 100644
--- a/test/sql/test_operators.py
+++ b/test/sql/test_operators.py
@@ -7,7 +7,7 @@ from sqlalchemy.sql.expression import BinaryExpression, \
from sqlalchemy.sql import operators
from sqlalchemy import exc
from sqlalchemy.schema import Column, Table, MetaData
-from sqlalchemy.types import Integer, TypeEngine, TypeDecorator
+from sqlalchemy.types import Integer, TypeEngine, TypeDecorator, UserDefinedType
from sqlalchemy.dialects import mysql, firebird
from sqlalchemy import text, literal_column
@@ -239,6 +239,42 @@ class NewOperatorTest(_CustomComparatorTests, fixtures.TestBase):
def _assert_not_add_override(self, expr):
assert not hasattr(expr, "foob")
+class ExtensionOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL):
+ __dialect__ = 'default'
+
+ def test_getitem(self):
+ class MyType(UserDefinedType):
+ class comparator_factory(UserDefinedType.Comparator):
+ def __getitem__(self, index):
+ return self.op("->")(index)
+
+ self.assert_compile(
+ Column('x', MyType())[5],
+ "x -> :x_1"
+ )
+
+ def test_lshift(self):
+ class MyType(UserDefinedType):
+ class comparator_factory(UserDefinedType.Comparator):
+ def __lshift__(self, other):
+ return self.op("->")(other)
+
+ self.assert_compile(
+ Column('x', MyType()) << 5,
+ "x -> :x_1"
+ )
+
+ def test_rshift(self):
+ class MyType(UserDefinedType):
+ class comparator_factory(UserDefinedType.Comparator):
+ def __rshift__(self, other):
+ return self.op("->")(other)
+
+ self.assert_compile(
+ Column('x', MyType()) >> 5,
+ "x -> :x_1"
+ )
+
from sqlalchemy import and_, not_, between
class OperatorPrecedenceTest(fixtures.TestBase, testing.AssertsCompiledSQL):