Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- #include <stdlib.h>
- #include <sstream>
- using namespace std;
- string str(string data)
- {
- return data;
- }
- string str_replace(string fin, string rep, string str)
- {
- if(str.find(fin) == string::npos) return str;
- while(str.find(fin) != string::npos) str.replace(str.find(fin), fin.length(), rep);
- return str;
- }
- string quotes(string str)
- {
- string r = str_replace("\"", "", str);
- return r;
- }
- string squotes(string str)
- {
- string r = str_replace("'", "", str);
- return r;
- }
- bool file_exists(const char *name)
- {
- ifstream file(name);
- return file.good();
- }
- bool table_exists(string table)
- {
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string file = floc.str();
- return file_exists(file.c_str());
- }
- vector<string> explode(string str, string del)
- {
- int slen = str.length();
- int dlen = del.length();
- int found_a;
- vector<string> final;
- vector<string> fail (0);
- if(dlen == 0) return fail;
- if(slen == 0) return fail;
- while(str.find(del) != string::npos)
- {
- final.push_back(str.substr(0, str.find(del)));
- str.erase(0, str.find(del) + dlen);
- }
- if(str.length() > 0)
- {
- final.push_back(str);
- str.erase(0, str.length());
- }
- return final;
- }
- string implode(vector<string> arr, string del = "")
- {
- string r;
- for(int i = 0; i < arr.size(); i++) r.append(arr.at(i) + del);
- return r;
- }
- bool column_exists(string table, string column)
- {
- ostringstream floc;
- bool exists;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- if(!file_exists(chfloc)) return false;
- ifstream file(chfloc);
- if(file.is_open())
- {
- while(file.good())
- {
- string temp;
- getline(file, temp);
- if(!temp.empty())
- {
- vector<string> line_temp (explode(temp, "|-|-|"));
- for(int i = 0; i < line_temp.size(); i++)
- {
- vector<string> column_temp (explode(line_temp.at(i), "="));
- if(column_temp.at(0) == column)
- {
- exists = true;
- }
- }
- }
- }
- file.close();
- }
- else
- {
- file.close();
- return false;
- }
- if(exists = true)
- {
- return true;
- }
- return false;
- }
- int create_table(string table)
- {
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- ofstream file;
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- file.open(chfloc);
- file.close();
- return true;
- }
- bool write_table(string table, vector<string> data)
- {
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- if(!file_exists(chfloc)) return false;
- fstream file;
- file.open(chfloc, fstream::out | fstream::app);
- for(int i = 0; i < data.size(); i++) file << data.at(i) << "|-|-|";
- file << "\n";
- file.close();
- return true;
- }
- string get_line_column(string line, string column)
- {
- vector<string> arr (explode(line, "|-|-|"));
- string r;
- for(int i = 0; i < arr.size(); i++)
- {
- vector<string> temp (explode(arr.at(i), "="));
- if(temp.at(0) == column) r = quotes(temp.at(1));
- }
- if(r.empty()) return str("<column_does_not_exist>");
- return r;
- }
- bool add_column(string table, string column, string data)
- {
- vector<string> data_;
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- if(!file_exists(chfloc)) return false;
- ifstream file(chfloc);
- if(file.is_open())
- {
- while(file.good())
- {
- string temp;
- getline(file, temp);
- if(!temp.empty())
- {
- ostringstream add;
- add << column << "=" << "\"" << data << "\"" << "|-|-|";
- temp.append(add.str());
- data_.push_back(temp);
- }
- }
- file.close();
- }
- else
- {
- file.close();
- return false;
- }
- ofstream filein;
- filein.open(chfloc);
- for(int i = 0; i < data_.size(); i++) filein << data_.at(i) << "\n";
- filein.close();
- return true;
- }
- string get_value(string table, string id, string column)
- {
- string line;
- vector<string> temp;
- string r;
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- if(!file_exists(chfloc)) return 0;
- ifstream file(chfloc);
- if(file.is_open())
- {
- while(file.good())
- {
- getline(file, line);
- vector<string> line_temp (explode(line, "|-|-|"));
- for(int i = 0; i < line_temp.size(); i++)
- {
- vector<string> column_temp (explode(line_temp.at(i), "="));
- if(get_line_column(line, "id") == id)
- {
- if(column_temp.at(0) == column)
- {
- r.append(quotes(column_temp.at(1)));
- return r;
- }
- }
- }
- }
- file.close();
- }
- else return str("<Could not open file>");
- return str("<Could not match id and columns>");
- }
- bool edit_values(string table, string column, string data, string conditions = "")
- {
- vector<string> data_;
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- if(!file_exists(chfloc)) return false;
- ifstream file(chfloc);
- if(file.is_open())
- {
- while(file.good())
- {
- string temp;
- getline(file, temp);
- if(!temp.empty())
- {
- vector<string> temp_vect (explode(temp, "|-|-|"));
- for(int x = 0; x < temp_vect.size(); x++)
- {
- vector<string> column_temp (explode(temp_vect.at(x), "="));
- if(column_temp.at(0) == column)
- {
- if(!conditions.empty())
- {
- vector<string> conditionals (explode(conditions, "="));
- if(conditionals.size() > 1)
- {
- if(squotes(get_line_column(temp, conditionals.at(0))) == squotes(conditionals.at(1)))
- {
- ostringstream fin;
- fin << column_temp.at(0) << "=" << column_temp.at(1);
- ostringstream rep;
- rep << column_temp.at(0) << "=" << "\"" << data << "\"";
- string fin_ = fin.str();
- string rep_ = rep.str();
- if(temp.find(fin_) != string::npos) temp.replace(temp.find(fin_), fin_.length(), rep_);
- else return false;
- }
- }
- else
- {
- temp = "<incorrect_conditionals>";
- break;
- }
- }
- else
- {
- }
- }
- }
- data_.push_back(temp);
- }
- }
- file.close();
- }
- else
- {
- file.close();
- return false;
- }
- ofstream filein;
- filein.open(chfloc);
- for(int i = 0; i < data_.size(); i++) filein << data_.at(i) << "\n";
- filein.close();
- return true;
- }
- bool clean_table(string table)
- {
- vector<string> data_;
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- if(!file_exists(chfloc)) return false;
- ifstream file(chfloc);
- if(file.is_open())
- {
- while(file.good())
- {
- string temp;
- getline(file, temp);
- if(!temp.empty())
- {
- data_.push_back(temp);
- }
- }
- file.close();
- }
- else
- {
- file.close();
- return false;
- }
- ofstream filein;
- filein.open(chfloc);
- for(int i = 0; i < data_.size(); i++) filein << data_.at(i) << "\n";
- filein.close();
- return true;
- }
- vector<string> get_column(string table, string column, string conditions = "")
- {
- string line;
- vector<string> temp;
- vector<string> r;
- ostringstream floc;
- floc << "database/" << table << ".tbl";
- string sfloc = floc.str();
- const char *chfloc = sfloc.c_str();
- vector<string> fail (0);
- if(!file_exists(chfloc)) return fail;
- ifstream file(chfloc);
- if(file.is_open())
- {
- while(file.good())
- {
- getline(file, line);
- if(!conditions.empty())
- {
- vector<string> conditionals (explode(conditions, "="));
- if(squotes(get_line_column(line, conditionals.at(0))) == squotes(conditionals.at(1)))
- {
- vector<string> line_temp (explode(line, "|-|-|"));
- for(int i = 0; i < line_temp.size(); i++)
- {
- vector<string> column_temp (explode(line_temp.at(i), "="));
- if(column_temp.at(0) == column) r.push_back(quotes(column_temp.at(1)));
- }
- }
- }
- else
- {
- vector<string> line_temp (explode(line, "|-|-|"));
- for(int i = 0; i < line_temp.size(); i++)
- {
- vector<string> column_temp (explode(line_temp.at(i), "="));
- if(column_temp.at(0) == column) r.push_back(quotes(column_temp.at(1)));
- }
- }
- }
- file.close();
- }
- else r.push_back("<could_not_open_file>");
- if(r.size() == 0);
- return r;
- }
- vector<string> db(string input)
- {
- string extra;
- if(input.find("\"") != string::npos)
- {
- extra += quotes(input.substr(input.find("\"") + 1));
- input.replace(input.find("\""), extra.length() + 1, "extra");
- }
- vector<string> comm (explode(input, "_"));
- vector<string> get;
- if(comm.at(0) == str("GET"))
- {
- if(comm.at(2) == str("FROM"))
- {
- if(table_exists(comm.at(3)))
- {
- if(comm.size() > 5)
- {
- if(comm.at(4) == str("WHERE"))
- {
- vector<string> conditionals (explode(comm.at(5), "="));
- if(conditionals.size() == 2)
- {
- if(column_exists(comm.at(3), conditionals.at(0)))
- {
- vector<string> r (get_column(comm.at(3), comm.at(1), comm.at(5)));
- }
- else cout << "INCORRECT SYNTAX: WHERE column \"" << conditionals.at(0) << "\" does not exit.";
- }
- else cout << "INCORRECT SYNTAX: Conditionals could not be found.";
- }
- else cout << "INCORRECT SYNTAX: Third command \"" << comm.at(4) << " not recognized; expecting \"WHERE\"";
- }
- else
- {
- if(column_exists(comm.at(3), comm.at(1)))
- {
- vector<string> r (get_column(comm.at(3), comm.at(1)));
- return r;
- }
- else cout << "INCORRECT SYNTAX: GET column \"" << comm.at(1) << "\" does not exist.";
- }
- }
- else cout << "INCORRECT SYNTAX: GET table \"" << comm.at(3) << "\" does not exist.";
- }
- else cout << "INCORRECT SYNTAX: SECONDARY COMMAND \"" << comm.at(2) << "\" was not recognized; expecting \"FROM\"";
- }
- else if(comm.at(0) == str("NEW"))
- {
- if(comm.at(1) == str("TABLE"))
- {
- if(comm.size() == 3)
- {
- if(!table_exists(comm.at(2)))
- {
- create_table(comm.at(2));
- }
- else cout << "INCORRECT SYNTAX: Table \"" << comm.at(2) << "\" already exists.";
- }
- else if(comm.size() < 3) cout << "INCORRECT SYNTAX: Too few commands for NEW TABLE";
- else if(comm.size() > 3) cout << "INCORRECT SYNTAX: Too many commands for NEW TABLE";
- }
- else if(comm.at(1) == str("COLUMN"))
- {
- if(comm.size() == 5)
- {
- if(comm.at(4) == str("IN"))
- {
- if(table_exists(comm.at(4)))
- {
- if(!column_exists(comm.at(2), comm.at(4)))
- {
- if(!extra.empty())
- {
- if(!add_column(comm.at(4), comm.at(2), extra)) cout << "ERROR: Could not complete commands.";
- else cout << "OPPERATION COMPLETE";
- }
- else
- {
- if(!add_column(comm.at(4), comm.at(2), "")) cout << "ERROR: Could not complete commands.";
- else cout << "OPPERATION COMPLETE";
- }
- }
- }
- }
- else cout << "INCORRECT SYNTAX: Third command \"" << comm.at(4) << "\" was not recognized; expecting \"IN\"";
- }
- else if(comm.size() < 5) cout << "INCORRECT SYNTAX: Too few commands for NEW COLUMN";
- else if(comm.size() > 5) cout << "INCORRECT SYNTAX: Too many commands for NEW COLUMN";
- }
- else cout << "INCORRECT SYNTAX: SECONDARY COMMAND \"" << comm.at(2) << "\" was not recognized; expecting \"TABLE\" or \"COLUMN\"";
- }
- else if(comm.at(0) == str("EDIT"))
- {
- if(comm.at(1) == str("COLUMN"))
- {
- if(comm.at(3) == str("IN"))
- {
- if(table_exists(comm.at(4)))
- {
- if(comm.at(5) == str("WHERE"))
- {
- if(comm.at(7) == str("DATA"))
- {
- if(column_exists(comm.at(4), comm.at(6)))
- {
- if(!extra.empty())
- {
- vector<string> conds (explode(comm.at(6), "="));
- if(conds.size() == 2)
- {
- if(!edit_values(comm.at(4), comm.at(6), extra, comm.at(6))) cout << "ERROR: Could not complete commands.";
- else cout << "OPPERATION COMPLETE";
- }
- else cout << "INCORRECT SYNTAX: Conditionals could not be found.";
- }
- else cout << "INCORRECT SYNTAX: DATA could not be found";
- }
- else cout << "INCORRECT SYNTAX: COLUMN column \"" << comm.at(1) << "\" does not exist.";
- }
- else cout << "INCORRECT SYNTAX: Fifth command \"" << comm.at(4) << " not recognized; expecting \"DATA\"";
- }
- else cout << "INCORRECT SYNTAX: Forth command \"" << comm.at(4) << " not recognized; expecting \"WHERE\"";
- }
- else cout << "INCORRECT SYNTAX: IN table \"" << comm.at(4) << "\" does not exist.";
- }
- else cout << "INCORRECT SYNTAX: Third command \"" << comm.at(3) << " not recognized; expecting \"IN\"";
- }
- else cout << "INCORRECT SYNTAX: SECONDARY COMMAND \"" << comm.at(2) << "\" was not recognized; expecting \"COLUMN\"";
- }
- else cout << "INCORRECT SYNTAX: COMMAND \"" << comm.at(0) << "\" was not recognized.";
- }
- int main()
- {
- vector<string> output (db("GET_color_FROM_fruit_WHERE_good='true'"));
- for(int i = 0; i < output.size(); i++) cout << endl << output.at(i);
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement