]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
`lshift` (<<) and `rshift` (>>) are also supported as optional operators.
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 4 Sep 2012 14:44:37 +0000 (10:44 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 4 Sep 2012 14:44:37 +0000 (10:44 -0400)
CHANGES
lib/sqlalchemy/sql/expression.py
lib/sqlalchemy/sql/operators.py
test/sql/test_operators.py

diff --git a/CHANGES b/CHANGES
index aa4e0bfc1b7c260777833a8831d907ee8f830c2d..c81022aa3b4d83f7e6069d1a6dd9bd9153619b35 100644 (file)
--- 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
index 4edbeafe245980c07753b5407457f7c142e2eb47..7ef8e5b53753bc26b6e7ac6b432a7189c778f526 100644 (file)
@@ -2107,6 +2107,8 @@ class _DefaultColumnComparator(operators.ColumnOperators):
         "between_op": (_between_impl, ),
         "neg": (_neg_impl,),
         "getitem": (_unsupported_impl,),
+        "lshift": (_unsupported_impl,),
+        "rshift": (_unsupported_impl,),
     }
 
 
index ba33d016aa3a22945446b8af6e01cc3538c21c23..38936231dc4a4579e7345004ae6cc01092107fed 100644 (file)
@@ -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.
 
index 69a22172f02e312d068c6ba498e60c46fd9ea174..5b7aa67acbec1ea8bd640eca068d9ef8c166fa0c 100644 (file)
@@ -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):