从零开始写屎山数据库代码

阅读: 评论:0

从零开始写屎山数据库代码

从零开始写屎山数据库代码

第一章:进行数据库的创建。

本篇文章专门记录从零开始写屎山数据库代码的,希望走过路过跑一下给一点意见,将后续进行持续优化。所有给我提出真知灼见的同学们对此表示深深的感激,并且将在后边的实验中永远加入特别鸣谢的附录中。本人代码能力很弱,希望各位甲方大佬可以提出任何可以优化我代码的私聊我,我将进行长久的不间断磕头表示感谢,重复提供思路与解答和意见的将持续加入特别鸣谢列表中。

同时本人的数据库基础非常的差,真的是第一次学,如SQL语法都没学全。同时代码分类很乱,后期在github上进行代码开源分板块优化吧。。。

工作的改造前景:模仿SSMS开发一个图形化界面,然后分一下文件。

特别鸣谢列表:

1、18级计工本李浩然师哥:对于drop_database的path路径的修正。

#include<iostream>
#include<vector>
#include<windows.h>
#include<cctype>
#include<fstream>
#include <direct.h>
#include <sstream>#define PIT pair<string, string>
#define PII pair<string, int>using namespace std;class TABLE{
private:string table_Name;vector<PIT>table_Field_Type;//前边是字段,后边是字符类型,对字符类型去做一个解释vector<PII>table_Field_Length;//字段长度FILE* index;
public:void set_Name(string name, string path, vector<PIT>t_F_T, vector<PII>t_F_L){this->table_Name = name;this->table_Field_Type = t_F_T;this->table_Field_Length = t_F_L;this->index = fopen(path.c_str(), "w");}string get_Table_Name(){return this->table_Name;}vector<PIT> get_Table_Field_Type(){return this->table_Field_Type;}vector<PII> get_Table_Field_Length(){return this->table_Field_Length;}FILE* get_FILE(){return this->index;}
};
//vector<TABLE>a;//动态建表
class SYSTEM{//存放指令集,模拟SQL语言
public:int SQL = 0;void start();void help();//查看所有指令集 命令为:-helpvoid create_Database(string);//创建数据库string open_Database(string);//打开数据库void drop_Database(string);//删除数据库void create_Table(string, string);//创建表void drop_Table(string, string);//删除表string lowercase(string a){for(int i = 0; i < a.size(); i ++)a[i] = tolower(a[i]);return a;}string next(){string input;cin >> input;return lowercase(input);}
}administer;
void SYSTEM::start(){//后续尽量更改成图形化界面printf("----------------WELCOME TO SQL-SERVER!----------------n");printf("If you need any help, please input '-help'n");
}
void SYSTEM::help(){printf("中文使用说明书:n");printf("打开该说明书:使用-help指令 n");printf("创建数据库:使用CREATE DATABASE 库名n");printf("打开数据库:使用USE 库名n");printf("删除数据库:使用DROP DATABASE 库名n");printf("创建表:使用CREATE TABLE 表名 n");//我需要知道如何创建表的名称,文件索引,行列信息,和表的字段和类型//其中对表的类型的处理需要对类型进行一个设置printf("删除表:使用DROP TABLE 表名 n");
}
void SYSTEM::create_Database(string name) {char* cwd;cwd = _getcwd(NULL, 0);string* path = new string(cwd);*path += "\" + name;cout << *path<<endl;//获取当前路径并创建一个数据库文件夹if(0 != access(path->c_str(), 0)){if(0 == mkdir(path->c_str())){cout << "Create accomplish!" << endl;}elsecout << "ERROR! Create failed!" << endl;}elseprintf("ERROR! This Database has existed!n");
}
void SYSTEM::drop_Database(string name) {string path = open_Database(name);if(0 == access(path.c_str(), 0)){path = "rd " + path;if(0 == system(path.c_str())){cout << "The database named " << name << " has been deleted!" << endl;}else {cout << "ERROR! The database named " << name << " has some troubles to delete" << endl;}}elsecout << "ERROR! The database named " << name << " does not exist!" << endl;
}
string SYSTEM::open_Database(string name){char* cwd;cwd = _getcwd(NULL, 0);string* path = new string(cwd);*path += "\" + name;//cout << *path<<endl;//获取当前路径并创建一个数据库文件夹if(0 == access(path->c_str(), 0)){//cout << "The database named " << name << " opened successfully!" << endl;return *path;}elsecout << "The database named " << name << " does not exist!" << endl;return name;
}
void SYSTEM::create_Table(string path, string name) {string table_name = name.substr(0, name.size() - 1);vector<PII>f_To_L;vector<PIT>f_To_T;getchar();string order;getline(cin, order);while(order != ")"){stringstream sc(order);string temp1, temp2;sc >> temp1;sc >> temp2;if(temp2.find("(") == string::npos){f_place_back(temp1, temp2);f_place_back(temp1, 1);}else{int pos = temp2.find("(");string type = temp2.substr(0, pos);f_place_back(temp1, type);int length = stoi(temp2.substr(pos + 1, temp2.size() - 1 - (pos + 1)));f_place_back(temp1, length);}getline(cin, order);}name = table_name + ".txt";path = path + "\" + name;cout << path << endl;if(0 == access(path.c_str(), 0)){cout << "The table already exists." << endl;return;}TABLE *table = new TABLE;table->set_Name(table_name, path, f_To_T, f_To_L);string information = "";for(int i = 0; i < f_To_T.size(); i ++){information += f_To_T[i].first + ";" + f_To_T[i].second + ";" + to_string(f_To_L[i].second) + ";";}information += 'n';fprintf(table->get_FILE(), information.c_str()) ;::fclose(table->get_FILE());cout << "The data table is created successfully!" << 'n' ;}
void SYSTEM::drop_Table(string path, string name) {name = name + ".txt";path = path + "\" + name;cout << path << endl;if(0 != access(path.c_str(), 0)){cout << "The table does not exist!" << endl;return;}path = "del " + path;if(0 == system(path.c_str()))cout << "Deleted successfully!" << 'n' ;   //删除成功elsecout << "Deleted failed!" << 'n' ; //删除失败
}
void init()
{system("chcp 65001");administer.start();string order;//指令string database_Name;//记录上一个创建的数据库名while(true){if(administer.SQL == 0) printf(">");//指令提示符else if(administer.SQL == 1) cout << ">" << database_Name << ">";order = ();//cout << order << endl;if(order == "-help"){administer.help();}else if(order == "create"){//创建这里分层判断,以后创建数据库操作依然可用order = ();if(order == "table"){string name = ();//表名string path = administer.open_Database(database_Name);administer.SQL = ate_Table(path, name);}else if(order == "database"){string name = ();//库名ate_Database(name);administer.SQL = 1;database_Name = name;}else{printf("ERROR 1064 (4200): You have an error in your SQL syntax;n");}}else if(order == "drop"){//删除这里分层判断,以后创建数据库操作依然可用order = ();if(order == "table"){string name = ();//表名string path = administer.open_Database(database_Name);administer.drop_Table(path, name);}else if(order == "database"){string name = ();//库名administer.drop_Database(name);}else{printf("ERROR 1064 (4200): You have an error in your SQL syntax;n");}}else if(order == "use"){string name = ();administer.open_Database(name);administer.SQL = 1;database_Name = name;}elseprintf("ERROR 1064 (4200): You have an error in your SQL syntax;n");}
}
int main()
{init();system("pause");return 0;
}

本文发布于:2024-02-04 19:52:24,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170715207759038.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23