From d65f8e7889c3b5a0b1dab9d3d19c3af87297c008 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 25 Oct 2020 23:03:40 +0530 Subject: [PATCH 01/25] Civilize main() --- .gitignore | 2 + README.md | 2 +- SemiSQL.cpp | 152 ++++++++++++++++++++++------------------------------ 3 files changed, 66 insertions(+), 90 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de74700 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.out +*.o diff --git a/README.md b/README.md index 7940811..8853041 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # Semi_SQL -This code mimics SQL in a very basic way using c++ +This code mimics SQL in a very basic way using C++. diff --git a/SemiSQL.cpp b/SemiSQL.cpp index e8d092b..a65c931 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -1,12 +1,8 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include + +const std::string PROMPT = "semi_sql> "; + /* create database done create table done @@ -17,64 +13,41 @@ delete table delete column add,average,percentage */ -void syntax(char a[]); -void database(char cj[]); -void createtable(char i[]); -void shreadtable(char gh[], char tt[]); -int linecr(char x[],char j[]); -void main() -{ - clrscr(); - _setcursortype(_NOCURSOR); - int i=0; - while(1) - { - gotoxy(15,6); - cout << "\t! READ THIS !"; - gotoxy(15,7); - if(i==0) - i++; - cprintf("********************************************************"); - textcolor(i); - gotoxy(15,8); - cprintf("Type CREATEDB to create a database."); - textcolor(i+1); - gotoxy(15,9); - cprintf("Type OPENDB to open the database."); - textcolor(i+2); - gotoxy(15,10); - cprintf("Type CREATETABLE to create the table."); - textcolor(i+3); - gotoxy(15,11); - cprintf("Type OPENTABLE to open the table."); - textcolor(i+4); - gotoxy(15,12); - cprintf("Type DPTABLE to display the table."); - textcolor(i+5); - gotoxy(15,13); - cprintf("Type SHOWTABLES to display the names of all the tables"); - textcolor(i+6); - gotoxy(15,14); - cprintf("Type SELECTTABLE to select a specific table."); - textcolor(i+7); - gotoxy(15,15); - cprintf("Type SELECTCOLUMN to select a column name."); - textcolor(i+8); - gotoxy(15,16); - cprintf("Type EXIT to exit."); - textcolor(i); - gotoxy(15,17); - i++; - cprintf("********************************************************"); - delay(2000); - if(i > 5 ) break; - } - cout << "\nNow start entering data\n"; - char a[10]; - gets(a); - syntax(a); - getch(); +int parse(std::string); +void database(char*); +void createtable(char*); +void shreadtable(char*, char*); +int linecr(char*, char*); + +int main(void) { + std::string a; + + std::cout << "Instructions" << std::endl + << "============" << std::endl + << "* Type CREATEDB to create a database." << std::endl + << "* Type OPENDB to open the database." << std::endl + << "* Type CREATETABLE to create the table." << std::endl + << "* Type OPENTABLE to open the table." << std::endl + << "* Type DPTABLE to display the table." << std::endl + << "* Type SHOWTABLES to display the names of all the tables" << std::endl + << "* Type SELECTTABLE to select a specific table." << std::endl + << "* Type SELECTCOLUMN to select a column name." << std::endl + << "* Type EXIT to exit." << std::endl; + + do { + std::cout << std::endl << PROMPT; + getline(std::cin, a); + } while (parse(a)); + + return 0; +} + +int parse(std::string a) { + // This will replace syntax() + if (a == "EXIT") + return 0; + return 1; } void database(char db[]) { @@ -88,7 +61,7 @@ void database(char db[]) int i=strlen(db); z[i+1]='@'; z[i+2]='\0'; - strcpy(db,z); // to be changed + strcpy(db,z); // to be changed // TODO: figure out what to change } void shreadtable(char db[],char table[]) { @@ -98,7 +71,7 @@ void shreadtable(char db[],char table[]) till it detects another @.... */ char myline[100],line[100],ch; - cout <<"\n Enter column name\n"; + std::cout <<"\n Enter column name\n"; gets(myline); ifstream file(db); while(1) @@ -137,10 +110,10 @@ void shreadtable(char db[],char table[]) if(line[i] == ',') {k++;i++;} if(k>j || line[i] == ';') break; - if(k == j)cout << line[i]; + if(k == j)std::cout << line[i]; i++; }i=0; - cout << "\n" ; + std::cout << "\n" ; delay(2000); } file.close(); @@ -151,23 +124,23 @@ void createtable(char db[]) { char a[10]; int i=0,n=0,k=0; - cout << "\nEnter table name\n"; + std::cout << "\nEnter table name\n"; gets(a); ofstream fout; fout.open(db,ios::app|ios::nocreate); if(!fout) - {cout<<"\nNO dB OPENED!!\n";}; //syntax + {std::cout<<"\nNO dB OPENED!!\n";}; //syntax fout << "@"<< a << "@\n"; - cout<<"\nEnter column names\n"; //excess data warning; + std::cout<<"\nEnter column names\n"; //excess data warning; gets(a); while(a[i] != '\0') { if(a[i] == ',') n++; i++; } - cout << endl; + std::cout << std::endl; fout << a<< "\n"; - cout <<"\nEnter column data\n"; + std::cout <<"\nEnter column data\n"; i=0; do { @@ -177,7 +150,7 @@ void createtable(char db[]) while(a[i] != '\0') { if(a[i] == ',') k++; - if(k > n){ cout <<"\nYou're entering excess data!" + if(k > n){ std::cout <<"\nYou're entering excess data!" << "\n Recheck your data\n"; goto maaza;} i++; }i=0; @@ -224,33 +197,34 @@ int linecr(char line[],char line2[]) } void syntax(char a[]) { + // THIS IS HELL! char syn[10],var[10],ch,line[50], line2[50]; static char db[20],table[30]; int i=0,k=0; fstream infile,tout; if(strcmp("CREATEDB",a) == 0) // done { - cout << "\nCREATING" ; - cout << "\nEnter database name-\n"; + std::cout << "\nCREATING" ; + std::cout << "\nEnter database name-\n"; gets(db); database(db); puts(db); infile.open(db,ios::out|ios::noreplace); - cout << "\nDatabase created!\n"; + std::cout << "\nDatabase created!\n"; gets(syn); syntax(syn); } else if(strcmp("OPENDB",a) ==0) //done { - cout << "\nEnter database name-\n "; + std::cout << "\nEnter database name-\n "; gets(db); database(db); tout.open(db,ios::in); if(!tout) { - cout << "\nDB not created!\n "; + std::cout << "\nDB not created!\n "; } - else cout << "\nDatabase opened!\n"; + else std::cout << "\nDatabase opened!\n"; tout.close(); gets(syn); syntax(syn); @@ -262,7 +236,7 @@ void syntax(char a[]) if(strcmp("DPTABLE",a) == 0) { ifstream file(db); - if(!file) cout << "\nDB not entered!\n"; + if(!file) std::cout << "\nDB not entered!\n"; while(1) { while(k != 1) @@ -278,13 +252,13 @@ void syntax(char a[]) i=0; while(line[i] != ';' ) { - if(line[i] == ',') {cout << "\t\t"; i++;} + if(line[i] == ',') {std::cout << "\t\t"; i++;} if(line[i] == '}') goto jojol; - cout << line[i]; + std::cout << line[i]; i++; delay(100); } - if(line[i] == ';')cout << ch; + if(line[i] == ';')std::cout << ch; delay(1000); } if(file.eof() ==1) break; @@ -303,7 +277,7 @@ void syntax(char a[]) tout.get(ch); if(var[0] == '@') { linecr(var); - cout << var << ","; + std::cout << var << ","; } if(tout.eof()) break; } @@ -313,7 +287,7 @@ void syntax(char a[]) } if(strcmp("SELECTTABLE",a) == 0) { - cout << "\nEnter table name-\n"; + std::cout << "\nEnter table name-\n"; gets(table); gets(syn); syntax(syn); @@ -323,7 +297,7 @@ void syntax(char a[]) shreadtable(db,table); //enter any column name and it will display the column name } if(strcmp("EXIT",a) == 0) exit(1); - else cout << "\t\t --WRONG SYNTAX!\n"; + else std::cout << "\t\t --WRONG SYNTAX!\n"; gets(syn); syntax(syn); } From b09118a1bc0861fb9ee516f71e3b15e7bbe2d1e3 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 26 Oct 2020 13:37:47 +0530 Subject: [PATCH 02/25] Add pseudocodes for commands in parse() --- SemiSQL.cpp | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index a65c931..f67a04f 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -1,5 +1,6 @@ #include #include +#include const std::string PROMPT = "semi_sql> "; @@ -23,8 +24,8 @@ int linecr(char*, char*); int main(void) { std::string a; - std::cout << "Instructions" << std::endl - << "============" << std::endl + std::cout << "\t! READ THIS !" << std::endl + << "********************************************************" << std::endl << "* Type CREATEDB to create a database." << std::endl << "* Type OPENDB to open the database." << std::endl << "* Type CREATETABLE to create the table." << std::endl @@ -33,7 +34,8 @@ int main(void) { << "* Type SHOWTABLES to display the names of all the tables" << std::endl << "* Type SELECTTABLE to select a specific table." << std::endl << "* Type SELECTCOLUMN to select a column name." << std::endl - << "* Type EXIT to exit." << std::endl; + << "* Type EXIT to exit." << std::endl + << "********************************************************" << std::endl; do { std::cout << std::endl << PROMPT; @@ -44,16 +46,41 @@ int main(void) { } int parse(std::string a) { - // This will replace syntax() - if (a == "EXIT") + if (a == "CREATEDB") { + // input dbname; + // call to createdb(dbname); + } else if (a == "OPENDB") { + // input dbname; + // call to opendb(dbname); + } else if (a == "CREATETABLE") { + // call to create_table(table_name); + } else if (a == "OPENTABLE") { + // TODO: Figure out the purpose of this command + } else if (a == "DPTABLE") { + // TODO: Figure out what to do here + } else if (a == "SHOWTABLES") { + // call to showtables(); + } else if (a == "SELECTTABLE") { + // input table_name; + // call to select_table(table_name); + } else if (a == "SELECTCOLUMN") { + // input column_name; + // disp_column(column_name); + } else if (a == "EXIT") return 0; + else + std::cout << "Invalid Command!" << std::endl; + return 1; } -void database(char db[]) +void database(std::string db) { + // Basically what's happening here: + // db = '@' + db + '@' + char z[20]; int j=strlen(db); - for (int k=0,a=k+1;k Date: Mon, 26 Oct 2020 16:02:20 +0530 Subject: [PATCH 03/25] Implement createdb() --- SemiSQL.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index f67a04f..9472d66 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -16,6 +16,7 @@ add,average,percentage */ int parse(std::string); +void createdb(std::string); void database(char*); void createtable(char*); void shreadtable(char*, char*); @@ -47,8 +48,11 @@ int main(void) { int parse(std::string a) { if (a == "CREATEDB") { - // input dbname; - // call to createdb(dbname); + std::string dbname; + std::cout << "Enter database name- "; + getline(std::cin, dbname); + createdb(dbname); + } else if (a == "OPENDB") { // input dbname; // call to opendb(dbname); @@ -73,6 +77,20 @@ int parse(std::string a) { return 1; } + +void createdb(std::string dbname) { + std::fstream dbfile(dbname, std::ios::in); + if (dbfile) + std::cout << "Database exists!" << std::endl; + else { + dbfile.close(); + dbfile.open(dbname, std::ios::out); + + } + dbfile.close(); + std::cout << "Database created!" << std::endl; +} + void database(std::string db) { // Basically what's happening here: From 37f705eee7517ef417bcf77a93a86f574fe7aba2 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 26 Oct 2020 18:27:59 +0530 Subject: [PATCH 04/25] Implement opendb() --- SemiSQL.cpp | 68 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 9472d66..845f13f 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -2,29 +2,44 @@ #include #include -const std::string PROMPT = "semi_sql> "; +const std::string DEFAULT_PROMPT_TEXT = "semi_sql"; +const std::string PROMPT_SEP = "> "; /* -create database done -create table done -show tables done -select *table done +create database done & refactored +open database done & refactored +create table done +show tables done +select *table done select table done -delete table -delete column -add,average,percentage +drop table +drop column +funcs: add() + average() + (?) percentage() */ int parse(std::string); void createdb(std::string); +void opendb(std::string); +std::string get_prompt(); void database(char*); void createtable(char*); void shreadtable(char*, char*); int linecr(char*, char*); +struct db { + std::string name; + // add tables handler +}; + +struct db* currentdb; + int main(void) { std::string a; + currentdb = nullptr; + std::cout << "\t! READ THIS !" << std::endl << "********************************************************" << std::endl << "* Type CREATEDB to create a database." << std::endl @@ -39,7 +54,7 @@ int main(void) { << "********************************************************" << std::endl; do { - std::cout << std::endl << PROMPT; + std::cout << std::endl << get_prompt(); getline(std::cin, a); } while (parse(a)); @@ -54,8 +69,11 @@ int parse(std::string a) { createdb(dbname); } else if (a == "OPENDB") { - // input dbname; - // call to opendb(dbname); + std::string dbname; + std::cout << "Enter database name- "; + getline(std::cin, dbname); + opendb(dbname); + } else if (a == "CREATETABLE") { // call to create_table(table_name); } else if (a == "OPENTABLE") { @@ -80,17 +98,37 @@ int parse(std::string a) { void createdb(std::string dbname) { std::fstream dbfile(dbname, std::ios::in); - if (dbfile) + + if (dbfile) { std::cout << "Database exists!" << std::endl; - else { dbfile.close(); - dbfile.open(dbname, std::ios::out); - + return; } + + dbfile.open(dbname, std::ios::out); dbfile.close(); + std::cout << "Database created!" << std::endl; } +void opendb(std::string dbname) { + std::fstream dbfile(dbname, std::ios::in); + + if (!dbfile) { + std::cout << "DB not created!" << std::endl; + return; + } + + if (currentdb) delete currentdb; + currentdb = new db; + currentdb->name = dbname; +} + +std::string get_prompt() { + if (currentdb) + return currentdb->name + PROMPT_SEP; + return DEFAULT_PROMPT_TEXT + PROMPT_SEP; +} void database(std::string db) { // Basically what's happening here: From bd0abbe3b8b04b54cf708c580994b2ae1edfc28e Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Tue, 27 Oct 2020 11:50:39 +0530 Subject: [PATCH 05/25] Refactor create_table() --- SemiSQL.cpp | 99 ++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 845f13f..971b16d 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -1,6 +1,7 @@ #include #include #include +#include const std::string DEFAULT_PROMPT_TEXT = "semi_sql"; const std::string PROMPT_SEP = "> "; @@ -8,7 +9,7 @@ const std::string PROMPT_SEP = "> "; /* create database done & refactored open database done & refactored -create table done +create table done & refactored show tables done select *table done select table done @@ -22,9 +23,9 @@ funcs: add() int parse(std::string); void createdb(std::string); void opendb(std::string); +void create_table(void); std::string get_prompt(); void database(char*); -void createtable(char*); void shreadtable(char*, char*); int linecr(char*, char*); @@ -75,7 +76,8 @@ int parse(std::string a) { opendb(dbname); } else if (a == "CREATETABLE") { - // call to create_table(table_name); + create_table(); + } else if (a == "OPENTABLE") { // TODO: Figure out the purpose of this command } else if (a == "DPTABLE") { @@ -124,9 +126,52 @@ void opendb(std::string dbname) { currentdb->name = dbname; } +void create_table(void) { + std::string table_name, cscols, csdata; + size_t field_count; + + if(!currentdb) { + std::cout << "NO dB OPENED!!" << std::endl; + return; + } + + std::cout << "Enter table name: "; + getline(std::cin, table_name); + + std::ofstream fout(currentdb->name, std::ios::app); + + fout << "@" << table_name << "@" << std::endl; // TODO: Figure out what's with the @'s + + std::cout << "Enter column names:" << std::endl; + getline(std::cin, cscols); + field_count = std::count(cscols.begin(), cscols.end(), ',') + 1; + + fout << cscols << std::endl; + + std::cout << std::endl << "Enter column data:" << std::endl; + do { + bool excess_data; + + do { + getline(std::cin, csdata); + + excess_data = (std::count(csdata.begin(), csdata.end(), ',') >= field_count); + if (excess_data) + std::cout << "You're entering excess data!" << std::endl + << "Recheck your data" << std::endl; + } while (excess_data); + + fout << csdata << std::endl; + + } while (csdata.find('}') == std::string::npos); + + fout.close(); +} + std::string get_prompt() { if (currentdb) return currentdb->name + PROMPT_SEP; + return DEFAULT_PROMPT_TEXT + PROMPT_SEP; } void database(std::string db) @@ -203,54 +248,6 @@ void shreadtable(char db[],char table[]) gets(line); syntax(line); } -void createtable(char db[]) -{ - char a[10]; - int i=0,n=0,k=0; - std::cout << "\nEnter table name\n"; - gets(a); - ofstream fout; - fout.open(db,ios::app|ios::nocreate); - if(!fout) - {std::cout<<"\nNO dB OPENED!!\n";}; //syntax - fout << "@"<< a << "@\n"; - std::cout<<"\nEnter column names\n"; //excess data warning; - gets(a); - while(a[i] != '\0') - { - if(a[i] == ',') n++; - i++; - } - std::cout << std::endl; - fout << a<< "\n"; - std::cout <<"\nEnter column data\n"; - i=0; - do - { - maaza: - k= 0; - gets(a); - while(a[i] != '\0') - { - if(a[i] == ',') k++; - if(k > n){ std::cout <<"\nYou're entering excess data!" - << "\n Recheck your data\n"; goto maaza;} - i++; - }i=0; - fout << a; - while(a[i] != '\0') - { - if(a[i] == '}'){fout << '\n' ;goto cas;} - i++; - } - if(a[i] == '\0') fout << '\n'; - i=0; - }while(1); - cas: - fout.close(); - gets(a); - syntax(a); -} void linecr(char line[]) { char trans[10]; From c7a86ff9171d9df1970aebd67e3083667c4523f9 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Tue, 27 Oct 2020 19:52:36 +0530 Subject: [PATCH 06/25] Update documentation --- README.md | 3 ++- SemiSQL.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9c14fca..807e122 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Semi_SQL -This code mimics SQL in a very basic way using C++. +This code basically mimics a SQL DBMS using C++ in a CLI approach. + As of now understand the working of the code by yourself detailed working will be published later with better updates. diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 971b16d..1626c63 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -31,7 +31,7 @@ int linecr(char*, char*); struct db { std::string name; - // add tables handler + // TODO: Add tables handler }; struct db* currentdb; @@ -250,6 +250,7 @@ void shreadtable(char db[],char table[]) } void linecr(char line[]) { + // Gets rid of first and last characters of line[]. char trans[10]; int n,i=0; strcpy(trans,line); @@ -264,7 +265,10 @@ void linecr(char line[]) } int linecr(char line[],char line2[]) { - + // if (line.substr(1, line2.length()) == line2) + // return 1 + // else + // return 0 int j=0,i=0; while(line2[i] != '\0') { j++; From 5bc5dee681fd70caab94ecde7b4c096dd72946e1 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Wed, 28 Oct 2020 11:53:40 +0530 Subject: [PATCH 07/25] Refactor show_tables() --- SemiSQL.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 1626c63..08f45f1 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -10,7 +10,7 @@ const std::string PROMPT_SEP = "> "; create database done & refactored open database done & refactored create table done & refactored -show tables done +show tables done & refactored select *table done select table done drop table @@ -24,6 +24,7 @@ int parse(std::string); void createdb(std::string); void opendb(std::string); void create_table(void); +void show_tables(void); std::string get_prompt(); void database(char*); void shreadtable(char*, char*); @@ -83,7 +84,8 @@ int parse(std::string a) { } else if (a == "DPTABLE") { // TODO: Figure out what to do here } else if (a == "SHOWTABLES") { - // call to showtables(); + show_tables(); + } else if (a == "SELECTTABLE") { // input table_name; // call to select_table(table_name); @@ -140,7 +142,7 @@ void create_table(void) { std::ofstream fout(currentdb->name, std::ios::app); - fout << "@" << table_name << "@" << std::endl; // TODO: Figure out what's with the @'s + fout << "@" << table_name << "@" << std::endl; std::cout << "Enter column names:" << std::endl; getline(std::cin, cscols); @@ -168,6 +170,31 @@ void create_table(void) { fout.close(); } +void show_tables(void) { + if(!currentdb) { + std::cout << "NO dB OPENED!!" << std::endl; + return; + } + + std::ifstream dbfile(currentdb->name); + char line[100]; + + do { + dbfile.get(line, 100); + dbfile.get(); // eat trailing newline char + + if (line[0] == '@') { // @'s are table name identification markers + std::string table_name(line); + std::cout << table_name.substr(1, table_name.length()-2) << ","; + } + + } while (!dbfile.eof()); + + std::cout << std::endl; + + dbfile.close(); +} + std::string get_prompt() { if (currentdb) return currentdb->name + PROMPT_SEP; From 02432fc8627c7ed15c549cbfc3240c63d2b9c5ed Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Wed, 28 Oct 2020 22:15:14 +0530 Subject: [PATCH 08/25] Add extension to db files --- .gitignore | 1 + SemiSQL.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index de74700..e5d4e9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.out *.o +*.db diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 08f45f1..d18d6d0 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -5,6 +5,7 @@ const std::string DEFAULT_PROMPT_TEXT = "semi_sql"; const std::string PROMPT_SEP = "> "; +const std::string DB_EXT = ".db"; /* create database done & refactored @@ -101,7 +102,7 @@ int parse(std::string a) { } void createdb(std::string dbname) { - std::fstream dbfile(dbname, std::ios::in); + std::fstream dbfile(dbname + DB_EXT, std::ios::in); if (dbfile) { std::cout << "Database exists!" << std::endl; @@ -109,14 +110,14 @@ void createdb(std::string dbname) { return; } - dbfile.open(dbname, std::ios::out); + dbfile.open(dbname + DB_EXT, std::ios::out); dbfile.close(); std::cout << "Database created!" << std::endl; } void opendb(std::string dbname) { - std::fstream dbfile(dbname, std::ios::in); + std::fstream dbfile(dbname + DB_EXT, std::ios::in); if (!dbfile) { std::cout << "DB not created!" << std::endl; @@ -140,7 +141,7 @@ void create_table(void) { std::cout << "Enter table name: "; getline(std::cin, table_name); - std::ofstream fout(currentdb->name, std::ios::app); + std::ofstream fout(currentdb->name + DB_EXT, std::ios::app); fout << "@" << table_name << "@" << std::endl; @@ -176,7 +177,7 @@ void show_tables(void) { return; } - std::ifstream dbfile(currentdb->name); + std::ifstream dbfile(currentdb->name + DB_EXT); char line[100]; do { From e31fa55cf6551aec000f5583d96342019c13e5cd Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Fri, 30 Oct 2020 16:05:55 +0530 Subject: [PATCH 09/25] Implement get_tables() function --- SemiSQL.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index d18d6d0..583ee77 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -2,6 +2,7 @@ #include #include #include +#include const std::string DEFAULT_PROMPT_TEXT = "semi_sql"; const std::string PROMPT_SEP = "> "; @@ -26,7 +27,8 @@ void createdb(std::string); void opendb(std::string); void create_table(void); void show_tables(void); -std::string get_prompt(); +std::vector get_tables(void); +std::string get_prompt(void); void database(char*); void shreadtable(char*, char*); int linecr(char*, char*); @@ -177,7 +179,19 @@ void show_tables(void) { return; } + std::vector tables = get_tables(); + + for (int i = 0; i < tables.size(); i++) { + std::cout << tables[i]; + if (i == tables.size() - 1) continue; // don't print the last comma + std::cout << ","; + } + std::cout << std::endl; +} + +std::vector get_tables(void) { std::ifstream dbfile(currentdb->name + DB_EXT); + std::vector tables; char line[100]; do { @@ -186,17 +200,15 @@ void show_tables(void) { if (line[0] == '@') { // @'s are table name identification markers std::string table_name(line); - std::cout << table_name.substr(1, table_name.length()-2) << ","; + tables.push_back(table_name.substr(1, table_name.length()-2)); } - } while (!dbfile.eof()); - std::cout << std::endl; - dbfile.close(); + return tables; } -std::string get_prompt() { +std::string get_prompt(void) { if (currentdb) return currentdb->name + PROMPT_SEP; From 62a6b79bed8ca12aa1b1811071e3bcf2e1acd574 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Fri, 30 Oct 2020 16:20:30 +0530 Subject: [PATCH 10/25] Typedef std::vector to tables_vector --- SemiSQL.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 583ee77..c9cf7df 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -22,12 +22,14 @@ funcs: add() (?) percentage() */ +typedef std::vector tables_vector; + int parse(std::string); void createdb(std::string); void opendb(std::string); void create_table(void); void show_tables(void); -std::vector get_tables(void); +tables_vector get_tables(void); std::string get_prompt(void); void database(char*); void shreadtable(char*, char*); @@ -179,7 +181,7 @@ void show_tables(void) { return; } - std::vector tables = get_tables(); + tables_vector tables = get_tables(); for (int i = 0; i < tables.size(); i++) { std::cout << tables[i]; @@ -189,9 +191,9 @@ void show_tables(void) { std::cout << std::endl; } -std::vector get_tables(void) { +tables_vector get_tables(void) { std::ifstream dbfile(currentdb->name + DB_EXT); - std::vector tables; + tables_vector tables; char line[100]; do { From 6a9bb027fc42af8810242bae33fd467b3fd6bd29 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Fri, 30 Oct 2020 16:32:05 +0530 Subject: [PATCH 11/25] Implement Ctrl-D (EOF) to exit --- SemiSQL.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index c9cf7df..aea4a63 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -62,9 +62,13 @@ int main(void) { do { std::cout << std::endl << get_prompt(); - getline(std::cin, a); + if (!getline(std::cin, a)) { + std::cout << "EXIT" << std::endl; + break; + } } while (parse(a)); + std::cout << "OK, Bye!" << std::endl; return 0; } From d05b67c5187c6da1b4b8912dca5492e661d3ad8e Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sat, 31 Oct 2020 00:42:31 +0530 Subject: [PATCH 12/25] Implement select_table() --- SemiSQL.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index aea4a63..10d7f74 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -5,6 +5,7 @@ #include const std::string DEFAULT_PROMPT_TEXT = "semi_sql"; +const std::string DB_TABLE_SEP = "::"; const std::string PROMPT_SEP = "> "; const std::string DB_EXT = ".db"; @@ -13,7 +14,7 @@ create database done & refactored open database done & refactored create table done & refactored show tables done & refactored -select *table done +select *table done & refactored select table done drop table drop column @@ -29,6 +30,7 @@ void createdb(std::string); void opendb(std::string); void create_table(void); void show_tables(void); +void select_table(void); tables_vector get_tables(void); std::string get_prompt(void); void database(char*); @@ -37,7 +39,7 @@ int linecr(char*, char*); struct db { std::string name; - // TODO: Add tables handler + std::string* table; }; struct db* currentdb; @@ -68,6 +70,12 @@ int main(void) { } } while (parse(a)); + if (currentdb) { + if (currentdb->table) + delete currentdb->table; + delete currentdb; + } + std::cout << "OK, Bye!" << std::endl; return 0; } @@ -75,13 +83,13 @@ int main(void) { int parse(std::string a) { if (a == "CREATEDB") { std::string dbname; - std::cout << "Enter database name- "; + std::cout << "Enter database name: "; getline(std::cin, dbname); createdb(dbname); } else if (a == "OPENDB") { std::string dbname; - std::cout << "Enter database name- "; + std::cout << "Enter database name: "; getline(std::cin, dbname); opendb(dbname); @@ -96,8 +104,8 @@ int parse(std::string a) { show_tables(); } else if (a == "SELECTTABLE") { - // input table_name; - // call to select_table(table_name); + select_table(); + } else if (a == "SELECTCOLUMN") { // input column_name; // disp_column(column_name); @@ -135,6 +143,7 @@ void opendb(std::string dbname) { if (currentdb) delete currentdb; currentdb = new db; currentdb->name = dbname; + currentdb->table = nullptr; } void create_table(void) { @@ -195,6 +204,28 @@ void show_tables(void) { std::cout << std::endl; } +void select_table(void) { + if(!currentdb) { + std::cout << "NO dB OPENED!!" << std::endl; + return; + } + + std::string table_name; + + std::cout << "Enter table name: "; + getline(std::cin, table_name); + + tables_vector tables = get_tables(); + + if (std::find(tables.begin(), tables.end(), table_name) == tables.end()) { + std::cout << "Table does not exist in database." << std::endl; + return; + } + + if (currentdb->table) delete currentdb->table; + currentdb->table = new std::string(table_name); +} + tables_vector get_tables(void) { std::ifstream dbfile(currentdb->name + DB_EXT); tables_vector tables; @@ -215,10 +246,16 @@ tables_vector get_tables(void) { } std::string get_prompt(void) { - if (currentdb) - return currentdb->name + PROMPT_SEP; + std::string prompt; + + if (currentdb) { + prompt = currentdb->name; + if (currentdb->table) + prompt += DB_TABLE_SEP + *(currentdb->table); + } else + prompt = DEFAULT_PROMPT_TEXT; - return DEFAULT_PROMPT_TEXT + PROMPT_SEP; + return prompt + PROMPT_SEP; } void database(std::string db) { From a959d7631c9356e3eeddd2d00cd9d4eadb598534 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sat, 31 Oct 2020 17:45:41 +0530 Subject: [PATCH 13/25] Fix a few datatypes --- SemiSQL.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 10d7f74..cd06989 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -148,7 +148,7 @@ void opendb(std::string dbname) { void create_table(void) { std::string table_name, cscols, csdata; - size_t field_count; + int field_count; if(!currentdb) { std::cout << "NO dB OPENED!!" << std::endl; @@ -196,7 +196,7 @@ void show_tables(void) { tables_vector tables = get_tables(); - for (int i = 0; i < tables.size(); i++) { + for (size_t i = 0; i < tables.size(); i++) { std::cout << tables[i]; if (i == tables.size() - 1) continue; // don't print the last comma std::cout << ","; From bd820d1d0856ba3d4df7eafd05ffdf1023b340e1 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 00:25:39 +0530 Subject: [PATCH 14/25] Refactor display_table() --- SemiSQL.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 15 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index cd06989..e78b5bd 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -15,6 +15,7 @@ open database done & refactored create table done & refactored show tables done & refactored select *table done & refactored +display table done & refactored select table done drop table drop column @@ -23,16 +24,16 @@ funcs: add() (?) percentage() */ -typedef std::vector tables_vector; - -int parse(std::string); -void createdb(std::string); -void opendb(std::string); +int parse(std::string const&); +void createdb(std::string const&); +void opendb(std::string const&); void create_table(void); void show_tables(void); void select_table(void); -tables_vector get_tables(void); +void display_table(void); +void get_tables(std::vector&); std::string get_prompt(void); +void tokenize(std::string const&, const char, std::vector&); void database(char*); void shreadtable(char*, char*); int linecr(char*, char*); @@ -80,7 +81,7 @@ int main(void) { return 0; } -int parse(std::string a) { +int parse(std::string const &a) { if (a == "CREATEDB") { std::string dbname; std::cout << "Enter database name: "; @@ -99,7 +100,8 @@ int parse(std::string a) { } else if (a == "OPENTABLE") { // TODO: Figure out the purpose of this command } else if (a == "DPTABLE") { - // TODO: Figure out what to do here + display_table(); + } else if (a == "SHOWTABLES") { show_tables(); @@ -117,7 +119,7 @@ int parse(std::string a) { return 1; } -void createdb(std::string dbname) { +void createdb(std::string const &dbname) { std::fstream dbfile(dbname + DB_EXT, std::ios::in); if (dbfile) { @@ -132,7 +134,7 @@ void createdb(std::string dbname) { std::cout << "Database created!" << std::endl; } -void opendb(std::string dbname) { +void opendb(std::string const &dbname) { std::fstream dbfile(dbname + DB_EXT, std::ios::in); if (!dbfile) { @@ -194,7 +196,8 @@ void show_tables(void) { return; } - tables_vector tables = get_tables(); + std::vector tables; + get_tables(tables); for (size_t i = 0; i < tables.size(); i++) { std::cout << tables[i]; @@ -215,7 +218,8 @@ void select_table(void) { std::cout << "Enter table name: "; getline(std::cin, table_name); - tables_vector tables = get_tables(); + std::vector tables; + get_tables(tables); if (std::find(tables.begin(), tables.end(), table_name) == tables.end()) { std::cout << "Table does not exist in database." << std::endl; @@ -226,9 +230,62 @@ void select_table(void) { currentdb->table = new std::string(table_name); } -tables_vector get_tables(void) { +void display_table(void) { + if(!currentdb) { + std::cout << "NO dB OPENED!!" << std::endl; + return; + } + + if (!currentdb->table) { + std::cout << "NO Table OPENED!!" << std::endl; + return; + } + + std::ifstream dbfile(currentdb->name + DB_EXT); + std::string str_line; + char line[100]; + + do { + dbfile.getline(line, 100); + + if (line[0] == '@') + str_line = line; + if (str_line.substr(1, str_line.size()-2) == *(currentdb->table)) + break; + } while(!dbfile.eof()); + + // Get the fields + std::vector fields; + dbfile.getline(line, 100); + str_line = line; + tokenize(str_line, ',', fields); + + for (int i = 0; i < fields.size(); i++) { + std::cout << fields[i]; + if (i != fields.size() - 1) + std::cout << "\t\t"; + } + std::cout << std::endl; + + // Get values + std::vector values; + do { + dbfile.getline(line, 100); + str_line = line; + tokenize(str_line, ',', values); + for (int i = 0; i < values.size(); i++) { + std::cout << values[i]; + if (i != values.size() - 1) + std::cout << "\t\t"; + } + std::cout << std::endl; + } while (str_line.find('}') == std::string::npos); + + dbfile.close(); +} + +void get_tables(std::vector &tables) { std::ifstream dbfile(currentdb->name + DB_EXT); - tables_vector tables; char line[100]; do { @@ -242,7 +299,6 @@ tables_vector get_tables(void) { } while (!dbfile.eof()); dbfile.close(); - return tables; } std::string get_prompt(void) { @@ -257,6 +313,18 @@ std::string get_prompt(void) { return prompt + PROMPT_SEP; } + +void tokenize(std::string const &str, const char delim, + std::vector &out) { + size_t start; + size_t end=0; + + out.clear(); + while ((start = str.find_first_not_of(delim, end)) != std::string::npos) { + end = str.find(delim, start); + out.push_back(str.substr(start, end - start)); + } +} void database(std::string db) { // Basically what's happening here: From bb08df2934bfc6ac2907fd8ebb568cc5c99b3ea2 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 13:01:58 +0530 Subject: [PATCH 15/25] Improve tokenize() --- SemiSQL.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index e78b5bd..d8affc3 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -8,6 +8,7 @@ const std::string DEFAULT_PROMPT_TEXT = "semi_sql"; const std::string DB_TABLE_SEP = "::"; const std::string PROMPT_SEP = "> "; const std::string DB_EXT = ".db"; +const std::string DELIMS = ",;}"; /* create database done & refactored @@ -33,7 +34,7 @@ void select_table(void); void display_table(void); void get_tables(std::vector&); std::string get_prompt(void); -void tokenize(std::string const&, const char, std::vector&); +void tokenize(std::string const&, std::string const&, std::vector&); void database(char*); void shreadtable(char*, char*); int linecr(char*, char*); @@ -248,19 +249,20 @@ void display_table(void) { do { dbfile.getline(line, 100); - if (line[0] == '@') + if (line[0] == '@') { str_line = line; if (str_line.substr(1, str_line.size()-2) == *(currentdb->table)) break; + } } while(!dbfile.eof()); // Get the fields std::vector fields; dbfile.getline(line, 100); str_line = line; - tokenize(str_line, ',', fields); + tokenize(str_line, DELIMS, fields); - for (int i = 0; i < fields.size(); i++) { + for (size_t i = 0; i < fields.size(); i++) { std::cout << fields[i]; if (i != fields.size() - 1) std::cout << "\t\t"; @@ -272,8 +274,8 @@ void display_table(void) { do { dbfile.getline(line, 100); str_line = line; - tokenize(str_line, ',', values); - for (int i = 0; i < values.size(); i++) { + tokenize(str_line, DELIMS, values); + for (size_t i = 0; i < values.size(); i++) { std::cout << values[i]; if (i != values.size() - 1) std::cout << "\t\t"; @@ -314,16 +316,20 @@ std::string get_prompt(void) { return prompt + PROMPT_SEP; } -void tokenize(std::string const &str, const char delim, +void tokenize(std::string const &str, std::string const &delims, std::vector &out) { - size_t start; - size_t end=0; + size_t start=0; + size_t end; out.clear(); - while ((start = str.find_first_not_of(delim, end)) != std::string::npos) { - end = str.find(delim, start); + while ((end = str.find_first_of(delims, start)) != std::string::npos) { out.push_back(str.substr(start, end - start)); + start = end + 1; } + + // push_back the last element iff it's not empty + if (start != str.size()) + out.push_back(str.substr(start, str.size() - start)); } void database(std::string db) { From 2b9dd2932444ae652bd5c558187301c498b29840 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 19:08:06 +0530 Subject: [PATCH 16/25] Add a print_row() for display_table() --- SemiSQL.cpp | 66 +++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index d8affc3..45d5dd5 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -32,6 +32,7 @@ void create_table(void); void show_tables(void); void select_table(void); void display_table(void); +void print_row(std::vector const&); void get_tables(std::vector&); std::string get_prompt(void); void tokenize(std::string const&, std::string const&, std::vector&); @@ -42,9 +43,7 @@ int linecr(char*, char*); struct db { std::string name; std::string* table; -}; - -struct db* currentdb; +} *currentdb; int main(void) { std::string a; @@ -243,61 +242,52 @@ void display_table(void) { } std::ifstream dbfile(currentdb->name + DB_EXT); - std::string str_line; - char line[100]; + std::string line; do { - dbfile.getline(line, 100); + getline(dbfile, line); if (line[0] == '@') { - str_line = line; - if (str_line.substr(1, str_line.size()-2) == *(currentdb->table)) + if (line.substr(1, line.size()-2) == *(currentdb->table)) break; } } while(!dbfile.eof()); - // Get the fields - std::vector fields; - dbfile.getline(line, 100); - str_line = line; - tokenize(str_line, DELIMS, fields); + std::vector fields, values; - for (size_t i = 0; i < fields.size(); i++) { - std::cout << fields[i]; - if (i != fields.size() - 1) - std::cout << "\t\t"; - } - std::cout << std::endl; + // Get the fields and print 'em + getline(dbfile, line); + tokenize(line, DELIMS, fields); + print_row(fields); - // Get values - std::vector values; + // Get values and print 'em do { - dbfile.getline(line, 100); - str_line = line; - tokenize(str_line, DELIMS, values); - for (size_t i = 0; i < values.size(); i++) { - std::cout << values[i]; - if (i != values.size() - 1) - std::cout << "\t\t"; - } - std::cout << std::endl; - } while (str_line.find('}') == std::string::npos); + getline(dbfile, line); + tokenize(line, DELIMS, values); + print_row(values); + } while (line.find('}') == std::string::npos); dbfile.close(); } +void print_row(std::vector const &cols) { + for (size_t i = 0; i < cols.size(); i++) { + std::cout << cols[i]; + if (i != cols.size() - 1) + std::cout << "\t\t"; + } + std::cout << std::endl; +} + void get_tables(std::vector &tables) { std::ifstream dbfile(currentdb->name + DB_EXT); - char line[100]; + std::string line; do { - dbfile.get(line, 100); - dbfile.get(); // eat trailing newline char + getline(dbfile, line); - if (line[0] == '@') { // @'s are table name identification markers - std::string table_name(line); - tables.push_back(table_name.substr(1, table_name.length()-2)); - } + if (line[0] == '@') // @'s are table name identification markers + tables.push_back(line.substr(1, line.length()-2)); } while (!dbfile.eof()); dbfile.close(); From 045eca5afa5cedd3423d997cc3db71cb254e435f Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 20:50:26 +0530 Subject: [PATCH 17/25] Refactor select_field() --- SemiSQL.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 45d5dd5..43efb7b 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -17,7 +17,7 @@ create table done & refactored show tables done & refactored select *table done & refactored display table done & refactored -select table done +select table done & reafctored drop table drop column funcs: add() @@ -32,6 +32,7 @@ void create_table(void); void show_tables(void); void select_table(void); void display_table(void); +void select_field(void); void print_row(std::vector const&); void get_tables(std::vector&); std::string get_prompt(void); @@ -59,7 +60,7 @@ int main(void) { << "* Type DPTABLE to display the table." << std::endl << "* Type SHOWTABLES to display the names of all the tables" << std::endl << "* Type SELECTTABLE to select a specific table." << std::endl - << "* Type SELECTCOLUMN to select a column name." << std::endl + << "* Type SELECTFIELD to select a field name." << std::endl << "* Type EXIT to exit." << std::endl << "********************************************************" << std::endl; @@ -108,9 +109,9 @@ int parse(std::string const &a) { } else if (a == "SELECTTABLE") { select_table(); - } else if (a == "SELECTCOLUMN") { - // input column_name; - // disp_column(column_name); + } else if (a == "SELECTFIELD") { + select_field(); + } else if (a == "EXIT") return 0; else @@ -237,13 +238,14 @@ void display_table(void) { } if (!currentdb->table) { - std::cout << "NO Table OPENED!!" << std::endl; + std::cout << "NO Table SELECTED!!" << std::endl; return; } std::ifstream dbfile(currentdb->name + DB_EXT); std::string line; + // Navigate to the current table do { getline(dbfile, line); @@ -270,6 +272,59 @@ void display_table(void) { dbfile.close(); } +void select_field(void) { + if(!currentdb) { + std::cout << "NO dB OPENED!!" << std::endl; + return; + } + + if (!currentdb->table) { + std::cout << "NO Table SELECTED!!" << std::endl; + return; + } + + std::ifstream dbfile(currentdb->name + DB_EXT); + std::string line, field_name; + + std::cout << "Enter field name: "; + getline(std::cin, field_name); + + // Navigate to the current table + do { + getline(dbfile, line); + + if (line[0] == '@') { + if (line.substr(1, line.size()-2) == *(currentdb->table)) + break; + } + } while(!dbfile.eof()); + + std::vector fields, values; + + // Get the fields + getline(dbfile, line); + tokenize(line, DELIMS, fields); + + // Get the index of `field_name` in `fields` + auto it = std::find(fields.begin(), fields.end(), field_name); + if (it == fields.end()) { + std::cout << "Field does not exist!" << std::endl; + return; + } + size_t index = it - fields.begin(); + + std::cout << fields[index] << std::endl; + + // Print values corresponding to `field_name` + do { + getline(dbfile, line); + tokenize(line, DELIMS, values); + std::cout << values[index] << std::endl; + } while (line.find('}') == std::string::npos); + + dbfile.close(); +} + void print_row(std::vector const &cols) { for (size_t i = 0; i < cols.size(); i++) { std::cout << cols[i]; From 9d4ec44ec1176df4522b1282ead81d01218113b6 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 20:54:07 +0530 Subject: [PATCH 18/25] Remove original author's code This project is mine now --- SemiSQL.cpp | 211 ---------------------------------------------------- 1 file changed, 211 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 43efb7b..0cf4b9e 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -376,214 +376,3 @@ void tokenize(std::string const &str, std::string const &delims, if (start != str.size()) out.push_back(str.substr(start, str.size() - start)); } -void database(std::string db) -{ - // Basically what's happening here: - // db = '@' + db + '@' - - char z[20]; - int j=strlen(db); - for (int k=0,a=k+1;kj || line[i] == ';') break; - if(k == j)std::cout << line[i]; - i++; - }i=0; - std::cout << "\n" ; - delay(2000); - } - file.close(); - gets(line); - syntax(line); -} -void linecr(char line[]) -{ - // Gets rid of first and last characters of line[]. - char trans[10]; - int n,i=0; - strcpy(trans,line); - n = strlen(trans); - trans[n-1] = '\0'; - while(trans[i] != '\0') - { - trans[i] = trans[i+1]; - i++; - } - strcpy(line,trans); -} -int linecr(char line[],char line2[]) -{ - // if (line.substr(1, line2.length()) == line2) - // return 1 - // else - // return 0 - int j=0,i=0; - while(line2[i] != '\0') - { j++; - i++; - if(line2[i] != line[i+1])goto fun; - } - fun: - if(j == strlen(line2))return 1; - else return 0; -} -void syntax(char a[]) -{ - // THIS IS HELL! - char syn[10],var[10],ch,line[50], line2[50]; - static char db[20],table[30]; - int i=0,k=0; - fstream infile,tout; - if(strcmp("CREATEDB",a) == 0) // done - { - std::cout << "\nCREATING" ; - std::cout << "\nEnter database name-\n"; - gets(db); - database(db); - puts(db); - infile.open(db,ios::out|ios::noreplace); - std::cout << "\nDatabase created!\n"; - gets(syn); - syntax(syn); - } - else if(strcmp("OPENDB",a) ==0) //done - { - std::cout << "\nEnter database name-\n "; - gets(db); - database(db); - tout.open(db,ios::in); - if(!tout) - { - std::cout << "\nDB not created!\n "; - } - else std::cout << "\nDatabase opened!\n"; - tout.close(); - gets(syn); - syntax(syn); - } - if(strcmp("CREATETABLE",a) ==0) - { - createtable(db); - } - if(strcmp("DPTABLE",a) == 0) - { - ifstream file(db); - if(!file) std::cout << "\nDB not entered!\n"; - while(1) - { - while(k != 1) - { - file.get(line,100); - file.get(ch); - if(line[0] == '@'){k=linecr(line,table);} - } - if(k==1 ) - { - file.get(line,100); - file.get(ch); - i=0; - while(line[i] != ';' ) - { - if(line[i] == ',') {std::cout << "\t\t"; i++;} - if(line[i] == '}') goto jojol; - std::cout << line[i]; - i++; - delay(100); - } - if(line[i] == ';')std::cout << ch; - delay(1000); - } - if(file.eof() ==1) break; - } - jojol: - gets(syn); - syntax(syn); - } - if(strcmp("SHOWTABLES",a) == 0) - { - ifstream tout(db,ios::nocreate); - tout.seekg(0); - while(1) - { - tout.get(var,100); // add variable - tout.get(ch); - if(var[0] == '@') - { linecr(var); - std::cout << var << ","; - } - if(tout.eof()) break; - } - tout.close(); - gets(syn); - syntax(syn); - } - if(strcmp("SELECTTABLE",a) == 0) - { - std::cout << "\nEnter table name-\n"; - gets(table); - gets(syn); - syntax(syn); - } - if(strcmp("SELECTCOLUMN",a) == 0) - { - shreadtable(db,table); //enter any column name and it will display the column name - } - if(strcmp("EXIT",a) == 0) exit(1); - else std::cout << "\t\t --WRONG SYNTAX!\n"; - gets(syn); - syntax(syn); -} From d995b7d5836e9e516b0496e4feda81974a8a732b Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 21:03:26 +0530 Subject: [PATCH 19/25] Add Makefile --- .gitignore | 1 + Makefile | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index e5d4e9e..761cbd4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.out *.o *.db +SemiSQL diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b9e087 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +CXXFLAGS=-Wall -Wextra -pedantic -std=c++11 + +all: SemiSQL.o + $(CXX) SemiSQL.o -o SemiSQL + +SemiSQL.o: SemiSQL.cpp + $(CXX) $(CXXFLAGS) -c SemiSQL.cpp From f42ad43d2ddcf2ba66bc4c33abd2dfafdb01ad64 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 1 Nov 2020 21:11:02 +0530 Subject: [PATCH 20/25] Add rule for `clean` in Makefile --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 0b9e087..e8beb6b 100644 --- a/Makefile +++ b/Makefile @@ -5,3 +5,6 @@ all: SemiSQL.o SemiSQL.o: SemiSQL.cpp $(CXX) $(CXXFLAGS) -c SemiSQL.cpp + +clean: + $(RM) SemiSQL *.o From 84dd3a0189957cef36489db81171f0a5381e3663 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 2 Nov 2020 10:41:27 +0530 Subject: [PATCH 21/25] Add functions for database and table select checks --- SemiSQL.cpp | 57 +++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 0cf4b9e..3baa884 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -35,11 +35,10 @@ void display_table(void); void select_field(void); void print_row(std::vector const&); void get_tables(std::vector&); +bool db_not_opened(void); +bool table_not_selected(void); std::string get_prompt(void); void tokenize(std::string const&, std::string const&, std::vector&); -void database(char*); -void shreadtable(char*, char*); -int linecr(char*, char*); struct db { std::string name; @@ -150,14 +149,11 @@ void opendb(std::string const &dbname) { } void create_table(void) { + if (db_not_opened()) return; + std::string table_name, cscols, csdata; int field_count; - if(!currentdb) { - std::cout << "NO dB OPENED!!" << std::endl; - return; - } - std::cout << "Enter table name: "; getline(std::cin, table_name); @@ -192,10 +188,7 @@ void create_table(void) { } void show_tables(void) { - if(!currentdb) { - std::cout << "NO dB OPENED!!" << std::endl; - return; - } + if (db_not_opened()) return; std::vector tables; get_tables(tables); @@ -209,10 +202,7 @@ void show_tables(void) { } void select_table(void) { - if(!currentdb) { - std::cout << "NO dB OPENED!!" << std::endl; - return; - } + if (db_not_opened()) return; std::string table_name; @@ -232,15 +222,7 @@ void select_table(void) { } void display_table(void) { - if(!currentdb) { - std::cout << "NO dB OPENED!!" << std::endl; - return; - } - - if (!currentdb->table) { - std::cout << "NO Table SELECTED!!" << std::endl; - return; - } + if (table_not_selected()) return; std::ifstream dbfile(currentdb->name + DB_EXT); std::string line; @@ -273,15 +255,7 @@ void display_table(void) { } void select_field(void) { - if(!currentdb) { - std::cout << "NO dB OPENED!!" << std::endl; - return; - } - - if (!currentdb->table) { - std::cout << "NO Table SELECTED!!" << std::endl; - return; - } + if (table_not_selected()) return; std::ifstream dbfile(currentdb->name + DB_EXT); std::string line, field_name; @@ -348,6 +322,21 @@ void get_tables(std::vector &tables) { dbfile.close(); } +bool db_not_opened(void) { + if (currentdb) return false; + std::cout << "NO dB OPENED!!" << std::endl; + return true; +} + +bool table_not_selected(void) { + if (!db_not_opened()) { + if (currentdb->table) + return false; + std::cout << "NO Table SELECTED!!" << std::endl; + } + return true; +} + std::string get_prompt(void) { std::string prompt; From f759adac62dedf53a175e34ec081a7d9e07c74d3 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 2 Nov 2020 10:47:28 +0530 Subject: [PATCH 22/25] Change get_prompt() to print_prompt() --- SemiSQL.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 3baa884..78a3d8e 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -37,7 +37,7 @@ void print_row(std::vector const&); void get_tables(std::vector&); bool db_not_opened(void); bool table_not_selected(void); -std::string get_prompt(void); +void print_prompt(void); void tokenize(std::string const&, std::string const&, std::vector&); struct db { @@ -64,7 +64,8 @@ int main(void) { << "********************************************************" << std::endl; do { - std::cout << std::endl << get_prompt(); + std::cout << std::endl; + print_prompt(); if (!getline(std::cin, a)) { std::cout << "EXIT" << std::endl; break; @@ -337,17 +338,15 @@ bool table_not_selected(void) { return true; } -std::string get_prompt(void) { - std::string prompt; - +void print_prompt(void) { if (currentdb) { - prompt = currentdb->name; + std::cout << currentdb->name; if (currentdb->table) - prompt += DB_TABLE_SEP + *(currentdb->table); + std::cout << DB_TABLE_SEP << *(currentdb->table); } else - prompt = DEFAULT_PROMPT_TEXT; + std::cout << DEFAULT_PROMPT_TEXT; - return prompt + PROMPT_SEP; + std::cout << PROMPT_SEP; } void tokenize(std::string const &str, std::string const &delims, From 669c0ad9a8269f77ceecad0b194c9eef590bd3a9 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 2 Nov 2020 11:01:02 +0530 Subject: [PATCH 23/25] Create a function for navigation to table for data in file --- SemiSQL.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 78a3d8e..33d0edd 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -33,6 +33,7 @@ void show_tables(void); void select_table(void); void display_table(void); void select_field(void); +void navigate_to_table(std::ifstream&, std::string const&); void print_row(std::vector const&); void get_tables(std::vector&); bool db_not_opened(void); @@ -228,15 +229,7 @@ void display_table(void) { std::ifstream dbfile(currentdb->name + DB_EXT); std::string line; - // Navigate to the current table - do { - getline(dbfile, line); - - if (line[0] == '@') { - if (line.substr(1, line.size()-2) == *(currentdb->table)) - break; - } - } while(!dbfile.eof()); + navigate_to_table(dbfile, *(currentdb->table)); std::vector fields, values; @@ -264,15 +257,7 @@ void select_field(void) { std::cout << "Enter field name: "; getline(std::cin, field_name); - // Navigate to the current table - do { - getline(dbfile, line); - - if (line[0] == '@') { - if (line.substr(1, line.size()-2) == *(currentdb->table)) - break; - } - } while(!dbfile.eof()); + navigate_to_table(dbfile, *(currentdb->table)); std::vector fields, values; @@ -300,6 +285,19 @@ void select_field(void) { dbfile.close(); } +void navigate_to_table(std::ifstream &file, std::string const &table_name) { + std::string line; + + do { + getline(file, line); + + if (line[0] == '@') { + if (line.substr(1, line.size()-2) == table_name) + break; + } + } while(!file.eof()); +} + void print_row(std::vector const &cols) { for (size_t i = 0; i < cols.size(); i++) { std::cout << cols[i]; From 0cf88dfab00b638ccfadd11ee7728b159be746ca Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 2 Nov 2020 11:15:27 +0530 Subject: [PATCH 24/25] Update README --- README.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 807e122..915122e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,32 @@ -# Semi_SQL +# SemiSQL This code basically mimics a SQL DBMS using C++ in a CLI approach. As of now understand the working of the code by yourself detailed working will be published later with better updates. + +This project is in active development and many of the features aren't implemented. Use at your own risk. + +## Plan +### Operations +- [x] Create Database +- [x] Open Database +- [x] Create Table +- [x] Show Tables +- [x] Select Table +- [x] Display Table +- [x] Select specific Field of Table +- [ ] Drop Table +- [ ] Drop Column +### Functions +- [ ] ADD() +- [ ] AVERAGE() + +## Dependencies +None, use any C++ 11 compatible compiler. + +## Building +```sh +$ make +``` + +Developed and tested under Linux environment. From 4314cb48f61a672eb078fba18e2d645752e57c48 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Mon, 2 Nov 2020 11:34:41 +0530 Subject: [PATCH 25/25] Remove Plan comment from code --- SemiSQL.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/SemiSQL.cpp b/SemiSQL.cpp index 33d0edd..fcd4de6 100644 --- a/SemiSQL.cpp +++ b/SemiSQL.cpp @@ -10,21 +10,6 @@ const std::string PROMPT_SEP = "> "; const std::string DB_EXT = ".db"; const std::string DELIMS = ",;}"; -/* -create database done & refactored -open database done & refactored -create table done & refactored -show tables done & refactored -select *table done & refactored -display table done & refactored -select table done & reafctored -drop table -drop column -funcs: add() - average() - (?) percentage() -*/ - int parse(std::string const&); void createdb(std::string const&); void opendb(std::string const&);