ToB企服应用市场:ToB评测及商务社交产业平台

标题: 【手写数据库内核miniToadb】第1天 模拟数据库流程,分析数据库内核的组成 [打印本页]

作者: 滴水恩情    时间: 2024-12-18 08:43
标题: 【手写数据库内核miniToadb】第1天 模拟数据库流程,分析数据库内核的组成
专栏内容
  
  一、概述


解析器就是将用户输入的命令字符串转换为程序内部的数据结构,然后就可以执行命令并返回结果。
通过一个简朴的C程序来看一下数据库的解析和执行流程。

二、模拟数据库


程序是一个模拟数据库创建表,插入数据,查询数据,删除数据,更新数据等操作,整个数据生存在内存当中,每张表由一个二维数组来存储。
表的数据结构成员有表名、表的字段fields、表中每行的数据records和数据行数。
每个字段界说包罗字段名和字段类型两部分,数组采用固定大小,所以字段和数据行数量都不能超过上限。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_FIELDS 10
  5. #define MAX_FIELD_LEN 20
  6. #define MAX_RECORDS 100
  7. struct field
  8. {
  9.     char name[MAX_FIELD_LEN];
  10.     char type[MAX_FIELD_LEN];
  11. };
  12. struct record
  13. {
  14.     int id;
  15.     char fields[MAX_FIELDS][MAX_FIELD_LEN];
  16. };
  17. struct table
  18. {
  19.     char name[MAX_FIELD_LEN];
  20.     struct field fields[MAX_FIELDS];
  21.     int num_fields;
  22.     struct record records[MAX_RECORDS];
  23.     int num_records;
  24. };
复制代码
在主函数中开始先创建表,然后根据菜单来选择操作命令,分别将输入的信息分发给命令处理函数。
  1. int main()
  2. {
  3.     struct table t;
  4.     create_table(&t);
  5.     while (1)
  6.     {
  7.         printf("Enter command (insert/select/update/delete/exit): ");
  8.         char command[MAX_FIELD_LEN];
  9.         scanf("%s", command);
  10.         if (strcmp(command, "insert") == 0)
  11.         {
  12.             insert_record(&t);
  13.         } else if (strcmp(command, "select") == 0)
  14.         {
  15.             print_table(&t);
  16.         } else if (strcmp(command, "update") == 0)
  17.         {
  18.             update_record(&t);
  19.         } else if (strcmp(command, "delete") == 0)
  20.         {
  21.             delete_record(&t);
  22.         } else if (strcmp(command, "exit") == 0)
  23.         {
  24.             break;
  25.         } else
  26.         {
  27.             printf("Invalid command\n");
  28.         }
  29.     }
  30.     return 0;
  31. }
