Exemple #1
0
    def query(self, request):
        cdata = self.cleaned_data
        id = cdata.get('id')
        text = cdata.get('text')
        context = []
        if id == '#':
            # 返回当前用户所有授权的库
            query = f"select distinct `db` from mysql.tables_priv where `user`='{request.user.username}' and `db` like 'query_%'"
            cnx = self._local_cnx()
            with cnx.cursor() as cursor:
                cursor.execute(query)
                for row in cursor.fetchall():
                    id = row['db'].split('_')[1]
                    schema = '_'.join(row['db'].split('_')[2:])
                    if MysqlConfig.objects.filter(pk=id).exists():
                        obj = MysqlConfig.objects.get(pk=id)
                        show_schema = '_'.join((obj.comment, schema))
                        context.append({
                            'id': '___'.join((obj.host, str(obj.port), schema)),
                            'text': show_schema,
                            'children': True,
                        })

        if len(id.split('___')) == 3:
            # 获取当前用户授权库的表
            host = id.split('___')[0]
            port = id.split('___')[1]
            queryset = MysqlConfig.objects.get(host=host, port=port)
            schema = id.split('___')[2]
            data = GetGrantSchemaMeta(request.user.username, queryset.id, schema).get_table()
            context = data

        return context
Exemple #2
0
 def query(self):
     cdata = self.cleaned_data
     host, port, schema = cdata.get('schema').split('___')
     id = MysqlConfig.objects.get(host=host, port=port).id
     if len(schema.split('.')) == 2:
         schema = schema
     if len(schema.split('.')) == 3:
         schema = '.'.join(schema.split('.')[:2])
     data = GetGrantSchemaMeta(id=id, schema=schema).get_index()
     context = {'status': 0, 'data': data}
     return context