def connect_master(self): if self.connect_flag: self.cur.close() self.conn.close() self.connect_flag = False configer = Configer() host = configer.get_configer('MYSQL', 'host') port = configer.get_configer('MYSQL', 'port') user = configer.get_configer('MYSQL', 'user') passwd = configer.get_configer('MYSQL', 'passwd') db = configer.get_configer('MYSQL', 'db') charset = configer.get_configer('MYSQL', 'charset') try: self.conn = MySQLdb.connect(host=host, port=int(port), user=user, passwd=passwd, db=db, charset=charset) self.cur = self.conn.cursor() self.connect_flag = True except MySQLdb.Error, e: self.status = self.status_enum.CONN_ERR msg = 'connect failed' LOG.error('Error:%s' % str(e)) raise DBException(msg)
def select_collection(self, collection): try: self.collection = self.db[collection] except ServerSelectionTimeoutError as e: LOG.error('mongo select failed [%s]' % e) raise DBException('mongo select failed')
def exec_event(self, sql, **kwds): if self.event_flag: res = self.query(sql, **kwds) return res else: self.status = self.status_enum.EVENT_ERR raise DBException('event failed')
def test_post_ap_data_db_exception(self): data = correct_dict() self.sample_service.save_ap_data_for_sample = Mock( side_effect=DBException("message")) response = self.fetch('/', method='POST', body=json.dumps(data)) self.assertEqual(response.code, 500) self.assertEqual( 1, self.sample_service.save_ap_data_for_sample.call_count) created_ap_data = self.sample_service.save_ap_data_for_sample.call_args[ 0][0] self.check_ap_data(created_ap_data)
def connect(self, db): configer = Configer() mongo_host = configer.get_configer('MONGO', 'host') mongo_port = int(configer.get_configer('MONGO', 'port')) try: self.client = MongoClient(host=mongo_host, port=mongo_port) self.db = self.client[db] except ConnectionFailure, e: LOG.error('mongo connect failed [%s]' % e) raise DBException('mongo connect failed')
def connect_test(self): if self.connect_flag: self.cur.close() self.conn.close() self.connect_flag = False try: self.conn = MySQLdb.connect(host='10.60.0.151', port=3306, user='******', passwd='1a2s3dqwe', db='test', charset='utf8') self.cur = self.conn.cursor() except MySQLdb.Error, e: self.status = self.status_enum.CONN_ERR msg = 'connect failed' LOG.error('Error:%s' % str(e)) raise DBException(msg)
try: self.sql = sql % kwds LOG.info('execute SQL[%s]' % (self.sql)) self.cur.execute(self.sql) except MySQLdb.Warning, w: LOG.warn('Warning:%s' % str(w)) self.status = self.status_enum.QUERY_WAR except MySQLdb.Error, e: self.rollback() self.event_flag = False LOG.error('Error:%s' % str(e)) self.status = self.status_enum.QUERY_ERR raise DBException('query failed') except: self.status = self.status_enum.OTHER_ERR LOG.error('format failed') raise DBException('format failed') if self.cur.rowcount: return True else: return False def fetch(self):