Esempio n. 1
0
    def query(self):
        cdata = self.cleaned_data
        schema = cdata['schema']
        host, port, schema = schema.split(',')

        status, msg = checkdbstatus(host, port)
        if status:
            table_list = GetTableInfo(host, port, schema).get_column_info()
            context = {'status': 0, 'msg': '', 'data': table_list}
        else:
            context = {
                'status': 2,
                'msg': f'无法连接到数据库,请联系管理员,\n主机: {host}\n端口: {port}'
            }
        return context
Esempio n. 2
0
    def save(self, request):
        sdata = self.validated_data
        id = sdata.get('id')
        envi_id = sdata.get('envi_id')

        data = Orders.objects.get(pk=id)
        s, msg = checkdbstatus(host=data.host, port=data.port)
        if not s:
            return False, msg
        if s:
            if data.progress not in ('0', '1'):
                # 判断当前工单的子任务是否存在,存在直接跳转
                if OrdersTasks.objects.filter(order_id=id).exists():
                    taskid = OrdersTasks.objects.filter(
                        order_id=id).first().taskid
                    jump_url = f'/orders/subtasks/list/{taskid}'
                    return True, jump_url
                else:
                    # 分割SQL,转换成sql列表
                    # 移除sql头尾的分号;
                    split_sqls = [
                        sql.strip(';')
                        for sql in sqlparse.split(data.contents,
                                                  encoding='utf8')
                    ]
                    taskid = uuid.uuid1().hex

                    # 生成子任务
                    for sql in split_sqls:
                        OrdersTasks.objects.create(
                            applicant=data.applicant,
                            taskid=taskid,
                            host=data.host,
                            port=data.port,
                            database=data.database,
                            sql=sql.strip(';'),
                            sql_type=data.sql_type,
                            envi_id=envi_id,
                            file_format=data.file_format,
                            order_id=id)
                    jump_url = f'/orders/subtasks/list/{taskid}'
                    return True, jump_url
            else:
                return False, '工单未完成,无法执行'