-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
149 lines (138 loc) · 4.5 KB
/
Database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Created by Christo on 3/5/2019.
//
#include "Database.h"
/**
* \brief a Constructor used to create a new Database
* @param name is the name of the new Database
* @param username is the name of the user who created the Database
*/
Database::Database(std::string name, std::string username) {
databaseName=name;
databaseFilePath = name+"_database";
databaseFile.open(databaseFilePath);
/*If the database File didnt open (because it doesnt exist yet), create
* a file in the databaseFilePath and write as the first line the path
* of the database*/
if(!databaseFile){
databaseFile.open(databaseFilePath, std::ios::out | std::ios::app);
databaseFile<<databaseFilePath<<std::endl;
}
databaseFile.close();
//Now, open the file of the user and add a new line with the path of the
//database file.
databaseFile.open(username+"_user",std::ios::out | std::ios::app);
if(databaseFile){
databaseFile<<databaseFilePath<<std::endl;
}
databaseFile.close();
}
/**
* \brief method used to return the name of the database
* @param withoutSignature is a boolean that specifies if the
* returning value must contain the "_database" signature or not.
* @return the name of the Database
*/
std::string Database::getDatabaseName(bool withoutSignature){
refreshDatabaseInfo();
if(withoutSignature){
for(std::string info : databaseInfoVector){
//std::cout<<info<<"\n";
if(endsWith(info,"_database")){
return info.substr(0,(info.size()-9));
}
}
std::cout<<"\nThe database has no name";
}else{
for(std::string info : databaseInfoVector){
//std::cout<<info<<"\n";
if(endsWith(info,"_database")){
return info;
}
}
std::cout<<"\nThe database has no name";
}
}
/**
* \brief a Constructor used to open an existant Database.
* @param databaseFilePath is the path to the file where the
* information of the Database is stored.
*/
Database::Database(std::string databaseFilePath) {
this->databaseFilePath = databaseFilePath;
this->databaseName = getDatabaseName(true);
this->databaseTableVector = getTables();
}
/**
* \brief method used to return the path to the file of the Database.
* @return the path to the file of the database.
*/
std::string &Database::getDatabaseFilePath() {
return databaseFilePath;
}
/**
* \brief default Constructor
*/
Database::Database() {}
/**
* \brief method used to refresh the vectors where the information
* of the Database is stored, it is used when a Database is opened or a
* change has occured.
*/
void Database::refreshDatabaseInfo() {
std::string databaseInfo;
databaseFile.open(databaseFilePath,std::ios::in);
if(databaseFile){
databaseInfoVector.clear();
while(databaseFile>>databaseInfo){
databaseInfoVector.push_back(databaseInfo);
}
}
databaseFile.close();
databaseTableVector = getTables();
}
/**
* \brief method used to delete all the tables of the database.
*/
void Database::deleteAllTables(){
refreshDatabaseInfo();
for(std::string tableName:databaseTableVector){
deleteTable(std::shared_ptr<Table>(new Table(tableName)));
}
}
/**
* \brief method used to delete a specific table from the database.
* @param table is the name of the table to delete.
*/
void Database::deleteTable(std::shared_ptr<Table> table){
table->deleteAllColumns();
std::string tableDelete = table->getTableFilePath();
databaseFile.open(databaseFilePath, std::ios::in);
std::ofstream temp;
temp.open("temp.txt");
std::string line;
while(databaseFile>>line){
if(line!=tableDelete){
temp<<line<<std::endl;
}
}
databaseFile.close();
temp.close();
remove((databaseFilePath).c_str());
rename("temp.txt",(databaseFilePath).c_str());
std::remove((tableDelete).c_str());
}
/**
* \brief method used to retrieve all the tables contained in the database.
* @return a vector with the file path of the tables of the database.
*/
std::vector<std::string> Database::getTables() {
std::vector<std::string> toRet;
for(std::string info : databaseInfoVector){
//std::cout<<info<<"\n";
if(endsWith(info,"_table")){
toRet.push_back(info);
}
}
return toRet;
}