1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
"""
SQLAlchemy-Utils provides wide variety of range data types. All range data
types return Interval objects of intervals_ package. In order to use range data
types you need to install intervals_ with:
::
pip install intervals
Intervals package provides good chunk of additional interval operators that for
example psycopg2 range objects do not support.
Some good reading for practical interval implementations:
http://wiki.postgresql.org/images/f/f0/Range-types.pdf
Range type initialization
-------------------------
::
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(IntRangeType)
You can also set a step parameter for range type. The values that are not
multipliers of given step will be rounded up to nearest step multiplier.
::
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(IntRangeType(step=1000))
event = Event(estimated_number_of_persons=[100, 1200])
event.estimated_number_of_persons.lower # 0
event.estimated_number_of_persons.upper # 1000
Range type operators
--------------------
SQLAlchemy-Utils supports many range type operators. These operators follow the
`intervals` package interval coercion rules.
So for example when we make a query such as:
::
session.query(Car).filter(Car.price_range == 300)
It is essentially the same as:
::
session.query(Car).filter(Car.price_range == DecimalInterval([300, 300]))
Comparison operators
^^^^^^^^^^^^^^^^^^^^
All range types support all comparison operators (>, >=, ==, !=, <=, <).
::
Car.price_range < [12, 300]
Car.price_range == [12, 300]
Car.price_range < 300
Car.price_range > (300, 500)
# Whether or not range is strictly left of another range
Car.price_range << [300, 500]
# Whether or not range is strictly right of another range
Car.price_range >> [300, 500]
Membership operators
^^^^^^^^^^^^^^^^^^^^
::
Car.price_range.contains([300, 500])
Car.price_range.contained_by([300, 500])
Car.price_range.in_([[300, 500], [800, 900]])
~ Car.price_range.in_([[300, 400], [700, 800]])
Length
^^^^^^
SQLAlchemy-Utils provides length property for all range types. The
implementation of this property varies on different range types.
In the following example we find all cars whose price range's length is more
than 500.
::
session.query(Car).filter(
Car.price_range.length > 500
)
.. _intervals: https://github.com/kvesteri/intervals
"""
from collections import Iterable
from datetime import timedelta
import six
import sqlalchemy as sa
from sqlalchemy import types
from sqlalchemy.dialects.postgresql import (
DATERANGE,
INT4RANGE,
NUMRANGE,
TSRANGE
)
from ..exceptions import ImproperlyConfigured
from .scalar_coercible import ScalarCoercible
intervals = None
try:
import intervals
except ImportError:
pass
class RangeComparator(types.TypeEngine.Comparator):
@classmethod
def coerced_func(cls, func):
def operation(self, other, **kwargs):
other = self.coerce_arg(other)
return getattr(types.TypeEngine.Comparator, func)(
self, other, **kwargs
)
return operation
def coerce_arg(self, other):
coerced_types = (
self.type.interval_class.type,
tuple,
list,
) + six.string_types
if isinstance(other, coerced_types):
return self.type.interval_class(other)
return other
def in_(self, other):
if (
isinstance(other, Iterable) and
not isinstance(other, six.string_types)
):
other = map(self.coerce_arg, other)
return super(RangeComparator, self).in_(other)
def notin_(self, other):
if (
isinstance(other, Iterable) and
not isinstance(other, six.string_types)
):
other = map(self.coerce_arg, other)
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)
def contains(self, other, **kwargs):
other = self.coerce_arg(other)
return self.op('@>')(other)
def contained_by(self, other, **kwargs):
other = self.coerce_arg(other)
return self.op('<@')(other)
class DiscreteRangeComparator(RangeComparator):
@property
def length(self):
return sa.func.upper(self.expr) - self.step - sa.func.lower(self.expr)
class IntRangeComparator(DiscreteRangeComparator):
step = 1
class DateRangeComparator(DiscreteRangeComparator):
step = timedelta(days=1)
class ContinuousRangeComparator(RangeComparator):
@property
def length(self):
return sa.func.upper(self.expr) - sa.func.lower(self.expr)
funcs = [
'__eq__',
'__ne__',
'__lt__',
'__le__',
'__gt__',
'__ge__',
]
for func in funcs:
setattr(
RangeComparator,
func,
RangeComparator.coerced_func(func)
)
class RangeType(types.TypeDecorator, ScalarCoercible):
comparator_factory = RangeComparator
def __init__(self, *args, **kwargs):
if intervals is None:
raise ImproperlyConfigured(
'RangeType needs intervals package installed.'
)
self.step = kwargs.pop('step', None)
super(RangeType, self).__init__(*args, **kwargs)
def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
# Use the native range type for postgres.
return dialect.type_descriptor(self.impl)
else:
# Other drivers don't have native types.
return dialect.type_descriptor(sa.String(255))
def process_bind_param(self, value, dialect):
if value is not None:
return str(value)
return value
def process_result_value(self, value, dialect):
if value is not None:
if self.interval_class.step is not None:
return self.canonicalize_result_value(
self.interval_class(value, step=self.step)
)
else:
return self.interval_class(value, step=self.step)
return value
def canonicalize_result_value(self, value):
return intervals.canonicalize(value, True, True)
def _coerce(self, value):
if value is None:
return None
return self.interval_class(value, step=self.step)
class IntRangeType(RangeType):
"""
IntRangeType provides way for saving ranges of integers into database. On
PostgreSQL this type maps to native INT4RANGE 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(IntRangeType)
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'
IntRangeType 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 = INT4RANGE
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
PostgreSQL this type maps to native DATERANGE type while on other drivers
this maps to simple string column.
Example::
from sqlalchemy_utils import DateRangeType
class Reservation(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, autoincrement=True)
room_id = sa.Column(sa.Integer))
during = sa.Column(DateRangeType)
"""
impl = DATERANGE
comparator_factory = DateRangeComparator
def __init__(self, *args, **kwargs):
super(DateRangeType, self).__init__(*args, **kwargs)
self.interval_class = intervals.DateInterval
class NumericRangeType(RangeType):
"""
NumericRangeType provides way for saving ranges of decimals into database.
On PostgreSQL this type maps to native NUMRANGE type while on other drivers
this maps to simple string column.
Example::
from sqlalchemy_utils import NumericRangeType
class Car(Base):
__tablename__ = 'car'
id = sa.Column(sa.Integer, autoincrement=True)
name = sa.Column(sa.Unicode(255)))
price_range = sa.Column(NumericRangeType)
"""
impl = NUMRANGE
comparator_factory = ContinuousRangeComparator
def __init__(self, *args, **kwargs):
super(NumericRangeType, self).__init__(*args, **kwargs)
self.interval_class = intervals.DecimalInterval
class DateTimeRangeType(RangeType):
impl = TSRANGE
comparator_factory = ContinuousRangeComparator
def __init__(self, *args, **kwargs):
super(DateTimeRangeType, self).__init__(*args, **kwargs)
self.interval_class = intervals.DateTimeInterval
|