mysql.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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) 2012-2015 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. # mysql -u root -p
  20. # create database pwmantest
  21. # create user 'pwman'@'localhost' IDENTIFIED BY '123456';
  22. # grant all on pwmantest.* to 'pwman'@'localhost';
  23. """MySQL Database implementation."""
  24. from pwman.data.database import Database, __DB_FORMAT__
  25. import pymysql as mysql
  26. mysql.install_as_MySQLdb()
  27. class MySQLDatabase(Database):
  28. @classmethod
  29. def check_db_version(cls, dburi):
  30. port = 3306
  31. credentials, host = dburi.netloc.split('@')
  32. user, passwd = credentials.split(':')
  33. if ':' in host:
  34. host, port = host.split(':')
  35. port = int(port)
  36. con = mysql.connect(host=host, port=port, user=user, passwd=passwd,
  37. db=dburi.path.lstrip('/'))
  38. cur = con.cursor()
  39. try:
  40. cur.execute("SELECT VERSION FROM DBVERSION")
  41. version = cur.fetchone()
  42. cur.close()
  43. con.close()
  44. return version[-1]
  45. except mysql.ProgrammingError:
  46. con.rollback()
  47. return str(__DB_FORMAT__)
  48. def __init__(self, mysqluri, dbformat=__DB_FORMAT__):
  49. self.dburi = mysqluri
  50. self.dbversion = dbformat
  51. self._sub = "%s"
  52. self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = %s "
  53. self._add_node_sql = ("INSERT INTO NODE(USERNAME, PASSWORD, URL, "
  54. "NOTES) "
  55. "VALUES(%s, %s, %s, %s)")
  56. self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(%s)"
  57. self.ProgrammingError = mysql.ProgrammingError
  58. def _open(self):
  59. port = 3306
  60. credentials, host = self.dburi.netloc.split('@')
  61. user, passwd = credentials.split(':')
  62. if ':' in host:
  63. host, port = host.split(':')
  64. port = int(port)
  65. self._con = mysql.connect(host=host, port=port, user=user,
  66. passwd=passwd,
  67. db=self.dburi.path.lstrip('/'))
  68. self._cur = self._con.cursor()
  69. self._create_tables()