def check_execute_sql(self, db_id, sql_content, action_type, triggerstmp=''): db_instance = Dbconf.objects.get(id=db_id) db_conf = self.get_db_conf(db_instance.user, db_instance.password, db_instance.host, db_instance.port, action_type) sql_review = Inception(sql_content, db_instance.name).inception_handle( db_conf, triggerstmp) result, status = sql_review.get('result'), sql_review.get('status') if status == -1 or len(result) == 1: raise ParseError({self.connect_error: result}) success_sql_list = [] exception_sql_list = [] for sql_result in result: error_message = sql_result[4] if error_message == 'None': success_sql_list.append(sql_result) else: exception_sql_list.append(error_message) if exception_sql_list and action_type == self.action_type_check: raise NotValid(exception_sql_list) return success_sql_list, exception_sql_list, json.dumps(result)
def rollback(self, instance): self.filter_select_type(instance) self.check_rollback_able(instance) db_instance = instance.db rollback_opid_list = instance.rollback_opid rollback_db = instance.rollback_db back_sql_list = '' for opid in eval(rollback_opid_list): back_source = 'select tablename from $_$Inception_backup_information$_$ where opid_time = "{}" '.format( opid) back_table = Inception(back_source, rollback_db).get_back_table() statement_sql = 'select rollback_statement from {} where opid_time = "{}" '.format( back_table, opid) rollback_statement = Inception(statement_sql, rollback_db).get_back_sql() if not rollback_statement: instance.status = 2 instance.handle_result_rollback = json.dumps( [self.get_rollback_fail]) return instance, instance.affected_rows back_sql_list += rollback_statement db_conf = self.get_db_conf(db_instance.user, db_instance.password, db_instance.host, db_instance.port, self.action_type_execute) execute_results = Inception( back_sql_list, db_instance.name).inception_handle(db_conf).get('result') status = -3 for result in execute_results: if result[4] != 'None': status = 2 break instance.status = status instance.handle_result_rollback = json.dumps(execute_results) return instance, instance.affected_rows
def rollback(self, request, *args, **kwargs): ret = res.get_ret() instance = self.get_object() self.filter_select_type(instance) self.check_rollbackable(instance) dbobj = instance.db rollback_opid_list = instance.rollback_opid rollback_db = instance.rollback_db back_sqls = '' for opid in eval(rollback_opid_list): back_source = 'select tablename from $_$Inception_backup_information$_$ where opid_time = "{}" '.format(opid) back_table = Inception(back_source, rollback_db).get_back_table() statement_sql = 'select rollback_statement from {} where opid_time = "{}" '.format(back_table, opid) rollback_statement = Inception(statement_sql, rollback_db).get_back_sql() if not rollback_statement: raise ParseError(self.get_rollback_fail) back_sqls += rollback_statement db_addr = self.get_db_addr(dbobj.user, dbobj.password, dbobj.host, dbobj.port, self.action_type_execute) execute_results = Inception(back_sqls, dbobj.name).inception_handle(db_addr).get('result') success_num = 0 for result in execute_results: if result[4] == 'None': success_num += 1 if success_num != len(execute_results): ret['status'] = -1 status = -4 roll_affected_rows = None else: status = -3 roll_affected_rows = ret['data']['affected_rows'] = len(execute_results) - 1 instance.status = status instance.handle_result_rollback = json.dumps(execute_results) instance.roll_affected_rows = roll_affected_rows self.replace_remark(instance) return Response(ret)
def check_execute_sql(self, db_id, sql_content): dbobj = Dbconf.objects.get(id=db_id) db_addr = self.get_db_addr(dbobj.user, dbobj.password, dbobj.host, dbobj.port, self.action_type) sql_review = Inception(sql_content, dbobj.name).inception_handle(db_addr) result, status = sql_review.get('result'), sql_review.get('status') if status == -1 or len(result) == 1: raise ParseError({self.connect_error: result}) success_sqls = [] exception_sqls = [] for sql_result in result: error_message = sql_result[4] if error_message == 'None' or re.findall('Warning', error_message): success_sqls.append(sql_result) else: exception_sqls.append(error_message) if exception_sqls and self.action_type == '--enable-check': raise ParseError({self.exception_sqls: exception_sqls}) return (success_sqls, exception_sqls, json.dumps(result))
def get_type_data(self): index_list = Inception('desc inception.statistic').get_index_list() index_data = [] for index in index_list: sql = 'SELECT `statistic`.`{}`, COUNT(`statistic`.`{}`) ' \ 'AS `num` FROM `statistic` WHERE {} > 0 ' \ 'GROUP BY `statistic`.`{}` ORDER BY NULL;'\ .format(index, index, index, index) records = Inception(sql, 'inception').manual() total_execute_counts = 0 total_execute_times = 0 if records: for record in records: total_execute_counts += record[0] * record[1] total_execute_times += record[1] index_data.append({ 'index': index, 'total_execute_counts': total_execute_counts, 'total_execute_times': total_execute_times }) return index_data
def rollback(self, request, *args, **kwargs): instance = self.get_object() dbobj = instance.db rollback_opid_list = instance.rollback_opid rollback_db = instance.rollback_db # 回滚库 # 拼接回滚语句 back_sqls = '' # 回滚语句 for opid in eval(rollback_opid_list)[1:]: # 1 从回滚总表中获取表名 back_source = 'select tablename from $_$Inception_backup_information$_$ where opid_time = "{}" '.format(opid) back_table = Inception(back_source, rollback_db).get_back_table() # 2 从回滚子表中获取回滚语句 back_content = 'select rollback_statement from {} where opid_time = "{}" '.format(back_table, opid) back_sqls += Inception(back_content, rollback_db).get_back_sql() # 拼接回滚语句 执行回滚操作,修改sql状态 db_addr = self.get_db_addr(dbobj.user, dbobj.password, dbobj.host, dbobj.port, self.action_type) execute_results = Inception(back_sqls, dbobj.name).inception_handle(db_addr).get('result') instance.status = -3 instance.roll_affected_rows = self.ret['data']['affected_rows'] = len(execute_results) - 1 # 执行回滚语句的结果,除去第一个use 数据库的 self.replace_remark(instance) return Response(self.ret)
def rollback(self, request, *args, **kwargs): instance = self.get_object() self.filter_select_type(instance) dbobj = instance.db rollback_opid_list = instance.rollback_opid rollback_db = instance.rollback_db back_sqls = '' for opid in eval(rollback_opid_list)[1:]: back_source = 'select tablename from $_$Inception_backup_information$_$ where opid_time = "{}" '.format( opid) back_table = Inception(back_source, rollback_db).get_back_table() back_content = 'select rollback_statement from {} where opid_time = "{}" '.format( back_table, opid) back_sqls += Inception(back_content, rollback_db).get_back_sql() db_addr = self.get_db_addr(dbobj.user, dbobj.password, dbobj.host, dbobj.port, self.action_type_execute) execute_results = Inception( back_sqls, dbobj.name).inception_handle(db_addr).get('result') instance.status = -3 instance.roll_affected_rows = self.ret['data']['affected_rows'] = len( execute_results) - 1 self.replace_remark(instance) return Response(self.ret)