Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream> // ostringstream
- #include <iomanip>
- #include <clocale> // setlocale
- #include <cctype> // toupper
- #include "DBhandler.h"
- #include "winutf8.h"
- #include "libpq-fe.h"
- #include "terminal.h"
- #include "PWinput.h"
- using std::string;
- using std::ostringstream;
- using std::toupper;
- using std::cerr;
- using std::cout;
- using std::cin;
- using std::endl;
- using std::setw;
- using std::left;
- void logIn();
- int main(int argc, char **argv)
- {
- Terminal term; // To manipulate colors
- std::setlocale(LC_ALL, "Swedish_Sweden.1252"); // To show swedish characters in windows console
- term.pushColor(TerminalColor(COLOR::CYAN, COLOR::BLACK));
- cout << "************* Postgresql C/C++ interface demo *************\n\n";
- term.popColor();
- const string PG_PASSWORD { "kattfluff5" }; // Set your postgres password here
- ostringstream oss; // string stream to concatenate strings
- string utf8QueryStr; // string converted to UTF8
- // Build string with connection info
- oss << "user=postgres password=" << PG_PASSWORD << " dbname=postgres hostaddr=127.0.0.1 port=5432";
- win1252utf8(oss.str(), utf8QueryStr); // Convert to UTF8, result in utf8QueryStr
- DBhandler db { utf8QueryStr }; // Create our database handler object
- /* Make a connection to the database */
- bool opResult = db.connectDB();
- if (!opResult) {
- term.pushColor(TerminalColor(COLOR::RED, COLOR::BLACK));
- cerr << "Wrong creditials, you are not logged in to postgres. Exiting." << endl;
- term.clearColors();
- return 1;
- }
- term.pushColor(TerminalColor(COLOR::GREEN, COLOR::BLACK));
- cout << "Connected to Postgres DB on localhost\n\n";
- /*---------------------------------------------------------------------------------------*/
- /////////////// HÄR BÖRJAR MINA ÄNDRINGAR//////////////////////////////////////
- // Let's show all persons
- std::string firstname, email, passWord;
- std::cout << "The database is only available for the duck" << std::endl;
- std::cout << "Enter firstname for the duck: ";
- std::getline(std::cin, firstname);
- firstname[0] = std::toupper(firstname[0]);
- std::cout << std::endl << "Enter email: ";
- std::getline(std::cin, email);
- std::cout << std::endl << "Enter password: ";
- passWord = getPassword();
- oss.str(""); // Reset the stream and make it empty
- //oss << "SELECT förnamn, efternamn, gatuadress, postnr, epost FROM sportsclub.person;";
- oss << "SELECT förnamn, epost FROM";
- oss << " sportsclub8.person ";
- oss << " WHERE person.förnamn = '" << firstname << "' AND person.epost = '" << email << "'AND person.lösenord = md5('" << passWord << "'); ";
- win1252utf8(oss.str(), utf8QueryStr); // convert to utf8
- // Start transaction
- if (!db.beginTransaction()) {
- cerr << "beginTransaction failed" << endl;
- db.closeDBconn();
- return 1;
- }
- // Make the query
- PGresult* qResult1 = db.makeQuery(utf8QueryStr.c_str());
- if (!qResult1) // Failure
- return 1;
- int tuples = PQntuples(qResult1);
- int nFields = PQnfields(qResult1);
- if (tuples == 1)
- {
- string valueStr1; // utf8 convertered to Windows cp 1252
- string subStr1;
- string subStr2;
- for (int i = 0; i < PQntuples(qResult1); i++)
- {
- term.pushColor(TerminalColor(COLOR::WHITE, COLOR::BLACK));
- for (int j = 0; j < nFields; j++) {
- utf8win1252(PQgetvalue(qResult1, i, j), valueStr1);
- //cout << valueStr1 << "\t";
- if (j == 0)
- {
- string subStr1 = valueStr1;
- }
- if (j == 1)
- {
- string subStr2 = valueStr1;
- }
- cout << subStr1 << endl << subStr2 << endl;
- }
- }
- if (subStr1 == "Donald" && subStr2 =="d.duck@quacksville.net")
- /////////////// HÄR SLUTAR MINA ÄNDRINGAR//////////////////////////////////////
- {
- // ORGINIAL START
- oss.str(""); // Reset the stream and make it empty
- //oss << "SELECT förnamn, efternamn, gatuadress, postnr, epost FROM sportsclub.person;";
- oss << "SELECT förnamn, efternamn, gatuadress, person.postnr, postort, epost FROM";
- oss << " sportsclub8.person, sportsclub8.postort WHERE person.postnr = postort.postnr;";
- win1252utf8(oss.str(), utf8QueryStr); // convert to utf8
- // Start transaction
- if (!db.beginTransaction()) {
- cerr << "beginTransaction failed" << endl;
- db.closeDBconn();
- return 1;
- }
- // Make the query
- PGresult* qResult = db.makeQuery(utf8QueryStr.c_str());
- if (!qResult) // Failure
- return 1;
- // OK, let's get the field names, first get the number of fields
- int nFields = PQnfields(qResult);
- // Prepare the header with employee table field name
- cout << "\nAll persons in Sportsclub8:" << endl;
- term.pushColor(TerminalColor(COLOR::YELLOW, COLOR::BLACK));
- cout << "\n******************************************************************************************************\n";
- for (int i = 0; i < nFields; i++) {
- string fieldStr;
- utf8win1252(PQfname(qResult, i), fieldStr); // convert from utf8
- fieldStr[0] = std::toupper(fieldStr[0]);
- cout << left << setw(10) << fieldStr << "\t";
- }
- cout << endl;
- // Next, print out the record for each row
- for (int i = 0; i < PQntuples(qResult); i++)
- {
- term.pushColor(TerminalColor(COLOR::WHITE, COLOR::BLACK));
- for (int j = 0; j < nFields; j++) {
- string valueStr; // utf8 convertered to Windows cp 1252
- utf8win1252(PQgetvalue(qResult, i, j), valueStr);
- cout << left << setw(10) << valueStr << "\t";
- }
- cout << endl;
- term.popColor();
- }
- cout << "\n******************************************************************************************************\n";
- term.clearColors();
- PQclear(qResult);
- db.endTransaction();
- }
- }
- /* close the connection to the database and cleanup */
- db.closeDBconn();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement