专栏内容:
- 手写数据库toadb
本专栏主要介绍怎样从零开发,开发的步骤,以及开发过程中的涉及的原理,遇到的问题等,让大家能跟上并且可以一起开发,让每个需要的人成为参与者,在开源无穷的公众号更新会更及时。
一、概述
解析器就是将用户输入的命令字符串转换为程序内部的数据结构,然后就可以执行命令并返回结果。
通过一个简朴的C程序来看一下数据库的解析和执行流程。
二、模拟数据库
程序是一个模拟数据库创建表,插入数据,查询数据,删除数据,更新数据等操作,整个数据生存在内存当中,每张表由一个二维数组来存储。
表的数据结构成员有表名、表的字段fields、表中每行的数据records和数据行数。
每个字段界说包罗字段名和字段类型两部分,数组采用固定大小,所以字段和数据行数量都不能超过上限。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define MAX_FIELDS 10
- #define MAX_FIELD_LEN 20
- #define MAX_RECORDS 100
-
- struct field
- {
- char name[MAX_FIELD_LEN];
- char type[MAX_FIELD_LEN];
- };
-
- struct record
- {
- int id;
- char fields[MAX_FIELDS][MAX_FIELD_LEN];
- };
-
- struct table
- {
- char name[MAX_FIELD_LEN];
- struct field fields[MAX_FIELDS];
- int num_fields;
- struct record records[MAX_RECORDS];
- int num_records;
- };
复制代码 在主函数中开始先创建表,然后根据菜单来选择操作命令,分别将输入的信息分发给命令处理函数。
- int main()
- {
- struct table t;
- create_table(&t);
- while (1)
- {
- printf("Enter command (insert/select/update/delete/exit): ");
- char command[MAX_FIELD_LEN];
- scanf("%s", command);
- if (strcmp(command, "insert") == 0)
- {
- insert_record(&t);
- } else if (strcmp(command, "select") == 0)
- {
- print_table(&t);
- } else if (strcmp(command, "update") == 0)
- {
- update_record(&t);
- } else if (strcmp(command, "delete") == 0)
- {
- delete_record(&t);
- } else if (strcmp(command, "exit") == 0)
- {
- break;
- } else
- {
- printf("Invalid command\n");
- }
- }
- return 0;
- }
复制代码 每一个命令对应了一个处理函数:
- 创建表命令添补表的结构中字段数量,字段的名称与类型界说;
- 插入一条记录,将用户输入的各字段数据插入表数据的尾部,并递增记录数;
- 查询表中的数据时,遍历字段界说并打印,然后遍历记录数据的二维数据,每行按字段分别打印;
- 删除和更新数据时,默认按数据行的序号作为索引,操作时输入对应序号。
- void create_table(struct table *t)
- {
- printf("Enter table name: ");
- scanf("%s", t->name);
- printf("Enter number of fields: ");
- scanf("%d", &t->num_fields);
- printf("Enter field names and types:\n");
- for (int i = 0; i < t->num_fields; i++)
- {
- printf("Field %d name: ", i+1);
- scanf("%s", t->fields[i].name);
- printf("Field %d type: ", i+1);
- scanf("%s", t->fields[i].type);
- }
- }
-
- void insert_record(struct table *t)
- {
- if (t->num_records >= MAX_RECORDS)
- {
- printf("Table is full\n");
- return;
- }
- t->records[t->num_records].id = t->num_records + 1;
- printf("Enter field values:\n");
- for (int i = 0; i < t->num_fields; i++)
- {
- printf("%s: ", t->fields[i].name);
- scanf("%s", t->records[t->num_records].fields[i]);
- }
- t->num_records++;
- }
-
- void print_table(struct table *t)
- {
- printf("%s:\n\t", t->name);
- for (int i = 0; i < t->num_fields; i++)
- {
- printf("%s\t", t->fields[i].name);
- }
- printf("\n");
- for (int i = 0; i < t->num_records; i++)
- {
- printf("%d\t", t->records[i].id);
- for (int j = 0; j < t->num_fields; j++)
- {
- printf("%s\t", t->records[i].fields[j]);
- }
- printf("\n");
- }
- }
-
- void update_record(struct table *t)
- {
- int id;
- printf("Enter record id: ");
- scanf("%d", &id);
- if (id < 1 || id > t->num_records)
- {
- printf("Invalid id\n");
- return;
- }
- printf("Enter field values:\n");
- for (int i = 0; i < t->num_fields; i++)
- {
- printf("%s: ", t->fields[i].name);
- scanf("%s", t->records[id-1].fields[i]);
- }
- }
-
- void delete_record(struct table *t)
- {
- int id;
- printf("Enter record id: ");
- scanf("%d", &id);
- if (id < 1 || id > t->num_records)
- {
- printf("Invalid id\n");
- return;
- }
- for (int i = id-1; i < t->num_records-1; i++)
- {
- t->records[i] = t->records[i+1];
- }
- t->num_records--;
- }
复制代码 当前模拟程序,只能支持对一张表的操作,基本包罗了数据库的核心模块:解析模块,执行模块,存储模块。
在主函数中的菜单选项,再通太过发到命令处理函数,这个过程是将用户的意图和用户数据解析的过程,转换为程序内部的数据结构,对应就是解析模块。
命令处理函数将命令对应的数据,按照命令的业务流程处理,修改存储数据,返回用户结果,其实数据库的执行模块大要也是这样的思路。
在存储部分,主要管理数据存储的构造结构,内存中的形式与磁盘上存储的形式,一般内存与磁盘的数据有对应的规则,两者存储的结构并不相同。
二、操作运行结果
编译运行上面的模拟演示程序,增长一个student门生表,有门生号和门生名两个属性列,数据可以自由添加。
- [senllang@hatch exam_01]$ gcc demo_1.c -o demo
- [senllang@hatch exam_01]$ ./demo
- Enter table name: student
- Enter number of fields: 2
- Enter field names and types:
- Field 1 name: sid
- Field 1 type: int
- Field 2 name: sname
- Field 2 type: varchar
- Enter command (insert/select/update/delete/exit): insert
- Enter field values:
- sid: 100
- sname: liqiang
- Enter command (insert/select/update/delete/exit): insert
- Enter field values:
- sid: 101
- sname: liuye
- Enter command (insert/select/update/delete/exit): select
- student:
- sid sname
- 1 100 liqiang
- 2 101 liuye
- Enter command (insert/select/update/delete/exit): update
- Enter record id: 2
- Enter field values:
- sid: 101
- sname: liuyewen
- Enter command (insert/select/update/delete/exit): select
- student:
- sid sname
- 1 100 liqiang
- 2 101 liuyewen
- Enter command (insert/select/update/delete/exit): exit
复制代码 三、结尾
通过一个简朴的C语言程序,来模拟数据库的运行机制,固然数据库功能非常繁多,但此中最核心的SQL的解析,命令的执行,结果的输出原理是一样的,别的怎样让执行性能提拔,并发时怎样处理冲突,故障时保障数据等机制将在此核心功能之上进一步使数据库更加强盛。
大家对数据库内核有兴趣的话,可以关注我,从今天开始从零编写一个可以利用的数据库内核。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |