Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Int8RangeType #401

Merged
merged 1 commit into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sqlalchemy_utils/__init__.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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