sqlite.py 3.8 KB

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