Ejemplo n.º 1
0
 def select(self, instance):
     sql_query = SqlQuery(instance.db)
     data = sql_query.main(instance.sql_content)
     affected_rows = len(data)
     instance.handle_result_execute = json.dumps([str(row) for row in data],
                                                 cls=DateEncoder)
     instance.status = 0
     return instance, affected_rows
 def execute(self, request, *args, **kwargs):
     ret = res.get_ret()
     instance = self.get_object()
     if instance.status != -1:
         ret = {'status': -2, 'msg': self.executed}
         return Response(ret)
     affected_rows = 0
     instance.status = 0
     if instance.type == self.type_select_tag:
         sql_query = SqlQuery(instance.db)
         data = sql_query.main(instance.sql_content)
         affected_rows = len(data)
         instance.handle_result_execute = json.dumps(
             [list(row) for row in data], cls=DateEncoder)
     else:
         execute_time = 0
         opids = []
         rollback_able = False
         success_sqls, exception_sqls, handle_result_execute = self.check_execute_sql(
             instance.db.id, instance.sql_content, self.action_type_execute)
         for success_sql in success_sqls:
             instance.rollback_db = success_sql[8]
             affected_rows += success_sql[6]
             execute_time += float(success_sql[9])
             if re.findall(self.success_tag, success_sql[3]):
                 rollback_able = True
                 opids.append(success_sql[7].replace("'", ""))
         if exception_sqls:
             instance.status = 2
             instance.execute_errors = exception_sqls
             ret['status'] = -1
         instance.rollback_opid = opids
         instance.rollback_able = rollback_able
         instance.handle_result_execute = handle_result_execute
         ret['data']['execute_time'] = '%.3f' % execute_time
     instance.exe_affected_rows = affected_rows
     ret['data']['affected_rows'] = affected_rows
     self.replace_remark(instance)
     self.handle_approve(2, 1, 2)
     self.mail(instance, self.action_type_execute)
     return Response(ret)