sqlite.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute iut and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. """SQLite Database implementation."""
  22. from __future__ import print_function
  23. from ..database import Database, __DB_FORMAT__
  24. import sqlite3 as sqlite
  25. class SQLite(Database):
  26. @classmethod
  27. def check_db_version(cls, fname):
  28. """
  29. check the database version.
  30. """
  31. try:
  32. con = sqlite.connect(fname)
  33. except sqlite.OperationalError as E:
  34. print("could not open %s" % fname)
  35. raise E
  36. cur = con.cursor()
  37. cur.execute("PRAGMA TABLE_INFO(DBVERSION)")
  38. row = cur.fetchone()
  39. try:
  40. return row[-2]
  41. except TypeError:
  42. return str(__DB_FORMAT__)
  43. def __init__(self, filename, dbformat=__DB_FORMAT__):
  44. """Initialise SQLitePwmanDatabase instance."""
  45. self._filename = filename
  46. self.dbformat = dbformat
  47. self._add_node_sql = ("INSERT INTO NODE(USER, PASSWORD, URL, NOTES)"
  48. "VALUES(?, ?, ?, ?)")
  49. self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = ? "
  50. self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(?)"
  51. self._sub = '?'
  52. self._data_wrapper = lambda x: x
  53. def _open(self):
  54. self._con = sqlite.connect(self._filename)
  55. self._cur = self._con.cursor()
  56. self._create_tables()
  57. def _create_tables(self):
  58. self._cur.execute("PRAGMA TABLE_INFO(NODE)")
  59. if self._cur.fetchone() is not None:
  60. return
  61. self._cur.execute("CREATE TABLE NODE (ID INTEGER PRIMARY KEY "
  62. "AUTOINCREMENT, "
  63. "USER TEXT NOT NULL, "
  64. "PASSWORD TEXT NOT NULL, "
  65. "URL TEXT NOT NULL,"
  66. "NOTES TEXT NOT NULL)")
  67. self._cur.execute("CREATE TABLE TAG"
  68. "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
  69. "DATA BLOB NOT NULL)")
  70. self._cur.execute("CREATE TABLE LOOKUP ("
  71. "nodeid INTEGER NOT NULL, "
  72. "tagid INTEGER NOT NULL, "
  73. "FOREIGN KEY(nodeid) REFERENCES NODE(ID),"
  74. "FOREIGN KEY(tagid) REFERENCES TAG(ID))")
  75. self._cur.execute("CREATE TABLE CRYPTO"
  76. "(SEED TEXT,"
  77. " DIGEST TEXT)")
  78. # create a table to hold DB version info
  79. self._cur.execute("CREATE TABLE DBVERSION"
  80. "(VERSION TEXT NOT NULL DEFAULT '%s')" %
  81. self.dbformat)
  82. self._cur.execute("INSERT INTO DBVERSION VALUES('%s')" %
  83. self.dbformat)
  84. try:
  85. self._con.commit()
  86. except Exception as e: # pragma: no cover
  87. self._con.rollback()
  88. raise e