#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cstdlib>
#include<fstream>
#include <windows.h>
using namespace std;
struct Table
{
string table_name;///表名
vector<string> column;///列名
vector<string> table_property;///属性
vector<int> length;///长度
vector<vector<string> > constraint;///约束
vector<vector<string> > data;///数据
};
struct View
{
string view_name;///视图名
vector<string> View_proproperty;///视图属性;
};
struct Index
{
string index_name;///索引名
vector<vector<string> > index_data;///索引数据
};
class User
{
private:
string user;///用户名
string password;///密码
string Userstatus; ///用户的身份
public:
vector<string> User_select;///对于用户表的select权限
vector<string> User_insert;///对于用户表的insert权限
vector<string> User_delete;///对于用户表的delete权限
vector<string> User_update;///对于用户表的update权限
vector<string> User_drop; ///对于用户表的drop权限
User()
{
user = "root";
password = "root";
Userstatus = "user";
}
User(string _user,string _password,string _Userstatus,vector<string>_select,vector<string> _insert,vector<string>_delete,vector<string> _update,
vector<string> _drop)
{
user = _user;
password = _password;
Userstatus = _Userstatus;
User_select = _select;///对于用户表的select权限
User_insert = _insert;///对于用户表的insert权限
User_delete = _delete;///对于用户表的delete权限
User_update = _update;///对于用户表的update权限
User_drop = _drop;
}
void setUser(string _user)
{
user = _user;
}
string getUser()
{
return user;
}
void setPassword(string _password)
{
password = _password;
}
string getPassword()
{
return password;
}
void setUserstatus(string _Userstatus)
{
Userstatus = _Userstatus;
}
string getUserstatus()
{
return Userstatus;
}
};
class Database
{
private:
string DatabaseName;///数据库的名字
string path;///路径
string Allpath;///全局路径
string file_root_name;///存储表的txt文件
string file_view;///存储视图文件名;
string file_index;///存储索引文件名
string file_user_name;///存储用户信息的TXT文件
public:
void CreateDatabase();///数据库
void CreateTable(Table&,Database,User);///表
void DeleteTable(string, User,Database);///删除表中的信息
void DropTable(string,User,Database);///删除表
void DeleteRow();///删除一个j记录内的信息
void Addrow();///添加一行
///void AddTable();///添加一个表
void Select(string,User,Database);///选择语句
void AppointSelect(string ,string ,User ,Database);///
void UIMSelect(string,string,User,Database,vector<string>,string);///集合查询
void NESTSelect(string,string,User,Database,vector<string>&);///嵌套查询
void Insert(string,User,Database);///添加数据
void Update(string,User,Database);///更新数据
void Grant(string,User&,Database);///授予用户权限
void Revoke(string,User&,Database);///收回权限
Database()
{
Allpath = "0";
}
string getAllpath()
{
return Allpath;
}
string getDatabaseName()
{
return DatabaseName;
}
string getFile_root_name()
{
return file_root_name;
}
string getFile_view()
{
return file_view;
}
string getFile_index()
{
return file_index;
}
};
void Database::CreateDatabase()///建数据库
{
cout<<"Please enter the database name: ";
cin>>DatabaseName;
path="F:\\"+DatabaseName; ///路径=在F盘内,加上数据库的名字
Allpath = path+"\\"; ///全局记录路径
path="md " + path; ///MD创建路径
system(path.c_str()); ///把字符串转入命令
file_root_name = Allpath + DatabaseName + ".txt";
file_view = Allpath + "VIEW" + ".txt";
file_index = Allpath + "INDEX" + ".txt";
cout<<file_root_name<<endl;
FILE *fp;
ofstream file;///定义输出文件
if((fp = fopen(file_root_name.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果打开
{
return;
}
file.open(file_root_name.c_str()); ///作为输出文件打开
file.close();
file.open(file_view.c_str());
file.close();
file.open(file_index.c_str());
file.close();
}
int AboutTable_property(string table_property)
{
if(table_property == "int")
return 0;
else if(table_property == "char")
return 0;
else if(table_property == "double")
return 0;
else if(table_property == "varchar")
return 0;
else if(table_property == "string")
return 0;
else
return 1;
}
void Database::CreateTable(Table &table,Database database,User user)
/// create table table_name
///column1 属性 长度 <约束>,
///column1 属性 长度 <约束>;
{
FILE *fp;///定义一个名叫fp文件指针
string table_name;///表名
string column;///列名
string property;///属性
string terminator;///一行结束符
//string null;///空值约束
//string notnull;
//string primary;///主键约束
//string foreign;///外键约束
int length;///属性长度
string file_name;///文件名
cin>>table_name;
table.table_name = table_name;
ofstream ofile;///定义输出文件
file_name = table_name;
file_name = database.getAllpath() + file_name;
string fileType = ".txt";
file_name = file_name + fileType;
while((fp = fopen(file_name.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果打开
{
///printf("The file can not be opened.\n");
cout<<"The data table has been established. Please re-establish it :"<<endl;
///exit(1);///结束程序的执行
cin>>table_name;
table.table_name = table_name;
file_name = table_name;
file_name = database.getAllpath() + file_name;
string fileType = ".txt";
file_name = file_name + fileType;
}
ofile.open(database.getFile_root_name().c_str(),ios::app);///存储表名
ofile<<table_name<<endl;
ofile.close();
ofile.open(file_name.c_str(),ios::app);///作为输出文件打开
cin>>column;
while(column!= ";")
{
//cin>>property>>length>>null>>notnull>>primary>>foreign;
cin>>property>>length>>terminator;
while(AboutTable_property(property))
{
cout<<"property error!"<<endl;
//cin>>property>>length>>null>>notnull>>primary>>foreign;
cin>>property>>length>>terminator;
}
table.column.push_back(column);
table.table_property.push_back(property);
table.length.push_back(length);
int number = 0;///设计约束的数量即约束的列数
vector<string> constraint1;///临时记录一行的约束
while(terminator != "," && terminator != ";")
{
transform(terminator.begin(),terminator.end(),terminator.begin(),towupper);
if(terminator == "NULL" || terminator == "NOTNULL"||terminator == "PRIMARY" || terminator =="FOREIGN")
{
constraint1.push_back(terminator);
}
//table.constraint.push_back(constraintofhang);
cin>>terminator;
number++;
}
//cout<<"NUMBER: "<<number<<endl;
if(number == 0)///若没有输入约束项
{
if(terminator == ",")
{
constraint1.push_back("NOTNULL");
}
else if(terminator == ";")
{
constraint1.push_back("NOTNULL");
}
}
table.constraint.push_back(constraint1);
constraint1.clear();
if(terminator == ";")
{
break;
}
cin>>column;
cout<<"BBB"<<endl;
}
ofile<<table.table_name<<endl;
ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - -"<<endl;
ofile<<left << setw(10)<<"Column"<<left << setw(10)<<"Type"<<left << setw(10)<<"Length"<<endl;
//left << setw(10)<<"Null"<<left << setw(10)<<"Not null"<<
// left << setw(10)<<"Primary"<<left << setw(10)<<"Foreign"<<endl;
ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - -"<<endl;
for(int i = 0; i < table.column.size(); ++i)
{
ofile<<left << setw(10)<<table.column[i]<<left << setw(10) <<table.table_property[i]<< left << setw(10) <<table.length[i];
for(int j = 0 ; j < table.constraint[i].size() ; ++j)
{
ofile<<left << setw(10)<<table.constraint[i][j];
}
ofile<<endl;
//left << setw(10) <<table.null[i]<<left << setw(10) <<table.notnull[i]<<left << setw(10) <<table.primary[i]<<left << setw(10) <<table.foreign[i]<<endl;
}
ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - ---"<<endl;
for(int i = 0; i < table.column.size(); ++i)
{
ofile<<left << setw(10)<<table.column[i];
}
ofile<<endl;
ofile.close();
///-----------------------对用户设置对该表的权限-------------------------------------------------///
user.User_delete.push_back(table_name);
user.User_drop.push_back(table_name);
user.User_insert.push_back(table_name);
user.User_select.push_back(table_name);
user.User_update.push_back(table_name);
string path = "F:\\";
path = path + user.getUser() + ".txt";
///----------清除表数据-----///
ofile.open(path.c_str(),ios::trunc);///删除表中的数据
ofile.close();
///---------------------------------------------///
///------------将数据重新输入-----------------///
ofile.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < user.User_select.size(); ++i)
ofile<<left << setw(5)<<user.User_select[i];
ofile<<endl;
for(int i = 0 ; i < user.User_insert.size(); ++i)
ofile<<left << setw(5)<<user.User_insert[i];
ofile<<endl;
for(int i = 0 ; i < user.User_delete.size(); ++i)
ofile<<left << setw(5)<<user.User_delete[i];
ofile<<endl;
for(int i = 0 ; i < user.User_update.size(); ++i)
ofile<<left << setw(5)<<user.User_update[i];
ofile<<endl;
for(int i = 0 ; i < user.User_drop.size(); ++i)
ofile<<left << setw(5)<<user.User_drop[i];
ofile<<endl;
ofile.close();
///-----------------------------------------------------------------------------------------------///
return;
}
void show_Table(string name,Database database)
{
int i;
char line[100];
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
i=0;
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
fin.close();
}
void show_View(string name,Database database)///CREATE VIEW V_Customer
///AS
{
int i;
char line[100];
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
i=0;
cout<<"CREATE VIEW " + name + " AS ";
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
fin.close();
}
void show_Index(string name,Database database)///CREATE INDEX V_Customer
///AS
{
int i;
char line[100];
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
i=0;
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
fin.close();
}
void show_database(Database database)
{
int i;
char line[100];
string path_table;
string path_view;
string path_index;
path_table = database.getAllpath() + database.getDatabaseName()+".txt";
path_view = database.getFile_view();
path_index = database.getFile_index();
ifstream fin(path_table.c_str());
i=0;
cout<<"Table"<<endl;
cout<<"-------------"<<endl;
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
cout<<"-------------"<<endl<<endl;
fin.close();
cout<<"VIEW"<<endl;
cout<<"-------------"<<endl;
fin.open(path_view.c_str());
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
cout<<"-------------"<<endl;
fin.close();
cout<<"INDEX"<<endl;
cout<<"-------------"<<endl;
fin.open(path_index.c_str());
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
cout<<"-------------"<<endl;
fin.close();
}
void Database::DropTable(string table_name,User user,Database database)///DROP 类型 名称
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_drop.begin(); it!=user.User_drop.end();)
{
if(*it == table_name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
FILE *fp;
ofstream file;
string table_file;
string database_file;///存放表名的文件
/// string data;///数据
table_file = database.Allpath + table_name +".txt";
while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>table_name;
table_file = database.Allpath + table_name +".txt";
}
fclose(fp);
DeleteFile(table_file.c_str());
cout<<"DROP SUCCESS"<<endl;
///-------------------------------------
///将数据存入data中---------------///
database_file = database.Allpath + database.getDatabaseName() +".txt";
char line[100];
ifstream fin(database_file.c_str());
vector<string> data;///数据
int number = 0;
while(fin.getline(line,100))
{
if(strcmp(table_name.c_str(),line) != 0)
{
data.push_back(line);
++number;
}
}
fin.close();
///----------------------///
file.open(database_file.c_str(),ios::trunc);///删除表中的数据
file.clear();
file.close();
///-------------------------------/
///----------将数据重新写入文件中----------------------///
file.open(database_file.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < number; ++i)
{
file<<left << setw(10)<<data[i]<<endl;
}
file.close();
///-------------------------------------///
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
vector<vector<string> >::iterator it;///迭代器
it = data2.begin();
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(table_name == data2[i][0])///存在
{
///----------------------删除table_name_index文件---------------------------------///
vector<vector<string> > data3;///存索引数据
string file_index = database.getAllpath() + table_name + "_index"+".txt";
DeleteFile(file_index.c_str());
///----------------------------------------------------------------------------------///
///进入存储索引名称的文件中、删除对应的索引------------------------------------------//
// fin.open(t_file.c_str());
// fin.clear();
// fin.close();
///在data2中删除对应索引名
data2.erase(it);
exist = 1;
}
it++;
}
if(exist == 1)
{
///进入存储索引名称的文件中、删除对应的索引------------------------------------------//
ofstream fin_index(t_file.c_str());
fin_index.close();
///------------将数据重新输入--------///
file.open(t_file.c_str(),ios::app); ///作为输出文件打开
for(int j = 0 ; j < data2.size(); ++j)
{
file<<data2[j][0]<<"_"<<data2[j][1];
}
file<<endl;
file.close();
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::DeleteTable(string table_name,User user,Database database) ///DELETE FROM 表名称 WHERE 列名称 = 值
///DELETE FROM 表名称 ;
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_delete.begin(); it!=user.User_delete.end();)
{
if(*it == table_name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
FILE *fp;
ofstream file;
string table_file;
string command;///命令
string column;
table_file = database.Allpath + table_name +".txt";
while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>table_name;
table_file = database.Allpath + table_name +".txt";
}
fclose(fp);
///--------------------------------将表数据存入data1中---------------///
char line[100];
vector<string> data;///表格式无数据
vector<string> rowdata;///只存数据(行数据)
vector<vector<string> > data1;///存数据
string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
int number = 0;
int temporary1 = 0;
int temporary2 = 0;
ifstream fin(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 0 || temporary2 == -1)
{
data.push_back(line);
++number;
}
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
temporary2++;
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
temporary2 = -1;
}
}
fin.close();
///------------------------------------------------------
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
///----------------------------------//
///----------清除表数据-----///
file.open(table_file.c_str(),ios::trunc);///删除表中的数据
file.close();
///---------------------------///
///------------将数据重新输入--------///
file.open(table_file.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < number; ++i)
{
file<<left << setw(10)<<data[i]<<endl;
}
file.close();
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == command)
{
positionOfColumn2 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;///输入命令
string value1;
string value2;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>value1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")///select * form table where column = values ;
{
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
for(int kk = 1 ; kk < data1.size(); ++kk)
{
for(int mm = 0; mm < data1[kk].size(); ++mm)
{
cout<<left << setw(10)<<data1[kk][mm];
}
cout<<endl;
}
}
else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1)
{
///cout<<left << setw(10)<<data[i][k];
for(int j1 = 0 ; j1 < data1[i].size(); ++j1)
{
if(j1 == positionOfColumn3)
{
if(data1[i][j1] == value2)
{
data1.erase(dataiterator);
}
}
}
}
}
}
dataiterator++;
}
}
}
else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(positionOfColumn2 == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1 || data1[i][j] == value2)
{
data1.erase(dataiterator);
}
}
}
else
{
if(j == positionOfColumn2 || j == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1)
{
data1.erase(dataiterator);
}
}
else if(j == positionOfColumn3)
{
if(data1[i][j] == value2)
{
data1.erase(dataiterator);
}
}
}
}
}
dataiterator++;
}
}
}
}
else if(command == ">")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer > integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "<")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer < integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "<>")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer != integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == ">=")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer >= integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "<=")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer <= integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
cin>>command;
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
data1.erase(dataiterator);
}
}
}
}
dataiterator++;
}
}
}
file.open(table_file.c_str(),ios::trunc);///删除表中的数据
file.close();
///---------------------------///
///------------将数据重新输入--------///
file.open(table_file.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < number; ++i)
{
file<<left << setw(10)<<data[i]<<endl;
}
for(int i = 1 ; i < data1.size(); ++i)
{
for(int j =0; j < data1[i].size(); ++j)
{
file<<left<<setw(10)<<data1[i][j];
}
file<<endl;
}
file.close();
}
vector<vector<string> > data11;///存数据
temporary1 = 0;
fin.open(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data11.push_back(rowdata);
rowdata.clear();
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
}
}
fin.close();
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
//vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(table_name == data2[i][0])///存在
{
exist = 1;
}
}
if(exist == 1)
{
///**************读取索引文件内容*********************----//
string file_index = database.getAllpath() + table_name + "_index.txt";
fin.open(file_index.c_str());
string column;
while(fin.getline(line,100))///*****************************************************************************问题
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
column = p;
break;
}
break;
}
fin.close();
///***********************读取结束----------------------------------------------------------------------///
///进入索引的文件中、更新索引------------------------------------------//
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data11[0].size(); ++j)
{
if(data11[0][j] == column)///**************问题
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
ofstream fin_index(file_index.c_str());///删除文件中的内容
fin_index<<left<<setw(10)<<column<<endl;
for(int i = 1 ; i < data11.size(); ++i)
{
fin_index<<data11[i][positionOfColumn];
fin_index<<endl;
}
fin_index.close();
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::Insert(string table_name,User user,Database database)///INSERT INTO 表名称 VALUES (值1, 值2,....)
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_insert.begin(); it!=user.User_insert.end();)
{
if(*it == table_name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
FILE *fp;
string table_file;
string data;///数据
table_file = database.Allpath + table_name +".txt";
ofstream ofile;///向文件中写入数据
while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>table_name;
if(table_name == "ENDINPUT")
return;
table_file = database.Allpath + table_name +".txt";
}
fclose(fp);
///--------------------求属性个数------------------------------//
char line[100];
int number = 0;///属性个数
int numberofline = 0;
ifstream fin(table_file.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))
{
numberofline++;
if(numberofline > 4)
{
number++;
}
if(strcmp(s.c_str(),line) == 0)
{
break;
}
}
number = number - 1;
//cout<<number<<endl;
fin.close();
///---------------------------------------------------///
string datas[number];///输入的内容
cin>>data;
int i = 0;
transform(data.begin(),data.end(),data.begin(),towupper);
if(data == "VALUES")
{
ofile.open(table_file.c_str(),ios::app);
//cout<<table_file.c_str()<<endl;
while(i <= number - 1)
{
cin>>datas[i];
ofile<<left<<setw(10)<<datas[i];
++i;
}
ofile<<endl;
cout<<"Add Success"<<endl;
}
fclose(fp);
///--------------------------------将数据存入data1中---------------///
vector<string> rowdata;///只存数据(行数据)
vector<vector<string> > data1;///存数据
string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
int temporary1 = 0;
fin.open(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
}
}
fin.close();
///------------------------------------------------------
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
//vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(table_name == data2[i][0])///存在
{
exist = 1;
}
}
if(exist == 1)
{
///**************读取索引文件内容*********************----//
string file_index = database.getAllpath() + table_name + "_index.txt";
fin.open(file_index.c_str());
string column;
while(fin.getline(line,100))///*****************************************************************************问题
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
column = p;
break;
}
break;
}
fin.close();
///***********************读取结束----------------------------------------------------------------------///
///进入索引的文件中、更新索引------------------------------------------//
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
ofstream fin_index(file_index.c_str());///删除文件中的内容
fin_index.close();
///------------将数据重新输入--------///
fin_index.open(file_index.c_str(),ios::app); ///作为输出文件打开
fin_index<<left<<setw(10)<<column<<endl;
for(int i = 1 ; i < data1.size(); ++i)
{
fin_index<<data1[i][positionOfColumn];
fin_index<<endl;
}
fin_index.close();
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::Update(string name,User user,Database database)///UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
///UPDATE 表名称 SET 列名称 = 新值 ;
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_update.begin(); it!=user.User_update.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
string command;
string column1;
string value1;///操作数1
string column2;
string value2;///操作数2
char line[100];
FILE *fp;
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
number=0;
ofstream file;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>name;
if(name == "ENDINPUT")
return;
path = database.Allpath + name +".txt";
}
fclose(fp);
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "SET")
{
cin>>column1;
int number2 = -1;///记录行数;
int number1 = -1;///记录column1的所在位置;
int number = 0;///记录是否存在column1;
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
///----------------判断是否存在column1---------------//
while(fin.getline(line,100))
{
number2++;
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
vector<string> str;
while(p)
{
///printf("%s\n",p);
str.push_back(p);
p=strtok(NULL,d);
}
for(int i = 0 ; i < str.size(); ++i)
{
if(str[i] == column1)
{
number1 = i;
}
}
if(number1 == -1)
{
return;
}
break;
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
///------------------------------------------------------------------------//
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int temp = 0;
int hang = 0;///data行
ifstream fin(path.c_str());
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
if(temp == 0)
{
rowdata.push_back(line);
data.push_back(rowdata);
rowdata.clear();
}
else
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
temp = 1;
}
}
fin.close();
// for(int i = 0 ; i < data.size(); ++i)
// {
// for(int j = 0 ; j < data[i].size(); ++j)
// cout<<data[i][j];
// cout<<endl;
// }
///-----------------------------------------------------------------------------//
cin>>command;
if(command == "=")
{
cin>>value1;///输入操作数
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == number1)
{
data[i][j] = value1;
}
}
}
}
}
else if(command == "WHERE")
{
cin>>column2;
int positionOfColumn2;
temp = 0;
///----------------判断是否存在column2---------------//
for(int i = 0 ; i < data.size(); ++i)
{
if(i == number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(data[i][j] == column2)
{
positionOfColumn2 = j;
temp = 1;
}
}
}
}
if(temp == 0)
return;
//cout<<positionOfColumn2<<endl;
cin>>command;///输入命令
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>value2;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 1; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
///cout<<left << setw(10)<<data[i][k];
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn3)
{
if(data[i][j1] == value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
}
}
}
else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
// for(int i = 0 ; i < data[0].size(); ++i)
// cout<<left << setw(10)<<data[0][i];
// cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(positionOfColumn2 == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1 || data[i][j] == value2)
{
data[i][number1] = value1;
}
}
}
else
{
if(j == positionOfColumn2 || j == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
data[i][number1] = value1;
}
}
else if(j == positionOfColumn3)
{
if(data[i][j] == value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
}
}
}
else if(command == ">")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] > value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "<")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] < value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "<>")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] != value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == ">=")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] >= value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "<=")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] <= value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "BETWEEN")
{
int number3;
int number4;
cin>>number3;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number4;
int maxn = number3;
if(maxn > number4)
{
maxn = number4;
number4 = number3;
number3 = maxn;
}
for(int i = 0; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number3 && integer < number4)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
cin>>command;
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 1; i < data.size(); ++i)
{
if(i >number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
}
}
}
///----------清除表数据-----///
file.open(path.c_str(),ios::trunc);///删除表中的数据
file.close();
///------------将数据重新输入--------///
file.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
file<<left << setw(10)<<data[i][j];
file<<endl;
}
file.close();
}
///--------------------------------将数据存入data1中---------------///
vector<string> rowdata;///只存数据(行数据)
vector<vector<string> > data1;///存数据
string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
string table_file = database.Allpath + name +".txt";
int temporary1 = 0;
fin.open(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
}
}
fin.close();
///------------------------------------------------------
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
//vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
// cout<<"GETFILE_VIEW: "<<t_file<<endl;
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(name == data2[i][0])///存在
{
exist = 1;
}
}
// cout<<"EXIT: "<<exit<<endl;
if(exist == 1)
{
///**************读取索引文件内容*********************----//
string file_index = database.getAllpath() + name + "_index.txt";
fin.open(file_index.c_str());
string column;
while(fin.getline(line,100))///*****************************************************************************问题
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
column = p;
break;
}
break;
}
fin.close();
///***********************读取结束----------------------------------------------------------------------///
///进入索引的文件中、更新索引------------------------------------------//
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
// cout<<"TEMP: "<<temp<<endl;
if(temp == 0)
return;
ofstream fin_index(file_index.c_str());///删除文件中的内容
fin_index.close();
///------------将数据重新输入--------///
fin_index.open(file_index.c_str(),ios::app); ///作为输出文件打开
fin_index<<left<<setw(10)<<column<<endl;
for(int i = 1 ; i < data1.size(); ++i)
{
fin_index<<data1[i][positionOfColumn];
fin_index<<endl;
}
fin_index.close();
}
cout<<"UPDATE SUCCESS!"<<endl;
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void OrderBy(string command, vector<vector<string> >&data)
{
int commandposition = -1;
for(int j = 0 ; j < data[0].size(); ++j)
{
if(command == data[0][j])
{
commandposition = j;
}
}
string temp;
if(commandposition == -1)
{
cout<<"Input error"<<endl;
}
else
{
for(int i = 1 ; i < data.size(); ++i)
{
for(int j = i ; j < data.size(); ++j)
{
if(data[i][commandposition] > data[j][commandposition])
{
temp = data[i][commandposition];
data[i][commandposition] = data[j][commandposition];
data[j][commandposition] = temp;
}
}
}
}
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
cout<<left << setw(10)<<data[i][j];
}
cout<<endl;
}
}
else if(command == "DECS")
{
for(int j = 0 ; j < data[0].size(); ++j)
{
cout<<left << setw(10)<<data[0][j];
}
cout<<endl;
for(int i = data.size() - 1 ; i > 0; --i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
cout<<left << setw(10)<<data[i][j];
}
cout<<endl;
}
}
}
void Database::Select(string name,User user,Database database)///选择语句 select * from TABLE WHERE 属性 = values
///select * from table ;///*********************************************************************************************************///
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_select.begin(); it!=user.User_select.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
char line[100];
FILE *fp;
string path;
string command;///命令
path = database.getAllpath() + name+".txt";
number=0;
ofstream ofile;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>name;
if(name == "ENDINPUT")
return;
path = database.getAllpath() + name +".txt";
}
fclose(fp);
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
//cout<<"DDDDDDD"<<endl;///1
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
//cout<<p;///2
rowdata.push_back(p);
p=strtok(NULL,d);
}
//cout<<endl;///5
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
///--------------------------------------------------------------------------------///
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
cout<<left << setw(10)<<data[i][j];
}
cout<<endl;
}
}
else if(command == "ORDER")
{
cin>>command;
int commandposition = -1;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "BY")
{
cin>>command;///属性
OrderBy(command, data);
}
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;///输入命令
string value1;
string value2;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>value1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")///select * form table where column = values ;
{
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
///cout<<left << setw(10)<<data[i][k];
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn3)
{
if(data[i][j1] == value2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
}
}
}
else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(positionOfColumn2 == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1 || data[i][j] == value2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
else
{
if(j == positionOfColumn2 || j == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
else if(j == positionOfColumn3)
{
if(data[i][j] == value2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
}
}
}
else if(command == ">")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer > integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "<")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer < integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "<>")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer != integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == ">=")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer >= integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "<=")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer <= integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
cin>>command;
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
}
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::AppointSelect(string column,string name,User user,Database database)///选择语句 select column from TABLE WHERE 属性 = values
///select column from TABLE ;--*****************************************问题
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_select.begin(); it!=user.User_select.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
char line[100];
FILE *fp;
string path;
string command;///命令
path = database.getAllpath() + name+".txt";
number=0;
ofstream ofile;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
cout<<"Table name input error: ";
cin>>name;
if(name == "ENDINPUT")
return;
path = database.getAllpath() + name +".txt";
}
fclose(fp);
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(name == data2[i][0])///存在
{
exist = 1;
}
}
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
// vector<string> rowdata;
int hang = 0;///data行
//cout<<"DDDDDDD"<<endl;///1
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
//cout<<p;///2
rowdata.push_back(p);
p=strtok(NULL,d);
}
//cout<<endl;///5
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
if(exist == 1)
{
vector<string> indexdata;
string file_index = database.getAllpath() + name + "_index.txt";
fin1.open(file_index.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
indexdata.push_back(p);
p=strtok(NULL,d);
}
}
fin1.close();
if(column == indexdata[0])
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < indexdata.size(); ++i)
{
cout<<indexdata[i]<<endl;
}
}
else if(command == "UNION" || command == "INTERSECT" || command == "MINUS")
{
string command1;
cin>>command1;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.UIMSelect(command1,Newcommand,user,database,indexdata,command);
}
}
}
else if(command == "ORDER")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "BY")
{
cin>>command;
if(command == column)
{
string temp;
int positionOfColumn = -1;///column的位置
int LineOfCommand = 0;///
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
}
}
if(positionOfColumn == -1)
return;
for(int i = 1 ; i < data.size(); ++i)
{
for(int j = i ; j < data.size(); ++j)
{
if(data[i][positionOfColumn] > data[j][positionOfColumn])
{
temp = data[i][positionOfColumn];
data[i][positionOfColumn] = data[j][positionOfColumn];
data[j][positionOfColumn] = temp;
}
}
}
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
else if(command == "DECS")
{
cout<<data[0][positionOfColumn];
cout<<endl;
for(int i = data.size() - 1 ; i > 0; --i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
}
}
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;///command的位置
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp1 = 0;
int temp2 = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp1 = 1;
}
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp1 == 0 || temp2 == 0)
return;
//cout<<positionOfColumn2<<endl;
cin>>command;///输入命令
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == command)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == ">")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) > atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "<")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) < atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == ">=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) >= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "<=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) <= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "<>")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) != atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
vector<string> values1;
cin>>command;
string command1 = command;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT sno FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.NESTSelect(command1,Newcommand,user,database,values1);
if(values1.size() == 0)
{
cout<<"SELECT ERROR"<<endl;
return;
}
else
{
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
string integer = data[i][j];
vector<string>::iterator val;
for(val = values1.begin(); val < values1.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
break;
}
}
}
}
}
}
}
}
else
{
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val < values.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
}
}
}
}
}
}
}
}
}
}
}
else
{
///--------------------------------------------------------------------------------///
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
for(int i = 0 ; i < data.size(); ++i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
else if(command == "ORDER")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "BY")
{
cin>>command;
if(command == column)
{
string temp;
int positionOfColumn = -1;///column的位置
int LineOfCommand = 0;///
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
}
}
if(positionOfColumn == -1)
return;
for(int i = 1 ; i < data.size(); ++i)
{
for(int j = i ; j < data.size(); ++j)
{
if(data[i][positionOfColumn] > data[j][positionOfColumn])
{
temp = data[i][positionOfColumn];
data[i][positionOfColumn] = data[j][positionOfColumn];
data[j][positionOfColumn] = temp;
}
}
}
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
else if(command == "DECS")
{
cout<<data[0][positionOfColumn];
cout<<endl;
for(int i = data.size() - 1 ; i > 0; --i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
}
}
}
else if(command == "UNION" || command == "INTERSECT" || command == "MINUS")
{
vector<string> onedata;
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
for(int i = 0 ; i < data.size(); ++i)
{
onedata.push_back(data[i][positionOfColumn]);
}
string command1;
cin>>command1;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.UIMSelect(command1,Newcommand,user,database,onedata,command);
}
}
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;///command的位置
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp1 = 0;
int temp2 = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp1 = 1;
}
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp1 == 0 || temp2 == 0)
return;
//cout<<positionOfColumn2<<endl;
cin>>command;///输入命令
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == command)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == ">")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) > atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == "<")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) < atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == ">=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) >= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == "<=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) <= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == "<>")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) != atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
vector<string> values1;
cin>>command;
string command1 = command;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.NESTSelect(command1,Newcommand,user,database,values1);
// for(int i = 1; i < values1.size(); ++i)
// cout<<values[i]<<endl;
cout<<values1[0]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
string integer = data[i][j];
vector<string>::iterator val;
for(val = values1.begin(); val < values1.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
}
}
}
}
}
}
}
else
{
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data[0].size(); ++i)///********************************************
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
}
}
}
}
}
}
}
}
}
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::NESTSelect(string column,string name,User user,Database database,vector<string>&values)///嵌套查询
{
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
string path = database.getAllpath() + name +".txt";
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
char line[100];
int number = 0;
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
//cout<<p;///2
rowdata.push_back(p);
p=strtok(NULL,d);
}
//cout<<endl;///5
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
string command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ")")///SELECT SNO FROM TABLE
{
int positionOfColumn;
int temp2 = 0;
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp2 == 1)
{
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn)
{
values.push_back(data[i][j]);
}
}
}
}
else
{
cout<<"ERROR"<<endl;
return;
}
}
else if(command == "WHERE")///SELECT SNO FROM TABLE WHERE CNO = VALUES
{
cin>>command;
int positionOfColumn2;///command的位置
int positionOfColumn;///column的位置
int temp1 = 0;
int temp2 = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp1 = 1;
}
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp1 == 0 || temp2 == 0)
return;
// for(int t1 = 0 ; t1 < data.size(); ++t1)
// {
// for(int t2 = 0; t2 < data[t1].size() ; ++t2)
// cout<<data[t1][t2]<<endl;
// }
cin>>command;///输入命令
if(command == "=")
{
cin>>command;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == command)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
values.push_back(data[i][j1]);
}
}
}
}
}
}
}
}
}
void Database::UIMSelect(string column,string name,User user,Database database,vector<string>onedata,string command1)///集合查询
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
vector<string> twodata;
for(it=user.User_select.begin(); it!=user.User_select.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
char line[100];
FILE *fp;
string path;
string command;///命令
path = database.getAllpath() + name+".txt";
number=0;
ofstream ofile;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
cout<<"Table name input error: ";
cin>>name;
if(name == "ENDINPUT")
return;
path = database.Allpath + name +".txt";
}
fclose(fp);
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
//cout<<"DDDDDDD"<<endl;///1
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
///--------------------------------------------------------------------------------///
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
for(int i = 0 ; i < data.size(); ++i)
{
twodata.push_back(data[i][positionOfColumn]);
}
if(onedata[0] == twodata[0])
{
vector<string>::iterator oneiterator;
vector<string>::iterator twoiterator;
if(command1 == "UNION")
{
vector<string> uniondata;
sort(onedata.begin(),onedata.end());
sort(twodata.begin(),twodata.end());
set_union(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(uniondata));//求交集
for(int i = uniondata.size() - 1; i > -1; --i)
cout<<uniondata[i]<<endl;
}
else if(command1 == "INTERSECT")
{
///两个vector求交集
vector<string> intersectdata;
sort(onedata.begin(),onedata.end());
sort(twodata.begin(),twodata.end());
set_intersection(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(intersectdata));//求交集
for(int i = intersectdata.size() - 1; i > -1; --i)
cout<<intersectdata[i]<<endl;
}
else if(command1 == "MINUS")
{
///两个vector求交集
vector<string> minusdata;
sort(onedata.begin(),onedata.end());
sort(twodata.begin(),twodata.end());
set_intersection(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(minusdata));//求交集
cout<<onedata[onedata.size() - 1]<<endl;
for(int i = 0; i < onedata.size() -1; ++i)
{
int datatemp = 0;
for(int j = 0 ; j < twodata.size() - 1 ; ++j)
{
if(onedata[i] == twodata[j])
{
datatemp = 1;
}
}
if(datatemp == 0)
{
cout<<onedata[i]<<endl;
}
}
}
}
else
{
cout<<"ERROR: Different attributes"<<endl;
}
}
}
}
void Database::Grant(string jurisdiction,User &user,Database database)///授权语句
{
string command;
string type;///对象类型
string object;///对象
string role;///用户
ofstream ofile;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "ON")
{
cin>>type;
transform(type.begin(),type.end(),type.begin(),towupper);
if(type == "TABLE")
{
cin>>object;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "TO")
{
cin>>role;
fstream _file;
string path = "F:\\" + role + ".txt";
_file.open(path.c_str(),ios::in);
if(!_file)
{
cout<<"User does not exist"<<endl;
_file.close();
return;
}
else
{
_file.close();
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
ifstream fin(path.c_str());
ofstream ofile;
char line[100];
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
fin.close();
///-----------------------对用户添加对该表的权限-------------------------------------------------//
if(jurisdiction == "DELETE")
data[2].push_back(object);
else if(jurisdiction == "DROP")
data[4].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
else if(jurisdiction == "INSERT")
data[1].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
else if(jurisdiction == "SELECT")
data[0].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
else if(jurisdiction == "UPDATE")
data[3].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
///----------清除表数据-----///
ofile.open(path.c_str(),ios::trunc);///删除表中的数据
ofile.close();
///---------------------------///
///------------将数据重新输入--------///
ofile.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < data[0].size(); ++i)
ofile<<left << setw(14)<<data[0][i];
ofile<<endl;
for(int i = 0 ; i < data[1].size(); ++i)
ofile<<left << setw(14)<<data[1][i];
ofile<<endl;
for(int i = 0 ; i < data[2].size(); ++i)
ofile<<left << setw(14)<<data[2][i];
ofile<<endl;
for(int i = 0 ; i < data[3].size(); ++i)
ofile<<left << setw(14)<<data[3][i];
ofile<<endl;
for(int i = 0 ; i < data[4].size(); ++i)
ofile<<left << setw(14)<<data[4][i];
ofile<<endl;
ofile.close();
///-----------------------------------------------------------------------------------------------///
cout<<"GRANT success"<<endl;
}
}
}
}
return;
}
void Database::Revoke(string jurisdiction,User &user,Database database)///收回权限
{
string command;
string type;///对象类型
string object;///对象
string role;///用户
ofstream ofile;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "ON")
{
cin>>type;
transform(type.begin(),type.end(),type.begin(),towupper);
if(type == "TABLE")
{
cin>>object;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "FROM")
{
cin>>role;
fstream _file;
string path = "F:\\" + role + ".txt";
_file.open(path.c_str(),ios::in);
if(!_file)
{
cout<<"User does not exist"<<endl;
_file.close();
return;
}
else
{
_file.close();
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
ifstream fin(path.c_str());
ofstream ofile;
char line[100];
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
fin.close();
///-----------------------对用户删除对该表的权限-------------------------------------------------///
vector<string>::iterator it;
if(jurisdiction == "DELETE")
for(it=data[2].begin(); it!=data[2].end();)
{
if(*it == object)
{
data[2].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "DROP")
for(it=data[4].begin(); it!= data[4].end();)
{
if(*it == object)
{
data[4].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "INSERT")
for(it=data[1].begin(); it!=data[1].end();)
{
if(object == *it)
{
data[1].erase(it); //删除元素,返回值指向已删除元素的下一个位置
cout<<*it<<endl;
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "SELECT")
for(it=data[0].begin(); it!=data[0].end();)
{
if(*it == object)
{
data[0].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "UPDATE")
for(it=data[3].begin(); it!=data[3].end();)
{
if(*it == object)
{
data[3].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
///----------清除表数据-----///
ofile.open(path.c_str(),ios::trunc);///删除表中的数据
ofile.close();
///---------------------------///
///------------将数据重新输入--------///
ofile.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < data[0].size(); ++i)
ofile<<left << setw(14)<<data[0][i];
ofile<<endl;
for(int i = 0 ; i < data[1].size(); ++i)
ofile<<left << setw(14)<<data[1][i];
ofile<<endl;
for(int i = 0 ; i < data[2].size(); ++i)
ofile<<left << setw(14)<<data[2][i];
ofile<<endl;
for(int i = 0 ; i < data[3].size(); ++i)
ofile<<left << setw(14)<<data[3][i];
ofile<<endl;
for(int i = 0 ; i < data[4].size(); ++i)
ofile<<left << setw(14)<<data[4][i];
ofile<<endl;
ofile.close();
///-----------------------------------------------------------------------------------------------///
cout<<"Revoke success"<<endl;
}
}
}
}
return;
}
void CreateView(View &view,Database database)///CREATE VIEW V_Customer
///AS SELECT First_Name, Last_Name, Country
///FROM Customer;
{
FILE *fp;
ofstream ofile;
string table_file;
string command;
table_file = database.getAllpath() + view.view_name +".txt";
while((fp = fopen(table_file.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果存在
{
///printf("The file can not be opened.\n");
cout<<"Naming error";
return;
}
fclose(fp);
ofile.open(database.getFile_view().c_str(),ios::app);///存储表名
ofile<<view.view_name<<endl;
ofile.close();
ofile.open(table_file.c_str(),ios::app);
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "SELECT")
{
while(command != ";")
{
ofile<<left <<" "<<command;
cin>>command;
if(command == "stop")
return;
}
}
ofile.close();
}
void CreateIndex(Index &index,Database database)///CREATE INDEX index_name ON table_name (column_name)
///输入表名,判断是否存在表,若不存在则return。若存在,将该表的数据存入vector<vector<string> >中,输入列名。
///判断列名是否存在,若不存在,则return。若存在则将该列按照升序存入index.file中。
{
FILE *fp;
ofstream ofile;///写入文件中
ifstream fin;///从文件中输出
int temp = 0;///当temp为0时,不存在该数据表。当temp为1时,存在该数据表。
string index_file;
string command;
string table_file;///表的路径
int column_position;///记录列的位置
index_file = database.getAllpath() + index.index_name +".txt";
while((fp = fopen(index_file.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果存在
{
///printf("The file can not be opened.\n");
cout<<"Naming error";
return;
}
fclose(fp);
fin.open(index_file.c_str());
fin.close();
ofile.open(database.getFile_index().c_str(),ios::app);///存储索引名
ofile<<index.index_name<<endl;
ofile.close();
cin>>command;///table_name
///---------------------------查询是否存在table_name------------------/
char line[100];
fin.open(database.getFile_root_name().c_str());
while(fin.getline(line,100))
{
if(strcmp(command.c_str(),line) == 0)
{
temp = 1;
break;
}
}
fin.close();
///------------------------------------------------------------------------/
table_file = database.getAllpath() + command + ".txt";
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
///--------------------------------将s之后的数据保存在data里--------------------/
vector<vector<string> > data;///数据
vector<string> rowdata;
fin.open(table_file.c_str());
int s_temp = 0;///临时变量
while(fin.getline(line,100))///从第零行第零列开始
{
if(strcmp(s.c_str(),line) == 0)
{
s_temp = 1;
}
if(s_temp == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
}
fin.close();
///--------------------------------------------------------------------------------///
if(temp == 1)
{
cin>>command;
if(command == "(")
{
cin>>command;///输入列
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0; j < data[i].size(); ++j)
{
if(command.c_str() == data[i][j])
{
ofile.open(index_file.c_str(),ios::app);///存储索引名
for(int comi = 1; comi < data.size(); ++comi)
{
ofile<<data[comi][j]<<endl;
}
}
}
}
cin>>command;
if(command == ")")
{
cout<<"INDEX Success"<<endl;
return ;
}
}
}
return;
}
int main()
{
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
string path = "F:\\user.txt";
ifstream fin(path.c_str());
char line[100];
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
fin.close();
///--------------------------------------------------------------------------------///
string name;
string password;
int Userstatus = 0;
cout<<"Input user: ";
cin>>name;
while(Userstatus == 0)
{
for(int i =1; i < data.size(); ++i)
{
if(data[i][0] == name)
{
Userstatus = i;
}
}
if(Userstatus != 0)
{
break;
}
else
{
cout<<"ERROR: No user exists"<<endl;
cout<<"Input user: ";
cin>>name;
}
}
//cout<<"STATUS: "<<data[Userstatus][2]<<endl;
string fileType = ".txt";
vector<vector<string> > data1;///存储name的权限;
path = "F:\\";
path = path + name + fileType;
fin.open(path.c_str());
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
}
fin.close();
User user(data[Userstatus][0],data[Userstatus][1],data[Userstatus][2],data1[0],data1[1],data1[2],data1[3],data1[4]);
cout<<"Enter password: ";
cin>>password;
while( password!= user.getPassword())
{
cout<<"ERROR: Password error"<<endl;
cout<<"Enter password: ";
cin>>password;
}
cout<<"Welcom to the DCF"<<endl;
cout<<"DatabaseName: "<<"database"<<endl;
cout<<"create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)"<<endl;
cout<<"create table NULL NOTNULL PRIMARY FOREIGN"<<endl;
cout<<"drop table tabname "<<endl;
cout<<"Alter table tabname add column col type"<<endl;
cout<<"drop index idxname"<<endl;
int countoftable = 0;
Table table[100];
Database database;
database.CreateDatabase();
FILE file_root;
while(1)
{
cout<<"sql> ";
string command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "CREATE")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "TABLE")
{
database.CreateTable(table[countoftable],database,user);
show_Table(table[countoftable].table_name,database);
countoftable++;
}
else if(command == "VIEW")///CREATE VIEW V_Customer AS SELECT First_Name, Last_Name, Country FROM Customer;
{
View view;
cin>>command;
view.view_name = command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AS")
{
CreateView(view,database);
}
}
else if(command == "INDEX")///CREATE INDEX index_name ON table_name (column_name)
{
Index index;
cin>>command;
index.index_name= command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "ON")
{
CreateIndex(index,database);
}
}
}
else if(command == "HELP")
{
cin>>command;
if(command == "table")
{
cin>>command;
show_Table(command,database);
}
else if(command == "database")
{
show_database(database);
}
else if(command == "view")
{
cin>>command;
show_View(command,database);
}
else if(command == "index")
{
cin>>command;
show_Index(command,database);
}
}
else if(command == "INSERT")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "INTO")
{
cin>>command;
database.Insert(command,user,database);
}
}
else if(command == "DROP")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "TABLE")
{
cin>>command;
database.DropTable(command,user,database);
}
}
else if(command == "DELETE")///DELETE FROM Person WHERE LastName = 'Wilson'
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "FROM")
{
cin>>command;
database.DeleteTable(command,user,database);
}
}
else if(command == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command;
if(command == "*")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "FROM")
{
cin>>command;
database.Select(command,user,database);
}
}
else
{
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.AppointSelect(command,Newcommand,user,database);
}
}
}
else if(command == "UPDATE")
{
cin>>command;
database.Update(command,user,database);
}
else if(command == "GRANT")
{
if(user.getUserstatus() == "Administrators")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
database.Grant(command,user,database);
}
else
{
cout<<"The user has no permissions"<<endl;
}
}
else if(command == "REVOKE")
{
if(user.getUserstatus() == "Administrators")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
database.Revoke(command,user,database);
}
else
{
cout<<"The user has no permissions"<<endl;
}
}
else if(command == "MODIFY")///修改用户
{
if(user.getUserstatus() == "user")
{
cout<<"YOU DO NOT HAVE THIS AUTHORITY"<<endl;
}
else
{
string username;
cin>>username;
int tempname = 0;
for(int i = 1 ; i < data.size(); ++i)
{
if(username == data[i][0])
{
tempname = i;
break;
}
}
if(tempname != 0)
{
string password;
cin>>username>>password;
data[tempname][0] = username;
data[tempname][1] = password;
ofstream ofile;
ofile.open("F:\\user.txt");
//ofile.clear();
for(int i = 0; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
ofile<<data[i][j]<<" ";
}
ofile<<endl;
}
ofile.close();
cout<<"Successful operation"<<endl;
}
}
}
else if(command == "ADD")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "USER")
{
string username,password,status;
vector<string> da;
cin>>username>>password>>status;
int t = 0;
for(int i = 1 ; i < data.size(); ++i)
{
if(username == data[i][0])
{
t = 1;
break;
}
}
if(t == 1)
{
cout<<"The user has already existed"<<endl;
}
else
{
if(status == "user" || status == "authority")
{
da.push_back(username);
da.push_back(password);
da.push_back(status);
data.push_back(da);
ofstream ofile;
ofile.open("F:\\user.txt");
//ofile.clear();
for(int i = 0; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
ofile<<data[i][j]<<" ";
}
ofile<<endl;
}
ofile.close();
cout<<"Successful operation"<<endl;
}
}
}
}
}
}
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cstdlib>
#include<fstream>
#include <windows.h>
using namespace std;
struct Table
{
string table_name;///表名
vector<string> column;///列名
vector<string> table_property;///属性
vector<int> length;///长度
vector<vector<string> > constraint;///约束
vector<vector<string> > data;///数据
};
struct View
{
string view_name;///视图名
vector<string> View_proproperty;///视图属性;
};
struct Index
{
string index_name;///索引名
vector<vector<string> > index_data;///索引数据
};
class User
{
private:
string user;///用户名
string password;///密码
string Userstatus; ///用户的身份
public:
vector<string> User_select;///对于用户表的select权限
vector<string> User_insert;///对于用户表的insert权限
vector<string> User_delete;///对于用户表的delete权限
vector<string> User_update;///对于用户表的update权限
vector<string> User_drop; ///对于用户表的drop权限
User()
{
user = "root";
password = "root";
Userstatus = "user";
}
User(string _user,string _password,string _Userstatus,vector<string>_select,vector<string> _insert,vector<string>_delete,vector<string> _update,
vector<string> _drop)
{
user = _user;
password = _password;
Userstatus = _Userstatus;
User_select = _select;///对于用户表的select权限
User_insert = _insert;///对于用户表的insert权限
User_delete = _delete;///对于用户表的delete权限
User_update = _update;///对于用户表的update权限
User_drop = _drop;
}
void setUser(string _user)
{
user = _user;
}
string getUser()
{
return user;
}
void setPassword(string _password)
{
password = _password;
}
string getPassword()
{
return password;
}
void setUserstatus(string _Userstatus)
{
Userstatus = _Userstatus;
}
string getUserstatus()
{
return Userstatus;
}
};
class Database
{
private:
string DatabaseName;///数据库的名字
string path;///路径
string Allpath;///全局路径
string file_root_name;///存储表的txt文件
string file_view;///存储视图文件名;
string file_index;///存储索引文件名
string file_user_name;///存储用户信息的TXT文件
public:
void CreateDatabase();///数据库
void CreateTable(Table&,Database,User);///表
void DeleteTable(string, User,Database);///删除表中的信息
void DropTable(string,User,Database);///删除表
void DeleteRow();///删除一个j记录内的信息
void Addrow();///添加一行
///void AddTable();///添加一个表
void Select(string,User,Database);///选择语句
void AppointSelect(string ,string ,User ,Database);///
void UIMSelect(string,string,User,Database,vector<string>,string);///集合查询
void NESTSelect(string,string,User,Database,vector<string>&);///嵌套查询
void Insert(string,User,Database);///添加数据
void Update(string,User,Database);///更新数据
void Grant(string,User&,Database);///授予用户权限
void Revoke(string,User&,Database);///收回权限
Database()
{
Allpath = "0";
}
string getAllpath()
{
return Allpath;
}
string getDatabaseName()
{
return DatabaseName;
}
string getFile_root_name()
{
return file_root_name;
}
string getFile_view()
{
return file_view;
}
string getFile_index()
{
return file_index;
}
};
void Database::CreateDatabase()///建数据库
{
cout<<"Please enter the database name: ";
cin>>DatabaseName;
path="F:\\"+DatabaseName; ///路径=在F盘内,加上数据库的名字
Allpath = path+"\\"; ///全局记录路径
path="md " + path; ///MD创建路径
system(path.c_str()); ///把字符串转入命令
file_root_name = Allpath + DatabaseName + ".txt";
file_view = Allpath + "VIEW" + ".txt";
file_index = Allpath + "INDEX" + ".txt";
cout<<file_root_name<<endl;
FILE *fp;
ofstream file;///定义输出文件
if((fp = fopen(file_root_name.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果打开
{
return;
}
file.open(file_root_name.c_str()); ///作为输出文件打开
file.close();
file.open(file_view.c_str());
file.close();
file.open(file_index.c_str());
file.close();
}
int AboutTable_property(string table_property)
{
if(table_property == "int")
return 0;
else if(table_property == "char")
return 0;
else if(table_property == "double")
return 0;
else if(table_property == "varchar")
return 0;
else if(table_property == "string")
return 0;
else
return 1;
}
void Database::CreateTable(Table &table,Database database,User user)
/// create table table_name
///column1 属性 长度 <约束>,
///column1 属性 长度 <约束>;
{
FILE *fp;///定义一个名叫fp文件指针
string table_name;///表名
string column;///列名
string property;///属性
string terminator;///一行结束符
//string null;///空值约束
//string notnull;
//string primary;///主键约束
//string foreign;///外键约束
int length;///属性长度
string file_name;///文件名
cin>>table_name;
table.table_name = table_name;
ofstream ofile;///定义输出文件
file_name = table_name;
file_name = database.getAllpath() + file_name;
string fileType = ".txt";
file_name = file_name + fileType;
while((fp = fopen(file_name.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果打开
{
///printf("The file can not be opened.\n");
cout<<"The data table has been established. Please re-establish it :"<<endl;
///exit(1);///结束程序的执行
cin>>table_name;
table.table_name = table_name;
file_name = table_name;
file_name = database.getAllpath() + file_name;
string fileType = ".txt";
file_name = file_name + fileType;
}
ofile.open(database.getFile_root_name().c_str(),ios::app);///存储表名
ofile<<table_name<<endl;
ofile.close();
ofile.open(file_name.c_str(),ios::app);///作为输出文件打开
cin>>column;
while(column!= ";")
{
//cin>>property>>length>>null>>notnull>>primary>>foreign;
cin>>property>>length>>terminator;
while(AboutTable_property(property))
{
cout<<"property error!"<<endl;
//cin>>property>>length>>null>>notnull>>primary>>foreign;
cin>>property>>length>>terminator;
}
table.column.push_back(column);
table.table_property.push_back(property);
table.length.push_back(length);
int number = 0;///设计约束的数量即约束的列数
vector<string> constraint1;///临时记录一行的约束
while(terminator != "," && terminator != ";")
{
transform(terminator.begin(),terminator.end(),terminator.begin(),towupper);
if(terminator == "NULL" || terminator == "NOTNULL"||terminator == "PRIMARY" || terminator =="FOREIGN")
{
constraint1.push_back(terminator);
}
//table.constraint.push_back(constraintofhang);
cin>>terminator;
number++;
}
//cout<<"NUMBER: "<<number<<endl;
if(number == 0)///若没有输入约束项
{
if(terminator == ",")
{
constraint1.push_back("NOTNULL");
}
else if(terminator == ";")
{
constraint1.push_back("NOTNULL");
}
}
table.constraint.push_back(constraint1);
constraint1.clear();
if(terminator == ";")
{
break;
}
cin>>column;
cout<<"BBB"<<endl;
}
ofile<<table.table_name<<endl;
ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - -"<<endl;
ofile<<left << setw(10)<<"Column"<<left << setw(10)<<"Type"<<left << setw(10)<<"Length"<<endl;
//left << setw(10)<<"Null"<<left << setw(10)<<"Not null"<<
// left << setw(10)<<"Primary"<<left << setw(10)<<"Foreign"<<endl;
ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - -"<<endl;
for(int i = 0; i < table.column.size(); ++i)
{
ofile<<left << setw(10)<<table.column[i]<<left << setw(10) <<table.table_property[i]<< left << setw(10) <<table.length[i];
for(int j = 0 ; j < table.constraint[i].size() ; ++j)
{
ofile<<left << setw(10)<<table.constraint[i][j];
}
ofile<<endl;
//left << setw(10) <<table.null[i]<<left << setw(10) <<table.notnull[i]<<left << setw(10) <<table.primary[i]<<left << setw(10) <<table.foreign[i]<<endl;
}
ofile<<"- - - - - - - - - - - - - - - - - - - - - - - - ---"<<endl;
for(int i = 0; i < table.column.size(); ++i)
{
ofile<<left << setw(10)<<table.column[i];
}
ofile<<endl;
ofile.close();
///-----------------------对用户设置对该表的权限-------------------------------------------------///
user.User_delete.push_back(table_name);
user.User_drop.push_back(table_name);
user.User_insert.push_back(table_name);
user.User_select.push_back(table_name);
user.User_update.push_back(table_name);
string path = "F:\\";
path = path + user.getUser() + ".txt";
///----------清除表数据-----///
ofile.open(path.c_str(),ios::trunc);///删除表中的数据
ofile.close();
///---------------------------------------------///
///------------将数据重新输入-----------------///
ofile.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < user.User_select.size(); ++i)
ofile<<left << setw(5)<<user.User_select[i];
ofile<<endl;
for(int i = 0 ; i < user.User_insert.size(); ++i)
ofile<<left << setw(5)<<user.User_insert[i];
ofile<<endl;
for(int i = 0 ; i < user.User_delete.size(); ++i)
ofile<<left << setw(5)<<user.User_delete[i];
ofile<<endl;
for(int i = 0 ; i < user.User_update.size(); ++i)
ofile<<left << setw(5)<<user.User_update[i];
ofile<<endl;
for(int i = 0 ; i < user.User_drop.size(); ++i)
ofile<<left << setw(5)<<user.User_drop[i];
ofile<<endl;
ofile.close();
///-----------------------------------------------------------------------------------------------///
return;
}
void show_Table(string name,Database database)
{
int i;
char line[100];
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
i=0;
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
fin.close();
}
void show_View(string name,Database database)///CREATE VIEW V_Customer
///AS
{
int i;
char line[100];
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
i=0;
cout<<"CREATE VIEW " + name + " AS ";
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
fin.close();
}
void show_Index(string name,Database database)///CREATE INDEX V_Customer
///AS
{
int i;
char line[100];
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
i=0;
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
fin.close();
}
void show_database(Database database)
{
int i;
char line[100];
string path_table;
string path_view;
string path_index;
path_table = database.getAllpath() + database.getDatabaseName()+".txt";
path_view = database.getFile_view();
path_index = database.getFile_index();
ifstream fin(path_table.c_str());
i=0;
cout<<"Table"<<endl;
cout<<"-------------"<<endl;
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
cout<<"-------------"<<endl<<endl;
fin.close();
cout<<"VIEW"<<endl;
cout<<"-------------"<<endl;
fin.open(path_view.c_str());
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
cout<<"-------------"<<endl;
fin.close();
cout<<"INDEX"<<endl;
cout<<"-------------"<<endl;
fin.open(path_index.c_str());
while(fin.getline(line,100))
{
cout<<line<<endl;
i++;
}
cout<<"-------------"<<endl;
fin.close();
}
void Database::DropTable(string table_name,User user,Database database)///DROP 类型 名称
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_drop.begin(); it!=user.User_drop.end();)
{
if(*it == table_name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
FILE *fp;
ofstream file;
string table_file;
string database_file;///存放表名的文件
/// string data;///数据
table_file = database.Allpath + table_name +".txt";
while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>table_name;
table_file = database.Allpath + table_name +".txt";
}
fclose(fp);
DeleteFile(table_file.c_str());
cout<<"DROP SUCCESS"<<endl;
///-------------------------------------
///将数据存入data中---------------///
database_file = database.Allpath + database.getDatabaseName() +".txt";
char line[100];
ifstream fin(database_file.c_str());
vector<string> data;///数据
int number = 0;
while(fin.getline(line,100))
{
if(strcmp(table_name.c_str(),line) != 0)
{
data.push_back(line);
++number;
}
}
fin.close();
///----------------------///
file.open(database_file.c_str(),ios::trunc);///删除表中的数据
file.clear();
file.close();
///-------------------------------/
///----------将数据重新写入文件中----------------------///
file.open(database_file.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < number; ++i)
{
file<<left << setw(10)<<data[i]<<endl;
}
file.close();
///-------------------------------------///
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
vector<vector<string> >::iterator it;///迭代器
it = data2.begin();
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(table_name == data2[i][0])///存在
{
///----------------------删除table_name_index文件---------------------------------///
vector<vector<string> > data3;///存索引数据
string file_index = database.getAllpath() + table_name + "_index"+".txt";
DeleteFile(file_index.c_str());
///----------------------------------------------------------------------------------///
///进入存储索引名称的文件中、删除对应的索引------------------------------------------//
// fin.open(t_file.c_str());
// fin.clear();
// fin.close();
///在data2中删除对应索引名
data2.erase(it);
exist = 1;
}
it++;
}
if(exist == 1)
{
///进入存储索引名称的文件中、删除对应的索引------------------------------------------//
ofstream fin_index(t_file.c_str());
fin_index.close();
///------------将数据重新输入--------///
file.open(t_file.c_str(),ios::app); ///作为输出文件打开
for(int j = 0 ; j < data2.size(); ++j)
{
file<<data2[j][0]<<"_"<<data2[j][1];
}
file<<endl;
file.close();
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::DeleteTable(string table_name,User user,Database database) ///DELETE FROM 表名称 WHERE 列名称 = 值
///DELETE FROM 表名称 ;
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_delete.begin(); it!=user.User_delete.end();)
{
if(*it == table_name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
FILE *fp;
ofstream file;
string table_file;
string command;///命令
string column;
table_file = database.Allpath + table_name +".txt";
while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>table_name;
table_file = database.Allpath + table_name +".txt";
}
fclose(fp);
///--------------------------------将表数据存入data1中---------------///
char line[100];
vector<string> data;///表格式无数据
vector<string> rowdata;///只存数据(行数据)
vector<vector<string> > data1;///存数据
string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
int number = 0;
int temporary1 = 0;
int temporary2 = 0;
ifstream fin(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 0 || temporary2 == -1)
{
data.push_back(line);
++number;
}
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
temporary2++;
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
temporary2 = -1;
}
}
fin.close();
///------------------------------------------------------
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
///----------------------------------//
///----------清除表数据-----///
file.open(table_file.c_str(),ios::trunc);///删除表中的数据
file.close();
///---------------------------///
///------------将数据重新输入--------///
file.open(table_file.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < number; ++i)
{
file<<left << setw(10)<<data[i]<<endl;
}
file.close();
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == command)
{
positionOfColumn2 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;///输入命令
string value1;
string value2;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>value1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")///select * form table where column = values ;
{
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
for(int kk = 1 ; kk < data1.size(); ++kk)
{
for(int mm = 0; mm < data1[kk].size(); ++mm)
{
cout<<left << setw(10)<<data1[kk][mm];
}
cout<<endl;
}
}
else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1)
{
///cout<<left << setw(10)<<data[i][k];
for(int j1 = 0 ; j1 < data1[i].size(); ++j1)
{
if(j1 == positionOfColumn3)
{
if(data1[i][j1] == value2)
{
data1.erase(dataiterator);
}
}
}
}
}
}
dataiterator++;
}
}
}
else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(positionOfColumn2 == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1 || data1[i][j] == value2)
{
data1.erase(dataiterator);
}
}
}
else
{
if(j == positionOfColumn2 || j == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data1[i][j] == value1)
{
data1.erase(dataiterator);
}
}
else if(j == positionOfColumn3)
{
if(data1[i][j] == value2)
{
data1.erase(dataiterator);
}
}
}
}
}
dataiterator++;
}
}
}
}
else if(command == ">")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer > integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "<")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer < integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "<>")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer != integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == ">=")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer >= integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "<=")
{
cin>>command;
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer <= integer1)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
data1.erase(dataiterator);
}
}
}
dataiterator++;
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
cin>>command;
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data1[0].size(); ++i)
cout<<left << setw(10)<<data1[0][i];
cout<<endl;
vector<vector<string> >::iterator dataiterator;
dataiterator = data1.begin();
dataiterator++;
for(int i = 1; i < data1.size(); ++i)
{
for(int j = 0 ; j < data1[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data1[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
data1.erase(dataiterator);
}
}
}
}
dataiterator++;
}
}
}
file.open(table_file.c_str(),ios::trunc);///删除表中的数据
file.close();
///---------------------------///
///------------将数据重新输入--------///
file.open(table_file.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < number; ++i)
{
file<<left << setw(10)<<data[i]<<endl;
}
for(int i = 1 ; i < data1.size(); ++i)
{
for(int j =0; j < data1[i].size(); ++j)
{
file<<left<<setw(10)<<data1[i][j];
}
file<<endl;
}
file.close();
}
vector<vector<string> > data11;///存数据
temporary1 = 0;
fin.open(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data11.push_back(rowdata);
rowdata.clear();
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
}
}
fin.close();
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
//vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(table_name == data2[i][0])///存在
{
exist = 1;
}
}
if(exist == 1)
{
///**************读取索引文件内容*********************----//
string file_index = database.getAllpath() + table_name + "_index.txt";
fin.open(file_index.c_str());
string column;
while(fin.getline(line,100))///*****************************************************************************问题
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
column = p;
break;
}
break;
}
fin.close();
///***********************读取结束----------------------------------------------------------------------///
///进入索引的文件中、更新索引------------------------------------------//
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data11[0].size(); ++j)
{
if(data11[0][j] == column)///**************问题
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
ofstream fin_index(file_index.c_str());///删除文件中的内容
fin_index<<left<<setw(10)<<column<<endl;
for(int i = 1 ; i < data11.size(); ++i)
{
fin_index<<data11[i][positionOfColumn];
fin_index<<endl;
}
fin_index.close();
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::Insert(string table_name,User user,Database database)///INSERT INTO 表名称 VALUES (值1, 值2,....)
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_insert.begin(); it!=user.User_insert.end();)
{
if(*it == table_name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
FILE *fp;
string table_file;
string data;///数据
table_file = database.Allpath + table_name +".txt";
ofstream ofile;///向文件中写入数据
while((fp = fopen(table_file.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>table_name;
if(table_name == "ENDINPUT")
return;
table_file = database.Allpath + table_name +".txt";
}
fclose(fp);
///--------------------求属性个数------------------------------//
char line[100];
int number = 0;///属性个数
int numberofline = 0;
ifstream fin(table_file.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))
{
numberofline++;
if(numberofline > 4)
{
number++;
}
if(strcmp(s.c_str(),line) == 0)
{
break;
}
}
number = number - 1;
//cout<<number<<endl;
fin.close();
///---------------------------------------------------///
string datas[number];///输入的内容
cin>>data;
int i = 0;
transform(data.begin(),data.end(),data.begin(),towupper);
if(data == "VALUES")
{
ofile.open(table_file.c_str(),ios::app);
//cout<<table_file.c_str()<<endl;
while(i <= number - 1)
{
cin>>datas[i];
ofile<<left<<setw(10)<<datas[i];
++i;
}
ofile<<endl;
cout<<"Add Success"<<endl;
}
fclose(fp);
///--------------------------------将数据存入data1中---------------///
vector<string> rowdata;///只存数据(行数据)
vector<vector<string> > data1;///存数据
string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
int temporary1 = 0;
fin.open(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
}
}
fin.close();
///------------------------------------------------------
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
//vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(table_name == data2[i][0])///存在
{
exist = 1;
}
}
if(exist == 1)
{
///**************读取索引文件内容*********************----//
string file_index = database.getAllpath() + table_name + "_index.txt";
fin.open(file_index.c_str());
string column;
while(fin.getline(line,100))///*****************************************************************************问题
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
column = p;
break;
}
break;
}
fin.close();
///***********************读取结束----------------------------------------------------------------------///
///进入索引的文件中、更新索引------------------------------------------//
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
ofstream fin_index(file_index.c_str());///删除文件中的内容
fin_index.close();
///------------将数据重新输入--------///
fin_index.open(file_index.c_str(),ios::app); ///作为输出文件打开
fin_index<<left<<setw(10)<<column<<endl;
for(int i = 1 ; i < data1.size(); ++i)
{
fin_index<<data1[i][positionOfColumn];
fin_index<<endl;
}
fin_index.close();
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::Update(string name,User user,Database database)///UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
///UPDATE 表名称 SET 列名称 = 新值 ;
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_update.begin(); it!=user.User_update.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
string command;
string column1;
string value1;///操作数1
string column2;
string value2;///操作数2
char line[100];
FILE *fp;
string path;
path = database.getAllpath() + name+".txt";
ifstream fin(path.c_str());
number=0;
ofstream file;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>name;
if(name == "ENDINPUT")
return;
path = database.Allpath + name +".txt";
}
fclose(fp);
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "SET")
{
cin>>column1;
int number2 = -1;///记录行数;
int number1 = -1;///记录column1的所在位置;
int number = 0;///记录是否存在column1;
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
///----------------判断是否存在column1---------------//
while(fin.getline(line,100))
{
number2++;
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
vector<string> str;
while(p)
{
///printf("%s\n",p);
str.push_back(p);
p=strtok(NULL,d);
}
for(int i = 0 ; i < str.size(); ++i)
{
if(str[i] == column1)
{
number1 = i;
}
}
if(number1 == -1)
{
return;
}
break;
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
///------------------------------------------------------------------------//
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int temp = 0;
int hang = 0;///data行
ifstream fin(path.c_str());
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
if(temp == 0)
{
rowdata.push_back(line);
data.push_back(rowdata);
rowdata.clear();
}
else
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
temp = 1;
}
}
fin.close();
// for(int i = 0 ; i < data.size(); ++i)
// {
// for(int j = 0 ; j < data[i].size(); ++j)
// cout<<data[i][j];
// cout<<endl;
// }
///-----------------------------------------------------------------------------//
cin>>command;
if(command == "=")
{
cin>>value1;///输入操作数
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == number1)
{
data[i][j] = value1;
}
}
}
}
}
else if(command == "WHERE")
{
cin>>column2;
int positionOfColumn2;
temp = 0;
///----------------判断是否存在column2---------------//
for(int i = 0 ; i < data.size(); ++i)
{
if(i == number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(data[i][j] == column2)
{
positionOfColumn2 = j;
temp = 1;
}
}
}
}
if(temp == 0)
return;
//cout<<positionOfColumn2<<endl;
cin>>command;///输入命令
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>value2;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 1; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
///cout<<left << setw(10)<<data[i][k];
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn3)
{
if(data[i][j1] == value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
}
}
}
else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
// for(int i = 0 ; i < data[0].size(); ++i)
// cout<<left << setw(10)<<data[0][i];
// cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(positionOfColumn2 == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1 || data[i][j] == value2)
{
data[i][number1] = value1;
}
}
}
else
{
if(j == positionOfColumn2 || j == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
data[i][number1] = value1;
}
}
else if(j == positionOfColumn3)
{
if(data[i][j] == value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
}
}
}
else if(command == ">")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] > value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "<")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] < value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "<>")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] != value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == ">=")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] >= value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "<=")
{
cin>>value2;
for(int i = 0 ; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] <= value2)
{
data[i][number1] = value1;
}
}
}
}
}
}
else if(command == "BETWEEN")
{
int number3;
int number4;
cin>>number3;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number4;
int maxn = number3;
if(maxn > number4)
{
maxn = number4;
number4 = number3;
number3 = maxn;
}
for(int i = 0; i < data.size(); ++i)
{
if(i > number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number3 && integer < number4)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
cin>>command;
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 1; i < data.size(); ++i)
{
if(i >number2)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
data[i][number1] = value1;
}
}
}
}
}
}
}
}
}
}
///----------清除表数据-----///
file.open(path.c_str(),ios::trunc);///删除表中的数据
file.close();
///------------将数据重新输入--------///
file.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
file<<left << setw(10)<<data[i][j];
file<<endl;
}
file.close();
}
///--------------------------------将数据存入data1中---------------///
vector<string> rowdata;///只存数据(行数据)
vector<vector<string> > data1;///存数据
string ss = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
string table_file = database.Allpath + name +".txt";
int temporary1 = 0;
fin.open(table_file.c_str());
while(fin.getline(line,100))///*****************************************************************************问题
{
if(temporary1 == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
}
if(strcmp(ss.c_str(),line) == 0)
{
temporary1 = 1;
}
}
fin.close();
///------------------------------------------------------
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
//vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
// cout<<"GETFILE_VIEW: "<<t_file<<endl;
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(name == data2[i][0])///存在
{
exist = 1;
}
}
// cout<<"EXIT: "<<exit<<endl;
if(exist == 1)
{
///**************读取索引文件内容*********************----//
string file_index = database.getAllpath() + name + "_index.txt";
fin.open(file_index.c_str());
string column;
while(fin.getline(line,100))///*****************************************************************************问题
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
column = p;
break;
}
break;
}
fin.close();
///***********************读取结束----------------------------------------------------------------------///
///进入索引的文件中、更新索引------------------------------------------//
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data1[0].size(); ++j)
{
if(data1[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
// cout<<"TEMP: "<<temp<<endl;
if(temp == 0)
return;
ofstream fin_index(file_index.c_str());///删除文件中的内容
fin_index.close();
///------------将数据重新输入--------///
fin_index.open(file_index.c_str(),ios::app); ///作为输出文件打开
fin_index<<left<<setw(10)<<column<<endl;
for(int i = 1 ; i < data1.size(); ++i)
{
fin_index<<data1[i][positionOfColumn];
fin_index<<endl;
}
fin_index.close();
}
cout<<"UPDATE SUCCESS!"<<endl;
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void OrderBy(string command, vector<vector<string> >&data)
{
int commandposition = -1;
for(int j = 0 ; j < data[0].size(); ++j)
{
if(command == data[0][j])
{
commandposition = j;
}
}
string temp;
if(commandposition == -1)
{
cout<<"Input error"<<endl;
}
else
{
for(int i = 1 ; i < data.size(); ++i)
{
for(int j = i ; j < data.size(); ++j)
{
if(data[i][commandposition] > data[j][commandposition])
{
temp = data[i][commandposition];
data[i][commandposition] = data[j][commandposition];
data[j][commandposition] = temp;
}
}
}
}
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
cout<<left << setw(10)<<data[i][j];
}
cout<<endl;
}
}
else if(command == "DECS")
{
for(int j = 0 ; j < data[0].size(); ++j)
{
cout<<left << setw(10)<<data[0][j];
}
cout<<endl;
for(int i = data.size() - 1 ; i > 0; --i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
cout<<left << setw(10)<<data[i][j];
}
cout<<endl;
}
}
}
void Database::Select(string name,User user,Database database)///选择语句 select * from TABLE WHERE 属性 = values
///select * from table ;///*********************************************************************************************************///
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_select.begin(); it!=user.User_select.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
char line[100];
FILE *fp;
string path;
string command;///命令
path = database.getAllpath() + name+".txt";
number=0;
ofstream ofile;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
///printf("The file can not be opened.\n");
cout<<"Table name input error: ";
///exit(1);///结束程序的执行
cin>>name;
if(name == "ENDINPUT")
return;
path = database.getAllpath() + name +".txt";
}
fclose(fp);
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
//cout<<"DDDDDDD"<<endl;///1
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
//cout<<p;///2
rowdata.push_back(p);
p=strtok(NULL,d);
}
//cout<<endl;///5
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
///--------------------------------------------------------------------------------///
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
cout<<left << setw(10)<<data[i][j];
}
cout<<endl;
}
}
else if(command == "ORDER")
{
cin>>command;
int commandposition = -1;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "BY")
{
cin>>command;///属性
OrderBy(command, data);
}
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;///输入命令
string value1;
string value2;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>value1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")///select * form table where column = values ;
{
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "AND")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
///cout<<left << setw(10)<<data[i][k];
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn3)
{
if(data[i][j1] == value2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
}
}
}
else if(command == "OR")///SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter
{
cin>>command;
int positionOfColumn3;///command的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn3 = j;
temp = 1;
break;
}
}
if(temp == 0)
return;
cin>>command;
if(command == "=")
{
cin>>value2;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(positionOfColumn2 == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1 || data[i][j] == value2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
else
{
if(j == positionOfColumn2 || j == positionOfColumn3)
{
if(j == positionOfColumn2)
{
if(data[i][j] == value1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
else if(j == positionOfColumn3)
{
if(data[i][j] == value2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
}
}
}
else if(command == ">")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer > integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "<")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer < integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "<>")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer != integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == ">=")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer >= integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "<=")
{
cin>>command;
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
int integer1 = atoi(command.c_str());
if(integer <= integer1)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
cin>>command;
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
for(int k = 0; k < data[i].size(); ++k)
{
cout<<left << setw(10)<<data[i][k];
}
cout<<endl;
}
}
}
}
}
}
}
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::AppointSelect(string column,string name,User user,Database database)///选择语句 select column from TABLE WHERE 属性 = values
///select column from TABLE ;--*****************************************问题
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
for(it=user.User_select.begin(); it!=user.User_select.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
char line[100];
FILE *fp;
string path;
string command;///命令
path = database.getAllpath() + name+".txt";
number=0;
ofstream ofile;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
cout<<"Table name input error: ";
cin>>name;
if(name == "ENDINPUT")
return;
path = database.getAllpath() + name +".txt";
}
fclose(fp);
///----------------------读出INDEX存入data2中---------------------------------///
vector<vector<string> > data2;///存索引数据
vector<string> rowdata;
string t_file = "F:\\database\\INDEX.txt";
ifstream fin1(t_file.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = "_";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data2.push_back(rowdata);
rowdata.clear();
}
fin1.close();
///-------------------------------------------------------///
///---------------------判断表是否拥有索引----------------------------------///
int exist = 0;
for(int i = 0 ; i < data2.size(); ++i)
{
if(name == data2[i][0])///存在
{
exist = 1;
}
}
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
// vector<string> rowdata;
int hang = 0;///data行
//cout<<"DDDDDDD"<<endl;///1
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
//cout<<p;///2
rowdata.push_back(p);
p=strtok(NULL,d);
}
//cout<<endl;///5
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
if(exist == 1)
{
vector<string> indexdata;
string file_index = database.getAllpath() + name + "_index.txt";
fin1.open(file_index.c_str());
while(fin1.getline(line,100))
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
indexdata.push_back(p);
p=strtok(NULL,d);
}
}
fin1.close();
if(column == indexdata[0])
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < indexdata.size(); ++i)
{
cout<<indexdata[i]<<endl;
}
}
else if(command == "UNION" || command == "INTERSECT" || command == "MINUS")
{
string command1;
cin>>command1;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.UIMSelect(command1,Newcommand,user,database,indexdata,command);
}
}
}
else if(command == "ORDER")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "BY")
{
cin>>command;
if(command == column)
{
string temp;
int positionOfColumn = -1;///column的位置
int LineOfCommand = 0;///
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
}
}
if(positionOfColumn == -1)
return;
for(int i = 1 ; i < data.size(); ++i)
{
for(int j = i ; j < data.size(); ++j)
{
if(data[i][positionOfColumn] > data[j][positionOfColumn])
{
temp = data[i][positionOfColumn];
data[i][positionOfColumn] = data[j][positionOfColumn];
data[j][positionOfColumn] = temp;
}
}
}
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
else if(command == "DECS")
{
cout<<data[0][positionOfColumn];
cout<<endl;
for(int i = data.size() - 1 ; i > 0; --i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
}
}
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;///command的位置
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp1 = 0;
int temp2 = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp1 = 1;
}
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp1 == 0 || temp2 == 0)
return;
//cout<<positionOfColumn2<<endl;
cin>>command;///输入命令
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == command)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == ">")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) > atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "<")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) < atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == ">=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) >= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "<=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) <= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "<>")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) != atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
vector<string> values1;
cin>>command;
string command1 = command;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT sno FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.NESTSelect(command1,Newcommand,user,database,values1);
if(values1.size() == 0)
{
cout<<"SELECT ERROR"<<endl;
return;
}
else
{
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
string integer = data[i][j];
vector<string>::iterator val;
for(val = values1.begin(); val < values1.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
break;
}
}
}
}
}
}
}
}
else
{
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val < values.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
}
}
}
}
}
}
}
}
}
}
}
else
{
///--------------------------------------------------------------------------------///
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
for(int i = 0 ; i < data.size(); ++i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
else if(command == "ORDER")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "BY")
{
cin>>command;
if(command == column)
{
string temp;
int positionOfColumn = -1;///column的位置
int LineOfCommand = 0;///
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
}
}
if(positionOfColumn == -1)
return;
for(int i = 1 ; i < data.size(); ++i)
{
for(int j = i ; j < data.size(); ++j)
{
if(data[i][positionOfColumn] > data[j][positionOfColumn])
{
temp = data[i][positionOfColumn];
data[i][positionOfColumn] = data[j][positionOfColumn];
data[j][positionOfColumn] = temp;
}
}
}
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
for(int i = 0 ; i < data.size(); ++i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
else if(command == "DECS")
{
cout<<data[0][positionOfColumn];
cout<<endl;
for(int i = data.size() - 1 ; i > 0; --i)
{
cout<<data[i][positionOfColumn];
cout<<endl;
}
}
}
}
}
else if(command == "UNION" || command == "INTERSECT" || command == "MINUS")
{
vector<string> onedata;
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
for(int i = 0 ; i < data.size(); ++i)
{
onedata.push_back(data[i][positionOfColumn]);
}
string command1;
cin>>command1;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.UIMSelect(command1,Newcommand,user,database,onedata,command);
}
}
}
else if(command == "WHERE")
{
cin>>command;
int positionOfColumn2;///command的位置
int positionOfColumn;///column的位置
int LineOfCommand = 0;///
int temp1 = 0;
int temp2 = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp1 = 1;
}
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp1 == 0 || temp2 == 0)
return;
//cout<<positionOfColumn2<<endl;
cin>>command;///输入命令
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == command)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == ">")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) > atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == "<")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) < atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == ">=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) >= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == "<=")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) <= atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
if(command == "<>")
{
cin>>command;
cout<<data[0][positionOfColumn]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
//int integer=atoi(data[i][j].c_str());///string转为int
if(atoi(data[i][j].c_str()) != atoi(command.c_str()))
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
else if(command == "BETWEEN")
{
int number1;
int number2;
cin>>number1;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AND")
{
cin>>number2;
int maxn = number1;
if(maxn > number2)
{
maxn = number2;
number2 = number1;
number1 = maxn;
}
for(int i = 0 ; i < data[0].size(); ++i)
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
if(integer > number1 && integer < number2)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
cout<<data[i][j1]<<endl;
}
}
}
}
}
}
}
}
else if(command == "IN")
{
cin>>command;
if(command == "(")
{
vector<int> values;
vector<string> values1;
cin>>command;
string command1 = command;
transform(command1.begin(),command1.end(),command1.begin(),towupper);
if(command1 == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command1;
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.NESTSelect(command1,Newcommand,user,database,values1);
// for(int i = 1; i < values1.size(); ++i)
// cout<<values[i]<<endl;
cout<<values1[0]<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
string integer = data[i][j];
vector<string>::iterator val;
for(val = values1.begin(); val < values1.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
}
}
}
}
}
}
}
else
{
while(command != ")")
{
int commandint = atoi(command.c_str());
values.push_back(commandint);
cin>>command;
}
for(int i = 0 ; i < data[0].size(); ++i)///********************************************
cout<<left << setw(10)<<data[0][i];
cout<<endl;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
int integer=atoi(data[i][j].c_str());///string转为int
vector<int>::iterator val;
for(val = values.begin(); val <= values.end(); ++val)
{
if(integer == *val)
{
cout<<data[i][positionOfColumn]<<endl;
}
}
}
}
}
}
}
}
}
}
}
else
{
cout<<"The user does not have the authority"<<endl;
return;
}
}
void Database::NESTSelect(string column,string name,User user,Database database,vector<string>&values)///嵌套查询
{
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
string path = database.getAllpath() + name +".txt";
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
char line[100];
int number = 0;
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
//cout<<p;///2
rowdata.push_back(p);
p=strtok(NULL,d);
}
//cout<<endl;///5
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
string command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ")")///SELECT SNO FROM TABLE
{
int positionOfColumn;
int temp2 = 0;
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp2 == 1)
{
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn)
{
values.push_back(data[i][j]);
}
}
}
}
else
{
cout<<"ERROR"<<endl;
return;
}
}
else if(command == "WHERE")///SELECT SNO FROM TABLE WHERE CNO = VALUES
{
cin>>command;
int positionOfColumn2;///command的位置
int positionOfColumn;///column的位置
int temp1 = 0;
int temp2 = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == command)
{
positionOfColumn2 = j;
temp1 = 1;
}
if(data[0][j] == column)
{
positionOfColumn = j;
temp2 = 1;
}
}
if(temp1 == 0 || temp2 == 0)
return;
// for(int t1 = 0 ; t1 < data.size(); ++t1)
// {
// for(int t2 = 0; t2 < data[t1].size() ; ++t2)
// cout<<data[t1][t2]<<endl;
// }
cin>>command;///输入命令
if(command == "=")
{
cin>>command;
for(int i = 1; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
if(j == positionOfColumn2)
{
if(data[i][j] == command)
{
for(int j1 = 0 ; j1 < data[i].size(); ++j1)
{
if(j1 == positionOfColumn)
{
values.push_back(data[i][j1]);
}
}
}
}
}
}
}
}
}
void Database::UIMSelect(string column,string name,User user,Database database,vector<string>onedata,string command1)///集合查询
{
int key = 0;///是否拥有权限
vector<string>::iterator it;
vector<string> twodata;
for(it=user.User_select.begin(); it!=user.User_select.end();)
{
if(*it == name)
{
key = 1;
break;
}
else
++it; //指向下一个位置
}
if(key == 1)
{
int number;
char line[100];
FILE *fp;
string path;
string command;///命令
path = database.getAllpath() + name+".txt";
number=0;
ofstream ofile;///向文件中写入数据
while((fp = fopen(path.c_str(),"r+")) == NULL) ///判断按读方式打 file_name 文件是否失败,如果不存在
{
cout<<"Table name input error: ";
cin>>name;
if(name == "ENDINPUT")
return;
path = database.Allpath + name +".txt";
}
fclose(fp);
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
//cout<<"DDDDDDD"<<endl;///1
ifstream fin(path.c_str());
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
while(fin.getline(line,100))///从s开始读出存入data中。
{
if(number == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
if(strcmp(s.c_str(),line) == 0)
{
number = 1;
}
}
fin.close();
///--------------------------------------------------------------------------------///
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == ";")
{
int positionOfColumn;///column的位置
int temp = 0;
///----------------判断是否存在command---------------//
for(int j = 0 ; j < data[0].size(); ++j)
{
if(data[0][j] == column)
{
positionOfColumn = j;
temp = 1;
}
}
if(temp == 0)
return;
for(int i = 0 ; i < data.size(); ++i)
{
twodata.push_back(data[i][positionOfColumn]);
}
if(onedata[0] == twodata[0])
{
vector<string>::iterator oneiterator;
vector<string>::iterator twoiterator;
if(command1 == "UNION")
{
vector<string> uniondata;
sort(onedata.begin(),onedata.end());
sort(twodata.begin(),twodata.end());
set_union(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(uniondata));//求交集
for(int i = uniondata.size() - 1; i > -1; --i)
cout<<uniondata[i]<<endl;
}
else if(command1 == "INTERSECT")
{
///两个vector求交集
vector<string> intersectdata;
sort(onedata.begin(),onedata.end());
sort(twodata.begin(),twodata.end());
set_intersection(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(intersectdata));//求交集
for(int i = intersectdata.size() - 1; i > -1; --i)
cout<<intersectdata[i]<<endl;
}
else if(command1 == "MINUS")
{
///两个vector求交集
vector<string> minusdata;
sort(onedata.begin(),onedata.end());
sort(twodata.begin(),twodata.end());
set_intersection(onedata.begin(),onedata.end(),twodata.begin(),twodata.end(),back_inserter(minusdata));//求交集
cout<<onedata[onedata.size() - 1]<<endl;
for(int i = 0; i < onedata.size() -1; ++i)
{
int datatemp = 0;
for(int j = 0 ; j < twodata.size() - 1 ; ++j)
{
if(onedata[i] == twodata[j])
{
datatemp = 1;
}
}
if(datatemp == 0)
{
cout<<onedata[i]<<endl;
}
}
}
}
else
{
cout<<"ERROR: Different attributes"<<endl;
}
}
}
}
void Database::Grant(string jurisdiction,User &user,Database database)///授权语句
{
string command;
string type;///对象类型
string object;///对象
string role;///用户
ofstream ofile;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "ON")
{
cin>>type;
transform(type.begin(),type.end(),type.begin(),towupper);
if(type == "TABLE")
{
cin>>object;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "TO")
{
cin>>role;
fstream _file;
string path = "F:\\" + role + ".txt";
_file.open(path.c_str(),ios::in);
if(!_file)
{
cout<<"User does not exist"<<endl;
_file.close();
return;
}
else
{
_file.close();
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
ifstream fin(path.c_str());
ofstream ofile;
char line[100];
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
fin.close();
///-----------------------对用户添加对该表的权限-------------------------------------------------//
if(jurisdiction == "DELETE")
data[2].push_back(object);
else if(jurisdiction == "DROP")
data[4].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
else if(jurisdiction == "INSERT")
data[1].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
else if(jurisdiction == "SELECT")
data[0].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
else if(jurisdiction == "UPDATE")
data[3].push_back(object); //删除元素,返回值指向已删除元素的下一个位置
///----------清除表数据-----///
ofile.open(path.c_str(),ios::trunc);///删除表中的数据
ofile.close();
///---------------------------///
///------------将数据重新输入--------///
ofile.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < data[0].size(); ++i)
ofile<<left << setw(14)<<data[0][i];
ofile<<endl;
for(int i = 0 ; i < data[1].size(); ++i)
ofile<<left << setw(14)<<data[1][i];
ofile<<endl;
for(int i = 0 ; i < data[2].size(); ++i)
ofile<<left << setw(14)<<data[2][i];
ofile<<endl;
for(int i = 0 ; i < data[3].size(); ++i)
ofile<<left << setw(14)<<data[3][i];
ofile<<endl;
for(int i = 0 ; i < data[4].size(); ++i)
ofile<<left << setw(14)<<data[4][i];
ofile<<endl;
ofile.close();
///-----------------------------------------------------------------------------------------------///
cout<<"GRANT success"<<endl;
}
}
}
}
return;
}
void Database::Revoke(string jurisdiction,User &user,Database database)///收回权限
{
string command;
string type;///对象类型
string object;///对象
string role;///用户
ofstream ofile;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "ON")
{
cin>>type;
transform(type.begin(),type.end(),type.begin(),towupper);
if(type == "TABLE")
{
cin>>object;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "FROM")
{
cin>>role;
fstream _file;
string path = "F:\\" + role + ".txt";
_file.open(path.c_str(),ios::in);
if(!_file)
{
cout<<"User does not exist"<<endl;
_file.close();
return;
}
else
{
_file.close();
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
ifstream fin(path.c_str());
ofstream ofile;
char line[100];
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
fin.close();
///-----------------------对用户删除对该表的权限-------------------------------------------------///
vector<string>::iterator it;
if(jurisdiction == "DELETE")
for(it=data[2].begin(); it!=data[2].end();)
{
if(*it == object)
{
data[2].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "DROP")
for(it=data[4].begin(); it!= data[4].end();)
{
if(*it == object)
{
data[4].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "INSERT")
for(it=data[1].begin(); it!=data[1].end();)
{
if(object == *it)
{
data[1].erase(it); //删除元素,返回值指向已删除元素的下一个位置
cout<<*it<<endl;
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "SELECT")
for(it=data[0].begin(); it!=data[0].end();)
{
if(*it == object)
{
data[0].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
else if(jurisdiction == "UPDATE")
for(it=data[3].begin(); it!=data[3].end();)
{
if(*it == object)
{
data[3].erase(it); //删除元素,返回值指向已删除元素的下一个位置
break;
}
else
++it; //指向下一个位置
}
///----------清除表数据-----///
ofile.open(path.c_str(),ios::trunc);///删除表中的数据
ofile.close();
///---------------------------///
///------------将数据重新输入--------///
ofile.open(path.c_str(),ios::app); ///作为输出文件打开
for(int i = 0 ; i < data[0].size(); ++i)
ofile<<left << setw(14)<<data[0][i];
ofile<<endl;
for(int i = 0 ; i < data[1].size(); ++i)
ofile<<left << setw(14)<<data[1][i];
ofile<<endl;
for(int i = 0 ; i < data[2].size(); ++i)
ofile<<left << setw(14)<<data[2][i];
ofile<<endl;
for(int i = 0 ; i < data[3].size(); ++i)
ofile<<left << setw(14)<<data[3][i];
ofile<<endl;
for(int i = 0 ; i < data[4].size(); ++i)
ofile<<left << setw(14)<<data[4][i];
ofile<<endl;
ofile.close();
///-----------------------------------------------------------------------------------------------///
cout<<"Revoke success"<<endl;
}
}
}
}
return;
}
void CreateView(View &view,Database database)///CREATE VIEW V_Customer
///AS SELECT First_Name, Last_Name, Country
///FROM Customer;
{
FILE *fp;
ofstream ofile;
string table_file;
string command;
table_file = database.getAllpath() + view.view_name +".txt";
while((fp = fopen(table_file.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果存在
{
///printf("The file can not be opened.\n");
cout<<"Naming error";
return;
}
fclose(fp);
ofile.open(database.getFile_view().c_str(),ios::app);///存储表名
ofile<<view.view_name<<endl;
ofile.close();
ofile.open(table_file.c_str(),ios::app);
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "SELECT")
{
while(command != ";")
{
ofile<<left <<" "<<command;
cin>>command;
if(command == "stop")
return;
}
}
ofile.close();
}
void CreateIndex(Index &index,Database database)///CREATE INDEX index_name ON table_name (column_name)
///输入表名,判断是否存在表,若不存在则return。若存在,将该表的数据存入vector<vector<string> >中,输入列名。
///判断列名是否存在,若不存在,则return。若存在则将该列按照升序存入index.file中。
{
FILE *fp;
ofstream ofile;///写入文件中
ifstream fin;///从文件中输出
int temp = 0;///当temp为0时,不存在该数据表。当temp为1时,存在该数据表。
string index_file;
string command;
string table_file;///表的路径
int column_position;///记录列的位置
index_file = database.getAllpath() + index.index_name +".txt";
while((fp = fopen(index_file.c_str(),"r+")) != NULL) ///判断按读方式打 file_name 文件是否失败,如果存在
{
///printf("The file can not be opened.\n");
cout<<"Naming error";
return;
}
fclose(fp);
fin.open(index_file.c_str());
fin.close();
ofile.open(database.getFile_index().c_str(),ios::app);///存储索引名
ofile<<index.index_name<<endl;
ofile.close();
cin>>command;///table_name
///---------------------------查询是否存在table_name------------------/
char line[100];
fin.open(database.getFile_root_name().c_str());
while(fin.getline(line,100))
{
if(strcmp(command.c_str(),line) == 0)
{
temp = 1;
break;
}
}
fin.close();
///------------------------------------------------------------------------/
table_file = database.getAllpath() + command + ".txt";
string s = "- - - - - - - - - - - - - - - - - - - - - - - - ---";
///--------------------------------将s之后的数据保存在data里--------------------/
vector<vector<string> > data;///数据
vector<string> rowdata;
fin.open(table_file.c_str());
int s_temp = 0;///临时变量
while(fin.getline(line,100))///从第零行第零列开始
{
if(strcmp(s.c_str(),line) == 0)
{
s_temp = 1;
}
if(s_temp == 1)
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
}
fin.close();
///--------------------------------------------------------------------------------///
if(temp == 1)
{
cin>>command;
if(command == "(")
{
cin>>command;///输入列
for(int i = 0 ; i < data.size(); ++i)
{
for(int j = 0; j < data[i].size(); ++j)
{
if(command.c_str() == data[i][j])
{
ofile.open(index_file.c_str(),ios::app);///存储索引名
for(int comi = 1; comi < data.size(); ++comi)
{
ofile<<data[comi][j]<<endl;
}
}
}
}
cin>>command;
if(command == ")")
{
cout<<"INDEX Success"<<endl;
return ;
}
}
}
return;
}
int main()
{
///--------------------------------将数据保存在data里--------------------///
vector<vector<string> > data;///数据
vector<string> rowdata;
int hang = 0;///data行
string path = "F:\\user.txt";
ifstream fin(path.c_str());
char line[100];
///cout<<"DDDDDDD"<<endl;
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
///printf("%s\n",p);
rowdata.push_back(p);
p=strtok(NULL,d);
}
data.push_back(rowdata);
rowdata.clear();
}
fin.close();
///--------------------------------------------------------------------------------///
string name;
string password;
int Userstatus = 0;
cout<<"Input user: ";
cin>>name;
while(Userstatus == 0)
{
for(int i =1; i < data.size(); ++i)
{
if(data[i][0] == name)
{
Userstatus = i;
}
}
if(Userstatus != 0)
{
break;
}
else
{
cout<<"ERROR: No user exists"<<endl;
cout<<"Input user: ";
cin>>name;
}
}
//cout<<"STATUS: "<<data[Userstatus][2]<<endl;
string fileType = ".txt";
vector<vector<string> > data1;///存储name的权限;
path = "F:\\";
path = path + name + fileType;
fin.open(path.c_str());
while(fin.getline(line,100))///从第零行第零列开始
{
char *p;
const char *d = " ";
p = strtok(line,d);
while(p)
{
rowdata.push_back(p);
p=strtok(NULL,d);
}
data1.push_back(rowdata);
rowdata.clear();
}
fin.close();
User user(data[Userstatus][0],data[Userstatus][1],data[Userstatus][2],data1[0],data1[1],data1[2],data1[3],data1[4]);
cout<<"Enter password: ";
cin>>password;
while( password!= user.getPassword())
{
cout<<"ERROR: Password error"<<endl;
cout<<"Enter password: ";
cin>>password;
}
cout<<"Welcom to the DCF"<<endl;
cout<<"DatabaseName: "<<"database"<<endl;
cout<<"create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)"<<endl;
cout<<"create table NULL NOTNULL PRIMARY FOREIGN"<<endl;
cout<<"drop table tabname "<<endl;
cout<<"Alter table tabname add column col type"<<endl;
cout<<"drop index idxname"<<endl;
int countoftable = 0;
Table table[100];
Database database;
database.CreateDatabase();
FILE file_root;
while(1)
{
cout<<"sql> ";
string command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "CREATE")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "TABLE")
{
database.CreateTable(table[countoftable],database,user);
show_Table(table[countoftable].table_name,database);
countoftable++;
}
else if(command == "VIEW")///CREATE VIEW V_Customer AS SELECT First_Name, Last_Name, Country FROM Customer;
{
View view;
cin>>command;
view.view_name = command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "AS")
{
CreateView(view,database);
}
}
else if(command == "INDEX")///CREATE INDEX index_name ON table_name (column_name)
{
Index index;
cin>>command;
index.index_name= command;
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "ON")
{
CreateIndex(index,database);
}
}
}
else if(command == "HELP")
{
cin>>command;
if(command == "table")
{
cin>>command;
show_Table(command,database);
}
else if(command == "database")
{
show_database(database);
}
else if(command == "view")
{
cin>>command;
show_View(command,database);
}
else if(command == "index")
{
cin>>command;
show_Index(command,database);
}
}
else if(command == "INSERT")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "INTO")
{
cin>>command;
database.Insert(command,user,database);
}
}
else if(command == "DROP")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "TABLE")
{
cin>>command;
database.DropTable(command,user,database);
}
}
else if(command == "DELETE")///DELETE FROM Person WHERE LastName = 'Wilson'
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "FROM")
{
cin>>command;
database.DeleteTable(command,user,database);
}
}
else if(command == "SELECT")///SELECT * FROM 表名称
{
string Newcommand;
cin>>command;
if(command == "*")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "FROM")
{
cin>>command;
database.Select(command,user,database);
}
}
else
{
cin>>Newcommand;
transform(Newcommand.begin(),Newcommand.end(),Newcommand.begin(),towupper);
if(Newcommand == "FROM")
{
cin>>Newcommand;
database.AppointSelect(command,Newcommand,user,database);
}
}
}
else if(command == "UPDATE")
{
cin>>command;
database.Update(command,user,database);
}
else if(command == "GRANT")
{
if(user.getUserstatus() == "Administrators")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
database.Grant(command,user,database);
}
else
{
cout<<"The user has no permissions"<<endl;
}
}
else if(command == "REVOKE")
{
if(user.getUserstatus() == "Administrators")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
database.Revoke(command,user,database);
}
else
{
cout<<"The user has no permissions"<<endl;
}
}
else if(command == "MODIFY")///修改用户
{
if(user.getUserstatus() == "user")
{
cout<<"YOU DO NOT HAVE THIS AUTHORITY"<<endl;
}
else
{
string username;
cin>>username;
int tempname = 0;
for(int i = 1 ; i < data.size(); ++i)
{
if(username == data[i][0])
{
tempname = i;
break;
}
}
if(tempname != 0)
{
string password;
cin>>username>>password;
data[tempname][0] = username;
data[tempname][1] = password;
ofstream ofile;
ofile.open("F:\\user.txt");
//ofile.clear();
for(int i = 0; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
ofile<<data[i][j]<<" ";
}
ofile<<endl;
}
ofile.close();
cout<<"Successful operation"<<endl;
}
}
}
else if(command == "ADD")
{
cin>>command;
transform(command.begin(),command.end(),command.begin(),towupper);
if(command == "USER")
{
string username,password,status;
vector<string> da;
cin>>username>>password>>status;
int t = 0;
for(int i = 1 ; i < data.size(); ++i)
{
if(username == data[i][0])
{
t = 1;
break;
}
}
if(t == 1)
{
cout<<"The user has already existed"<<endl;
}
else
{
if(status == "user" || status == "authority")
{
da.push_back(username);
da.push_back(password);
da.push_back(status);
data.push_back(da);
ofstream ofile;
ofile.open("F:\\user.txt");
//ofile.clear();
for(int i = 0; i < data.size(); ++i)
{
for(int j = 0 ; j < data[i].size(); ++j)
{
ofile<<data[i][j]<<" ";
}
ofile<<endl;
}
ofile.close();
cout<<"Successful operation"<<endl;
}
}
}
}
}
}