dbapi2.py 3.5 KB

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