db.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import MySQLdb as mdb
  2. from datetime import datetime,timedelta
  3. class DB:
  4. age = datetime.now()
  5. def __init__(self, bot=None):
  6. self.bot = bot
  7. def _open(self):
  8. if self.bot is not None:
  9. dbusername = self.bot.conf.getDBUsername(self.bot.network)
  10. password = self.bot.conf.getDBPass(self.bot.network)
  11. dbname = self.bot.conf.getDBName(self.bot.network)
  12. else:
  13. dbusername = "pybot"
  14. password = "1q2w3e4r"
  15. dbname = "pybot"
  16. self.con = mdb.connect("localhost",dbusername,password,dbname)
  17. self.cur = self.con.cursor()
  18. def _close(self):
  19. self.con = None
  20. self.cur.close()
  21. # should prevent mysql has gone away errors.. ideally
  22. def _handle(self):
  23. global cur
  24. global age
  25. now = datetime.now()
  26. if now - self.age > timedelta(minutes=5):
  27. self.cur.close()
  28. self.con = mdb.connect("localhost","pybot","1q2w3e4r","pybot")
  29. self.cur = self.con.cursor()
  30. def select(self, where, what):
  31. try:
  32. self._open()
  33. self.cur.execute("""SELECT %s FROM %s""")
  34. data = self.cur.fetchall()
  35. self._close()
  36. except:
  37. self._close()
  38. return None
  39. return data
  40. def replace(self, where, which, what):
  41. try:
  42. self._open()
  43. self.cur.execute("""REPLACE INTO %s (%s) VALUES (%s)""",(where, which, what))
  44. self._close()
  45. except:
  46. self._close()
  47. return None
  48. def e(self, sql):
  49. try:
  50. self._open()
  51. self.cur.execute(sql)
  52. if "INSERT" in sql or "REPLACE" in sql:
  53. self.con.commit()
  54. self._close()
  55. elif "SELECT" in sql:
  56. e = self.cur.fetchall()
  57. self._close()
  58. return e
  59. except Exception, e:
  60. print e
  61. self.con.rollback()
  62. self._close()
  63. return None
  64. def insert(self, where, which, what):
  65. try:
  66. self._open()
  67. self.cur.execute("""INSERT INTO %s (%s) VALUES (%s)""",(where, which, what))
  68. self._close()
  69. except:
  70. self._close()
  71. return None
  72. def updateSeen(self,who,statement,event):
  73. self._open()
  74. #print "executing REPLACE INTO seen (user_name, statement, event) VALUES ( " + str(who) + " " + str(statement) + " " + str(event) + ")"
  75. self.cur.execute("REPLACE INTO seen (user_name, statement, event) VALUES (%s, %s, %s)", (who, statement, event))
  76. self._close()
  77. def getSeen(self, who):
  78. self._open()
  79. if who != "":
  80. self.cur.execute("SELECT user_name, date, statement, event FROM seen WHERE user_name = %s", who)
  81. data = self.cur.fetchone()
  82. return data;
  83. self._close()
  84. else:
  85. self._close()
  86. return None
  87. def _insertImg(self, user, url, channel):
  88. self._open()
  89. if user == "" or user == None:
  90. user = "nobody"
  91. try:
  92. self.cur.execute("""INSERT INTO img (user, url, channel) VALUES (%s, %s, %s)""", (user, url, channel))
  93. self.con.commit()
  94. except:
  95. self.con.rollback()
  96. self._close()
  97. def _getImgs(self):
  98. self._open()
  99. try:
  100. self.cur.execute("""SELECT * FROM img ORDER BY time DESC""")
  101. data = self.cur.fetchall()
  102. self._close()
  103. except:
  104. self._close()
  105. return None
  106. return data
  107. def _isAdmin(self, username):
  108. self._open()
  109. try:
  110. self.cur.execute("""SELECT * FROM admins WHERE username = %s""",(username))
  111. data = self.cur.fetchall()
  112. self._close()
  113. except:
  114. self._close()
  115. return None
  116. return data