|
19 | 19 | import six |
20 | 20 | import warnings |
21 | 21 |
|
22 | | -from cassandra.cqltypes import DateType |
23 | | -from cassandra.encoder import cql_quote |
24 | | - |
| 22 | +from cassandra import util |
| 23 | +from cassandra.cqltypes import DateType, SimpleDateType |
25 | 24 | from cassandra.cqlengine import ValidationError |
26 | 25 |
|
27 | 26 | log = logging.getLogger(__name__) |
@@ -361,13 +360,21 @@ def to_database(self, value): |
361 | 360 | class TinyInt(Integer): |
362 | 361 | """ |
363 | 362 | Stores an 8-bit signed integer value |
| 363 | +
|
| 364 | + .. versionadded:: 2.6.0 |
| 365 | +
|
| 366 | + requires C* 2.2+ and protocol v4+ |
364 | 367 | """ |
365 | 368 | db_type = 'tinyint' |
366 | 369 |
|
367 | 370 |
|
368 | 371 | class SmallInt(Integer): |
369 | 372 | """ |
370 | 373 | Stores a 16-bit signed integer value |
| 374 | +
|
| 375 | + .. versionadded:: 2.6.0 |
| 376 | +
|
| 377 | + requires C* 2.2+ and protocol v4+ |
371 | 378 | """ |
372 | 379 | db_type = 'smallint' |
373 | 380 |
|
@@ -466,35 +473,43 @@ def to_database(self, value): |
466 | 473 |
|
467 | 474 | class Date(Column): |
468 | 475 | """ |
469 | | - *Note: this type is overloaded, and will likely be changed or removed to accommodate distinct date type |
470 | | - in a future version* |
| 476 | + Stores a simple date, with no time-of-day |
| 477 | +
|
| 478 | + .. versionchanged:: 2.6.0 |
| 479 | +
|
| 480 | + removed overload of Date and DateTime. DateTime is a drop-in replacement for legacy models |
471 | 481 |
|
472 | | - Stores a date value, with no time-of-day |
| 482 | + requires C* 2.2+ and protocol v4+ |
473 | 483 | """ |
474 | | - db_type = 'timestamp' |
| 484 | + db_type = 'date' |
475 | 485 |
|
476 | | - def to_python(self, value): |
| 486 | + def to_database(self, value): |
| 487 | + value = super(Date, self).to_database(value) |
477 | 488 | if value is None: |
478 | 489 | return |
479 | | - if isinstance(value, datetime): |
480 | | - return value.date() |
481 | | - elif isinstance(value, date): |
482 | | - return value |
483 | | - try: |
484 | | - return datetime.utcfromtimestamp(value).date() |
485 | | - except TypeError: |
486 | | - return datetime.utcfromtimestamp(DateType.deserialize(value)).date() |
| 490 | + |
| 491 | + # need to translate to int version because some dates are not representable in |
| 492 | + # string form (datetime limitation) |
| 493 | + d = value if isinstance(value, util.Date) else util.Date(value) |
| 494 | + return d.days_from_epoch + SimpleDateType.EPOCH_OFFSET_DAYS |
| 495 | + |
| 496 | + |
| 497 | +class Time(Column): |
| 498 | + """ |
| 499 | + Stores a timezone-naive time-of-day, with nanosecond precision |
| 500 | +
|
| 501 | + .. versionadded:: 2.6.0 |
| 502 | +
|
| 503 | + requires C* 2.2+ and protocol v4+ |
| 504 | + """ |
| 505 | + db_type = 'time' |
487 | 506 |
|
488 | 507 | def to_database(self, value): |
489 | | - value = super(Date, self).to_database(value) |
| 508 | + value = super(Time, self).to_database(value) |
490 | 509 | if value is None: |
491 | 510 | return |
492 | | - if isinstance(value, datetime): |
493 | | - value = value.date() |
494 | | - if not isinstance(value, date): |
495 | | - raise ValidationError("{} '{}' is not a date object".format(self.column_name, repr(value))) |
496 | | - |
497 | | - return int((value - date(1970, 1, 1)).total_seconds() * 1000) |
| 511 | + # str(util.Time) yields desired CQL encoding |
| 512 | + return value if isinstance(value, util.Time) else util.Time(value) |
498 | 513 |
|
499 | 514 |
|
500 | 515 | class UUID(Column): |
@@ -852,7 +867,7 @@ class UserDefinedType(Column): |
852 | 867 |
|
853 | 868 | def __init__(self, user_type, **kwargs): |
854 | 869 | """ |
855 | | - :param type user_type: specifies the :class:`~.UserType` model of the column |
| 870 | + :param type user_type: specifies the :class:`~.cqlengine.usertype.UserType` model of the column |
856 | 871 | """ |
857 | 872 | self.user_type = user_type |
858 | 873 | self.db_type = "frozen<%s>" % user_type.type_name() |
|
0 commit comments