def db_sql_dumps(request): if request.method == "POST":#执行原生SQL try: dbServer = DataBase_Server_Config.objects.get(id=int(request.POST.get('dbId'))) except Exception, ex: dbServer = None if dbServer: mysql = MySQLPool(host=dbServer.db_host,port=dbServer.db_port,user=dbServer.db_user,passwd=dbServer.db_passwd,dbName=dbServer.db_name) result = mysql.execute(sql=request.POST.get('sql'),num=1000) if isinstance(result,str): return JsonResponse({'msg':"不支持导出功能","code":500,'data':result,'tag':3}) else: if result[0] == 0:return JsonResponse({'msg':"不支持导出功能","code":200,'data':"SQL执行成功",'tag':3}) file_name = "query_result.csv" with open(file_name,"w") as csvfile: writer = csv.writer(csvfile,dialect='excel') #先写入columns_name writer.writerow(result[2]) #写入多行用writerows for ds in result[1]: writer.writerows([list(ds)]) response = StreamingHttpResponse(base.file_iterator(file_name)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment; filename="{file_name}'.format(file_name=file_name) return response
result = mysql.queryMany(sql=request.POST.get('sql'),num=1000) if isinstance(result,str): recordSQL.delay(exe_user=str(request.user),exe_db=dbServer,exe_sql=request.POST.get('sql'),exec_status=0,exe_result=str) return JsonResponse({'msg':"数据查询失败","code":500,'data':result,'tag':3}) recordSQL.delay(exe_user=str(request.user),exe_db=dbServer,exe_sql=request.POST.get('sql'),exec_status=1,exe_result=None) return JsonResponse({'msg':"数据查询成功","code":200,'data':{"colName":result[2],"dataList":result[1]},'count':result[0],'tag':3}) else:return JsonResponse({'msg':"数据查询失败","code":500,'data':"不是DQL类型语句",'tag':3}) elif request.method == "POST" and request.POST.get('opsTag') == '4' and request.user.is_superuser:#执行原生SQL try: dbServer = DataBase_Server_Config.objects.get(id=int(request.POST.get('dbId'))) except Exception, ex: dbServer = None if dbServer: mysql = MySQLPool(host=dbServer.db_host,port=dbServer.db_port,user=dbServer.db_user,passwd=dbServer.db_passwd,dbName=dbServer.db_name) result = mysql.execute(sql=request.POST.get('sql'),num=1000) if isinstance(result,str): recordSQL.delay(exe_user=str(request.user),exe_db=dbServer,exe_sql=request.POST.get('sql'),exec_status=0,exe_result=result) return JsonResponse({'msg':"数据查询失败","code":500,'data':result,'tag':3}) else: if result[0] == 0:return JsonResponse({'msg':"数据查询成功","code":200,'data':"SQL执行成功",'tag':3}) recordSQL.delay(exe_user=str(request.user),exe_db=dbServer,exe_sql=request.POST.get('sql'),exec_status=1,exe_result=None) return JsonResponse({'msg':"数据查询成功","code":200,'data':{"colName":result[2],"dataList":result[1]},'count':result[0],'tag':3}) else:return JsonResponse({'msg':"数据查询失败","code":500,'data':str(ex),'tag':3}) else:return JsonResponse({'msg':"数据操作失败","code":500,'data':"您可能没有权限操作此项目",'tag':3}) @login_required() @permission_required('OpsManage.can_read_sql_execute_histroy',login_url='/noperm/') def db_sql_logs(request,page): if request.method == "GET":
}) elif request.method == "POST" and request.POST.get( 'opsTag') == '4' and request.user.is_superuser: #执行原生SQL try: dbServer = DataBase_Server_Config.objects.get( id=int(request.POST.get('dbId'))) except Exception, ex: dbServer = None if dbServer: mysql = MySQLPool(host=dbServer.db_host, port=dbServer.db_port, user=dbServer.db_user, passwd=dbServer.db_passwd, dbName=dbServer.db_name) result = mysql.execute(sql=request.POST.get('sql'), num=1000) if isinstance(result, str): recordSQL.delay(exe_user=str(request.user), exe_db=dbServer, exe_sql=request.POST.get('sql'), exec_status=0, exe_result=result) return JsonResponse({ 'msg': "数据查询失败", "code": 500, 'data': result, 'tag': 3 }) else: if result[0] == 0: return JsonResponse({