# Path: dbtool/models.py
# class Sqllog(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     STATUS = (
#         ('0', '已执行'),
#         ('1', '待执行'),
#         ('2', '作废'),
#     )
# 
# 
#     user_id = models.IntegerField(max_length=128, blank=True, null=True, verbose_name=u"user_id")
#     user_name = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"user_name")
#     db_name = models.CharField(max_length=50, blank=True, null=True, verbose_name=u"db_name")
#     sqllog = models.TextField(max_length=2000, blank = True, null = True, verbose_name = u"sqllog")
#     create_time=models.DateTimeField(default=datetime.now,blank = True)
#     check_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"check_mod_rows")
#     real_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"real_mod_rows")
#     status = models.IntegerField(max_length=2, blank=True, null=True, verbose_name=u"status",choices=STATUS)
#     comments = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"comments")
#     type = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"type")
# 
# 
# 
#     def __unicode__(self):
#         return self.user_id
# 
# Path: dbtool/models.py
# class Dblist(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     dbname=models.CharField(max_length=20, blank=True, null=True, verbose_name=u"DB_IP")
#     db_role = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"db_role")
# 
# 
#     def __unicode__(self):
#         return self.dbname
# 
# Path: dbtool/forms.py
# class SqllogForm(forms.ModelForm):
# 
#     class Meta:
#         model = Sqllog
#         ordering = ['-create_time']
# 
#         fields = [
#             "user_id","user_name","db_name","sqllog","check_mod_rows","real_mod_rows","create_time","status","comments","type"
#         ]


from juser.user_api import *
from jperm.perm_api import get_group_user_perm
from dbtool.models import Sqllog
from dbtool.models import Dblist
from dbtool.forms import SqllogForm
from django.http import HttpResponse
from django.db import connection
from django.db.models import Q
from warnings import filterwarnings
import re
import json
import MySQLdb as mdb
import datetime
import re
import dbtool_api
import os
import ConfigParser
        con = mdb.connect(readonly_db_host, readonly_db_user, readonly_db_password, db,charset='utf8')
        with con:
            # 获取普通的查询cursor
            cur = con.cursor()
            cur.execute(cmd)
            desc = cur.description
            #print desc[0]
            field_name=[]
            for i in desc:
                field_name.append(i[0])

    return my_render('dbtool/dbtool.html', locals(), request)
    #return my_render('dbtool/dbtool.html', {"a":"aaa"}, request)


#######返回查询数据结果的json
@require_role(role='user')
def dbtool_myjson(request):
    defend_attack(request)
    db  = request.POST.get('db')
    cmd = request.POST.get('cmd')
    cmd=cmd.replace("\r\n", " ")
    con = mdb.connect(readonly_db_host, readonly_db_user, readonly_db_password, db,charset='utf8')
    with con:
        cur = con.cursor()
        cur.execute(cmd)
        rows = cur.fetchall()
        desc = cur.description
    mylist=[]
    for i in rows:

# Path: dbtool/models.py
# class Sqllog(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     STATUS = (
#         ('0', '已执行'),
#         ('1', '待执行'),
#         ('2', '作废'),
#     )
# 
# 
#     user_id = models.IntegerField(max_length=128, blank=True, null=True, verbose_name=u"user_id")
#     user_name = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"user_name")
#     db_name = models.CharField(max_length=50, blank=True, null=True, verbose_name=u"db_name")
#     sqllog = models.TextField(max_length=2000, blank = True, null = True, verbose_name = u"sqllog")
#     create_time=models.DateTimeField(default=datetime.now,blank = True)
#     check_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"check_mod_rows")
#     real_mod_rows = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"real_mod_rows")
#     status = models.IntegerField(max_length=2, blank=True, null=True, verbose_name=u"status",choices=STATUS)
#     comments = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"comments")
#     type = models.IntegerField(max_length=20, blank=True, null=True, verbose_name=u"type")
# 
# 
# 
#     def __unicode__(self):
#         return self.user_id
# 
# Path: dbtool/models.py
# class Dblist(models.Model):
#     """
#     asset modle
#         create_time = models.DateTime(default=datetime.now)
#     """
#     dbname=models.CharField(max_length=20, blank=True, null=True, verbose_name=u"DB_IP")
#     db_role = models.CharField(max_length=20, blank=True, null=True, verbose_name=u"db_role")
# 
# 
#     def __unicode__(self):
#         return self.dbname
# 
# Path: dbtool/forms.py
# class SqllogForm(forms.ModelForm):
# 
#     class Meta:
#         model = Sqllog
#         ordering = ['-create_time']
# 
#         fields = [
#             "user_id","user_name","db_name","sqllog","check_mod_rows","real_mod_rows","create_time","status","comments","type"
#         ]


# Path: dbtool/views.py
from juser.user_api import *
from jperm.perm_api import get_group_user_perm
from dbtool.models import Sqllog
from dbtool.models import Dblist
from dbtool.forms import SqllogForm
from django.http import HttpResponse
from django.db import connection
from django.db.models import Q
from warnings import filterwarnings
import re
import json
import MySQLdb as mdb
import datetime
import re
import dbtool_api
import os
import ConfigParser

        con = mdb.connect(readonly_db_host, readonly_db_user, readonly_db_password, db,charset='utf8')
        with con:
            # 获取普通的查询cursor
            cur = con.cursor()
            cur.execute(cmd)
            desc = cur.description
            #print desc[0]
            field_name=[]
            for i in desc:
                field_name.append(i[0])

    return my_render('dbtool/dbtool.html', locals(), request)
    #return my_render('dbtool/dbtool.html', {"a":"aaa"}, request)


#######返回查询数据结果的json
@require_role(role='user')
def dbtool_myjson(request):
    defend_attack(request)
    db  = request.POST.get('db')
    cmd = request.POST.get('cmd')
    cmd=cmd.replace("\r\n", " ")
    con = mdb.connect(readonly_db_host, readonly_db_user, readonly_db_password, db,charset='utf8')
    with con:
        cur = con.cursor()
        cur.execute(cmd)
        rows = cur.fetchall()
        desc = cur.description
    mylist=[]
    for i in rows:

        mydisc={}