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  from _sqlite3 import _deprecated_version
      29  
      30  _deprecated_names = frozenset({"version", "version_info"})
      31  
      32  paramstyle = "qmark"
      33  
      34  apilevel = "2.0"
      35  
      36  Date = datetime.date
      37  
      38  Time = datetime.time
      39  
      40  Timestamp = datetime.datetime
      41  
      42  def DateFromTicks(ticks):
      43      return Date(*time.localtime(ticks)[:3])
      44  
      45  def TimeFromTicks(ticks):
      46      return Time(*time.localtime(ticks)[3:6])
      47  
      48  def TimestampFromTicks(ticks):
      49      return Timestamp(*time.localtime(ticks)[:6])
      50  
      51  _deprecated_version_info = tuple(map(int, _deprecated_version.split(".")))
      52  sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
      53  
      54  Binary = memoryview
      55  collections.abc.Sequence.register(Row)
      56  
      57  def register_adapters_and_converters():
      58      from warnings import warn
      59  
      60      msg = ("The default {what} is deprecated as of Python 3.12; "
      61             "see the sqlite3 documentation for suggested replacement recipes")
      62  
      63      def adapt_date(val):
      64          warn(msg.format(what="date adapter"), DeprecationWarning, stacklevel=2)
      65          return val.isoformat()
      66  
      67      def adapt_datetime(val):
      68          warn(msg.format(what="datetime adapter"), DeprecationWarning, stacklevel=2)
      69          return val.isoformat(" ")
      70  
      71      def convert_date(val):
      72          warn(msg.format(what="date converter"), DeprecationWarning, stacklevel=2)
      73          return datetime.date(*map(int, val.split(b"-")))
      74  
      75      def convert_timestamp(val):
      76          warn(msg.format(what="timestamp converter"), DeprecationWarning, stacklevel=2)
      77          datepart, timepart = val.split(b" ")
      78          year, month, day = map(int, datepart.split(b"-"))
      79          timepart_full = timepart.split(b".")
      80          hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
      81          if len(timepart_full) == 2:
      82              microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
      83          else:
      84              microseconds = 0
      85  
      86          val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
      87          return val
      88  
      89  
      90      register_adapter(datetime.date, adapt_date)
      91      register_adapter(datetime.datetime, adapt_datetime)
      92      register_converter("date", convert_date)
      93      register_converter("timestamp", convert_timestamp)
      94  
      95  register_adapters_and_converters()
      96  
      97  # Clean up namespace
      98  
      99  del(register_adapters_and_converters)
     100  
     101  def __getattr__(name):
     102      if name in _deprecated_names:
     103          from warnings import warn
     104  
     105          warn(f"{name} is deprecated and will be removed in Python 3.14",
     106               DeprecationWarning, stacklevel=2)
     107          return globals()[f"_deprecated_{name}"]
     108      raise AttributeError(f"module {__name__!r} has no attribute {name!r}")