mysql.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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
  26. import pymysql as mysql
  27. mysql.install_as_MySQLdb()
  28. class MySQLDatabase(Database):
  29. @classmethod
  30. def check_db_version(cls, dburi):
  31. port = 3306
  32. credentials, host = dburi.netloc.split('@')
  33. user, passwd = credentials.split(':')
  34. if ':' in host:
  35. host, port = host.split(':')
  36. port = int(port)
  37. con = mysql.connect(host=host, port=port, user=user, passwd=passwd,
  38. db=dburi.path.lstrip('/'))
  39. cur = con.cursor()
  40. try:
  41. cur.execute("SELECT VERSION FROM DBVERSION")
  42. version = cur.fetchone()
  43. cur.close()
  44. con.close()
  45. return version[-1]
  46. except mysql.ProgrammingError:
  47. con.rollback()
  48. return str(__DB_FORMAT__)
  49. def __init__(self, mysqluri, dbformat=__DB_FORMAT__):
  50. self.dburi = mysqluri
  51. self.dbversion = dbformat
  52. self._sub = "%s"
  53. self._list_nodes_sql = "SELECT NODEID FROM LOOKUP WHERE TAGID = %s "
  54. self._add_node_sql = ("INSERT INTO NODE(USERNAME, PASSWORD, URL, "
  55. "NOTES) "
  56. "VALUES(%s, %s, %s, %s)")
  57. self._insert_tag_sql = "INSERT INTO TAG(DATA) VALUES(%s)"
  58. self._data_wrapper = lambda x: x
  59. self.ProgrammingError = mysql.ProgrammingError
  60. def _open(self):
  61. port = 3306
  62. credentials, host = self.dburi.netloc.split('@')
  63. user, passwd = credentials.split(':')
  64. if ':' in host:
  65. host, port = host.split(':')
  66. port = int(port)
  67. self._con = mysql.connect(host=host, port=port, user=user,
  68. passwd=passwd,
  69. db=self.dburi.path.lstrip('/'))
  70. self._cur = self._con.cursor()
  71. try:
  72. self._create_tables()
  73. except pymysql.err.InternalError:
  74. pass