第一章:进行数据库的创建。
本篇文章专门记录从零开始写屎山数据库代码的,希望走过路过跑一下给一点意见,将后续进行持续优化。所有给我提出真知灼见的同学们对此表示深深的感激,并且将在后边的实验中永远加入特别鸣谢的附录中。本人代码能力很弱,希望各位甲方大佬可以提出任何可以优化我代码的私聊我,我将进行长久的不间断磕头表示感谢,重复提供思路与解答和意见的将持续加入特别鸣谢列表中。
同时本人的数据库基础非常的差,真的是第一次学,如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();//查看所有指令集 命令为:-help
void 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;
}
else
cout << "ERROR! Create failed!" << endl;
}
else
printf("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;
}
}
else
cout << "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;
}
else
cout << "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_To_T.emplace_back(temp1, temp2);
f_To_L.emplace_back(temp1, 1);
}
else{
int pos = temp2.find("(");
string type = temp2.substr(0, pos);
f_To_T.emplace_back(temp1, type);
int length = stoi(temp2.substr(pos + 1, temp2.size() - 1 - (pos + 1)));
f_To_L.emplace_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' ; //删除成功
else
cout << "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 = administer.next();
//cout << order << endl;
if(order == "-help"){
administer.help();
}
else if(order == "create"){//创建这里分层判断,以后创建数据库操作依然可用
order = administer.next();
if(order == "table"){
string name = administer.next();//表名
string path = administer.open_Database(database_Name);
administer.SQL = 1;
administer.create_Table(path, name);
}
else if(order == "database"){
string name = administer.next();//库名
administer.create_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 = administer.next();
if(order == "table"){
string name = administer.next();//表名
string path = administer.open_Database(database_Name);
administer.drop_Table(path, name);
}
else if(order == "database"){
string name = administer.next();//库名
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.next();
administer.open_Database(name);
administer.SQL = 1;
database_Name = name;
}
else
printf("ERROR 1064 (4200): You have an error in your SQL syntax;\n");
}
}
int main()
{
init();
system("pause");
return 0;
}