· 10 years ago · Feb 04, 2016, 09:39 PM
1#include <iostream>
2#include <vector>
3#include <stdio.h>
4#include <string>
5#include <stdlib.h>
6#include <fstream>
7#include <conio.h>
8#define OTL_STL
9#define OTL_ODBC_MYSQL
10#define OTL_ODBC // Compile OTL 4.0/ODBC
11#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
12// The following #define is required with MyODBC 3.51.11 and higher
13#include "otlv4.h" // include the OTL 4.0 header file
14#include <SDL/SDL.h>//Graphics,audio,input library (Audio required)
15#include <SDL/SDL_mixer.h>//Required to ease the audio code
16
17//Create a database object
18otl_connect db;
19std::ifstream fstream_IN;
20std::ofstream fstream_OUT;
21
22//Create an audio object
23Mix_Music* g_music;
24
25
26std::vector<std::string> v_DatabaseLabels;
27
28std::string connectionString;
29const std::string defaultPath = "defaultmusic.mp3";
30std::string filePath;
31std::string oldPath;
32char path;
33
34//Used for music settings (If the music path is set or not, and if it could find a path)
35enum TPathType {
36
37 PATH_NOT_SET = -1,
38 PATH_FAILURE = 0,
39 PATH_SUCCESS = 1
40
41};
42
43
44
45//Global volume variable
46short g_defaultVolume;
47//line = "---------------------------------------------------------";
48
49//Connection string is stored here
50struct Connection
51{
52 std::string label = "DefaultDatabaseLabel"; //Default value = DefaultDatabaseLabel
53 std::string driver = "MySQL ODBC 5.3 ANSI Driver"; //Default value = MySQL ODBC 5.3 ANSI Driver
54 std::string server = "localhost"; //Default value = localhost
55 std::string port = "3306"; //Default value = 3306
56 std::string database = "test"; //Default value = test
57 std::string user = "root"; //Default value = root
58 std::string pass;
59//defaultCONNECTION variables are used if user did not enter a value into the fields and then transfered to currentCONNECTION
60}defaultCONNECTION, currentCONNECTION;
61
62
63#pragma region FUNCTIONPROTOTYPING
64void saveDatabaseLabel();
65void deleteDatabaseLabel();
66void listDatabaseStatus();
67
68bool connectToDatabase(std::string &p_label);
69
70void mainMenu();
71void musicSettings();
72
73void tableMenu();
74void readDataMenu();
75void writeDataMenu();
76void deleteDataMenu();
77
78void showDatabaseLabels();
79
80void createTable(std::string &p_table, std::string &p_attributes);
81void deleteTable(std::string &p_table);
82
83void selectAll(std::string &p_table);
84void selectIndex(std::string &p_select, std::string &p_table);
85void selectCustom(std::string &p_SQL);
86
87void insertAll(std::string &p_table, std::string &p_values);
88void insertIndex(std::string &p_table, std::string &p_columns, std::string &p_values);
89void insertCustom(std::string &p_SQL);
90
91void deleteIndex(std::string &p_table, std::string &p_columns, std::string &p_values);
92void deleteCustom(std::string &p_SQL);
93
94void stringLoop(std::string string, int milliseconds);
95
96void stopMusic(Mix_Music* p_music);
97bool startMusic(Mix_Music* p_music, std::string p_musicfile);
98#pragma endregion
99
100
101//Application entry point
102int main(int argc, char** argv)
103{
104
105 //Custom variable type to check if path is set or not for music file
106 path = TPathType::PATH_NOT_SET;
107
108
109 filePath = defaultPath;
110 g_defaultVolume = 10;
111
112 //Initialize audio
113 SDL_Init(SDL_INIT_AUDIO);
114
115 //Custom function, play selected music, startMusic(music object, path for music file)
116 startMusic(g_music, defaultPath.c_str());
117
118 //Console window size
119 COORD coord;
120 coord.X = 81;
121 coord.Y = 20000;
122
123 //Adjust buffer size
124 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
125 if (!SetConsoleScreenBufferSize(handle, coord))
126 throw std::runtime_error("Unable to resize screen buffer.");
127
128 //Initialize OTL (database library)
129 otl_connect::otl_initialize();
130
131
132 //Custom function
133 mainMenu();
134
135 //Custom function, stop selected music object
136 stopMusic(g_music);
137}
138
139
140#pragma region FUNCTIONS
141void saveDatabaseLabel()
142{
143 std::string choice;
144 std::string line;
145
146 bool bSaved = false;
147 bool bNameExists = false;
148
149
150 //Open alldatabases.txt and put all the labels in a vector
151 fstream_IN.open("alldatabases.txt");
152 while (getline(fstream_IN, line))
153 {
154 v_DatabaseLabels.push_back(line);
155 }
156 fstream_IN.close();
157
158 //Create database label
159 do
160 {
161 std::cout << "---------------------------------CREATING LABEL---------------------------------" << std::endl << std::endl;
162 std::cout << "Tip: Leave the field empty for default value (Press ENTER for default value)" << std::endl;
163 std::cout << "--------------------------------------------------------------------------------" << std::endl;
164
165 //Ask for a label name until the label name doesn't exist
166 do{
167 std::cout << "Enter a label for your database (Default: DefaultDatabaseLabel): ";
168 getline(std::cin, currentCONNECTION.label);
169
170 if (currentCONNECTION.label == "")
171 {
172 currentCONNECTION.label = defaultCONNECTION.label;
173 }
174
175
176 if (v_DatabaseLabels.size() > 0)
177 {
178 for (int i = 0; i < v_DatabaseLabels.size(); i++)
179 {
180 if (v_DatabaseLabels.at(i) == currentCONNECTION.label)
181 {
182 bNameExists = true;
183 std::cout << "The name " << currentCONNECTION.label << " already exists! Try another name..." << std::endl << std::endl;
184
185 }
186 else
187 {
188 bNameExists = false;
189 }
190
191 }
192
193 }
194
195 } while (bNameExists);
196
197
198 std::cout << "Enter the driver of your database (Default: MySQL ODBC 5.3 ANSI Driver): ";
199 getline(std::cin, currentCONNECTION.driver);
200 std::cout << "Enter the IP (Default: localhost): ";
201 getline(std::cin, currentCONNECTION.server);
202 std::cout << "Enter the port (Default: 3306): ";
203 getline(std::cin, currentCONNECTION.port);
204 std::cout << "Enter the database name (Default: test): ";
205 getline(std::cin, currentCONNECTION.database);
206 std::cout << "Enter the username (Default: root): ";
207 getline(std::cin, currentCONNECTION.user);
208 std::cout << "Enter the password: ";
209 getline(std::cin, currentCONNECTION.pass);
210 std::cout << "----------------------------------------------------------------------------" << std::endl;
211
212 v_DatabaseLabels.clear();
213
214
215 if (currentCONNECTION.driver == "")
216 {
217 currentCONNECTION.driver = defaultCONNECTION.driver;
218 }
219 if (currentCONNECTION.server == "")
220 {
221 currentCONNECTION.server = defaultCONNECTION.server;
222 }
223 if (currentCONNECTION.port == "")
224 {
225 currentCONNECTION.port = defaultCONNECTION.port;
226 }
227 if (currentCONNECTION.database == "")
228 {
229 currentCONNECTION.database = defaultCONNECTION.database;
230 }
231 if (currentCONNECTION.user == "")
232 {
233 currentCONNECTION.user = defaultCONNECTION.user;
234 }
235
236
237 connectionString = "DRIVER={" + currentCONNECTION.driver + "};SERVER=" + currentCONNECTION.server + ";PORT=" + currentCONNECTION.port + ";DATABASE=" + currentCONNECTION.database + ";USER=" + currentCONNECTION.user + ";PASSWORD=" + currentCONNECTION.pass + ";";
238
239 //Loop through the connection string to make a small effect
240 for (int i = 0; i < connectionString.length(); i++)
241 {
242 std::cout << connectionString.at(i);
243 Sleep(30);
244
245 //Everytime the current index of the string is ; start writing on next line
246 if (connectionString.at(i) == ';')
247 {
248 std::cout << "\n";
249 }
250 }
251 std::cout << std::endl << "Save? (Y or N) type 'cancel' to return to main menu: ";
252 do
253 {
254
255 getline(std::cin, choice);
256
257 if (choice == "N" || choice == "n")
258 {
259 bSaved = false;
260 system("cls");
261 }
262 else if (choice == "Y" || choice == "y")
263 {
264 bSaved = true;
265 system("cls");
266 }
267 else if (choice == "cancel")
268 {
269
270 }
271 else
272 {
273 std::cout << std::endl << "Enter Y or N..." << std::endl;
274 }
275 } while (choice != "Y" && choice != "y" && choice != "N" && choice != "n");
276
277 } while (!bSaved);
278
279 //Write the database login information to a textfile
280 fstream_OUT.open("databaselogin.txt", std::ios::app);
281 fstream_OUT << currentCONNECTION.label << std::endl << currentCONNECTION.driver << std::endl << currentCONNECTION.server << std::endl << currentCONNECTION.port << std::endl << currentCONNECTION.database << std::endl << currentCONNECTION.user << std::endl << currentCONNECTION.pass << std::endl << "---------------------------------------------------------" << std::endl;
282 fstream_OUT.close();
283
284 //Write the name of the database to a different textfile
285 fstream_OUT.open("alldatabases.txt", std::ios::app);
286 fstream_OUT << currentCONNECTION.label << std::endl;
287 fstream_OUT.close();
288
289
290 mainMenu();
291
292}
293
294void deleteDatabaseLabel()
295{
296
297 std::string line;
298
299 fstream_IN.open("alldatabases.txt");
300 while (getline(fstream_IN, line))
301 {
302 v_DatabaseLabels.push_back(line);
303 }
304 fstream_IN.close();
305
306
307 if (v_DatabaseLabels.size() > 0)
308 {
309
310 std::string choice;
311
312 std::ofstream temp;
313
314 bool bNameExists = false;
315
316
317 std::cout << "---------------------------------DELETING LABEL---------------------------------" << std::endl << std::endl;
318 std::cout << "Enter the name of the label you wish to delete" << std::endl;
319 std::cout << "Type cancel to return to main menu" << std::endl;
320 std::cout << "--------------------------------------------------------------------------------" << std::endl;
321 fstream_IN.open("alldatabases.txt");
322 while (getline(fstream_IN, line))
323 {
324 std::cout << line << std::endl;
325 }
326 fstream_IN.close();
327 std::cout << "--------------------------------------------------------------------------------" << std::endl;
328
329 do{
330 getline(std::cin, choice);
331
332
333 if (choice == "cancel")
334 {
335 system("cls");
336 v_DatabaseLabels.clear();
337 mainMenu();
338 }
339
340
341 if (v_DatabaseLabels.size() > 0)
342 {
343 for (int i = 0; i < v_DatabaseLabels.size(); i++)
344 {
345 if (choice != v_DatabaseLabels.at(i))
346 {
347 bNameExists = false;
348 }
349 else
350 {
351 bNameExists = true;
352 break;
353 }
354 }
355 }
356
357 if (!bNameExists)
358 {
359 std::cout << std::endl << "The database " << choice << " does not exist! Try again..." << std::endl << std::endl;
360
361 }
362
363
364 } while (!bNameExists);
365
366
367
368
369 fstream_IN.open("databaselogin.txt");
370 temp.open("temp.txt");
371
372 while (getline(fstream_IN, line))
373 {
374 if (line != choice)
375 {
376 temp << line << std::endl;
377 }
378 else if (line == choice)
379 {
380 getline(fstream_IN, line);
381 getline(fstream_IN, line);
382 getline(fstream_IN, line);
383 getline(fstream_IN, line);
384 getline(fstream_IN, line);
385 getline(fstream_IN, line);
386 getline(fstream_IN, line);
387 }
388
389 }
390
391
392 fstream_IN.close();
393 temp.close();
394
395
396 if (remove("databaselogin.txt") != 0)
397 perror("Could not edit file!");
398
399 rename("temp.txt", "databaselogin.txt");
400
401 fstream_IN.open("alldatabases.txt");
402 temp.open("temp.txt");
403
404 while (getline(fstream_IN, line))
405 {
406 if (line != choice)
407 {
408 temp << line << std::endl;
409 }
410 }
411
412 fstream_IN.close();
413 temp.close();
414
415 if (remove("alldatabases.txt") != 0)
416 perror("Could not edit file/remove database!\n");
417 else
418 {
419 std::cout << "Database " << choice << " successfully removed!" << std::endl << std::endl;
420 }
421
422 rename("temp.txt", "alldatabases.txt");
423
424 system("PAUSE");
425 system("cls");
426
427
428 }
429 else
430 {
431 std::cout << "Theres no databases saved! Go create one." << std::endl;
432 system("PAUSE");
433 system("cls");
434 }
435
436 v_DatabaseLabels.clear();
437 mainMenu();
438
439}
440
441void listDatabaseStatus()
442{
443 std::string line;
444 Connection* tempConn = new Connection;
445
446 fstream_IN.open("alldatabases.txt");
447 while (getline(fstream_IN, line))
448 {
449 v_DatabaseLabels.push_back(line);
450 }
451 fstream_IN.close();
452
453
454
455
456 if (v_DatabaseLabels.size() > 0)
457 {
458
459
460 std::string tempConnectionString;
461 std::string tempString;
462
463
464 std::cout << "-----------------------------------DATABASES-----------------------------------" << std::endl << std::endl;;
465 std::cout << "LABEL IP-ADRESS STATUS" << std::endl;
466 std::cout << "===============================================================================" << std::endl;
467 //Loop through all databases and print the LABEL,IP and STATUS (ONLINE or OFFLINE)
468 fstream_IN.open("databaselogin.txt");
469 while (getline(fstream_IN, line))
470 {
471 if (v_DatabaseLabels.size() > 0)
472 {
473
474 for (int i = 0; i < v_DatabaseLabels.size(); i++)
475 {
476
477
478 if (v_DatabaseLabels.at(i) == line)
479 {
480
481 getline(fstream_IN, tempConn->driver);
482 getline(fstream_IN, tempConn->server);
483 getline(fstream_IN, tempConn->port);
484 getline(fstream_IN, tempConn->database);
485 getline(fstream_IN, tempConn->user);
486 getline(fstream_IN, tempConn->pass);
487
488
489 tempConnectionString = "DRIVER={" + tempConn->driver + "};SERVER=" + tempConn->server + ";PORT=" + tempConn->port + ";DATABASE=" + tempConn->database + ";USER=" + tempConn->user + ";PASSWORD=" + tempConn->pass + ";";
490 try
491 {
492
493
494 std::cout << v_DatabaseLabels.at(i);
495
496 tempString = v_DatabaseLabels.at(i);
497 for (int i = 0; i < 35 - tempString.length(); i++)
498 {
499 std::cout << " ";
500 }
501
502 std::cout << tempConn->server;
503
504 for (int i = 0; i < 37 - tempConn->server.length(); i++)
505 {
506 std::cout << " ";
507 }
508
509 db.rlogon(tempConnectionString.c_str());
510 std::cout << "ONLINE" << std::endl;
511 }
512 catch (otl_exception &e)
513 {
514 std::cout << "OFFLINE" << std::endl;
515 }
516
517
518 db.logoff();
519
520 }
521 }
522 }
523
524
525 }
526 }
527 else
528 {
529 std::cout << "-----------------------------------DATABASES-----------------------------------" << std::endl << std::endl;;
530 std::cout << "LABEL IP-ADRESS STATUS" << std::endl;
531 std::cout << "===============================================================================" << std::endl;
532 std::cout << "No databases found! Go create one." << std::endl;
533 }
534
535
536
537 fstream_IN.close();
538
539 std::cout << std::endl;
540 if (v_DatabaseLabels.size() > 0)
541 {
542 system("PAUSE");
543 }
544 else
545 {
546 Sleep(2000);
547 }
548 delete tempConn;
549 v_DatabaseLabels.clear();
550 system("cls");
551 mainMenu();
552}
553
554bool connectToDatabase(std::string &p_label)
555{
556
557 std::string line;
558 std::string tempConnectionString;
559
560 bool bNameExists = false;
561
562
563
564 Connection* tempConn = new Connection;
565
566 fstream_IN.open("databaselogin.txt");
567 while (getline(fstream_IN, line))
568 {
569 if (line == p_label)
570 {
571 getline(fstream_IN, tempConn->driver);
572 getline(fstream_IN, tempConn->server);
573 getline(fstream_IN, tempConn->port);
574 getline(fstream_IN, tempConn->database);
575 getline(fstream_IN, tempConn->user);
576 getline(fstream_IN, tempConn->pass);
577 }
578 }
579 fstream_IN.close();
580
581
582 tempConnectionString = "DRIVER={" + tempConn->driver + "};SERVER=" + tempConn->server + ";PORT=" + tempConn->port + ";DATABASE=" + tempConn->database + ";USER=" + tempConn->user + ";PASSWORD=" + tempConn->pass + ";";
583 delete tempConn;
584 try
585 {
586 db.rlogon(tempConnectionString.c_str());
587 return true;
588
589 }
590 catch (otl_exception &e)
591 {
592
593 v_DatabaseLabels.clear();
594 system("cls");
595 return false;
596 }
597}
598
599void mainMenu()
600{
601
602 std::string choice;
603
604 std::cout << "====================================WELCOME=====================================" << std::endl << std::endl;
605 std::cout << "Enter the number of the option you wish to proceed with" << std::endl;
606 std::cout << "NOTE: MySQL experience might be required!" << std::endl;
607 std::cout << "----------------------------------MAIN OPTIONS----------------------------------" << std::endl;
608 std::cout << "1. Save database login information" << std::endl;
609 std::cout << "2. Delete database login information" << std::endl;
610 std::cout << "3. List database status" << std::endl;
611 std::cout << "----------------------------------EDIT OPTIONS----------------------------------" << std::endl;
612 std::cout << "4. Create/Delete table 6. Write data to database" << std::endl;
613 std::cout << "5. Read data from database 7. Delete data from database" << std::endl;
614 std::cout << "-------------------------------------OTHER--------------------------------------" << std::endl;
615 std::cout << "P. Pause/Play music S. Music Settings" << std::endl;
616
617 std::cout << "Q. Quit" << std::endl;
618 std::cout << "--------------------------------------------------------------------------------" << std::endl;
619
620 getline(std::cin, choice);
621
622
623
624 if (choice == "1")
625 {
626 system("cls");
627 saveDatabaseLabel();
628 }
629 else if (choice == "2")
630 {
631 system("cls");
632 deleteDatabaseLabel();
633 }
634 else if (choice == "3")
635 {
636 system("cls");
637 listDatabaseStatus();
638 }
639 else if (choice == "4")
640 {
641 system("cls");
642 tableMenu();
643 }
644 else if (choice == "5")
645 {
646 system("cls");
647 readDataMenu();
648 }
649 else if (choice == "6")
650 {
651 system("cls");
652 writeDataMenu();
653 }
654 else if (choice == "7")
655 {
656 system("cls");
657 deleteDataMenu();
658 }
659 else if (choice == "P" || choice == "p")
660 {
661 if (Mix_PausedMusic() == 0)
662 Mix_PauseMusic();
663 else
664 Mix_ResumeMusic();
665
666 system("cls");
667 mainMenu();
668 }
669 else if (choice == "S" || choice == "s")
670 {
671
672 system("cls");
673 musicSettings();
674 }
675 else if (choice == "Q" || choice == "q")
676 {
677
678 }
679 else
680 {
681 std::cout << choice << " is an invalid option! Try again..." << std::endl;
682 Sleep(1000);
683 system("cls");
684 mainMenu();
685
686 }
687}
688void musicSettings()
689{
690
691
692 std::cout << "---------------------------------MUSIC SETTINGS---------------------------------" << std::endl;
693 std::cout << "Press + to increase the volume, Press - to decrease the volume." << std::endl;
694 std::cout << "Press ENTER to choose a music file (including path).\nSYNTAX: C:\\Users\\Music\\song.mp3" << std::endl;
695 std::cout << "Press ESC to return to main menu." << std::endl << std::endl;
696 std::cout << "Currently playing: " << filePath << std::endl;
697 std::cout << "--------------------------------------------------------------------------------" << std::endl;
698
699 if (path == TPathType::PATH_NOT_SET)
700 {
701
702 }
703 else if (path == TPathType::PATH_SUCCESS)
704 {
705 std::cout << "Successfully found " << filePath << std::endl;
706 path = TPathType::PATH_NOT_SET;
707 }
708 else if (path == TPathType::PATH_FAILURE)
709 {
710 std::cout << "Could not find " << oldPath << std::endl;
711 oldPath = TPathType::PATH_NOT_SET;
712 }
713
714 int c;
715
716 do
717 {
718 c = getch();
719
720 switch (c)
721 {
722 case '+':
723
724 if (g_defaultVolume < 128)
725 {
726 Mix_VolumeMusic(g_defaultVolume++);
727 std::cout << "Volume: " << g_defaultVolume << std::endl;
728 }
729 else
730 std::cout << "Volume cannot be greater than 128" << std::endl;
731 break;
732
733 case '-':
734 if (g_defaultVolume > 0)
735 {
736 Mix_VolumeMusic(g_defaultVolume--);
737 std::cout << "Volume: " << g_defaultVolume << std::endl;
738 }
739 else
740 std::cout << "Volume cannot be less than 0" << std::endl;
741 break;
742 case VK_RETURN:
743 std::cout << "PATH: ";
744 getline(std::cin, filePath);
745
746
747 stopMusic(g_music);
748
749 if (startMusic(g_music, filePath.c_str()))
750 {
751 path = TPathType::PATH_SUCCESS;
752 system("cls");
753 musicSettings();
754
755 }
756 else
757 {
758 path = TPathType::PATH_FAILURE;
759 oldPath = filePath;
760 filePath = "defaultmusic.mp3";
761
762 startMusic(g_music, defaultPath.c_str());
763
764 system("cls");
765 musicSettings();
766 }
767
768
769 break;
770 case VK_ESCAPE:
771 break;
772 case VK_SPACE:
773 std::cout << "SPACE OR BACKPSACE" << std::endl;
774 break;
775 default:
776 std::cout << "Press + to increase volume, Press - to decrease volume" << std::endl;
777 break;
778 }
779 } while (c != VK_ESCAPE);
780 system("cls");
781 mainMenu();
782
783}
784
785void tableMenu()
786{
787 bool bNameExists = false;
788
789 std::string line;
790 std::string choice;
791 std::string tempConnectionString;
792
793 fstream_IN.open("alldatabases.txt");
794 while (getline(fstream_IN, line))
795 {
796 v_DatabaseLabels.push_back(line);
797 }
798 fstream_IN.close();
799
800 if (v_DatabaseLabels.size() > 0)
801 {
802
803 std::cout << "------------------------------CREATE/DELETE TABLE----------------------------" << std::endl;
804 std::cout << "Choose a database to create/delete a table from" << std::endl;
805 std::cout << "Type cancel to return to main menu" << std::endl;
806 std::cout << "--------------------------------------------------------------------------------" << std::endl;
807 showDatabaseLabels();
808 std::cout << "--------------------------------------------------------------------------------" << std::endl;
809
810 do
811 {
812 getline(std::cin, choice);
813
814 if (choice == "cancel")
815 {
816 v_DatabaseLabels.clear();
817 system("cls");
818 mainMenu();
819 }
820
821 for (int i = 0; i < v_DatabaseLabels.size(); i++)
822 {
823 if (choice == v_DatabaseLabels.at(i))
824 {
825 bNameExists = true;
826 }
827 }
828
829 if (!bNameExists)
830 {
831 std::cout << "The database " << choice << " does not exist! Try again..." << std::endl;
832 }
833 } while (!bNameExists);
834
835 if (connectToDatabase(choice))
836 {
837 do
838 {
839
840 system("cls");
841
842 std::cout << "-----------------------------CONNECTED TO DATABASE------------------------------" << std::endl;
843 std::cout << "Enter the option you wish to proceed with!" << std::endl;
844 std::cout << "--------------------------------------------------------------------------------" << std::endl;
845 std::cout << "1. Create table" << std::endl;
846 std::cout << "2. Delete table" << std::endl;
847 std::cout << "3. Back to main menu" << std::endl;
848 std::cout << "--------------------------------------------------------------------------------" << std::endl;
849
850 getline(std::cin, choice);
851
852 if (choice == "1")
853 {
854 system("cls");
855 std::string tableName;
856 std::string columnChoice;
857
858 std::cout << "----------------------------------CREATE TABLE----------------------------------" << std::endl << std::endl;
859 std::cout << "CREATE SYNTAX" << std::endl;
860 std::cout << "Create string: CREATE TABLE table_name" << std::endl;
861 std::cout << "Attribute(s) string:" << std::endl;
862 std::cout << "Primary key example: ID INT(10) AUTO_INCREMENT PRIMARY KEY," << std::endl;
863 std::cout << "attribute_name variable_type(variable_max_size) optional_parameters," << std::endl;
864 std::cout << "Example: FirstName VARCHAR(30) NOT NULL," << std::endl;
865 std::cout << "--------------------------------------------------------------------------------" << std::endl;
866
867 std::cout << "Enter the name of your table(s): ";
868 getline(std::cin, tableName);
869 std::cout << "Enter the attribute(s) (All in same string): ";
870 getline(std::cin, columnChoice);
871
872 createTable(tableName, columnChoice);
873
874
875
876 }
877 else if (choice == "2")
878 {
879 system("cls");
880 std::string tableName;
881
882 std::cout << "----------------------------------DELETE TABLE----------------------------------" << std::endl << std::endl;
883 std::cout << "CREATE SYNTAX" << std::endl;
884 std::cout << "Create string: CREATE TABLE table_name" << std::endl;
885 std::cout << "Attribute(s) string:" << std::endl;
886 std::cout << "Primary key example: ID INT(10) AUTO_INCREMENT PRIMARY KEY," << std::endl;
887 std::cout << "attribute_name variable_type(variable_max_size) optional_parameters," << std::endl;
888 std::cout << "Example: FirstName VARCHAR(30) NOT NULL," << std::endl;
889 std::cout << "--------------------------------------------------------------------------------" << std::endl;
890
891 std::cout << "Enter the name of your table(s): ";
892 getline(std::cin, tableName);
893
894 deleteTable(tableName);
895 }
896 else if (choice == "3")
897 {
898
899
900
901
902 }
903 else
904 {
905 std::cout << choice << " is not a valid option! Try again..." << std::endl;
906 Sleep(2000);
907 }
908 } while (choice != "3");
909
910 }
911 else
912 {
913 std::cout << "Could not connect do database!" << std::endl;
914 Sleep(2000);
915 }
916
917 }
918 else
919 {
920 std::cout << "------------------------------CREATE/DELETE TABLE----------------------------" << std::endl;
921 std::cout << "Choose a database to create/delete a table from" << std::endl;
922 std::cout << "Type cancel to return to main menu" << std::endl;
923 std::cout << "--------------------------------------------------------------------------------" << std::endl;
924 std::cout << "No databases found! Go create one." << std::endl;
925 std::cout << "----------------------------------------------------------------------------" << std::endl;
926
927 Sleep(2000);
928
929 }
930
931 system("cls");
932 db.logoff();
933 mainMenu();
934}
935void readDataMenu()
936{
937 bool bNameExists = false;
938
939 std::string line;
940 std::string choice;
941 std::string tempConnectionString;
942
943 fstream_IN.open("alldatabases.txt");
944 while (getline(fstream_IN, line))
945 {
946 v_DatabaseLabels.push_back(line);
947 }
948 fstream_IN.close();
949
950 if (v_DatabaseLabels.size() > 0)
951 {
952
953
954 std::cout << "-----------------------------------READ DATA------------------------------------" << std::endl;
955 std::cout << "Choose a database to read data from" << std::endl;
956 std::cout << "Type cancel to return to main menu" << std::endl;
957 std::cout << "--------------------------------------------------------------------------------" << std::endl;
958 showDatabaseLabels();
959 std::cout << "--------------------------------------------------------------------------------" << std::endl;
960
961
962
963
964 do
965 {
966 getline(std::cin, choice);
967
968 if (choice == "cancel")
969 {
970 v_DatabaseLabels.clear();
971 system("cls");
972 mainMenu();
973 }
974
975 for (int i = 0; i < v_DatabaseLabels.size(); i++)
976 {
977 if (choice == v_DatabaseLabels.at(i))
978 {
979 bNameExists = true;
980 }
981 }
982
983 if (!bNameExists)
984 {
985 std::cout << "The database " << choice << " does not exist! Try again..." << std::endl;
986 }
987 } while (!bNameExists);
988
989 if (connectToDatabase(choice))
990 {
991 do
992 {
993
994 system("cls");
995
996 std::cout << "-----------------------------CONNECTED TO DATABASE------------------------------" << std::endl;
997 std::cout << "Enter the option you wish to proceed with!" << std::endl;
998 std::cout << "--------------------------------------------------------------------------------" << std::endl;
999 std::cout << "1. Read everything from chosen table(s)" << std::endl;
1000 std::cout << "2. Read chosen attributes from chosen table(s)" << std::endl;
1001 std::cout << "3. Custom SQL statement" << std::endl;
1002 std::cout << "4. Back to main menu" << std::endl;
1003 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1004
1005 getline(std::cin, choice);
1006
1007 if (choice == "1")
1008 {
1009 system("cls");
1010 std::string tableChoice;
1011
1012
1013 std::cout << "------------------------------------READ ALL------------------------------------" << std::endl << std::endl;
1014 std::cout << "SELECT SYNTAX" << std::endl;
1015 std::cout << "Select string: SELECT * FROM persons" << std::endl;
1016 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1017
1018 std::cout << "Enter the name of your desired table: FROM ";
1019 getline(std::cin, tableChoice);
1020
1021 selectAll(tableChoice);
1022
1023
1024
1025 }
1026 else if (choice == "2")
1027 {
1028 system("cls");
1029 std::string selectChoice;
1030 std::string tableChoice;
1031
1032
1033 std::cout << "-----------------------------READ CHOSEN ATTRIBUTES-----------------------------" << std::endl << std::endl;
1034 std::cout << "SELECT SYNTAX" << std::endl;
1035 std::cout << "Select string: SELECT FirstName, LastName" << std::endl;
1036 std::cout << "From string: persons" << std::endl;;
1037 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1038
1039 std::cout << "Enter an or many attribute(s) or * to select everything: SELECT ";
1040 getline(std::cin, selectChoice);
1041 std::cout << "Enter the name of your desired table(s): FROM ";
1042 getline(std::cin, tableChoice);
1043
1044
1045 selectIndex(selectChoice, tableChoice);
1046 }
1047 else if (choice == "3")
1048 {
1049 system("cls");
1050 std::string SQLStatement;
1051
1052
1053 std::cout << "------------------------------CUSTOM SQL STATEMENT------------------------------" << std::endl << std::endl;
1054 std::cout << "NOTE: Expect unorganized text if multiple tables are chosen!" << std::endl;
1055 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1056
1057 std::cout << "Enter a SQL statement: SELECT ";
1058 getline(std::cin, SQLStatement);
1059
1060 selectCustom(SQLStatement);
1061
1062
1063
1064 }
1065 else if (choice == "4")
1066 {
1067
1068 }
1069 else
1070 {
1071 std::cout << choice << " is not a valid option! Try again..." << std::endl;
1072 Sleep(2000);
1073 }
1074 } while (choice != "4");
1075 }
1076 else
1077 {
1078 std::cout << "Could not connect do database!" << std::endl;
1079 Sleep(2000);
1080 }
1081
1082
1083 }
1084 else
1085 {
1086 std::cout << "----------------------------------READ DATA---------------------------------" << std::endl << std::endl;
1087 std::cout << "Choose a database to read data from" << std::endl;
1088 std::cout << "Type cancel to return to main menu" << std::endl;
1089 std::cout << "----------------------------------------------------------------------------" << std::endl;
1090 std::cout << "No databases found! Go create one." << std::endl;
1091 std::cout << "----------------------------------------------------------------------------" << std::endl;
1092
1093 Sleep(2000);
1094
1095 }
1096
1097 v_DatabaseLabels.clear();
1098
1099 system("cls");
1100 db.logoff();
1101 mainMenu();
1102
1103}
1104void writeDataMenu()
1105{
1106
1107
1108 bool bNameExists = false;
1109
1110 std::string line;
1111 std::string choice;
1112 std::string tempConnectionString;
1113
1114 fstream_IN.open("alldatabases.txt");
1115 while (getline(fstream_IN, line))
1116 {
1117 v_DatabaseLabels.push_back(line);
1118 }
1119 fstream_IN.close();
1120
1121 if (v_DatabaseLabels.size() > 0)
1122 {
1123
1124
1125
1126
1127 std::cout << "-----------------------------------WRITE DATA-----------------------------------" << std::endl;
1128 std::cout << "Choose a database to write data to" << std::endl;
1129 std::cout << "Type cancel to return to main menu" << std::endl;
1130 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1131 showDatabaseLabels();
1132 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1133
1134 do
1135 {
1136 getline(std::cin, choice);
1137
1138 if (choice == "cancel")
1139 {
1140 v_DatabaseLabels.clear();
1141 system("cls");
1142 mainMenu();
1143 }
1144
1145 for (int i = 0; i < v_DatabaseLabels.size(); i++)
1146 {
1147 if (choice == v_DatabaseLabels.at(i))
1148 {
1149 bNameExists = true;
1150 }
1151 }
1152
1153 if (!bNameExists)
1154 {
1155 std::cout << "The database " << choice << " does not exist! Try again..." << std::endl;
1156 }
1157 } while (!bNameExists);
1158
1159 if (connectToDatabase(choice))
1160 {
1161 do
1162 {
1163
1164 system("cls");
1165
1166 std::cout << "-----------------------------CONNECTED TO DATABASE------------------------------" << std::endl;
1167 std::cout << "Enter the option you wish to proceed with!" << std::endl;
1168 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1169 std::cout << "1. Write to all indexes" << std::endl;
1170 std::cout << "2. Write to chosen indexes" << std::endl;
1171 std::cout << "3. Custom SQL statement" << std::endl;
1172 std::cout << "4. Back to main menu" << std::endl;
1173 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1174
1175 getline(std::cin, choice);
1176
1177 if (choice == "1")
1178 {
1179 system("cls");
1180 std::string tableChoice;
1181 std::string values;
1182
1183
1184 std::cout << "-----------------------------------WRITE ALL------------------------------------" << std::endl << std::endl;
1185 std::cout << "INSERT SYNTAX" << std::endl;
1186 std::cout << "Insert string: INSERT INTO persons" << std::endl;
1187 std::cout << "Value string: 'Apple','Tomato', 18, 'Strawberry'" << std::endl;
1188 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1189
1190 std::cout << "Enter the name of your desired table(s): INSERT INTO ";
1191 getline(std::cin, tableChoice);
1192 std::cout << "Enter the values to the attribute(s): ";
1193 getline(std::cin, values);
1194
1195
1196 insertAll(tableChoice, values);
1197
1198
1199
1200 }
1201 else if (choice == "2")
1202 {
1203 system("cls");
1204 std::string tableChoice;
1205 std::string columnChoice;
1206 std::string values;
1207
1208
1209 std::cout << "-----------------------------WRITE CHOSEN ATTRIBUTES----------------------------" << std::endl << std::endl;
1210 std::cout << "INSERT SYNTAX" << std::endl;
1211 std::cout << "Insert string: INSERT INTO persons" << std::endl;
1212 std::cout << "Attribute string: Fruit, Vegetable, Age, Berry" << std::endl;
1213 std::cout << "Value string: 'Apple','Tomato', 18, 'Strawberry'" << std::endl;
1214 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1215
1216 std::cout << "Enter the name of your desired table(s): INSERT INTO ";
1217 getline(std::cin, tableChoice);
1218 std::cout << "Enter the attribute(s): ";
1219 getline(std::cin, columnChoice);
1220 std::cout << "Enter the values to the attribute(s): ";
1221 getline(std::cin, values);
1222
1223
1224 insertIndex(tableChoice, columnChoice, values);
1225 }
1226 else if (choice == "3")
1227 {
1228 system("cls");
1229 std::string SQLStatement;
1230
1231
1232 std::cout << "------------------------------CUSTOM SQL STATEMENT------------------------------" << std::endl << std::endl;
1233
1234 std::cout << "Enter a SQL statement: INSERT INTO ";
1235 getline(std::cin, SQLStatement);
1236
1237 insertCustom(SQLStatement);
1238
1239
1240
1241 }
1242 else if (choice == "4")
1243 {
1244
1245 }
1246 else
1247 {
1248 std::cout << choice << " is not a valid option! Try again..." << std::endl;
1249 Sleep(2000);
1250 }
1251 } while (choice != "4");
1252
1253 }
1254 else
1255 {
1256 std::cout << "Could not connect do database!" << std::endl;
1257 Sleep(2000);
1258 }
1259
1260 }
1261 else
1262 {
1263 std::cout << "-----------------------------------WRITE DATA-----------------------------------" << std::endl;
1264 std::cout << "Choose a database to write data to" << std::endl;
1265 std::cout << "Type cancel to return to main menu" << std::endl;
1266 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1267 std::cout << "No databases found! Go create one." << std::endl;
1268 std::cout << "----------------------------------------------------------------------------" << std::endl;
1269
1270 Sleep(2000);
1271
1272 }
1273
1274 system("cls");
1275 db.logoff();
1276 mainMenu();
1277}
1278void deleteDataMenu()
1279{
1280
1281 bool bNameExists = false;
1282
1283 std::string line;
1284 std::string choice;
1285 std::string tempConnectionString;
1286
1287 fstream_IN.open("alldatabases.txt");
1288 while (getline(fstream_IN, line))
1289 {
1290 v_DatabaseLabels.push_back(line);
1291 }
1292 fstream_IN.close();
1293
1294 if (v_DatabaseLabels.size() > 0)
1295 {
1296
1297 std::cout << "-----------------------------------DELETE DATA----------------------------------" << std::endl;
1298 std::cout << "Choose a database to delete data from" << std::endl;
1299 std::cout << "Type cancel to return to main menu" << std::endl;
1300 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1301 showDatabaseLabels();
1302 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1303
1304 do
1305 {
1306 getline(std::cin, choice);
1307
1308 if (choice == "cancel")
1309 {
1310 v_DatabaseLabels.clear();
1311 system("cls");
1312 mainMenu();
1313 }
1314
1315 for (int i = 0; i < v_DatabaseLabels.size(); i++)
1316 {
1317 if (choice == v_DatabaseLabels.at(i))
1318 {
1319 bNameExists = true;
1320 }
1321 }
1322
1323 if (!bNameExists)
1324 {
1325 std::cout << "The database " << choice << " does not exist! Try again..." << std::endl;
1326 }
1327 } while (!bNameExists);
1328
1329 if (connectToDatabase(choice))
1330 {
1331 do
1332 {
1333
1334 system("cls");
1335
1336 std::cout << "-----------------------------CONNECTED TO DATABASE------------------------------" << std::endl;
1337 std::cout << "Enter the option you wish to proceed with!" << std::endl;
1338 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1339 std::cout << "1. Delete indexes" << std::endl;
1340 std::cout << "2. Custom SQL statement" << std::endl;
1341 std::cout << "3. Back to main menu" << std::endl;
1342 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1343
1344 getline(std::cin, choice);
1345
1346 if (choice == "1")
1347 {
1348 system("cls");
1349 std::string tableChoice;
1350 std::string columnChoice;
1351 std::string values;
1352
1353
1354 std::cout << "----------------------------------DELETE ROWS-----------------------------------" << std::endl << std::endl;
1355 std::cout << "DELETE SYNTAX" << std::endl;
1356 std::cout << "Delete string: DELETE FROM persons" << std::endl;
1357 std::cout << "Where string: WHERE City = Vasa" << std::endl;
1358 std::cout << "Delete all from table: DELETE * FROM persons" << std::endl;
1359 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1360
1361 std::cout << "Enter the name of your desired table(s): DELETE FROM ";
1362 getline(std::cin, tableChoice);
1363 std::cout << "Enter the attribute(s): ";
1364 getline(std::cin, columnChoice);
1365 std::cout << "Enter the values to the attribute(s): ";
1366 getline(std::cin, values);
1367
1368
1369 deleteIndex(tableChoice, columnChoice, values);
1370
1371
1372
1373 }
1374 else if (choice == "2")
1375 {
1376 system("cls");
1377 std::string SQLStatement;
1378
1379
1380 std::cout << "------------------------------CUSTOM SQL STATEMENT------------------------------" << std::endl << std::endl;
1381
1382 std::cout << "Enter a SQL statement: DELETE ";
1383 getline(std::cin, SQLStatement);
1384
1385 deleteCustom(SQLStatement);
1386 }
1387 else if (choice == "3")
1388 {
1389
1390
1391
1392
1393 }
1394 else
1395 {
1396 std::cout << choice << " is not a valid option! Try again..." << std::endl;
1397 Sleep(2000);
1398 }
1399 } while (choice != "3");
1400
1401 }
1402 else
1403 {
1404 std::cout << "Could not connect do database!" << std::endl;
1405 Sleep(2000);
1406 }
1407
1408 }
1409 else
1410 {
1411 std::cout << "-----------------------------------DELETE DATA----------------------------------" << std::endl;
1412 std::cout << "Choose a database to delete data from" << std::endl;
1413 std::cout << "Type cancel to return to main menu" << std::endl;
1414 std::cout << "--------------------------------------------------------------------------------" << std::endl;
1415 std::cout << "No databases found! Go create one." << std::endl;
1416 std::cout << "----------------------------------------------------------------------------" << std::endl;
1417
1418 Sleep(2000);
1419
1420 }
1421
1422 system("cls");
1423 db.logoff();
1424 mainMenu();
1425}
1426
1427void showDatabaseLabels()
1428{
1429 std::string line;
1430 fstream_IN.open("alldatabases.txt");
1431 while (getline(fstream_IN, line))
1432 {
1433 std::cout << line << std::endl;
1434 }
1435 fstream_IN.close();
1436}
1437
1438void createTable(std::string &p_table, std::string &p_attributes)
1439{
1440 try
1441 {
1442
1443 std::string insertString;
1444
1445 insertString = "CREATE TABLE " + p_table + " (" + p_attributes + ")";
1446
1447
1448 otl_stream dbStream;
1449
1450 system("cls");
1451 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1452 std::cout << p_table << std::endl;
1453 std::cout << "-----------------------------------ATTRIBUTES-----------------------------------" << std::endl;
1454 std::cout << p_attributes << std::endl;
1455
1456 dbStream.open(100, insertString.c_str(), db);
1457
1458 std::cout << "================================================================================" << std::endl;
1459 std::cout << std::endl << "Successfully created table(s) " << p_table << " with attributes " << p_attributes << std::endl;
1460 dbStream.close();
1461 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1462
1463
1464
1465
1466 }
1467 catch (otl_exception &e)
1468 {
1469 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1470 }
1471
1472 system("PAUSE");
1473}
1474void deleteTable(std::string &p_table)
1475{
1476 try
1477 {
1478
1479 std::string insertString;
1480
1481 insertString = "DROP TABLE " + p_table;
1482
1483
1484 otl_stream dbStream;
1485
1486 system("cls");
1487 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1488 std::cout << p_table << std::endl;
1489
1490 dbStream.open(100, insertString.c_str(), db);
1491
1492 std::cout << "================================================================================" << std::endl;
1493 std::cout << std::endl << "Successfully deleted table(s) " << p_table << std::endl;
1494 dbStream.close();
1495 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1496
1497
1498
1499
1500 }
1501 catch (otl_exception &e)
1502 {
1503 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1504 }
1505
1506 system("PAUSE");
1507}
1508
1509void selectAll(std::string &p_table)
1510{
1511 try
1512 {
1513
1514 std::string selectString;
1515 std::string line;
1516 std::string temp;
1517
1518 int desc_len;
1519 int rpc = 0;
1520 bool bReachedMaxLines = false;
1521
1522
1523
1524
1525
1526 selectString = "SELECT * FROM " + p_table;
1527
1528
1529 otl_stream dbStream;
1530
1531
1532
1533
1534 dbStream.set_all_column_types(otl_all_num2str);
1535
1536 dbStream.open(100, selectString.c_str(), db);
1537
1538 otl_column_desc* desc;
1539 desc = dbStream.describe_select(desc_len);
1540 system("cls");
1541 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1542 std::cout << p_table << std::endl;
1543 std::cout << "--------------------------------SELECT STATEMENT--------------------------------" << std::endl;
1544 std::cout << "*" << std::endl;
1545
1546 for (int i = 1; !dbStream.eof(); i++)
1547 {
1548
1549
1550 temp = std::to_string(i);
1551
1552 std::cout << "=======================================" << i; for (int i = 0; i < (41 - temp.length()); i++){ std::cout << "="; } std::cout << std::endl;
1553 for (int col_num = 0; col_num < desc_len; ++col_num)
1554 {
1555 rpc++;
1556 if (rpc > 15000)
1557 {
1558 bReachedMaxLines = true;
1559 }
1560
1561 dbStream >> line;
1562 if (dbStream.is_null())
1563 std::cout << desc[col_num].name << ": NULL" << std::endl;
1564 else
1565 std::cout << desc[col_num].name << ": " << line << std::endl;
1566
1567
1568 }
1569
1570 if (bReachedMaxLines)
1571 {
1572 bReachedMaxLines = false;
1573 rpc = 0;
1574 std::cout << std::endl << "You have reached the maximum amount of lines, get what information you need." << std::endl << std::endl;
1575 system("PAUSE");
1576 system("cls");
1577 }
1578
1579
1580 }
1581
1582 dbStream.close();
1583 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1584
1585 }
1586 catch (otl_exception &e)
1587 {
1588 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1589
1590 }
1591
1592 system("PAUSE");
1593}
1594void selectIndex(std::string &p_select, std::string &p_table)
1595{
1596 try
1597 {
1598
1599 std::string selectString;
1600 std::string line;
1601 std::string temp;
1602
1603 int desc_len;
1604 int rpc = 0;
1605 bool bReachedMaxLines = false;
1606
1607 selectString = "SELECT " + p_select + " FROM " + p_table;
1608
1609
1610
1611 otl_stream dbStream;
1612
1613
1614
1615
1616 dbStream.set_all_column_types(otl_all_num2str);
1617
1618 dbStream.open(100, selectString.c_str(), db);
1619
1620 otl_column_desc* desc;
1621 desc = dbStream.describe_select(desc_len);
1622 system("cls");
1623
1624 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1625 std::cout << p_table << std::endl;
1626 std::cout << "--------------------------------SELECT STATEMENT--------------------------------" << std::endl;
1627 std::cout << p_select << std::endl;
1628
1629
1630 for (int i = 1; !dbStream.eof(); i++)
1631 {
1632
1633 temp = std::to_string(i);
1634
1635 std::cout << "=======================================" << i; for (int i = 0; i < (41 - temp.length()); i++){ std::cout << "="; } std::cout << std::endl;
1636 for (int col_num = 0; col_num < desc_len; ++col_num)
1637 {
1638 rpc++;
1639 if (rpc > 15000)
1640 {
1641 bReachedMaxLines = true;
1642 }
1643
1644 dbStream >> line;
1645 if (dbStream.is_null())
1646 std::cout << desc[col_num].name << ": NULL" << std::endl;
1647 else
1648 std::cout << desc[col_num].name << ": " << line << std::endl;
1649
1650
1651 }
1652
1653 if (bReachedMaxLines)
1654 {
1655 bReachedMaxLines = false;
1656 rpc = 0;
1657 std::cout << std::endl << "You have reached the maximum amount of lines, get what information you need." << std::endl << std::endl;
1658 system("PAUSE");
1659 system("cls");
1660 }
1661
1662 }
1663
1664
1665
1666 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1667
1668 dbStream.close();
1669
1670 }
1671 catch (otl_exception &e)
1672 {
1673 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1674 }
1675
1676 system("PAUSE");
1677}
1678void selectCustom(std::string &p_SQL)
1679{
1680 try
1681 {
1682
1683 std::string line;
1684 std::string selectString;
1685 std::string temp;
1686
1687 int desc_len;
1688 int rpc = 0;
1689 bool bReachedMaxLines = false;
1690
1691
1692
1693
1694 otl_stream dbStream;
1695
1696
1697 selectString = "SELECT " + p_SQL;
1698
1699 dbStream.set_all_column_types(otl_all_num2str);
1700
1701 dbStream.open(100, selectString.c_str(), db);
1702
1703 otl_column_desc* desc;
1704 desc = dbStream.describe_select(desc_len);
1705 system("cls");
1706
1707 std::cout << "------------------------------CUSTOM SQL STATEMENT------------------------------" << std::endl << std::endl;
1708 std::cout << "SELECT " << p_SQL << std::endl;
1709
1710 for (int i = 1; !dbStream.eof(); i++)
1711 {
1712 temp = std::to_string(i);
1713
1714 std::cout << "=======================================" << i; for (int i = 0; i < (41 - temp.length()); i++){ std::cout << "="; } std::cout << std::endl;
1715
1716 for (int col_num = 0; col_num < desc_len; ++col_num)
1717 {
1718 rpc++;
1719 if (rpc > 15000)
1720 {
1721 bReachedMaxLines = true;
1722 }
1723
1724 dbStream >> line;
1725 if (dbStream.is_null())
1726 std::cout << desc[col_num].name << ": NULL" << std::endl;
1727 else
1728 std::cout << desc[col_num].name << ": " << line << std::endl;
1729
1730
1731 }
1732
1733 if (bReachedMaxLines)
1734 {
1735 bReachedMaxLines = false;
1736 rpc = 0;
1737 std::cout << std::endl << "You have reached the maximum amount of lines, get what information you need." << std::endl << std::endl;
1738 system("PAUSE");
1739 system("cls");
1740 }
1741
1742 }
1743
1744
1745
1746 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1747
1748 dbStream.close();
1749
1750 }
1751 catch (otl_exception &e)
1752 {
1753 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1754
1755 }
1756
1757 system("PAUSE");
1758}
1759
1760void insertAll(std::string &p_table, std::string &p_values)
1761{
1762 try
1763 {
1764
1765 std::string insertString;
1766
1767 insertString = "INSERT INTO " + p_table + " VALUES (" + p_values + ");";
1768
1769
1770 otl_stream dbStream;
1771
1772 system("cls");
1773 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1774 std::cout << p_table << std::endl;
1775 std::cout << "-------------------------------------VALUES-------------------------------------" << std::endl;
1776 std::cout << p_values << std::endl;
1777
1778 dbStream.open(100, insertString.c_str(), db);
1779
1780 std::cout << "================================================================================" << std::endl;
1781 std::cout << std::endl << "Successfully inserted value(s) " << p_values << " into " << p_table;
1782 dbStream.close();
1783 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1784
1785
1786
1787
1788 }
1789 catch (otl_exception &e)
1790 {
1791 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1792 std::cout << "Check the attribute(s) variable type(s)! Remove quotes if the type is a number." << std::endl;
1793 }
1794
1795 system("PAUSE");
1796}
1797void insertIndex(std::string &p_table, std::string &p_columns, std::string &p_values)
1798{
1799 try
1800 {
1801
1802 std::string insertString;
1803
1804 insertString = "INSERT INTO " + p_table + " (" + p_columns + ") " + "VALUES (" + p_values + ");";
1805
1806 otl_stream dbStream;
1807
1808 system("cls");
1809 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1810 std::cout << p_table << std::endl;
1811 std::cout << "-----------------------------------ATTRIBUTES-----------------------------------" << std::endl;
1812 std::cout << p_columns << std::endl;
1813 std::cout << "-------------------------------------VALUES-------------------------------------" << std::endl;
1814 std::cout << p_values << std::endl;
1815
1816 dbStream.open(100, insertString.c_str(), db);
1817
1818 std::cout << "================================================================================" << std::endl;
1819 std::cout << std::endl << "Successfully inserted value(s) " << p_values << " into " << p_table;
1820 dbStream.close();
1821 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1822
1823
1824
1825
1826 }
1827 catch (otl_exception &e)
1828 {
1829 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1830 std::cout << "Check the attribute(s) variable type(s)! Remove quotes if the type is a number." << std::endl;
1831 }
1832
1833 system("PAUSE");
1834}
1835void insertCustom(std::string &p_SQL)
1836{
1837 try
1838 {
1839
1840 std::string insertString;
1841
1842 otl_stream dbStream;
1843
1844 insertString = "INSERT " + p_SQL;
1845
1846 system("cls");
1847 std::cout << "------------------------------CUSTOM SQL STATEMENT------------------------------" << std::endl << std::endl;
1848 std::cout << "INSERT " << p_SQL << std::endl;
1849
1850
1851
1852
1853 dbStream.open(100, insertString.c_str(), db);
1854
1855 std::cout << "================================================================================" << std::endl;
1856 std::cout << std::endl << "Successfully executed " << p_SQL << std::endl;
1857 dbStream.close();
1858 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1859
1860
1861
1862
1863 }
1864 catch (otl_exception &e)
1865 {
1866 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1867 std::cout << "Check the attribute(s) variable type(s)! Remove quotes if the type is a number." << std::endl;
1868 }
1869
1870 system("PAUSE");
1871}
1872
1873void deleteIndex(std::string &p_table, std::string &p_columns, std::string &p_values)
1874{
1875 try
1876 {
1877
1878 std::string insertString;
1879
1880 insertString = "DELETE FROM " + p_table + " WHERE " + p_columns + " = " + p_values;
1881
1882
1883 otl_stream dbStream;
1884
1885 system("cls");
1886 std::cout << "------------------------------------TABLE(S)------------------------------------" << std::endl;
1887 std::cout << p_table << std::endl;
1888 std::cout << "-----------------------------------ATTRIBUTES-----------------------------------" << std::endl;
1889 std::cout << p_columns << std::endl;
1890 std::cout << "-------------------------------------VALUES-------------------------------------" << std::endl;
1891 std::cout << p_values << std::endl;
1892
1893 dbStream.open(100, insertString.c_str(), db);
1894
1895 std::cout << "================================================================================" << std::endl;
1896 std::cout << std::endl << "Successfully deleted row(s) where " << p_columns << " = " << p_values;
1897 dbStream.close();
1898 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1899
1900
1901
1902
1903 }
1904 catch (otl_exception &e)
1905 {
1906 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1907 std::cout << "Check the attribute(s) variable type(s)! Remove quotes if the type is a number." << std::endl;
1908 }
1909
1910 system("PAUSE");
1911}
1912void deleteCustom(std::string &p_SQL)
1913{
1914 try
1915 {
1916
1917 std::string insertString;
1918
1919 insertString = "DELETE " + p_SQL;
1920
1921
1922 otl_stream dbStream;
1923
1924 system("cls");
1925 std::cout << "------------------------------CUSTOM SQL STATEMENT------------------------------" << std::endl << std::endl;
1926 std::cout << "DELETE " << p_SQL << std::endl;
1927
1928 dbStream.open(100, insertString.c_str(), db);
1929
1930 std::cout << "================================================================================" << std::endl;
1931 std::cout << std::endl << "Successfully executed " << p_SQL << std::endl;
1932 dbStream.close();
1933 std::cout << std::endl << "**************************************DONE**************************************" << std::endl << std::endl;
1934
1935
1936
1937
1938 }
1939 catch (otl_exception &e)
1940 {
1941 std::cout << "Syntax error/Table not found/Connection lost!" << std::endl;
1942 std::cout << "Check the attribute(s) variable type(s)! Remove quotes if the type is a number." << std::endl;
1943 }
1944
1945 system("PAUSE");
1946}
1947
1948void stringLoop(std::string string, int milliseconds)
1949{
1950 for (int i = 0; i < string.length(); i++)
1951 {
1952 std::cout << string.at(i);
1953 Sleep(milliseconds);
1954 }
1955}
1956
1957bool startMusic(Mix_Music* p_music, std::string p_musicfile)
1958{
1959 Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096);
1960
1961
1962 p_music = Mix_LoadMUS(p_musicfile.c_str());
1963 Mix_VolumeMusic(10);
1964
1965 if (Mix_PlayMusic(p_music, -1) == 0)
1966 return true;
1967 else
1968 return false;
1969
1970
1971}
1972void stopMusic(Mix_Music* p_music)
1973{
1974 Mix_FreeMusic(p_music);
1975 Mix_CloseAudio();
1976}
1977#pragma endregion