dbapi2.py 2.6 KB

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