Beispiel #1
0
    def __get_db_servers(self, request, queues, model='queryMany'):
        dataList = []
        if request.POST.getlist('db[]'):
            dbList = request.POST.getlist('db[]')
        else:
            dbList = request.POST.get('db')

        if dbList:
            for db in dbList:
                try:
                    dbServer = DataBase_Server_Config.objects.get(
                        id=self.change(db))
                    dbRbt = MySQLPool(dbServer=dbServer,
                                      model=model,
                                      sql=request.POST.get('sql'),
                                      queues=queues)
                    dbRbt.start()
                except Exception as ex:
                    logger.error(msg="数据库不存在: {ex}".format(ex=ex))
                    return "数据库不存在"
            while True:
                while not queues.empty():
                    result = queues.get()
                    self.__record_operation(request, result.get('dbId'),
                                            result)
                    dataList.append(result)
                if 0 < len(dataList) >= len(dbList): break
            return dataList

        else:
            return "请选择先数据库"
Beispiel #2
0
 def __get_db_server(self, request):
     try:
         dbServer = self.__get_db(request)
         return MySQLPool(dbServer=dbServer)
     except Exception as ex:
         logger.error(msg="数据库不存在: {ex}".format(ex=ex))
         return ex
Beispiel #3
0
def db_org(request, id, format=None):
    try:
        dbServer = DataBase_Server_Config.objects.get(id=id)
    except DataBase_Server_Config.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'POST':
        MYSQL = MySQLPool(dbServer.to_connect())
        ARCH_INFO = MySQLARCH(MYSQL, dbServer.to_connect())
        if dbServer.db_mode == 'pxc': data = ARCH_INFO.pxc()
        elif dbServer.db_mode == 'slave': data = ARCH_INFO.master_slave()
        else:
            data = ARCH_INFO.single()
        return JsonResponse({"code": 200, "msg": "success", "data": data})
Beispiel #4
0
def db_user_db_table_list(request, uid, did, format=None):

    try:
        user_db = Database_User.objects.get(db=did, user=uid)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        user_table_list = []

        try:
            db_info = Database_Detail.objects.get(id=did)
        except Database_Detail.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        result = MySQLPool(dbServer=db_info.to_connect()).queryMany(
            'show tables', 10000)

        if not isinstance(result, str):
            count, tableList, colName = result[0], result[1], result[2]
            try:
                if user_db.tables: grant_tables = user_db.tables.split(",")
                else: grant_tables = []
            except:
                grant_tables = []

            if count > 0:
                for ds in tableList:
                    data = dict()
                    data["name"] = ds[0]
                    if ds[0] not in grant_tables:
                        data["count"] = 0
                    else:
                        data["count"] = 1
                    user_table_list.append(data)

        return Response(user_table_list)

    elif request.method == 'POST':
        if not request.user.has_perm(
                'databases.database_can_add_database_server_config'):
            return Response(status=status.HTTP_403_FORBIDDEN)

        try:
            user_db.tables = ",".join(request.data.getlist('table_name'))
            user_db.save()
        except Exception as ex:
            return Response(str(ex), status=status.HTTP_400_BAD_REQUEST)

        return Response(user_db.to_json(), status=status.HTTP_201_CREATED)
Beispiel #5
0
def db_status(request, id,format=None):
    try:
        dbServer = DataBase_Server_Config.objects.get(id=id)
    except DataBase_Server_Config.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)    
    if request.method == 'POST':
        MySQL_STATUS = {}
        MYSQL = MySQLPool(dbServer=dbServer)
        STATUS = MYSQL.get_status()
        GLOBAL = MYSQL.get_global_status()
        MySQL_STATUS['base'] = STATUS[0] + GLOBAL
        MySQL_STATUS['pxc'] = STATUS[1] 
        MySQL_STATUS['master'] = MYSQL.get_master_status()
        MySQL_STATUS['slave'] = MYSQL.get_slave_status()
        return JsonResponse({"code":200,"msg":"success","data":MySQL_STATUS})
Beispiel #6
0
    def get_user_db_tables(self, request):
        user_table_list = []
        try:
            userDbServer = Database_User.objects.get(
                id=request.GET.get('id', 0))
        except Exception as ex:
            logger.warn(msg="查询用户数据库失败: {ex}".format(ex=ex))
            return '用户数据库未分配'

        try:
            dbServer = DataBase_Server_Config.objects.get(id=userDbServer.db)
        except Exception as ex:
            logger.warn(msg="查询数据库失败: {ex}".format(ex=ex))
            return '数据库不存在'

        result = MySQLPool(dbServer=dbServer).queryMany('show tables', 10000)

        if not isinstance(result, str):
            count, tableList, colName = result[0], result[1], result[2]
            try:
                if userDbServer.tables:
                    grant_tables = userDbServer.tables.split(",")
                else:
                    grant_tables = []
            except Exception as ex:
                logger.warn(msg="查询用户授权表失败: {ex}".format(ex=ex))
                grant_tables = []

            if count > 0:
                for ds in tableList:
                    data = dict()
                    data["name"] = ds[0]
                    if ds[0] not in grant_tables:
                        data["count"] = 0
                    else:
                        data["count"] = 1
                    user_table_list.append(data)

        return user_table_list