|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | +#include <unordered_map> |
| 6 | +#include <limits> |
| 7 | +#include <algorithm> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +class Record { |
| 12 | +public: |
| 13 | + vector<string> values; |
| 14 | + Record(const vector<string>& vals) : values(vals) {} |
| 15 | + void display() const{ |
| 16 | + for (size_t i = 0; i < values.size(); i++) |
| 17 | + cout << values[i] << "\t"; |
| 18 | + cout << endl; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +class Table { |
| 23 | +private: |
| 24 | + string name; |
| 25 | + vector<string>columns; |
| 26 | + vector<Record>records; |
| 27 | + |
| 28 | +public: |
| 29 | + Table() { |
| 30 | + |
| 31 | + } |
| 32 | + Table(const string& tableName, const vector<string>& cols) : name(tableName), columns(cols) { |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + const string& getName() const { return name; } |
| 37 | + const vector<string>& getColumns() const { return columns; |
| 38 | + } |
| 39 | + |
| 40 | + void insertRecord(const vector<string>& values) { |
| 41 | + if (values.size() != columns.size()) { |
| 42 | + cerr << "Error: Column count mismatch!" << endl; |
| 43 | + return; |
| 44 | + } |
| 45 | + records.emplace_back(values); |
| 46 | + cout << "Record inserted successfully.\n"; |
| 47 | + } |
| 48 | + |
| 49 | + void displayAll() const { |
| 50 | + cout << "\nTable: " << name << endl; |
| 51 | + for (size_t i = 0; i < columns.size(); i++) |
| 52 | + cout << columns[i] << "\t"; |
| 53 | + cout << "\n--------------------------\n"; |
| 54 | + for (size_t i = 0; i < records.size(); i++) |
| 55 | + records[i].display(); |
| 56 | + } |
| 57 | + |
| 58 | + void deleteRecord(int index) { |
| 59 | + if (index < 0 || index >= (int)records.size()) { |
| 60 | + cerr << "Error: Invalid record index." << endl; |
| 61 | + return; |
| 62 | + } |
| 63 | + records.erase(records.begin() + index); |
| 64 | + cout << "Record deleted successfully.\n"; |
| 65 | + } |
| 66 | + |
| 67 | + void updateRecord(int index, const vector<string>& newValues) { |
| 68 | + if (index < 0 || index >= (int)records.size()) { |
| 69 | + cerr << "Error: Invalid record index." << endl; |
| 70 | + return; |
| 71 | + } |
| 72 | + if (newValues.size() != columns.size()) { |
| 73 | + cerr << "Error: Column count mismatch!" << endl; |
| 74 | + return; |
| 75 | + } |
| 76 | + records[index] = Record(newValues); |
| 77 | + cout << "Record updated successfully.\n"; |
| 78 | + } |
| 79 | + |
| 80 | + void query(const string& column, const string& value) const { |
| 81 | + int colIndex = -1; |
| 82 | + for (size_t i = 0; i < columns.size(); i++) { |
| 83 | + if (columns[i]==column) { colIndex = i; break; } |
| 84 | + } |
| 85 | + if (colIndex == -1) { cerr << "Error: Column not found!" << endl; return; } |
| 86 | + |
| 87 | + cout << "\nQuery Results for "<< column<< " = " << value << ":\n"; |
| 88 | + for (size_t i = 0; i < records.size(); i++) { |
| 89 | + if (records[i].values[colIndex] == value) |
| 90 | + records[i].display(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + void saveToFile(ofstream& out) const { |
| 95 | + out << name << "\n" << columns.size() << "\n"; |
| 96 | + for (size_t i = 0; i < columns.size(); i++) out << columns[i] << "\n"; |
| 97 | + out << records.size() << "\n"; |
| 98 | + for (size_t i = 0; i < records.size(); i++) { |
| 99 | + for (size_t j = 0; j < records[i].values.size(); j++) |
| 100 | + out << records[i].values[j] << "\n"; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + void loadFromFile(ifstream& in) { |
| 105 | + size_t colCount, recCount; |
| 106 | + in >> colCount; |
| 107 | + in.ignore(numeric_limits<streamsize>::max(),'\n'); |
| 108 | + columns.clear(); |
| 109 | + for (size_t i = 0; i < colCount; i++) { |
| 110 | + string col; |
| 111 | + getline(in, col); |
| 112 | + columns.push_back(col); |
| 113 | + } |
| 114 | + |
| 115 | + in >> recCount; |
| 116 | + in.ignore(numeric_limits<streamsize>::max(),'\n'); |
| 117 | + records.clear(); |
| 118 | + for (size_t i = 0; i < recCount; i++) { |
| 119 | + vector<string> vals; |
| 120 | + for (size_t j = 0; j < colCount; j++) { |
| 121 | + string val; |
| 122 | + getline(in, val); |
| 123 | + vals.push_back(val); |
| 124 | + } |
| 125 | + records.emplace_back(vals); |
| 126 | + } |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +class Database { |
| 131 | +private: |
| 132 | + unordered_map<string, Table> tables; |
| 133 | + |
| 134 | +public: |
| 135 | + void createTable(const string& name, const vector<string>& columns) { |
| 136 | + if (tables.find(name) != tables.end()) { cerr << "Error: Table already exists!" << endl; return; } |
| 137 | + tables[name] = Table(name, columns); |
| 138 | + cout << "Table '" << name << "' created successfully.\n"; |
| 139 | + } |
| 140 | + |
| 141 | + Table* getTable(const string& name) { |
| 142 | + auto it = tables.find(name); |
| 143 | + if (it == tables.end()) { cerr << "Error: Table not found!" << endl; return nullptr; } |
| 144 | + return &(it->second); |
| 145 | + } |
| 146 | + |
| 147 | + void saveDatabase(const string& filename)const { |
| 148 | + ofstream out(filename.c_str()); |
| 149 | + if (!out) { cerr << "Error: Cannot open file for saving.\n"; return; } |
| 150 | + out << tables.size() << "\n"; |
| 151 | + for (auto it = tables.begin(); it != tables.end(); ++it) |
| 152 | + it->second.saveToFile(out); |
| 153 | + cout << "Database saved to " << filename << endl; |
| 154 | + } |
| 155 | + |
| 156 | + void loadDatabase(const string& filename) { |
| 157 | + ifstream in(filename.c_str()); |
| 158 | + if (!in) { cerr << "No previous database found. Starting fresh.\n"; return; } |
| 159 | + size_t tableCount; |
| 160 | + in >> tableCount; in.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 161 | + tables.clear(); |
| 162 | + for (size_t i = 0; i < tableCount; i++) { |
| 163 | + string tableName; |
| 164 | + getline(in, tableName); |
| 165 | + Table t; |
| 166 | + t.loadFromFile(in); |
| 167 | + tables[tableName] = t; |
| 168 | + } |
| 169 | + cout << "Database loaded from " << filename << endl; |
| 170 | + } |
| 171 | +}; |
| 172 | + |
| 173 | +int main() { |
| 174 | + Database db; |
| 175 | + db.loadDatabase("database.txt"); |
| 176 | + |
| 177 | + int choice; |
| 178 | + while (true) { |
| 179 | + cout << "\n----- Mini Database Engine -----\n"; |
| 180 | + cout << "1. Create Table\n2. Insert Record\n3. Display Table\n4. Delete Record\n"; |
| 181 | + cout << "5. Update Record\n6. Query Data\n7. Save & Exit\nEnter choice: "; |
| 182 | + cin >> choice; |
| 183 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 184 | + if (choice == 1) { |
| 185 | + string name; |
| 186 | + int cols; |
| 187 | + cout << "Enter table name: "; |
| 188 | + getline(cin, name); |
| 189 | + cout << "Enter number of columns: "; |
| 190 | + cin >> cols; |
| 191 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 192 | + vector<string> columns(cols); |
| 193 | + cout << "Enter column names (one per line):\n"; |
| 194 | + for (int i = 0; i < cols; i++) |
| 195 | + getline(cin, columns[i]); |
| 196 | + |
| 197 | + db.createTable(name, columns); |
| 198 | + } |
| 199 | + else if (choice == 2) { |
| 200 | + string tname; |
| 201 | + cout << "Enter table name: "; |
| 202 | + getline(cin, tname); |
| 203 | + Table* t = db.getTable(tname); |
| 204 | + if (t) { |
| 205 | + vector<string> vals; |
| 206 | + for (size_t i = 0; i < t->getColumns().size(); i++) { |
| 207 | + string val; |
| 208 | + cout << t->getColumns()[i] << ": "; |
| 209 | + getline(cin, val); |
| 210 | + vals.push_back(val); |
| 211 | + } |
| 212 | + t->insertRecord(vals); |
| 213 | + } |
| 214 | + } |
| 215 | + else if (choice == 3) { |
| 216 | + string tname; |
| 217 | + cout << "Enter table name: "; |
| 218 | + getline(cin, tname); |
| 219 | + Table* t = db.getTable(tname); |
| 220 | + if (t) t->displayAll(); |
| 221 | + } |
| 222 | + else if (choice == 4) { |
| 223 | + string tname; |
| 224 | + int index; |
| 225 | + cout<< "Enter table name: "; |
| 226 | + getline(cin, tname); |
| 227 | + cout<< "Enter record index (starting from 0): "; |
| 228 | + cin>> index; |
| 229 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 230 | + Table* t = db.getTable(tname); |
| 231 | + if (t) t->deleteRecord(index); |
| 232 | + } |
| 233 | + else if (choice == 5) { |
| 234 | + string tname; |
| 235 | + int index; |
| 236 | + cout << "Enter table name: "; |
| 237 | + getline(cin, tname); |
| 238 | + Table* t = db.getTable(tname); |
| 239 | + if (t) { |
| 240 | + cout<< "Enter record index: "; |
| 241 | + cin>> index; |
| 242 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 243 | + |
| 244 | + vector<string> newVals; |
| 245 | + for (size_t i = 0; i < t->getColumns().size(); i++) { |
| 246 | + string val; |
| 247 | + cout<< t->getColumns()[i] << ": "; |
| 248 | + getline(cin, val); |
| 249 | + newVals.push_back(val); |
| 250 | + } |
| 251 | + t->updateRecord(index, newVals); |
| 252 | + } |
| 253 | + } |
| 254 | + else if (choice == 6) { |
| 255 | + string tname, col, val; |
| 256 | + cout<< "Enter table name: "; |
| 257 | + getline(cin, tname); |
| 258 | + cout<< "Enter column to search: "; |
| 259 | + getline(cin, col); |
| 260 | + cout<< "Enter value: "; |
| 261 | + getline(cin, val); |
| 262 | + Table* t = db.getTable(tname); |
| 263 | + if (t) t->query(col, val); |
| 264 | + } |
| 265 | + else if (choice == 7) { |
| 266 | + db.saveDatabase("database.txt"); |
| 267 | + cout << "Exiting...\n"; |
| 268 | + break; |
| 269 | + } |
| 270 | + else cout << "Invalid choice. Try again.\n"; |
| 271 | + } |
| 272 | + |
| 273 | + return 0; |
| 274 | +} |
| 275 | +#ifdef _WIN32 |
| 276 | +#include <windows.h> |
| 277 | +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { |
| 278 | + return main(); |
| 279 | +} |
| 280 | +#endif |
0 commit comments