Skip to content

Commit

Permalink
Add Int8RangeType (#401)
Browse files Browse the repository at this point in the history
This is just like IntRange, but supports greater bit depth.
  • Loading branch information
lpsinger authored and kvesteri committed Nov 1, 2019
1 parent af6291c commit 460184e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlalchemy_utils/__init__.py
Expand Up @@ -74,6 +74,7 @@
instrumented_list,
InstrumentedList,
IntRangeType,
Int8RangeType,
IPAddressType,
JSONType,
LocaleType,
Expand Down
1 change: 1 addition & 0 deletions sqlalchemy_utils/types/__init__.py
Expand Up @@ -29,6 +29,7 @@
DateRangeType,
DateTimeRangeType,
IntRangeType,
Int8RangeType,
NumericRangeType
)
from .scalar_list import ScalarListException, ScalarListType # noqa
Expand Down
53 changes: 53 additions & 0 deletions sqlalchemy_utils/types/range.py
Expand Up @@ -146,6 +146,7 @@ class Event(Base):
from sqlalchemy.dialects.postgresql import (
DATERANGE,
INT4RANGE,
INT8RANGE,
NUMRANGE,
TSRANGE
)
Expand Down Expand Up @@ -363,6 +364,58 @@ def __init__(self, *args, **kwargs):
self.interval_class = intervals.IntInterval


class Int8RangeType(RangeType):
"""
Int8RangeType provides way for saving ranges of 8-byte integers into
database. On PostgreSQL this type maps to native INT8RANGE type while on
other drivers this maps to simple string column.
Example::
from sqlalchemy_utils import IntRangeType
class Event(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, autoincrement=True)
name = sa.Column(sa.Unicode(255))
estimated_number_of_persons = sa.Column(Int8RangeType)
party = Event(name=u'party')
# we estimate the party to contain minium of 10 persons and at max
# 100 persons
party.estimated_number_of_persons = [10, 100]
print party.estimated_number_of_persons
# '10-100'
Int8RangeType returns the values as IntInterval objects. These objects
support many arithmetic operators::
meeting = Event(name=u'meeting')
meeting.estimated_number_of_persons = [20, 40]
total = (
meeting.estimated_number_of_persons +
party.estimated_number_of_persons
)
print total
# '30-140'
"""
impl = INT8RANGE
comparator_factory = IntRangeComparator

def __init__(self, *args, **kwargs):
super(IntRangeType, self).__init__(*args, **kwargs)
self.interval_class = intervals.IntInterval


class DateRangeType(RangeType):
"""
DateRangeType provides way for saving ranges of dates into database. On
Expand Down

0 comments on commit 460184e

Please sign in to comment.