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