{"id":20975964,"url":"https://github.com/banyc/minisql","last_synced_at":"2025-05-14T14:31:04.929Z","repository":{"id":89587124,"uuid":"261419364","full_name":"Banyc/MiniSQL","owner":"Banyc","description":"满分实验：An independent, standalone, and functioning database management system (DBMS) supporting a subset of SQL. Cross-platform. Totally from the scratch. B+ Tree indexes.","archived":false,"fork":false,"pushed_at":"2021-06-26T19:08:43.000Z","size":4430,"stargazers_count":14,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T19:03:57.796Z","etag":null,"topics":["antlr4","btree-indexes","cross-platform","csharp","database","dbms","dbms-homework","dbms-project","dotnet","from-scratch","minisql","sql","sqlite"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Banyc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-05T09:58:21.000Z","updated_at":"2024-11-03T01:22:05.000Z","dependencies_parsed_at":"2023-07-12T16:45:55.945Z","dependency_job_id":null,"html_url":"https://github.com/Banyc/MiniSQL","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2FMiniSQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2FMiniSQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2FMiniSQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2FMiniSQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Banyc","download_url":"https://codeload.github.com/Banyc/MiniSQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254160646,"owners_count":22024574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["antlr4","btree-indexes","cross-platform","csharp","database","dbms","dbms-homework","dbms-project","dotnet","from-scratch","minisql","sql","sqlite"],"created_at":"2024-11-19T04:48:14.737Z","updated_at":"2025-05-14T14:31:04.919Z","avatar_url":"https://github.com/Banyc.png","language":"C#","readme":"# MiniSQL\n\nA functioning database supporting a subset of SQL. Cross-platform.\n\n## Introduction\n\n![](img/2020-06-15-14-40-50.png)\n\n## Run\n\n1. Set `pwd` to `src/MiniSQL.Client/`\n1. Run command `dotnet run`\n1. Type in some supported SQL\n1. Type `exit` to exit\n    - Caution: typing `exit` is the only way to exit the process!\n\n## Environment\n\nYou will need .NET Core SDK to build and run this code.\n\nDownload link: \u003chttps://dotnet.microsoft.com/download/dotnet/5.0\u003e.\n\n## Supported Grammar\n\n```sql\ncreate table student (\n    sno char(8),\n    sname char(16) unique,\n    sage int,\n    sgender char (1),\n    primary key ( sno )\n);\n\ndrop table student;\n\ncreate index stunameidx on student ( sname );\n\ndrop index stunameidx;\n\nselect * from student;\nselect * from student where sno = '88888888';\nselect * from student where sage \u003e 20 and sgender = 'F';\nselect * from student where sno = '88888888' or sage \u003e 20 and sgender = 'F';\nselect * from student where sage \u003e (20 + 2) / 3;\n\ninsert into student values ('12345678','wy',22,'M');\n\ndelete from student;\ndelete from student where sno = '88888888';\n\nexecfile \"PATH/TO/SQL_FILE\" ;\n\n-- ensure writing back all dirty pages\nexit\n\n-- write all pages back to disk\nflush\n\nuse database DATABASE_NAME\ndrop database DATABASE_NAME\n\nshow tables;\n```\n\n## Terminology\n\n- file := the database file\n- file header := the header in the database file\n- table tree := the B+ tree designed to represent a table\n- index tree := the B+ tree designed to represent an index\n\n## Spec\n\n### Database File Format\n\nThe database file format mimicked that of SQLite but with some differences. \n\nDifferences:\n\n- the file headers omits many fields.\n- Index tree is also implemented with B+ tree, not B tree.\n- Index tree utilizes `InternalTableCell` and `LeafTableCell` rather than `InternalIndexCell` and `LeafIndexCell`. In order words, index trees have the same architecture as table trees. \n- Index tree could only support dropping and inserting along with the table tree but not deleting due to complicity (implemented in `DatabaseController`).\n- The `key` field of the table cells is implemented with type `DBRecord`, not `int`.\n- The nodes of B+ Tree each has a pointer `ParentPage` pointing to its parent.\n- Leaf nodes of B+ Tree each has a pointer `RightPage` pointing to next leaf node on the right.\n\nLearn more from:\n\n- with graph - \u003chttp://chi.cs.uchicago.edu/chidb/fileformat.html\u003e\n- official - \u003chttps://www.sqlite.org/fileformat.html\u003e\n\n### 数据类型\n\n只要求支持三种基本数据类型：int，char(n)，float。\n\n### 表定义\n\n一个表最多可以定义32个属性，各属性可以指定是否为unique；支持单属性的主键定义。\n\n### 索引的建立和删除\n\n对于表的主属性自动建立B+树索引，对于声明为unique的属性可以通过SQL语句由用户指定建立/删除B+树索引（因此，所有的B+树索引都是单属性单值的）。\n\n### 查找记录\n\n可以通过指定用and连接的多个条件进行查询，支持等值查询和区间查询。\n\n### 插入和删除记录\n\n支持每次一条记录的插入操作；支持每次一条或多条记录的删除操作。\n\n## Architecture\n\n![](img/2020-06-16-14-33-06.png)\n\n### Client\n\nClient 模块直接与用户交互，主要实现以下功能：\n\n1. 程序流程控制，即“启动并初始化 -\u003e ‘接收命令、处理命令、显示命令结果’循环 -\u003e 退出”流程。\n\n### Interpreter\n\n1. 接收并解释用户输入的命令，生成命令的内部数据结构表示，同时检查命令的语法正确性和语义正确性，对正确的命令调用API层提供的函数执行并显示执行结果，对不正确的命令显示错误信息。\n\n### API\n\nAPI模块是整个系统的核心，其主要功能为提供执行SQL语句的接口，供Interpreter层调用。该接口以Interpreter层解释生成的命令内部表示为输入，根据Catalog Manager提供的信息确定执行规则，并调用Record Manager、Index Manager和Catalog Manager提供的相应接口进行执行，最后返回执行结果给Interpreter模块。\n\n### Catalog Manager\n\nCatalog Manager负责管理数据库的所有模式信息，包括：\n\n1.\t数据库中所有表的定义信息，包括表的名称、表中字段（列）数、主键、定义在该表上的索引。\n2.\t表中每个字段的定义信息，包括字段类型、是否唯一等。\n3.\t数据库中所有索引的定义，包括所属表、索引建立在那个字段上等。\n\nCatalog Manager还必需提供访问及操作上述信息的接口，供Interpreter和API模块使用。\n\n### Record Manager\n\nRecord Manager负责管理记录表中数据的数据文件。主要功能为实现数据文件的创建与删除（由表的定义与删除引起）、记录的插入、删除与查找操作，并对外提供相应的接口。其中记录的查找操作要求能够支持不带条件的查找和带一个条件的查找（包括等值查找、不等值查找和区间查找）。\n\n数据文件由一个或多个数据块组成，块大小应与缓冲区块大小相同。一个块中包含一条至多条记录，为简单起见，只要求支持定长记录的存储，且不要求支持记录的跨块存储。\n\n### Index Manager\n\nIndex Manager负责B+树索引的实现，实现B+树的创建和删除（由索引的定义与删除引起）、等值查找、插入键值、删除键值等操作，并对外提供相应的接口。\n\nB+树中节点大小应与缓冲区的块大小相同，B+树的叉数由节点大小与索引键大小计算得到。\n\n### Buffer Manager\n\nBuffer Manager负责缓冲区的管理，主要功能有：\n\n1.\t根据需要，读取指定的数据到系统缓冲区或将缓冲区中的数据写出到文件\n2.\t实现缓冲区的替换算法，当缓冲区满时选择合适的页进行替换\n3.\t记录缓冲区中各页的状态，如是否被修改过等\n4.\t提供缓冲区页的pin功能，及锁定缓冲区的页，不允许替换出去\n\n为提高磁盘I/O操作的效率，缓冲区与文件系统交互的单位是块，块的大小应为文件系统与磁盘交互单位的整数倍，一般可定为4KB或8KB。\n\n### DB Files\n\nDB Files指构成数据库的所有数据文件，主要由记录数据文件、索引数据文件和Catalog数据文件组成。\n\n## Build Project\n\n1. Set `pwd` to the project folder.\n1. Make sure to uncomment the line in file `*.csproj`:\n    ```csharp\n    \u003c!-- \u003cOutputType\u003eExe\u003c/OutputType\u003e --\u003e\n    ```\n1. Run `dotnet run`\n\n## TODO\n\n- [x] Support `create database \u003cDATABASE_NAME\u003e`\n- [x] Support `drop database \u003cDATABASE_NAME\u003e`\n- [ ] Show number of rows that is modified\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanyc%2Fminisql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbanyc%2Fminisql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanyc%2Fminisql/lists"}