forked from OPCODE-Open-Spring-Fest/CPP_Mini_Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
280 lines (254 loc) · 8.76 KB
/
database.cpp
File metadata and controls
280 lines (254 loc) · 8.76 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <limits>
#include <algorithm>
using namespace std;
class Record {
public:
vector<string> values;
Record(const vector<string>& vals) : values(vals) {}
void display() const{
for (size_t i = 0; i < values.size(); i++)
cout << values[i] << "\t";
cout << endl;
}
};
class Table {
private:
string name;
vector<string>columns;
vector<Record>records;
public:
Table() {
}
Table(const string& tableName, const vector<string>& cols) : name(tableName), columns(cols) {
}
const string& getName() const { return name; }
const vector<string>& getColumns() const { return columns;
}
void insertRecord(const vector<string>& values) {
if (values.size() != columns.size()) {
cerr << "Error: Column count mismatch!" << endl;
return;
}
records.emplace_back(values);
cout << "Record inserted successfully.\n";
}
void displayAll() const {
cout << "\nTable: " << name << endl;
for (size_t i = 0; i < columns.size(); i++)
cout << columns[i] << "\t";
cout << "\n--------------------------\n";
for (size_t i = 0; i < records.size(); i++)
records[i].display();
}
void deleteRecord(int index) {
if (index < 0 || index >= (int)records.size()) {
cerr << "Error: Invalid record index." << endl;
return;
}
records.erase(records.begin() + index);
cout << "Record deleted successfully.\n";
}
void updateRecord(int index, const vector<string>& newValues) {
if (index < 0 || index >= (int)records.size()) {
cerr << "Error: Invalid record index." << endl;
return;
}
if (newValues.size() != columns.size()) {
cerr << "Error: Column count mismatch!" << endl;
return;
}
records[index] = Record(newValues);
cout << "Record updated successfully.\n";
}
void query(const string& column, const string& value) const {
int colIndex = -1;
for (size_t i = 0; i < columns.size(); i++) {
if (columns[i]==column) { colIndex = i; break; }
}
if (colIndex == -1) { cerr << "Error: Column not found!" << endl; return; }
cout << "\nQuery Results for "<< column<< " = " << value << ":\n";
for (size_t i = 0; i < records.size(); i++) {
if (records[i].values[colIndex] == value)
records[i].display();
}
}
void saveToFile(ofstream& out) const {
out << name << "\n" << columns.size() << "\n";
for (size_t i = 0; i < columns.size(); i++) out << columns[i] << "\n";
out << records.size() << "\n";
for (size_t i = 0; i < records.size(); i++) {
for (size_t j = 0; j < records[i].values.size(); j++)
out << records[i].values[j] << "\n";
}
}
void loadFromFile(ifstream& in) {
size_t colCount, recCount;
in >> colCount;
in.ignore(numeric_limits<streamsize>::max(),'\n');
columns.clear();
for (size_t i = 0; i < colCount; i++) {
string col;
getline(in, col);
columns.push_back(col);
}
in >> recCount;
in.ignore(numeric_limits<streamsize>::max(),'\n');
records.clear();
for (size_t i = 0; i < recCount; i++) {
vector<string> vals;
for (size_t j = 0; j < colCount; j++) {
string val;
getline(in, val);
vals.push_back(val);
}
records.emplace_back(vals);
}
}
};
class Database {
private:
unordered_map<string, Table> tables;
public:
void createTable(const string& name, const vector<string>& columns) {
if (tables.find(name) != tables.end()) { cerr << "Error: Table already exists!" << endl; return; }
tables[name] = Table(name, columns);
cout << "Table '" << name << "' created successfully.\n";
}
Table* getTable(const string& name) {
auto it = tables.find(name);
if (it == tables.end()) { cerr << "Error: Table not found!" << endl; return nullptr; }
return &(it->second);
}
void saveDatabase(const string& filename)const {
ofstream out(filename.c_str());
if (!out) { cerr << "Error: Cannot open file for saving.\n"; return; }
out << tables.size() << "\n";
for (auto it = tables.begin(); it != tables.end(); ++it)
it->second.saveToFile(out);
cout << "Database saved to " << filename << endl;
}
void loadDatabase(const string& filename) {
ifstream in(filename.c_str());
if (!in) { cerr << "No previous database found. Starting fresh.\n"; return; }
size_t tableCount;
in >> tableCount; in.ignore(numeric_limits<streamsize>::max(), '\n');
tables.clear();
for (size_t i = 0; i < tableCount; i++) {
string tableName;
getline(in, tableName);
Table t;
t.loadFromFile(in);
tables[tableName] = t;
}
cout << "Database loaded from " << filename << endl;
}
};
int main() {
Database db;
db.loadDatabase("database.txt");
int choice;
while (true) {
cout << "\n----- Mini Database Engine -----\n";
cout << "1. Create Table\n2. Insert Record\n3. Display Table\n4. Delete Record\n";
cout << "5. Update Record\n6. Query Data\n7. Save & Exit\nEnter choice: ";
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (choice == 1) {
string name;
int cols;
cout << "Enter table name: ";
getline(cin, name);
cout << "Enter number of columns: ";
cin >> cols;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> columns(cols);
cout << "Enter column names (one per line):\n";
for (int i = 0; i < cols; i++)
getline(cin, columns[i]);
db.createTable(name, columns);
}
else if (choice == 2) {
string tname;
cout << "Enter table name: ";
getline(cin, tname);
Table* t = db.getTable(tname);
if (t) {
vector<string> vals;
for (size_t i = 0; i < t->getColumns().size(); i++) {
string val;
cout << t->getColumns()[i] << ": ";
getline(cin, val);
vals.push_back(val);
}
t->insertRecord(vals);
}
}
else if (choice == 3) {
string tname;
cout << "Enter table name: ";
getline(cin, tname);
Table* t = db.getTable(tname);
if (t) t->displayAll();
}
else if (choice == 4) {
string tname;
int index;
cout<< "Enter table name: ";
getline(cin, tname);
cout<< "Enter record index (starting from 0): ";
cin>> index;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Table* t = db.getTable(tname);
if (t) t->deleteRecord(index);
}
else if (choice == 5) {
string tname;
int index;
cout << "Enter table name: ";
getline(cin, tname);
Table* t = db.getTable(tname);
if (t) {
cout<< "Enter record index: ";
cin>> index;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> newVals;
for (size_t i = 0; i < t->getColumns().size(); i++) {
string val;
cout<< t->getColumns()[i] << ": ";
getline(cin, val);
newVals.push_back(val);
}
t->updateRecord(index, newVals);
}
}
else if (choice == 6) {
string tname, col, val;
cout<< "Enter table name: ";
getline(cin, tname);
cout<< "Enter column to search: ";
getline(cin, col);
cout<< "Enter value: ";
getline(cin, val);
Table* t = db.getTable(tname);
if (t) t->query(col, val);
}
else if (choice == 7) {
db.saveDatabase("database.txt");
cout << "Exiting...\n";
break;
}
else cout << "Invalid choice. Try again.\n";
}
return 0;
}
#ifdef _WIN32
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
return main();
}
#endif