Пример #1
0
def check_check(table_name, column, value):
    path = get_table_info_path(dv.CURRENT_DB, table_name)
    data = rwfile.read_from_csv(path)
    row_num = get_row_index(data, column)
    check_content = data[row_num][7]
    if value != check_content:
        return False
    return True
Пример #2
0
def get_user_permissions(username, db_name):
    permissions_path = get_permission_path()
    data = rwfile.read_from_csv(permissions_path)
    permission = []
    for item in data:
        if item[0] == username and (item[1] == db_name or item[1] == '*'):
            permission.append(item)
    return permission
Пример #3
0
def get_all_columns(table_name):
    columns = []
    path = get_table_info_path(dv.CURRENT_DB, table_name)
    data = rwfile.read_from_csv(path)
    for item in data:
        if item[0] == 'key':
            continue
        columns.append(item[0])
    return columns
Пример #4
0
def check_not_null(table_name, column):
    path = get_table_info_path(dv.CURRENT_DB, table_name)
    data = rwfile.read_from_csv(path)
    row_index = get_row_index(data, column)
    column_num = len(data[0])
    for index in range(0, column_num):
        if data[row_index][index] == 'FALSE':
            print('未满足非空约束')
            return False
    return True
Пример #5
0
def parse(token):
    path = base.get_database_path(dv.CURRENT_DB)
    # 输出所有数据表、视图和索引的信息及其对象类型
    if token[1] == 'database':
        tables_name = rwfile.read_from_txt(path + r'\tables_name.txt')
        print('所有的数据表:')
        for table in tables_name:
            print(table)

        views_name = rwfile.read_from_txt(path + r'\views_name.txt')
        print('所有的视图:')
        for view in views_name:
            print(view)

        indexes_name = rwfile.read_from_txt(path + r'\indexes_name.txt')
        print('所有的索引:')
        for index in indexes_name:
            print(index)

    # 输出数据表中所有属性的详细信息
    elif token[1] == 'table':
        tables_name = rwfile.read_from_txt(path + r'\tables_name.txt')
        for table in tables_name:
            csv_path = base.get_table_info_path(dv.CURRENT_DB, table)
            content = rwfile.read_from_csv(csv_path)
            print(table + '表中所有属性的详细信息')
            for x in content:
                for y in x:
                    print('%20s' % y, end='')
                print()

    # 输出视图的定义语句
    elif token[1] == 'view':
        views_name = rwfile.read_from_txt(path + r'\views_name.txt')
        for view in views_name:
            txt_path = base.get_view_info_path(dv.CURRENT_DB, view)
            content = rwfile.read_from_txt(txt_path)
            print(view + '定义语句:')
            for item in content:
                print(item)

    # 输出索引的详细信息
    elif token[1] == 'index':
        indexes_name = rwfile.read_from_txt(path + r'\indexes_name.txt')
        for index in indexes_name:
            txt_path = base.get_index_info_path(dv.CURRENT_DB, index)
            content = rwfile.read_from_txt(txt_path)
            print(index + '详细信息:')
            for item in content:
                print(item)

    else:
        print("语法错误!!!")
Пример #6
0
def check_unique(table_name, column, value):
    path = get_table_data_path(dv.CURRENT_DB, table_name)
    data = rwfile.read_from_csv(path)
    row_num = len(data)
    if row_num == 2:
        return True
    column_index = get_column_index(data, column)
    for index in range(1, row_num):
        if str(data[index][column_index]) == str(value):
            print('未满足唯一约束')
            return False
    return True
Пример #7
0
def check_constraint(table_name, cv):
    path = get_table_info_path(dv.CURRENT_DB, table_name)
    info = rwfile.read_from_csv(path)
    row_num = len(info)
    columns = cv.keys()
    for row in range(1, row_num):
        # 数据类型

        if info[row][0] in columns:
            # print(type(cv[info[row][0]]))
            if not (cv[info[row][0]].isdigit() and info[row][1].lower() == 'int'):
                print('数据类型不匹配')
                return False
            matchObj = re.search(r'varchar\(.*\)', info[row][1].lower())
            if matchObj:
                data_type = str
                if isinstance(cv[info[row][0]], data_type):
                    print('数据类型不匹配')
                    return False
            else:
                return True

        # 主键
        if info[row][2].lower() == 'true':
            if info[row][0].lower() in columns:
                if not check_unique(table_name, info[row][0], cv[info[row][0]]):
                    return False
            else:
                return False
        # 外键
        if info[row][3].lower() == 'true':
            if info[row][0].lower() in columns:
                if not check_unique(table_name, info[row][0], cv[info[row][0]]):
                    return False
            else:
                return False
        # 唯一
        if info[row][4].lower() == 'true':
            if not check_unique(table_name, info[row][0], cv[info[row][0]]):
                return False
        # 非空
        if info[row][5].lower() == 'true':
            if info[row][0].lower() not in columns:
                return False
        # 检查约束
        if info[row][7].lower() == 'true':
            if info[row][0].lower() in columns:
                if not check_check(table_name, info[row][0], cv[info[row][0]]):
                    return False
            else:
                return False
    return True
Пример #8
0
def check_permission(table_name, permission):
    permission = permission.lower()
    permissions_path = get_permission_path()
    data = rwfile.read_from_csv(permissions_path)
    permissions = []
    for item in data:
        if item[0] == dv.CURRENT_USER and (item[1] == dv.CURRENT_DB or item[1] == '*') and (item[2] == 'table' or item[2] == '*') and (item[3] == table_name or item[3] == '*'):
            if item[4] == '*':
                permissions = ['insert', 'select', 'update', 'delete']
            else:
                permissions = item[4].split(',')
    if permission in permissions:
        return True
    else:
        return False