sqlite.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 pwman.data.database import Database
  23. from pwman.data.database import __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. con = sqlite.connect(fname)
  32. cur = con.cursor()
  33. cur.execute("PRAGMA TABLE_INFO(DBVERSION)")
  34. row = cur.fetchone()
  35. try:
  36. return row[-2]
  37. except TypeError:
  38. return str(__DB_FORMAT__)
  39. def __init__(self, filename, dbformat=__DB_FORMAT__):
  40. """Initialise SQLitePwmanDatabase instance."""
  41. self._filename = filename
  42. self.dbformat = dbformat
  43. self._add_node_sql = ("INSERT INTO NODE(USER, PASSWORD, URL, NOTES)"
  44. "VALUES(?, ?, ?, ?)")
  45. self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = ? "
  46. self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(?)"
  47. self._sub = '?'
  48. def _open(self):
  49. self._con = sqlite.connect(self._filename)
  50. self._cur = self._con.cursor()
  51. self._create_tables()
  52. def _create_tables(self):
  53. self._cur.execute("PRAGMA TABLE_INFO(NODE)")
  54. if self._cur.fetchone() is not None:
  55. return
  56. self._cur.execute("CREATE TABLE NODE (ID INTEGER PRIMARY KEY "
  57. "AUTOINCREMENT, "
  58. "USER TEXT NOT NULL, "
  59. "PASSWORD TEXT NOT NULL, "
  60. "URL TEXT NOT NULL,"
  61. "NOTES TEXT NOT NULL)")
  62. self._cur.execute("CREATE TABLE TAG"
  63. "(ID INTEGER PRIMARY KEY AUTOINCREMENT,"
  64. "DATA BLOB NOT NULL UNIQUE)")
  65. self._cur.execute("CREATE TABLE LOOKUP ("
  66. "nodeid INTEGER NOT NULL, "
  67. "tagid INTEGER NOT NULL, "
  68. "FOREIGN KEY(nodeid) REFERENCES NODE(ID),"
  69. "FOREIGN KEY(tagid) REFERENCES TAG(ID))")
  70. self._cur.execute("CREATE TABLE CRYPTO"
  71. "(SEED TEXT,"
  72. " DIGEST TEXT)")
  73. # create a table to hold DB version info
  74. self._cur.execute("CREATE TABLE DBVERSION"
  75. "(VERSION TEXT NOT NULL DEFAULT '%s')" %
  76. self.dbformat)
  77. self._cur.execute("INSERT INTO DBVERSION VALUES('%s')" %
  78. self.dbformat)
  79. try:
  80. self._con.commit()
  81. except Exception as e: # pragma: no cover
  82. self._con.rollback()
  83. raise e