复制代码
每一个命令对应了一个处理函数:

  1. void create_table(struct table *t)
  2. {
  3.     printf("Enter table name: ");
  4.     scanf("%s", t->name);
  5.     printf("Enter number of fields: ");
  6.     scanf("%d", &t->num_fields);
  7.     printf("Enter field names and types:\n");
  8.     for (int i = 0; i < t->num_fields; i++)
  9.     {
  10.         printf("Field %d name: ", i+1);
  11.         scanf("%s", t->fields[i].name);
  12.         printf("Field %d type: ", i+1);
  13.         scanf("%s", t->fields[i].type);
  14.     }
  15. }
  16. void insert_record(struct table *t)
  17. {
  18.     if (t->num_records >= MAX_RECORDS)
  19.     {
  20.         printf("Table is full\n");
  21.         return;
  22.     }
  23.     t->records[t->num_records].id = t->num_records + 1;
  24.     printf("Enter field values:\n");
  25.     for (int i = 0; i < t->num_fields; i++)
  26.     {
  27.         printf("%s: ", t->fields[i].name);
  28.         scanf("%s", t->records[t->num_records].fields[i]);
  29.     }
  30.     t->num_records++;
  31. }
  32. void print_table(struct table *t)
  33. {
  34.     printf("%s:\n\t", t->name);
  35.     for (int i = 0; i < t->num_fields; i++)
  36.     {
  37.         printf("%s\t", t->fields[i].name);
  38.     }
  39.     printf("\n");
  40.     for (int i = 0; i < t->num_records; i++)
  41.     {
  42.         printf("%d\t", t->records[i].id);
  43.         for (int j = 0; j < t->num_fields; j++)
  44.         {
  45.             printf("%s\t", t->records[i].fields[j]);
  46.         }
  47.         printf("\n");
  48.     }
  49. }
  50. void update_record(struct table *t)
  51. {
  52.     int id;
  53.     printf("Enter record id: ");
  54.     scanf("%d", &id);
  55.     if (id < 1 || id > t->num_records)
  56.     {
  57.         printf("Invalid id\n");
  58.         return;
  59.     }
  60.     printf("Enter field values:\n");
  61.     for (int i = 0; i < t->num_fields; i++)
  62.     {
  63.         printf("%s: ", t->fields[i].name);
  64.         scanf("%s", t->records[id-1].fields[i]);
  65.     }
  66. }
  67. void delete_record(struct table *t)
  68. {
  69.     int id;
  70.     printf("Enter record id: ");
  71.     scanf("%d", &id);
  72.     if (id < 1 || id > t->num_records)
  73.     {
  74.         printf("Invalid id\n");
  75.         return;
  76.     }
  77.     for (int i = id-1; i < t->num_records-1; i++)
  78.     {
  79.         t->records[i] = t->records[i+1];
  80.     }
  81.     t->num_records--;
  82. }
复制代码
当前模拟程序,只能支持对一张表的操作,基本包罗了数据库的核心模块:解析模块,执行模块,存储模块。
在主函数中的菜单选项,再通太过发到命令处理函数,这个过程是将用户的意图和用户数据解析的过程,转换为程序内部的数据结构,对应就是解析模块。
命令处理函数将命令对应的数据,按照命令的业务流程处理,修改存储数据,返回用户结果,其实数据库的执行模块大要也是这样的思路。
在存储部分,主要管理数据存储的构造结构,内存中的形式与磁盘上存储的形式,一般内存与磁盘的数据有对应的规则,两者存储的结构并不相同。
二、操作运行结果


编译运行上面的模拟演示程序,增长一个student门生表,有门生号和门生名两个属性列,数据可以自由添加。

  1. [senllang@hatch exam_01]$ gcc demo_1.c -o demo
  2. [senllang@hatch exam_01]$ ./demo
  3. Enter table name: student
  4. Enter number of fields: 2
  5. Enter field names and types:
  6. Field 1 name: sid
  7. Field 1 type: int
  8. Field 2 name: sname
  9. Field 2 type: varchar
  10. Enter command (insert/select/update/delete/exit): insert
  11. Enter field values:
  12. sid: 100
  13. sname: liqiang
  14. Enter command (insert/select/update/delete/exit): insert
  15. Enter field values:
  16. sid: 101
  17. sname: liuye
  18. Enter command (insert/select/update/delete/exit): select
  19. student:
  20.         sid     sname
  21. 1       100     liqiang
  22. 2       101     liuye
  23. Enter command (insert/select/update/delete/exit): update
  24. Enter record id: 2
  25. Enter field values:
  26. sid: 101
  27. sname: liuyewen
  28. Enter command (insert/select/update/delete/exit): select
  29. student:
  30.         sid     sname
  31. 1       100     liqiang
  32. 2       101     liuyewen
  33. Enter command (insert/select/update/delete/exit): exit
复制代码
三、结尾


通过一个简朴的C语言程序,来模拟数据库的运行机制,固然数据库功能非常繁多,但此中最核心的SQL的解析,命令的执行,结果的输出原理是一样的,别的怎样让执行性能提拔,并发时怎样处理冲突,故障时保障数据等机制将在此核心功能之上进一步使数据库更加强盛。
大家对数据库内核有兴趣的话,可以关注我,从今天开始从零编写一个可以利用的数据库内核。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4