-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreter.cpp
73 lines (68 loc) · 3.15 KB
/
Interpreter.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
// Interpreter.cpp
#include "Interpreter.h"
#include <fstream>
#include <iostream>
#include <sstream>
Interpreter::Interpreter(Database& db) : db(db) {}
void Interpreter::interpret(const std::string& filename) {
std::cout << "Interpreting " << filename << std::endl;
// Añade aquí el código para interpretar el archivo .sqlh
}
void Interpreter::executeScript(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: Cannot open file." << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string command;
iss >> command;
if (command == "CREATE") {
std::string tableName;
iss >> command >> tableName; // leer "TABLE" y el nombre de la tabla
if (db.createTable(tableName)) {
std::cout << "Table " << tableName << " created successfully." << std::endl;
} else {
std::cerr << "Error: Table " << tableName << " already exists." << std::endl;
}
} else if (command == "ADD") {
std::string tableName, columnName, columnType;
iss >> command >> tableName >> columnName >> columnType; // leer "COLUMN", nombre de la tabla, columna y tipo de columna
if (db.alterTableAddColumn(tableName, columnName, columnType)) {
std::cout << "Column " << columnName << " added to table " << tableName << " successfully." << std::endl;
} else {
std::cerr << "Error: Column " << columnName << " already exists in table " << tableName << " or table does not exist." << std::endl;
}
} else if (command == "INSERT") {
std::string tableName;
std::vector<std::string> values;
iss >> tableName;
std::string value;
while (iss >> value) {
values.push_back(value);
}
if (db.insertIntoTable(tableName, values)) {
std::cout << "Data inserted into table " << tableName << " successfully." << std::endl;
} else {
std::cerr << "Error: Table " << tableName << " does not exist." << std::endl;
}
} else if (command == "DELETE") {
std::string tableName, condition;
iss >> tableName >> condition;
if (db.deleteFromTable(tableName, condition)) {
std::cout << "Data deleted from table " << tableName << " successfully." << std::endl;
} else {
std::cerr << "Error: Could not delete data from table " << tableName << "." << std::endl;
}
} else if (command == "JOIN") {
std::string table1, table2, condition;
iss >> table1 >> table2 >> condition;
auto result = db.joinTables(table1, table2, condition);
std::cout << "Joined tables " << table1 << " and " << table2 << " successfully." << std::endl;
// Mostrar los resultados del join si es necesario
}
// Aquí puedes añadir más comandos como DATE FUNCTIONS, STRING FUNCTIONS, etc.
}
}