Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 1 | # pysqlite2/dbapi2.py: the DB-API 2.0 interface |
| 2 | # |
| 3 | # Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de> |
| 4 | # |
| 5 | # This file is part of pysqlite. |
| 6 | # |
| 7 | # This software is provided 'as-is', without any express or implied |
| 8 | # warranty. In no event will the authors be held liable for any damages |
| 9 | # arising from the use of this software. |
| 10 | # |
| 11 | # Permission is granted to anyone to use this software for any purpose, |
| 12 | # including commercial applications, and to alter it and redistribute it |
| 13 | # freely, subject to the following restrictions: |
| 14 | # |
| 15 | # 1. The origin of this software must not be misrepresented; you must not |
| 16 | # claim that you wrote the original software. If you use this software |
| 17 | # in a product, an acknowledgment in the product documentation would be |
| 18 | # appreciated but is not required. |
| 19 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 20 | # misrepresented as being the original software. |
| 21 | # 3. This notice may not be removed or altered from any source distribution. |
| 22 | |
| 23 | import datetime |
| 24 | import time |
| 25 | import collections.abc |
| 26 | |
| 27 | from _sqlite3 import * |
| 28 | |
| 29 | paramstyle = "qmark" |
| 30 | |
| 31 | threadsafety = 1 |
| 32 | |
| 33 | apilevel = "2.0" |
| 34 | |
| 35 | Date = datetime.date |
| 36 | |
| 37 | Time = datetime.time |
| 38 | |
| 39 | Timestamp = datetime.datetime |
| 40 | |
| 41 | def DateFromTicks(ticks): |
| 42 | return Date(*time.localtime(ticks)[:3]) |
| 43 | |
| 44 | def TimeFromTicks(ticks): |
| 45 | return Time(*time.localtime(ticks)[3:6]) |
| 46 | |
| 47 | def TimestampFromTicks(ticks): |
| 48 | return Timestamp(*time.localtime(ticks)[:6]) |
| 49 | |
| 50 | version_info = tuple([int(x) for x in version.split(".")]) |
| 51 | sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")]) |
| 52 | |
| 53 | Binary = memoryview |
| 54 | collections.abc.Sequence.register(Row) |
| 55 | |
| 56 | def register_adapters_and_converters(): |
| 57 | def adapt_date(val): |
| 58 | return val.isoformat() |
| 59 | |
| 60 | def adapt_datetime(val): |
| 61 | return val.isoformat(" ") |
| 62 | |
| 63 | def convert_date(val): |
| 64 | return datetime.date(*map(int, val.split(b"-"))) |
| 65 | |
| 66 | def convert_timestamp(val): |
| 67 | datepart, timepart = val.split(b" ") |
| 68 | year, month, day = map(int, datepart.split(b"-")) |
| 69 | timepart_full = timepart.split(b".") |
| 70 | hours, minutes, seconds = map(int, timepart_full[0].split(b":")) |
| 71 | if len(timepart_full) == 2: |
| 72 | microseconds = int('{:0<6.6}'.format(timepart_full[1].decode())) |
| 73 | else: |
| 74 | microseconds = 0 |
| 75 | |
| 76 | val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) |
| 77 | return val |
| 78 | |
| 79 | |
| 80 | register_adapter(datetime.date, adapt_date) |
| 81 | register_adapter(datetime.datetime, adapt_datetime) |
| 82 | register_converter("date", convert_date) |
| 83 | register_converter("timestamp", convert_timestamp) |
| 84 | |
| 85 | register_adapters_and_converters() |
| 86 | |
| 87 | # Clean up namespace |
| 88 | |
| 89 | del(register_adapters_and_converters) |