whenever-sqlalchemy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- whenever_sqlalchemy/__init__.py +126 -0
- whenever_sqlalchemy/py.typed +0 -0
- whenever_sqlalchemy-0.1.0.dist-info/METADATA +122 -0
- whenever_sqlalchemy-0.1.0.dist-info/RECORD +7 -0
- whenever_sqlalchemy-0.1.0.dist-info/WHEEL +5 -0
- whenever_sqlalchemy-0.1.0.dist-info/licenses/LICENSE +21 -0
- whenever_sqlalchemy-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""SQLAlchemy column types for ``whenever``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import datetime as _stdlib
|
|
6
|
+
|
|
7
|
+
import sqlalchemy as _sa
|
|
8
|
+
from sqlalchemy.types import TypeDecorator
|
|
9
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
10
|
+
|
|
11
|
+
_UTC = _stdlib.timezone.utc
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class InstantType(TypeDecorator): # type: ignore[type-arg]
|
|
15
|
+
"""SQLAlchemy column type for :class:`~whenever.Instant`.
|
|
16
|
+
|
|
17
|
+
Stored as ``TIMESTAMP WITH TIME ZONE`` (``TIMESTAMPTZ`` on PostgreSQL).
|
|
18
|
+
Nanoseconds are truncated to microseconds on storage.
|
|
19
|
+
|
|
20
|
+
On databases that do not preserve timezone information (e.g. SQLite),
|
|
21
|
+
the stored UTC value is returned correctly.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
impl = _sa.DateTime(timezone=True)
|
|
25
|
+
cache_ok = True
|
|
26
|
+
|
|
27
|
+
def process_bind_param(
|
|
28
|
+
self, value: Instant | None, dialect: object
|
|
29
|
+
) -> _stdlib.datetime | None:
|
|
30
|
+
if value is None:
|
|
31
|
+
return None
|
|
32
|
+
return value.to_stdlib()
|
|
33
|
+
|
|
34
|
+
def process_result_value(
|
|
35
|
+
self, value: _stdlib.datetime | None, dialect: object
|
|
36
|
+
) -> Instant | None:
|
|
37
|
+
if value is None:
|
|
38
|
+
return None
|
|
39
|
+
# SQLite (and other backends without native tz support) returns naive
|
|
40
|
+
# datetimes for TIMESTAMP WITH TIME ZONE columns. We stored UTC, so
|
|
41
|
+
# we can safely attach UTC tzinfo on retrieval.
|
|
42
|
+
if value.tzinfo is None:
|
|
43
|
+
value = value.replace(tzinfo=_UTC)
|
|
44
|
+
return Instant(value)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class PlainDateTimeType(TypeDecorator): # type: ignore[type-arg]
|
|
48
|
+
"""SQLAlchemy column type for :class:`~whenever.PlainDateTime`.
|
|
49
|
+
|
|
50
|
+
Stored as ``TIMESTAMP`` (without timezone).
|
|
51
|
+
Nanoseconds are truncated to microseconds on storage.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
impl = _sa.DateTime(timezone=False)
|
|
55
|
+
cache_ok = True
|
|
56
|
+
|
|
57
|
+
def process_bind_param(
|
|
58
|
+
self, value: PlainDateTime | None, dialect: object
|
|
59
|
+
) -> _stdlib.datetime | None:
|
|
60
|
+
if value is None:
|
|
61
|
+
return None
|
|
62
|
+
return value.to_stdlib()
|
|
63
|
+
|
|
64
|
+
def process_result_value(
|
|
65
|
+
self, value: _stdlib.datetime | None, dialect: object
|
|
66
|
+
) -> PlainDateTime | None:
|
|
67
|
+
if value is None:
|
|
68
|
+
return None
|
|
69
|
+
return PlainDateTime(value)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class DateType(TypeDecorator): # type: ignore[type-arg]
|
|
73
|
+
"""SQLAlchemy column type for :class:`~whenever.Date`.
|
|
74
|
+
|
|
75
|
+
Stored as ``DATE``.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
impl = _sa.Date()
|
|
79
|
+
cache_ok = True
|
|
80
|
+
|
|
81
|
+
def process_bind_param(
|
|
82
|
+
self, value: Date | None, dialect: object
|
|
83
|
+
) -> _stdlib.date | None:
|
|
84
|
+
if value is None:
|
|
85
|
+
return None
|
|
86
|
+
return value.to_stdlib()
|
|
87
|
+
|
|
88
|
+
def process_result_value(
|
|
89
|
+
self, value: _stdlib.date | None, dialect: object
|
|
90
|
+
) -> Date | None:
|
|
91
|
+
if value is None:
|
|
92
|
+
return None
|
|
93
|
+
return Date(value)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class TimeType(TypeDecorator): # type: ignore[type-arg]
|
|
97
|
+
"""SQLAlchemy column type for :class:`~whenever.Time`.
|
|
98
|
+
|
|
99
|
+
Stored as ``TIME``.
|
|
100
|
+
Nanoseconds are truncated to microseconds on storage.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
impl = _sa.Time()
|
|
104
|
+
cache_ok = True
|
|
105
|
+
|
|
106
|
+
def process_bind_param(
|
|
107
|
+
self, value: Time | None, dialect: object
|
|
108
|
+
) -> _stdlib.time | None:
|
|
109
|
+
if value is None:
|
|
110
|
+
return None
|
|
111
|
+
return value.to_stdlib()
|
|
112
|
+
|
|
113
|
+
def process_result_value(
|
|
114
|
+
self, value: _stdlib.time | None, dialect: object
|
|
115
|
+
) -> Time | None:
|
|
116
|
+
if value is None:
|
|
117
|
+
return None
|
|
118
|
+
return Time(value)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
type_annotation_map = {
|
|
122
|
+
Instant: InstantType(),
|
|
123
|
+
PlainDateTime: PlainDateTimeType(),
|
|
124
|
+
Date: DateType(),
|
|
125
|
+
Time: TimeType(),
|
|
126
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: whenever-sqlalchemy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: SQLAlchemy column types for the whenever datetime library
|
|
5
|
+
Author-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
6
|
+
Maintainer-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Repository, https://github.com/ariebovenberg/whenever-sqlalchemy
|
|
9
|
+
Project-URL: Issues, https://github.com/ariebovenberg/whenever-sqlalchemy/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/ariebovenberg/whenever/blob/main/whenever-sqlalchemy/CHANGELOG.md
|
|
11
|
+
Keywords: whenever,sqlalchemy,datetime,date,time,orm
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Classifier: Topic :: Database
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Requires-Python: >=3.10
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: whenever>=0.10.0
|
|
30
|
+
Requires-Dist: sqlalchemy>=1.4
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest<9,>=7; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov<8,>=7; extra == "dev"
|
|
34
|
+
Requires-Dist: black<26,>=25; extra == "dev"
|
|
35
|
+
Requires-Dist: isort<7,>=6; extra == "dev"
|
|
36
|
+
Requires-Dist: flake8<8,>=6; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy<1.20,>=1.19; extra == "dev"
|
|
38
|
+
Requires-Dist: build; extra == "dev"
|
|
39
|
+
Requires-Dist: twine>=6.2.0; extra == "dev"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# whenever-sqlalchemy
|
|
43
|
+
|
|
44
|
+
SQLAlchemy column types for the [whenever](https://github.com/ariebovenberg/whenever) datetime library.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install whenever-sqlalchemy
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Type mapping
|
|
53
|
+
|
|
54
|
+
| whenever type | SQL type |
|
|
55
|
+
|------------------|-----------------------------|
|
|
56
|
+
| `Instant` | `TIMESTAMP WITH TIME ZONE` |
|
|
57
|
+
| `PlainDateTime` | `TIMESTAMP` (no timezone) |
|
|
58
|
+
| `Date` | `DATE` |
|
|
59
|
+
| `Time` | `TIME` |
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### SQLAlchemy 2.x — `Mapped[]` style (recommended)
|
|
64
|
+
|
|
65
|
+
Register `type_annotation_map` on your `DeclarativeBase` once, then use
|
|
66
|
+
`Mapped[Instant]` exactly like you would use `Mapped[datetime]`:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
70
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
71
|
+
from whenever_sqlalchemy import type_annotation_map
|
|
72
|
+
|
|
73
|
+
class Base(DeclarativeBase):
|
|
74
|
+
type_annotation_map = type_annotation_map # merge with your own if needed
|
|
75
|
+
|
|
76
|
+
class Event(Base):
|
|
77
|
+
__tablename__ = "events"
|
|
78
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
79
|
+
timestamp: Mapped[Instant]
|
|
80
|
+
scheduled: Mapped[PlainDateTime | None]
|
|
81
|
+
date: Mapped[Date | None]
|
|
82
|
+
time: Mapped[Time | None]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### SQLAlchemy 1.4 / 2.x — `Column()` style
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from sqlalchemy import Column, Integer
|
|
89
|
+
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType
|
|
90
|
+
|
|
91
|
+
class Event(Base):
|
|
92
|
+
__tablename__ = "events"
|
|
93
|
+
id = Column(Integer, primary_key=True)
|
|
94
|
+
timestamp = Column(InstantType)
|
|
95
|
+
scheduled = Column(PlainDateTimeType, nullable=True)
|
|
96
|
+
date = Column(DateType, nullable=True)
|
|
97
|
+
time = Column(TimeType, nullable=True)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Notes
|
|
101
|
+
|
|
102
|
+
**Nanoseconds** — `whenever` types support nanosecond precision; SQL
|
|
103
|
+
`TIMESTAMP` columns store at most microsecond precision. Values are
|
|
104
|
+
truncated (not rounded) to microseconds on write, matching `to_stdlib()`.
|
|
105
|
+
|
|
106
|
+
**`server_default`** — `server_default=func.now()` works as expected: the
|
|
107
|
+
server sets the value and SQLAlchemy fetches it back, converting it to the
|
|
108
|
+
appropriate `whenever` type.
|
|
109
|
+
|
|
110
|
+
**Dialect notes**
|
|
111
|
+
|
|
112
|
+
- **PostgreSQL**: uses `TIMESTAMPTZ` natively; full roundtrip fidelity.
|
|
113
|
+
- **SQLite**: stores datetimes as text and drops timezone info. `InstantType`
|
|
114
|
+
reattaches UTC tzinfo on retrieval (the stored value is UTC).
|
|
115
|
+
- **MySQL / MariaDB / SQL Server**: similar to SQLite for timezone columns;
|
|
116
|
+
the UTC-reattach workaround covers these too.
|
|
117
|
+
|
|
118
|
+
## Compatibility
|
|
119
|
+
|
|
120
|
+
- Python 3.10+
|
|
121
|
+
- whenever 0.10+
|
|
122
|
+
- SQLAlchemy 1.4+ (`Column()` style); SQLAlchemy 2.0+ (`Mapped[]` style)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
whenever_sqlalchemy/__init__.py,sha256=I1IGPR0ErcSSw8qhrag_2XuN_Nuyq2wVASTx1txuxik,3476
|
|
2
|
+
whenever_sqlalchemy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
whenever_sqlalchemy-0.1.0.dist-info/licenses/LICENSE,sha256=C47Us7WBJceCzQjPBA-v8lzYf37N_N3RJ7qyaEjv3nE,1088
|
|
4
|
+
whenever_sqlalchemy-0.1.0.dist-info/METADATA,sha256=T2yBV4_nKS7d3V_xz_toTDngLUBwkErLy-HJdx8apdQ,4437
|
|
5
|
+
whenever_sqlalchemy-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
6
|
+
whenever_sqlalchemy-0.1.0.dist-info/top_level.txt,sha256=F3iWhKboM7lVodb8Wr41yhLEelvrdFB9OF9SKSmR1qI,20
|
|
7
|
+
whenever_sqlalchemy-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 - 2023 Arie Bovenberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
whenever_sqlalchemy
|