Files
postgrachelorcppapplication/project/dbclient.pgc

426 lines
11 KiB
Plaintext

#include <iostream>
#include <stdlib.h>
using namespace std;
void initdb();
void menuInsert();
void menuSelect();
void menuUpdate();
void menuDelete();
void insertCategory();
void insertQuestion();
void selectCategory();
void selectQuestion();
void updateCategory();
void updateQuestion();
void deleteCategory();
void deleteQuestion();
void cinClear();
int main()
{
initdb();
char choice;
int exit = 0;
for(;!exit;){
choice = 0;
cout << endl << endl << endl << "Was wollen Sie tun?:" << endl;
cout << "(e): Daten eingeben" << endl;
cout << "(a): Daten anzeigen" << endl;
cout << "(n): Daten aendern" << endl;
cout << "(l): Daten loeschen" << endl;
cout << "(s): schliessen" << endl;
cin >> choice;
switch(choice){
case 'e':
menuInsert();
break;
case 'a':
menuSelect();
break;
case 'n':
menuUpdate();
break;
case 'l':
menuDelete();
break;
case 's':
exit = 1;
break;
}
}
}
void initdb(){
EXEC SQL CONNECT TO "CSDB@lamp.wlan.hwr-berlin.de" USER gr2 IDENTIFIED BY "2Au-R2n-cQN-uuG";
cout << "state: " << sqlca.sqlstate << endl;
EXEC SQL COMMIT;
}
void menuInsert(){
char choice = 0;
cout << "Welche Tabelle?:" << endl;
cout << "(k): Kategorien" << endl;
cout << "(f): Fragen" << endl;
cin >> choice;
switch(choice){
case 'k':
insertCategory();
break;
case 'f':
insertQuestion();
break;
}
}
void menuSelect(){
char choice = 0;
cout << "Welche Tabelle?:" << endl;
cout << "(k): Kategorien" << endl;
cout << "(f): Fragen" << endl;
cin >> choice;
switch(choice){
case 'k':
selectCategory();
break;
case 'f':
selectQuestion();
break;
}
}
void menuUpdate(){
char choice = 0;
cout << "In welcher Tabelle?:" << endl;
cout << "(k): Kategorien" << endl;
cout << "(f): Fragen" << endl;
cin >> choice;
switch(choice){
case 'k':
updateCategory();
break;
case 'f':
updateQuestion();
break;
}
}
void menuDelete(){
char choice = 0;
cout << "Aus welcher Tabelle?:" << endl;
cout << "(k): Kategorien" << endl;
cout << "(f): Fragen" << endl;
cin >> choice;
switch(choice){
case 'k':
deleteCategory();
break;
case 'f':
deleteQuestion();
break;
}
}
void insertCategory(){
EXEC SQL BEGIN DECLARE SECTION;
char eingna[4001];
EXEC SQL END DECLARE SECTION;
string h = "";
cout << "Eingabe Name: ";
ws(cin);
getline(cin, h);
//cout << h << endl;
if (!(h.empty()||!strcmp(h.c_str(),"\n")))
{
strcpy(eingna, h.c_str());
EXEC SQL INSERT INTO gr2."Categories"(name) VALUES (:eingna);
if (!strcmp(sqlca.sqlstate, "23505")) cout << "GIBT'S SCHON" << endl;
else if(!strcmp(sqlca.sqlstate, "22021")) cout << "Umlaute bitte umschreiben (z.B. als \"ue\")!";
EXEC SQL COMMIT;
}
}
void insertQuestion(){
EXEC SQL BEGIN DECLARE SECTION;
char question[4001];
char ans0[4001];
char ans1[4001];
char ans2[4001];
char ans3[4001];
int category;
EXEC SQL END DECLARE SECTION;
cout << "Frage: ";
ws(cin);
cin.getline(question, sizeof(question));
cout << "Richtige Antwort: ";
ws(cin);
cin.getline(ans0, sizeof(ans0));
cout << "1. Falsche Antwort: ";
ws(cin);
cin.getline(ans1, sizeof(ans1));
cout << "2. Falsche Antwort: ";
ws(cin);
cin.getline(ans2, sizeof(ans2));
cout << "3. Falsche Antwort: ";
ws(cin);
cin.getline(ans3, sizeof(ans3));
cout << "ID der Kategorie:" << endl << "moegliche ";
selectCategory();
cin >> category;
if(category){
EXEC SQL INSERT INTO gr2."Questions"(question,answer0,answer1,answer2,answer3,category) VALUES (:question,:ans0,:ans1,:ans2,:ans3,:category);
if(!strcmp(sqlca.sqlstate, "23503")) cout << "Das ist keine gueltige Kategorie!" << endl;
else if(!strcmp(sqlca.sqlstate, "22021")) cout << "Umlaute bitte umschreiben (z.B. als \"ue\")!";
cout << "state: " << sqlca.sqlstate << endl;
EXEC SQL COMMIT;
}
}
void selectCategory(){
//Die Ausgabe funktioniert nur dann korrekt, wenn die ids eindeutig sind!!!
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[4001];
EXEC SQL END DECLARE SECTION;
cout << "Kategorien:" << endl;
EXEC SQL DECLARE xxx CURSOR FOR SELECT "ID",name FROM gr2."Categories" ORDER BY "ID";
EXEC SQL OPEN xxx;
int residl;
residl = id;
string hName = "";
EXEC SQL FETCH FIRST FROM xxx INTO :id, :name;
if(!strcmp(sqlca.sqlstate, "00000")){
hName = name;
hName.erase(hName.find_last_not_of(" \n\r\t")+1);
cout << id << " " << hName << endl;
}
else if(!strcmp(sqlca.sqlstate, "02000")) cout << "Die Tabelle ist leer!";
while(id != residl)
{
residl = id;
EXEC SQL FETCH NEXT FROM xxx INTO :id, :name;
if(!strcmp(sqlca.sqlstate, "00000")){
hName = name;
hName.erase(hName.find_last_not_of(" \n\r\t")+1);
cout << id << " " << hName << endl;
}
}
EXEC SQL COMMIT;
}
void selectQuestion(){
//Die Ausgabe funktioniert nur dann korrekt, wenn die ids eindeutig sind!!!
EXEC SQL BEGIN DECLARE SECTION;
int id;
char question[4001];
char ans0[4001];
char ans1[4001];
char ans2[4001];
char ans3[4001];
char cat[4001];
int sites;
int offset;
EXEC SQL END DECLARE SECTION;
EXEC SQL SELECT ceil(count(*)/5.0) INTO :sites FROM gr2."Questions";
cout << "Es gibt " << sites << " Seiten. Welche soll angezeigt werden?" << endl;
cin >> offset;
while(offset > sites || offset < 1){
cout << "Diese Seite gibt es nicht. Bitte gueltige Seitenzahl eingeben!" << endl;
cin >> offset;
}
offset = (offset-1)*5;
string hQuestion = "", hAns0 = "", hAns1 = "", hAns2 = "", hAns3 = "", hCat = "";
cout << "Fragen:" << endl << endl;
EXEC SQL DECLARE yyy CURSOR FOR SELECT q."ID",q.question,q.answer0,q.answer1,q.answer2,q.answer3,c.name FROM gr2."Questions" q JOIN gr2."Categories" c ON q.category = c."ID" ORDER BY c."ID" LIMIT 5 OFFSET :offset;
EXEC SQL OPEN yyy;
int residl;
residl = id;
EXEC SQL FETCH FIRST FROM yyy INTO :id,:question, :ans0, :ans1, :ans2, :ans3, :cat;
if(!strcmp(sqlca.sqlstate, "00000")){
hQuestion = question;
hAns0 = ans0;
hAns1 = ans1;
hAns2 = ans2;
hAns3 = ans3;
hCat = cat;
hQuestion.erase(hQuestion.find_last_not_of(" \n\r\t")+1);
hAns0.erase(hAns0.find_last_not_of(" \n\r\t")+1);
hAns1.erase(hAns1.find_last_not_of(" \n\r\t")+1);
hAns2.erase(hAns2.find_last_not_of(" \n\r\t")+1);
hAns3.erase(hAns3.find_last_not_of(" \n\r\t")+1);
hCat.erase(hCat.find_last_not_of(" \n\r\t")+1);
cout << "Schema:" << endl;
cout << "Frage: Kategorie" << endl << " Richtige Antwort" << endl << " Falsche Antwort 1" << endl << " Falsche Antwort 2" << endl << " Falsche Antwort 3" << endl << endl << endl;
cout << hQuestion << ": " << hCat << endl;
cout << " " << hAns0 << endl;
cout << " " << hAns1 << endl;
cout << " " << hAns2 << endl;
cout << " " << hAns3 << endl;
}
else if(!strcmp(sqlca.sqlstate, "02000")) cout << "Die Tabelle ist leer!";
while(id != residl)
{
residl = id;
EXEC SQL FETCH NEXT FROM yyy INTO :id,:question, :ans0, :ans1, :ans2, :ans3, :cat;
if(!strcmp(sqlca.sqlstate, "00000")){
hQuestion = question;
hAns0 = ans0;
hAns1 = ans1;
hAns2 = ans2;
hAns3 = ans3;
hCat = cat;
hQuestion.erase(hQuestion.find_last_not_of(" \n\r\t")+1);
hAns0.erase(hAns0.find_last_not_of(" \n\r\t")+1);
hAns1.erase(hAns1.find_last_not_of(" \n\r\t")+1);
hAns2.erase(hAns2.find_last_not_of(" \n\r\t")+1);
hAns3.erase(hAns3.find_last_not_of(" \n\r\t")+1);
hCat.erase(hCat.find_last_not_of(" \n\r\t")+1);
cout << hQuestion << ": " << hCat << endl;
cout << " " << hAns0 << endl;
cout << " " << hAns1 << endl;
cout << " " << hAns2 << endl;
cout << " " << hAns3 << endl;
}
}
EXEC SQL COMMIT;
}
void updateCategory(){
EXEC SQL BEGIN DECLARE SECTION;
int id;
char name[4001];
EXEC SQL END DECLARE SECTION;
cout << "Welche Kategorie soll bearbeitet werden? (ID eingeben):" << endl;
selectCategory();
cin >> id;
cout << "Welchen neuen Namen soll die Kategorie bekommen?" << endl;
ws(cin);
cin.getline(name, sizeof(name));
if(id){
EXEC SQL UPDATE gr2."Categories" SET name = :name WHERE "ID" = :id;
if(!strcmp(sqlca.sqlstate, "02000")) cout << "Kein derartiger Datensatz gefunden!";
else if(!strcmp(sqlca.sqlstate, "22021")) cout << "Umlaute bitte umschreiben (z.B. als \"ue\")!";
EXEC SQL COMMIT;
}
}
void updateQuestion(){
EXEC SQL BEGIN DECLARE SECTION;
int id;
char question[4001];
char ans0[4001];
char ans1[4001];
char ans2[4001];
char ans3[4001];
int cat;
EXEC SQL END DECLARE SECTION;
char choice = 0;
cout << "Welche Frage soll bearbeitet werden? (ID eingeben):" << endl;
cin >> id;
if(id){
cout << "Was soll an der Frage geaendert werden?" << endl << "(f): Die Fragenstellung" << endl << "(0): Die richtige Antwort" << endl << "(1)-(3): Eine der falschen Antworten" << endl << "(k): Die Kategorie" << endl;
cin >> choice;
switch(choice){
case 'f':
cout << "Neue Fragenstellung: ";
ws(cin);
cin.getline(question, sizeof(question));
EXEC SQL UPDATE gr2."Questions" SET question = :question WHERE "ID" = :id;
break;
case '0':
cout << "Neue richtige Antwort: ";
ws(cin);
cin.getline(ans0, sizeof(ans0));
EXEC SQL UPDATE gr2."Questions" SET answer0 = :ans0 WHERE "ID" = :id;
break;
case '1':
cout << "Neue falsche Antwort: ";
ws(cin);
cin.getline(ans1, sizeof(ans1));
EXEC SQL UPDATE gr2."Questions" SET answer1 = :ans1 WHERE "ID" = :id;
break;
case '2':
cout << "Neue falsche Antwort: ";
ws(cin);
cin.getline(ans2, sizeof(ans2));
EXEC SQL UPDATE gr2."Questions" SET answer2 = :ans2 WHERE "ID" = :id;
break;
case '3':
cout << "Neue falsche Antwort: ";
ws(cin);
cin.getline(ans3, sizeof(ans3));
EXEC SQL UPDATE gr2."Questions" SET answer3 = :ans3 WHERE "ID" = :id;
break;
case 'k':
cout << "Neue Kategorie (ID):" << endl;
selectCategory();
cin >> cat;
EXEC SQL UPDATE gr2."Questions" SET category = :cat WHERE "ID" = :id;
break;
}
if(!strcmp(sqlca.sqlstate, "02000")) cout << "Kein derartiger Datensatz gefunden!";
else if(!strcmp(sqlca.sqlstate, "22021")) cout << "Umlaute bitte umschreiben (z.B. als \"ue\")!";
EXEC SQL COMMIT;
}
}
void deleteCategory(){
EXEC SQL BEGIN DECLARE SECTION;
int id;
EXEC SQL END DECLARE SECTION;
cout << "Welche Kategorie? (ID)" << endl;
selectCategory();
cin >> id;
if(id){
EXEC SQL DELETE FROM gr2."Categories" WHERE "ID" = :id;
if(!strcmp(sqlca.sqlstate, "23503")) cout << "Kategorie kann nicht geloescht werden! (Existieren noch Fragen mit der Kategorie?)" << endl;
else if(!strcmp(sqlca.sqlstate, "02000")) cout << "Kategorie existiert nicht!" << endl;
EXEC SQL COMMIT;
}
}
void deleteQuestion(){
EXEC SQL BEGIN DECLARE SECTION;
int id;
EXEC SQL END DECLARE SECTION;
cout << "Welche Frage? (ID)" << endl;
selectCategory();
cin >> id;
if(id){
EXEC SQL DELETE FROM gr2."Questions" WHERE "ID" = :id;
if(!strcmp(sqlca.sqlstate, "02000")) cout << "Frage existiert nicht!" << endl;
EXEC SQL COMMIT;
}
}
void cinClear() {
char Muell;
cin.clear();
do {
cin.get(Muell);
} while (Muell != '\n');
}