Make range types use SA range types

This commit is contained in:
Konsta Vesterinen
2014-01-19 13:48:11 +02:00
parent 9d8f2badf0
commit 78bed57ba7
4 changed files with 25 additions and 61 deletions

View File

@@ -5,6 +5,7 @@ Range data types
DateRangeType
^^^^^^^^^^^^^
@@ -28,3 +29,9 @@ NumericRangeType
.. autoclass:: NumericRangeType
RangeComparator
^^^^^^^^^^^^^^^
.. autoclass:: RangeComparator
:members:

View File

@@ -49,10 +49,6 @@ from .types import (
TSVectorType,
URLType,
UUIDType,
INT4RANGE,
INT8RANGE,
DATERANGE,
NUMRANGE,
)
@@ -111,8 +107,4 @@ __all__ = (
TSVectorType,
URLType,
UUIDType,
INT4RANGE,
INT8RANGE,
DATERANGE,
NUMRANGE,
)

View File

@@ -9,10 +9,6 @@ from .ip_address import IPAddressType
from .json import JSONType
from .locale import LocaleType
from .range import (
INT4RANGE,
INT8RANGE,
DATERANGE,
NUMRANGE,
DateRangeType,
DateTimeRangeType,
IntRangeType,
@@ -56,10 +52,6 @@ __all__ = (
WeekDay,
WeekDays,
WeekDaysType,
INT4RANGE,
INT8RANGE,
DATERANGE,
NUMRANGE,
)

View File

@@ -35,9 +35,6 @@ It is essentially the same as:
session.query(Car).filter(Car.price_range == DecimalInterval([300, 300]))
The coercion is provided for convenience.
Comparison operators
^^^^^^^^^^^^^^^^^^^^
@@ -78,53 +75,17 @@ except ImportError:
pass
import six
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql.base import ischema_names
from sqlalchemy.dialects.postgresql import (
INT4RANGE,
DATERANGE,
NUMRANGE,
TSRANGE,
)
from sqlalchemy import types
from ..exceptions import ImproperlyConfigured
from .scalar_coercible import ScalarCoercible
class INT4RANGE(types.UserDefinedType):
"""
Raw number range type, only supports PostgreSQL for now.
"""
def get_col_spec(self):
return 'int4range'
class INT8RANGE(types.UserDefinedType):
def get_col_spec(self):
return 'int8range'
class NUMRANGE(types.UserDefinedType):
def get_col_spec(self):
return 'numrange'
class DATERANGE(types.UserDefinedType):
def get_col_spec(self):
return 'daterange'
class TSRANGE(types.UserDefinedType):
def get_col_spec(self):
return 'tsrange'
class TSTZRANGE(types.UserDefinedType):
def get_col_spec(self):
return 'tstzrange'
ischema_names['int4range'] = INT4RANGE
ischema_names['int8range'] = INT8RANGE
ischema_names['numrange'] = NUMRANGE
ischema_names['daterange'] = DATERANGE
ischema_names['tsrange'] = TSRANGE
ischema_names['tstzrange'] = TSTZRANGE
class RangeComparator(types.TypeEngine.Comparator):
@classmethod
def coerced_func(cls, func):
@@ -163,10 +124,22 @@ class RangeComparator(types.TypeEngine.Comparator):
return super(RangeComparator, self).notin_(other)
def __rshift__(self, other, **kwargs):
"""
Returns whether or not given interval is strictly right of another
interval.
[a, b] >> [c, d] True, if a > d
"""
other = self.coerce_arg(other)
return self.op('>>')(other)
def __lshift__(self, other, **kwargs):
"""
Returns whether or not given interval is strictly left of another
interval.
[a, b] << [c, d] True, if b < c
"""
other = self.coerce_arg(other)
return self.op('<<')(other)