store.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #-*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import MySQLdb
  16. from rrd import config
  17. from rrd.utils.logger import logging
  18. portal_db_cfg = {
  19. "DB_HOST": config.PORTAL_DB_HOST,
  20. "DB_PORT": config.PORTAL_DB_PORT,
  21. "DB_USER": config.PORTAL_DB_USER,
  22. "DB_PASS": config.PORTAL_DB_PASS,
  23. "DB_NAME": config.PORTAL_DB_NAME,
  24. }
  25. alarm_db_cfg = {
  26. "DB_HOST": config.ALARM_DB_HOST,
  27. "DB_PORT": config.ALARM_DB_PORT,
  28. "DB_USER": config.ALARM_DB_USER,
  29. "DB_PASS": config.ALARM_DB_PASS,
  30. "DB_NAME": config.ALARM_DB_NAME,
  31. }
  32. def connect_db(cfg):
  33. try:
  34. conn = MySQLdb.connect(
  35. host=cfg['DB_HOST'],
  36. port=cfg['DB_PORT'],
  37. user=cfg['DB_USER'],
  38. passwd=cfg['DB_PASS'],
  39. db=cfg['DB_NAME'],
  40. use_unicode=True,
  41. charset="utf8")
  42. return conn
  43. except Exception, e:
  44. logging.getLogger().critical('connect db: %s' % e)
  45. return None
  46. class DB(object):
  47. def __init__(self, cfg):
  48. self.config = cfg
  49. self.conn = None
  50. def get_conn(self):
  51. if self.conn is None:
  52. self.conn = connect_db(self.config)
  53. return self.conn
  54. def execute(self, *a, **kw):
  55. cursor = kw.pop('cursor', None)
  56. try:
  57. cursor = cursor or self.get_conn().cursor()
  58. cursor.execute(*a, **kw)
  59. except (AttributeError, MySQLdb.OperationalError):
  60. self.conn and self.conn.close()
  61. self.conn = None
  62. cursor = self.get_conn().cursor()
  63. cursor.execute(*a, **kw)
  64. return cursor
  65. # insert one record in a transaction
  66. # return last id
  67. def insert(self, *a, **kw):
  68. cursor = None
  69. try:
  70. cursor = self.execute(*a, **kw)
  71. row_id = cursor.lastrowid
  72. self.commit()
  73. return row_id
  74. except MySQLdb.IntegrityError:
  75. self.rollback()
  76. finally:
  77. cursor and cursor.close()
  78. # update in a transaction
  79. # return affected row count
  80. def update(self, *a, **kw):
  81. cursor = None
  82. try:
  83. cursor = self.execute(*a, **kw)
  84. self.commit()
  85. row_count = cursor.rowcount
  86. return row_count
  87. except MySQLdb.IntegrityError:
  88. self.rollback()
  89. finally:
  90. cursor and cursor.close()
  91. def query_all(self, *a, **kw):
  92. cursor = None
  93. try:
  94. cursor = self.execute(*a, **kw)
  95. return cursor.fetchall()
  96. finally:
  97. cursor and cursor.close()
  98. def query_one(self, *a, **kw):
  99. rows = self.query_all(*a, **kw)
  100. if rows:
  101. return rows[0]
  102. else:
  103. return None
  104. def query_column(self, *a, **kw):
  105. rows = self.query_all(*a, **kw)
  106. if rows:
  107. return [row[0] for row in rows]
  108. else:
  109. return []
  110. def commit(self):
  111. if self.conn:
  112. try:
  113. self.conn.commit()
  114. except MySQLdb.OperationalError:
  115. self.conn = None
  116. def rollback(self):
  117. if self.conn:
  118. try:
  119. self.conn.rollback()
  120. except MySQLdb.OperationalError:
  121. self.conn = None
  122. db = DB(portal_db_cfg)
  123. alarm_db = DB(alarm_db_cfg)