dbapi2.py 2.7 KB

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