# project-4 **Repository Path**: york_king/project-4 ## Basic Information - **Project Name**: project-4 - **Description**: No description available - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 10 - **Created**: 2022-06-17 - **Last Updated**: 2022-06-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 面向对象数据库 > 王永康 主要完成了以下功能: **前端部分**: - [X] OR 操作 - [X] 运算: - [X] 不等于 != - [X] 小于 < - [X] 小于等于 <= - [X] 大于等于 >= - [X] max 操作 - [X] min 操作 - [X] 多目标表格查询: - [X] insert - [X] update - [X] delete **后端部分**: - [X] 多用户表格管理 ## 前端 实现了基本的 SQL 操作。以下是一些简单的测试: ### OR ```python def orTest(): with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: 学校 = ctx.学校 省份 = ctx.省份 查询结果 = ctx.SQL( select=[学校.名称, 省份.省名], where=db.OR( 学校.是否985 != True, 省份.列_211高校数量 >= 1, ) ) return 查询结果 ``` 结果 ![](fig/or.PNG) ### MAX/MIN ```python def maxTest(): with db.transaction(db_id='CCTV中国经济年度人物', user_secrets='shuit') as ctx: 公司 = ctx.公司 查询结果 = ctx.SQL( select=db.MAX([公司.市值]), ) return 查询结果 ``` 结果: ![](fig/max.PNG) ### insert ```python with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: ctx.SQL( insertInto=ctx.学校, values=[ "item_enterprise_2_6", "中山大学", "广州", "综合", True, True ] ) with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: 学校 = ctx.学校 查询结果 = ctx.SQL( select=[学校.名称], ) return 查询结果 ``` 结果: ![](fig/insert.PNG) ### update ```python with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: 学校 = ctx.学校 ctx.SQL( update=学校, set=[学校.是否985, False], where=db.AND( 学校.名称 == "北京大学" ), ) with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: 学校 = ctx.学校 查询结果 = ctx.SQL( select=[学校.名称, 学校.是否985], ) return 查询结果 ``` 结果: ![](fig/update.PNG) ### delete ```python with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: 学校 = ctx.学校 ctx.SQL( delete=学校, where=db.OR( 学校.名称 == "中山大学", ), ) with db.transaction(db_id='2018年各校招生计划', user_secrets='shuit') as ctx: 学校 = ctx.学校 查询结果 = ctx.SQL( select=[学校.名称, 学校.是否985], ) return 查询结果 ``` 结果: ![](fig/delete.PNG) ## 后端——多用户表格管理 实现了不同用户对不同的表格具有不用的**访问权限**,具体而言,对一个表格,用户具有**不可读、读、写**三种级别的权限。 在每个数据库的 `meta/` 下用 `tableAccessedUsers.json` 和 `userAccessTables.json` 记录用户对该数据库中表格的访问权限。 - `tableAccessedUsers.json`: 记录表格中用户的访问权限; - `userAccessTables.json`: 记录用户可访问的表格 `db_files/2018年各校招生计划/meta/userAccessTables.json`: ```json { "luoj": [ "清华大学招生计划", "各省高校招生计划", "省份", "学校", "专业" ], "shuit": [ "专业", "清华大学招生计划", "各省高校招生计划", "省份", "学校" ] } ``` `db_files/2018年各校招生计划/meta/tableAccessedUsers.json` ```json { "专业": [ [ "shuit", 3 ], [ "luoj", 0 ] ], "各省高校招生计划": [ [ "luoj", 1 ], [ "shuit", 1 ] ], "学校": [ [ "luoj", 3 ], [ "shuit", 3 ] ], "清华大学招生计划": [ [ "luoj", 1 ], [ "shuit", 1 ] ], "省份": [ [ "luoj", 0 ], [ "shuit", 3 ] ] } ``` 后端在接收到前端的 SQL 语句后,首先检查用户是否具有对该表格的权限,然后再执行相应的操作。 ## 改进方向 - SQL 语句解析:使用语法分析器比如 Bison 实现对 SQL 语句的解析,这样才能支持更加复杂的 SQL 语句 - 后端的数据存储改用 B+/LSM tree 等数据结构来存储,使用 JSON 存储不利于数据的增加、修改和删除。