xref: /aosp_15_r20/prebuilts/build-tools/common/py3-stdlib/sqlite3/dbapi2.py (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1# pysqlite2/dbapi2.py: the DB-API 2.0 interface
2#
3# Copyright (C) 2004-2005 Gerhard Häring <[email protected]>
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
23import datetime
24import time
25import collections.abc
26
27from _sqlite3 import *
28
29paramstyle = "qmark"
30
31apilevel = "2.0"
32
33Date = datetime.date
34
35Time = datetime.time
36
37Timestamp = datetime.datetime
38
39def DateFromTicks(ticks):
40    return Date(*time.localtime(ticks)[:3])
41
42def TimeFromTicks(ticks):
43    return Time(*time.localtime(ticks)[3:6])
44
45def TimestampFromTicks(ticks):
46    return Timestamp(*time.localtime(ticks)[:6])
47
48version_info = tuple([int(x) for x in version.split(".")])
49sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
50
51Binary = memoryview
52collections.abc.Sequence.register(Row)
53
54def register_adapters_and_converters():
55    def adapt_date(val):
56        return val.isoformat()
57
58    def adapt_datetime(val):
59        return val.isoformat(" ")
60
61    def convert_date(val):
62        return datetime.date(*map(int, val.split(b"-")))
63
64    def convert_timestamp(val):
65        datepart, timepart = val.split(b" ")
66        year, month, day = map(int, datepart.split(b"-"))
67        timepart_full = timepart.split(b".")
68        hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
69        if len(timepart_full) == 2:
70            microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
71        else:
72            microseconds = 0
73
74        val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
75        return val
76
77
78    register_adapter(datetime.date, adapt_date)
79    register_adapter(datetime.datetime, adapt_datetime)
80    register_converter("date", convert_date)
81    register_converter("timestamp", convert_timestamp)
82
83register_adapters_and_converters()
84
85# bpo-24464: enable_shared_cache was deprecated in Python 3.10.  It's
86# scheduled for removal in Python 3.12.
87def enable_shared_cache(enable):
88    from _sqlite3 import enable_shared_cache as _old_enable_shared_cache
89    import warnings
90    msg = (
91        "enable_shared_cache is deprecated and will be removed in Python 3.12. "
92        "Shared cache is strongly discouraged by the SQLite 3 documentation. "
93        "If shared cache must be used, open the database in URI mode using"
94        "the cache=shared query parameter."
95    )
96    warnings.warn(msg, DeprecationWarning, stacklevel=2)
97    return _old_enable_shared_cache(enable)
98
99# Clean up namespace
100
101del(register_adapters_and_converters)
102