Advertisement
Echo89

C++ Basic Database Program.

Nov 21st, 2012
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <stdlib.h>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. string str(string data)
  11. {
  12.     return data;
  13. }
  14.  
  15. string str_replace(string fin, string rep, string str)
  16. {
  17.     if(str.find(fin) == string::npos) return str;
  18.     while(str.find(fin) != string::npos) str.replace(str.find(fin), fin.length(), rep);
  19.  
  20.     return str;
  21. }
  22.  
  23. string quotes(string str)
  24. {
  25.     string r = str_replace("\"", "", str);
  26.     return r;
  27. }
  28.  
  29. string squotes(string str)
  30. {
  31.     string r = str_replace("'", "", str);
  32.     return r;
  33. }
  34.  
  35. bool file_exists(const char *name)
  36. {
  37.     ifstream file(name);
  38.     return file.good();
  39. }
  40.  
  41. bool table_exists(string table)
  42. {
  43.     ostringstream floc;
  44.     floc << "database/" << table << ".tbl";
  45.     string file = floc.str();
  46.     return file_exists(file.c_str());
  47. }
  48.  
  49. vector<string> explode(string str, string del)
  50. {
  51.     int slen = str.length();
  52.     int dlen = del.length();
  53.     int found_a;
  54.     vector<string> final;
  55.     vector<string> fail (0);
  56.     if(dlen == 0) return fail;
  57.     if(slen == 0) return fail;
  58.     while(str.find(del) != string::npos)
  59.     {
  60.         final.push_back(str.substr(0, str.find(del)));
  61.         str.erase(0, str.find(del) + dlen);
  62.     }
  63.  
  64.     if(str.length() > 0)
  65.     {
  66.         final.push_back(str);
  67.         str.erase(0, str.length());
  68.     }
  69.  
  70.     return final;
  71. }
  72.  
  73. string implode(vector<string> arr, string del = "")
  74. {
  75.     string r;
  76.  
  77.     for(int i = 0; i < arr.size(); i++) r.append(arr.at(i) + del);
  78.  
  79.     return r;
  80. }
  81.  
  82. bool column_exists(string table, string column)
  83. {
  84.     ostringstream floc;
  85.     bool exists;
  86.     floc << "database/" << table << ".tbl";
  87.     string sfloc = floc.str();
  88.     const char *chfloc = sfloc.c_str();
  89.     if(!file_exists(chfloc)) return false;
  90.     ifstream file(chfloc);
  91.     if(file.is_open())
  92.     {
  93.         while(file.good())
  94.         {
  95.             string temp;
  96.             getline(file, temp);
  97.             if(!temp.empty())
  98.             {
  99.                 vector<string> line_temp (explode(temp, "|-|-|"));
  100.                 for(int i = 0; i < line_temp.size(); i++)
  101.                 {
  102.                     vector<string> column_temp (explode(line_temp.at(i), "="));
  103.                     if(column_temp.at(0) == column)
  104.                     {
  105.                         exists = true;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         file.close();
  111.     }
  112.     else
  113.     {
  114.         file.close();
  115.         return false;
  116.     }
  117.     if(exists = true)
  118.     {
  119.         return true;
  120.     }
  121.     return false;
  122. }
  123.  
  124. int create_table(string table)
  125. {
  126.     ostringstream floc;
  127.     floc << "database/" << table << ".tbl";
  128.     ofstream file;
  129.     string sfloc = floc.str();
  130.     const char *chfloc = sfloc.c_str();
  131.     file.open(chfloc);
  132.     file.close();
  133.  
  134.     return true;
  135. }
  136.  
  137. bool write_table(string table, vector<string> data)
  138. {
  139.     ostringstream floc;
  140.     floc << "database/" << table << ".tbl";
  141.     string sfloc = floc.str();
  142.     const char *chfloc = sfloc.c_str();
  143.     if(!file_exists(chfloc)) return false;
  144.     fstream file;
  145.     file.open(chfloc, fstream::out | fstream::app);
  146.     for(int i = 0; i < data.size(); i++) file << data.at(i) << "|-|-|";
  147.     file << "\n";
  148.     file.close();
  149.  
  150.     return true;
  151. }
  152.  
  153. string get_line_column(string line, string column)
  154. {
  155.     vector<string> arr (explode(line, "|-|-|"));
  156.     string r;
  157.     for(int i = 0; i < arr.size(); i++)
  158.     {
  159.         vector<string> temp (explode(arr.at(i), "="));
  160.         if(temp.at(0) == column) r = quotes(temp.at(1));
  161.     }
  162.     if(r.empty()) return str("<column_does_not_exist>");
  163.     return r;
  164. }
  165.  
  166. bool add_column(string table, string column, string data)
  167. {
  168.     vector<string> data_;
  169.     ostringstream floc;
  170.     floc << "database/" << table << ".tbl";
  171.     string sfloc = floc.str();
  172.     const char *chfloc = sfloc.c_str();
  173.     if(!file_exists(chfloc)) return false;
  174.     ifstream file(chfloc);
  175.     if(file.is_open())
  176.     {
  177.         while(file.good())
  178.         {
  179.             string temp;
  180.             getline(file, temp);
  181.             if(!temp.empty())
  182.             {
  183.                 ostringstream add;
  184.                 add << column << "=" << "\"" << data << "\"" << "|-|-|";
  185.                 temp.append(add.str());
  186.                 data_.push_back(temp);
  187.             }
  188.         }
  189.         file.close();
  190.     }
  191.     else
  192.     {
  193.         file.close();
  194.         return false;
  195.     }
  196.     ofstream filein;
  197.     filein.open(chfloc);
  198.     for(int i = 0; i < data_.size(); i++) filein << data_.at(i) << "\n";
  199.     filein.close();
  200.  
  201.     return true;
  202. }
  203.  
  204. string get_value(string table, string id, string column)
  205. {
  206.     string line;
  207.     vector<string> temp;
  208.     string r;
  209.     ostringstream floc;
  210.     floc << "database/" << table << ".tbl";
  211.     string sfloc = floc.str();
  212.     const char *chfloc = sfloc.c_str();
  213.     if(!file_exists(chfloc)) return 0;
  214.     ifstream file(chfloc);
  215.     if(file.is_open())
  216.     {
  217.         while(file.good())
  218.         {
  219.             getline(file, line);
  220.             vector<string> line_temp (explode(line, "|-|-|"));
  221.             for(int i = 0; i < line_temp.size(); i++)
  222.             {
  223.                 vector<string> column_temp (explode(line_temp.at(i), "="));
  224.                 if(get_line_column(line, "id") == id)
  225.                 {
  226.                     if(column_temp.at(0) == column)
  227.                     {
  228.                         r.append(quotes(column_temp.at(1)));
  229.                         return r;
  230.                     }
  231.                 }
  232.             }
  233.         }
  234.         file.close();
  235.     }
  236.     else return str("<Could not open file>");
  237.  
  238.     return str("<Could not match id and columns>");
  239. }
  240.  
  241. bool edit_values(string table, string column, string data, string conditions = "")
  242. {
  243.     vector<string> data_;
  244.     ostringstream floc;
  245.     floc << "database/" << table << ".tbl";
  246.     string sfloc = floc.str();
  247.     const char *chfloc = sfloc.c_str();
  248.     if(!file_exists(chfloc)) return false;
  249.     ifstream file(chfloc);
  250.     if(file.is_open())
  251.     {
  252.         while(file.good())
  253.         {
  254.             string temp;
  255.             getline(file, temp);
  256.             if(!temp.empty())
  257.             {
  258.                 vector<string> temp_vect (explode(temp, "|-|-|"));
  259.                 for(int x = 0; x < temp_vect.size(); x++)
  260.                 {
  261.                     vector<string> column_temp (explode(temp_vect.at(x), "="));
  262.                     if(column_temp.at(0) == column)
  263.                     {
  264.                         if(!conditions.empty())
  265.                         {
  266.                             vector<string> conditionals (explode(conditions, "="));
  267.                             if(conditionals.size() > 1)
  268.                             {
  269.                                 if(squotes(get_line_column(temp, conditionals.at(0))) == squotes(conditionals.at(1)))
  270.                                 {
  271.                                     ostringstream fin;
  272.                                     fin << column_temp.at(0) << "=" << column_temp.at(1);
  273.                                     ostringstream rep;
  274.                                     rep << column_temp.at(0) << "=" << "\"" << data << "\"";
  275.                                     string fin_ = fin.str();
  276.                                     string rep_ = rep.str();
  277.                                     if(temp.find(fin_) != string::npos) temp.replace(temp.find(fin_), fin_.length(), rep_);
  278.                                     else return false;
  279.                                 }
  280.                             }
  281.                             else
  282.                             {
  283.                                 temp = "<incorrect_conditionals>";
  284.                                 break;
  285.                             }
  286.                         }
  287.                         else
  288.                         {
  289.  
  290.                         }
  291.                     }
  292.                 }
  293.                 data_.push_back(temp);
  294.             }
  295.         }
  296.         file.close();
  297.     }
  298.     else
  299.     {
  300.         file.close();
  301.         return false;
  302.     }
  303.     ofstream filein;
  304.     filein.open(chfloc);
  305.     for(int i = 0; i < data_.size(); i++) filein << data_.at(i) << "\n";
  306.     filein.close();
  307.  
  308.     return true;
  309. }
  310.  
  311. bool clean_table(string table)
  312. {
  313.     vector<string> data_;
  314.     ostringstream floc;
  315.     floc << "database/" << table << ".tbl";
  316.     string sfloc = floc.str();
  317.     const char *chfloc = sfloc.c_str();
  318.     if(!file_exists(chfloc)) return false;
  319.     ifstream file(chfloc);
  320.     if(file.is_open())
  321.     {
  322.         while(file.good())
  323.         {
  324.             string temp;
  325.             getline(file, temp);
  326.             if(!temp.empty())
  327.             {
  328.                 data_.push_back(temp);
  329.             }
  330.         }
  331.         file.close();
  332.     }
  333.     else
  334.     {
  335.         file.close();
  336.         return false;
  337.     }
  338.     ofstream filein;
  339.     filein.open(chfloc);
  340.     for(int i = 0; i < data_.size(); i++) filein << data_.at(i) << "\n";
  341.     filein.close();
  342.  
  343.     return true;
  344. }
  345.  
  346. vector<string> get_column(string table, string column, string conditions = "")
  347. {
  348.     string line;
  349.     vector<string> temp;
  350.     vector<string> r;
  351.     ostringstream floc;
  352.     floc << "database/" << table << ".tbl";
  353.     string sfloc = floc.str();
  354.     const char *chfloc = sfloc.c_str();
  355.     vector<string> fail (0);
  356.     if(!file_exists(chfloc)) return fail;
  357.     ifstream file(chfloc);
  358.     if(file.is_open())
  359.     {
  360.         while(file.good())
  361.         {
  362.             getline(file, line);
  363.             if(!conditions.empty())
  364.             {
  365.                 vector<string> conditionals (explode(conditions, "="));
  366.                 if(squotes(get_line_column(line, conditionals.at(0))) == squotes(conditionals.at(1)))
  367.                 {
  368.                     vector<string> line_temp (explode(line, "|-|-|"));
  369.                     for(int i = 0; i < line_temp.size(); i++)
  370.                     {
  371.                         vector<string> column_temp (explode(line_temp.at(i), "="));
  372.                         if(column_temp.at(0) == column) r.push_back(quotes(column_temp.at(1)));
  373.                     }
  374.                 }
  375.             }
  376.             else
  377.             {
  378.                 vector<string> line_temp (explode(line, "|-|-|"));
  379.                 for(int i = 0; i < line_temp.size(); i++)
  380.                 {
  381.                     vector<string> column_temp (explode(line_temp.at(i), "="));
  382.                     if(column_temp.at(0) == column) r.push_back(quotes(column_temp.at(1)));
  383.                 }
  384.             }
  385.         }
  386.         file.close();
  387.     }
  388.     else r.push_back("<could_not_open_file>");
  389.     if(r.size() == 0);
  390.     return r;
  391. }
  392.  
  393. vector<string> db(string input)
  394. {
  395.     string extra;
  396.     if(input.find("\"") != string::npos)
  397.     {
  398.         extra += quotes(input.substr(input.find("\"") + 1));
  399.         input.replace(input.find("\""), extra.length() + 1, "extra");
  400.     }
  401.  
  402.     vector<string> comm (explode(input, "_"));
  403.     vector<string> get;
  404.     if(comm.at(0) == str("GET"))
  405.     {
  406.         if(comm.at(2) == str("FROM"))
  407.         {
  408.             if(table_exists(comm.at(3)))
  409.             {
  410.                 if(comm.size() > 5)
  411.                 {
  412.                     if(comm.at(4) == str("WHERE"))
  413.                     {
  414.                         vector<string> conditionals (explode(comm.at(5), "="));
  415.                         if(conditionals.size() == 2)
  416.                         {
  417.                             if(column_exists(comm.at(3), conditionals.at(0)))
  418.                             {
  419.                                 vector<string> r (get_column(comm.at(3), comm.at(1), comm.at(5)));
  420.                             }
  421.                             else cout << "INCORRECT SYNTAX: WHERE column \"" << conditionals.at(0) << "\" does not exit.";
  422.                         }
  423.                         else cout << "INCORRECT SYNTAX: Conditionals could not be found.";
  424.                     }
  425.                     else cout << "INCORRECT SYNTAX: Third command \"" << comm.at(4) << " not recognized; expecting \"WHERE\"";
  426.                 }
  427.                 else
  428.                 {
  429.                     if(column_exists(comm.at(3), comm.at(1)))
  430.                     {
  431.                         vector<string> r (get_column(comm.at(3), comm.at(1)));
  432.                         return r;
  433.                     }
  434.                     else cout << "INCORRECT SYNTAX: GET column \"" << comm.at(1) << "\" does not exist.";
  435.                 }
  436.             }
  437.             else cout << "INCORRECT SYNTAX: GET table \"" << comm.at(3) << "\" does not exist.";
  438.         }
  439.         else cout << "INCORRECT SYNTAX: SECONDARY COMMAND \"" << comm.at(2) << "\" was not recognized; expecting \"FROM\"";
  440.     }
  441.     else if(comm.at(0) == str("NEW"))
  442.     {
  443.         if(comm.at(1) == str("TABLE"))
  444.         {
  445.             if(comm.size() == 3)
  446.             {
  447.                 if(!table_exists(comm.at(2)))
  448.                 {
  449.                     create_table(comm.at(2));
  450.                 }
  451.                 else cout << "INCORRECT SYNTAX: Table \"" << comm.at(2) << "\" already exists.";
  452.             }
  453.             else if(comm.size() < 3) cout << "INCORRECT SYNTAX: Too few commands for NEW TABLE";
  454.             else if(comm.size() > 3) cout << "INCORRECT SYNTAX: Too many commands for NEW TABLE";
  455.         }
  456.         else if(comm.at(1) == str("COLUMN"))
  457.         {
  458.             if(comm.size() == 5)
  459.             {
  460.                 if(comm.at(4) == str("IN"))
  461.                 {
  462.                     if(table_exists(comm.at(4)))
  463.                     {
  464.                         if(!column_exists(comm.at(2), comm.at(4)))
  465.                         {
  466.                             if(!extra.empty())
  467.                             {
  468.                                 if(!add_column(comm.at(4), comm.at(2), extra)) cout << "ERROR: Could not complete commands.";
  469.                                 else cout << "OPPERATION COMPLETE";
  470.                             }
  471.                             else
  472.                             {
  473.                                 if(!add_column(comm.at(4), comm.at(2), "")) cout << "ERROR: Could not complete commands.";
  474.                                 else cout << "OPPERATION COMPLETE";
  475.                             }
  476.                         }
  477.                     }
  478.                 }
  479.                 else cout << "INCORRECT SYNTAX: Third command \"" << comm.at(4) << "\" was not recognized; expecting \"IN\"";
  480.             }
  481.             else if(comm.size() < 5) cout << "INCORRECT SYNTAX: Too few commands for NEW COLUMN";
  482.             else if(comm.size() > 5) cout << "INCORRECT SYNTAX: Too many commands for NEW COLUMN";
  483.         }
  484.         else cout << "INCORRECT SYNTAX: SECONDARY COMMAND \"" << comm.at(2) << "\" was not recognized; expecting \"TABLE\" or \"COLUMN\"";
  485.     }
  486.     else if(comm.at(0) == str("EDIT"))
  487.     {
  488.         if(comm.at(1) == str("COLUMN"))
  489.         {
  490.             if(comm.at(3) == str("IN"))
  491.             {
  492.                 if(table_exists(comm.at(4)))
  493.                 {
  494.                     if(comm.at(5) == str("WHERE"))
  495.                     {
  496.                         if(comm.at(7) == str("DATA"))
  497.                         {
  498.                             if(column_exists(comm.at(4), comm.at(6)))
  499.                             {
  500.                                 if(!extra.empty())
  501.                                 {
  502.                                     vector<string> conds (explode(comm.at(6), "="));
  503.                                     if(conds.size() == 2)
  504.                                     {
  505.                                         if(!edit_values(comm.at(4), comm.at(6), extra, comm.at(6))) cout << "ERROR: Could not complete commands.";
  506.                                         else cout << "OPPERATION COMPLETE";
  507.                                     }
  508.                                     else cout << "INCORRECT SYNTAX: Conditionals could not be found.";
  509.                                 }
  510.                                 else cout << "INCORRECT SYNTAX: DATA could not be found";
  511.                             }
  512.                             else cout << "INCORRECT SYNTAX: COLUMN column \"" << comm.at(1) << "\" does not exist.";
  513.                         }
  514.                         else cout << "INCORRECT SYNTAX: Fifth command \"" << comm.at(4) << " not recognized; expecting \"DATA\"";
  515.                     }
  516.                     else cout << "INCORRECT SYNTAX: Forth command \"" << comm.at(4) << " not recognized; expecting \"WHERE\"";
  517.                 }
  518.                 else cout << "INCORRECT SYNTAX: IN table \"" << comm.at(4) << "\" does not exist.";
  519.             }
  520.             else cout << "INCORRECT SYNTAX: Third command \"" << comm.at(3) << " not recognized; expecting \"IN\"";
  521.         }
  522.         else cout << "INCORRECT SYNTAX: SECONDARY COMMAND \"" << comm.at(2) << "\" was not recognized; expecting \"COLUMN\"";
  523.     }
  524.     else cout << "INCORRECT SYNTAX: COMMAND \"" << comm.at(0) << "\" was not recognized.";
  525. }
  526.  
  527. int main()
  528. {
  529.     vector<string> output (db("GET_color_FROM_fruit_WHERE_good='true'"));
  530.     for(int i = 0; i < output.size(); i++) cout << endl << output.at(i);
  531.  
  532.     cin.get();
  533.  
  534.     return 0;
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement