postgresql.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it 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) 2015 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. """Postgresql Database implementation."""
  22. import psycopg2 as pg
  23. from pwman.data.database import Database, __DB_FORMAT__
  24. class PostgresqlDatabase(Database):
  25. """
  26. Postgresql Database implementation
  27. This assumes that your database admin has created a pwman database
  28. for you and shared the user name and password with you.
  29. This driver send no clear text on wire. ONLY excrypted stuff is sent
  30. between the client and the server.
  31. Encryption and decryption are happening on your localhost, not on
  32. the Postgresql server.
  33. """
  34. @classmethod
  35. def check_db_version(cls, dburi):
  36. """
  37. Check the database version
  38. """
  39. con = pg.connect(dburi)
  40. cur = con.cursor()
  41. try:
  42. cur.execute("SELECT VERSION from DBVERSION")
  43. version = cur.fetchone()
  44. cur.close()
  45. con.close()
  46. return version[-1]
  47. except pg.ProgrammingError:
  48. con.rollback()
  49. return __DB_FORMAT__
  50. def __init__(self, pgsqluri, dbformat=__DB_FORMAT__):
  51. """
  52. Initialise PostgresqlDatabase instance.
  53. """
  54. self._pgsqluri = pgsqluri
  55. self.dbversion = dbformat
  56. self._sub = "%s"
  57. self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = %s "
  58. self._add_node_sql = ('INSERT INTO NODE(USERNAME, PASSWORD, URL, '
  59. 'NOTES) VALUES(%s, %s, %s, %s) RETURNING ID')
  60. self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(%s) RETURNING ID"
  61. self.ProgrammingError = pg.ProgrammingError
  62. def _open(self):
  63. self._con = pg.connect(self._pgsqluri.geturl())
  64. self._cur = self._con.cursor()
  65. self._create_tables()