12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- """Postgresql Database implementation."""
- import psycopg2 as pg
- from pwman.data.database import Database, __DB_FORMAT__
- class PostgresqlDatabase(Database):
- """
- Postgresql Database implementation
- This assumes that your database admin has created a pwman database
- for you and shared the user name and password with you.
- This driver send no clear text on wire. ONLY excrypted stuff is sent
- between the client and the server.
- Encryption and decryption are happening on your localhost, not on
- the Postgresql server.
- """
- @classmethod
- def check_db_version(cls, dburi):
- """
- Check the database version
- """
- con = pg.connect(dburi)
- cur = con.cursor()
- try:
- cur.execute("SELECT VERSION from DBVERSION")
- version = cur.fetchone()
- cur.close()
- con.close()
- return version[-1]
- except pg.ProgrammingError:
- con.rollback()
- return __DB_FORMAT__
- def __init__(self, pgsqluri, dbformat=__DB_FORMAT__):
- """
- Initialise PostgresqlDatabase instance.
- """
- self._pgsqluri = pgsqluri
- self.dbversion = dbformat
- self._sub = "%s"
- self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = %s "
- self._add_node_sql = ('INSERT INTO NODE(USERNAME, PASSWORD, URL, '
- 'NOTES) VALUES(%s, %s, %s, %s) RETURNING ID')
- self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(%s) RETURNING ID"
- self.ProgrammingError = pg.ProgrammingError
- def _open(self):
- self._con = pg.connect(self._pgsqluri.geturl())
- self._cur = self._con.cursor()
- self._create_tables()
|