|
1 | 1 | from __future__ import annotations |
2 | 2 | import contextvars |
3 | 3 | from dataclasses import dataclass |
| 4 | +import datetime |
4 | 5 | from decimal import Decimal |
5 | 6 | import os |
6 | 7 | import sqlite3 |
@@ -33,49 +34,65 @@ def convert_uuid_in(value): |
33 | 34 | """ |
34 | 35 | Converts the UUID value being passed into sqlite. |
35 | 36 | """ |
36 | | - return "uuid:" + str(value) |
| 37 | + return str(value) |
37 | 38 |
|
38 | 39 |
|
39 | | -def convert_numeric_out(value: bytes): |
| 40 | +def convert_time_in(value): |
| 41 | + """ |
| 42 | + Converts the time value being passed into sqlite. |
| 43 | + """ |
| 44 | + return value.isoformat() |
| 45 | + |
| 46 | + |
| 47 | +def convert_date_in(value): |
| 48 | + """ |
| 49 | + Converts the date value being passed into sqlite. |
| 50 | + """ |
| 51 | + return value.isoformat() |
| 52 | + |
| 53 | + |
| 54 | +def convert_numeric_out(value: bytes) -> Decimal: |
40 | 55 | """ |
41 | 56 | Convert float values into Decimals. |
42 | 57 | """ |
43 | 58 | return Decimal(value.decode("ascii")) |
44 | 59 |
|
45 | 60 |
|
46 | | -def convert_int_out(value: bytes): |
| 61 | +def convert_int_out(value: bytes) -> int: |
47 | 62 | """ |
48 | 63 | Make sure Integer values are actually of type int. |
49 | 64 | """ |
50 | 65 | return int(float(value)) |
51 | 66 |
|
52 | 67 |
|
53 | | -def convert_uuid_out(value: bytes): |
| 68 | +def convert_uuid_out(value: bytes) -> uuid.UUID: |
54 | 69 | """ |
55 | 70 | If the value is a uuid, convert it to a UUID instance. |
| 71 | + """ |
| 72 | + return uuid.UUID(value.decode("utf8")) |
| 73 | + |
| 74 | + |
| 75 | +def convert_date_out(value: bytes) -> datetime.date: |
| 76 | + return datetime.date.fromisoformat(value.decode("utf8")) |
| 77 | + |
56 | 78 |
|
57 | | - Performance wise, this isn't great, but SQLite isn't the preferred solution |
58 | | - in production, so it's acceptable. |
| 79 | +def convert_time_out(value: bytes) -> datetime.time: |
59 | 80 | """ |
60 | | - decoded = value.decode("utf8") |
61 | | - if decoded.startswith("uuid:"): |
62 | | - uuid_string = decoded.split("uuid:", 1)[1] |
63 | | - try: |
64 | | - _uuid = uuid.UUID(uuid_string) |
65 | | - except ValueError: |
66 | | - return decoded |
67 | | - else: |
68 | | - return _uuid |
69 | | - else: |
70 | | - return decoded |
| 81 | + If the value is a uuid, convert it to a UUID instance. |
| 82 | + """ |
| 83 | + return datetime.time.fromisoformat(value.decode("utf8")) |
71 | 84 |
|
72 | 85 |
|
73 | 86 | sqlite3.register_converter("Numeric", convert_numeric_out) |
74 | 87 | sqlite3.register_converter("Integer", convert_int_out) |
75 | | -sqlite3.register_converter("Varchar", convert_uuid_out) |
| 88 | +sqlite3.register_converter("UUID", convert_uuid_out) |
| 89 | +sqlite3.register_converter("Date", convert_date_out) |
| 90 | +sqlite3.register_converter("Time", convert_time_out) |
76 | 91 |
|
77 | 92 | sqlite3.register_adapter(Decimal, convert_numeric_in) |
78 | 93 | sqlite3.register_adapter(uuid.UUID, convert_uuid_in) |
| 94 | +sqlite3.register_adapter(datetime.time, convert_time_in) |
| 95 | +sqlite3.register_adapter(datetime.date, convert_date_in) |
79 | 96 |
|
80 | 97 | ############################################################################### |
81 | 98 |
|
|
0 commit comments