Guest User

Untitled

a guest
May 16th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream> // ostringstream
  4. #include <iomanip>
  5. #include <clocale> // setlocale
  6. #include <cctype> // toupper
  7. #include "DBhandler.h"
  8. #include "winutf8.h"
  9. #include "libpq-fe.h"
  10. #include "terminal.h"
  11. #include "PWinput.h"
  12.  
  13.  
  14. using std::string;
  15. using std::ostringstream;
  16. using std::toupper;
  17. using std::cerr;
  18. using std::cout;
  19. using std::cin;
  20. using std::endl;
  21. using std::setw;
  22. using std::left;
  23.  
  24. void logIn();
  25.  
  26. int main(int argc, char **argv)
  27. {
  28. Terminal term; // To manipulate colors
  29. std::setlocale(LC_ALL, "Swedish_Sweden.1252"); // To show swedish characters in windows console
  30. term.pushColor(TerminalColor(COLOR::CYAN, COLOR::BLACK));
  31. cout << "************* Postgresql C/C++ interface demo *************\n\n";
  32. term.popColor();
  33.  
  34. const string PG_PASSWORD { "kattfluff5" }; // Set your postgres password here
  35. ostringstream oss; // string stream to concatenate strings
  36. string utf8QueryStr; // string converted to UTF8
  37.  
  38. // Build string with connection info
  39. oss << "user=postgres password=" << PG_PASSWORD << " dbname=postgres hostaddr=127.0.0.1 port=5432";
  40.  
  41. win1252utf8(oss.str(), utf8QueryStr); // Convert to UTF8, result in utf8QueryStr
  42.  
  43. DBhandler db { utf8QueryStr }; // Create our database handler object
  44.  
  45. /* Make a connection to the database */
  46. bool opResult = db.connectDB();
  47. if (!opResult) {
  48. term.pushColor(TerminalColor(COLOR::RED, COLOR::BLACK));
  49. cerr << "Wrong creditials, you are not logged in to postgres. Exiting." << endl;
  50. term.clearColors();
  51. return 1;
  52. }
  53. term.pushColor(TerminalColor(COLOR::GREEN, COLOR::BLACK));
  54. cout << "Connected to Postgres DB on localhost\n\n";
  55.  
  56. /*---------------------------------------------------------------------------------------*/
  57. /////////////// HÄR BÖRJAR MINA ÄNDRINGAR//////////////////////////////////////
  58. // Let's show all persons
  59. std::string firstname, email, passWord;
  60. std::cout << "The database is only available for the duck" << std::endl;
  61. std::cout << "Enter firstname for the duck: ";
  62. std::getline(std::cin, firstname);
  63. firstname[0] = std::toupper(firstname[0]);
  64. std::cout << std::endl << "Enter email: ";
  65. std::getline(std::cin, email);
  66. std::cout << std::endl << "Enter password: ";
  67. passWord = getPassword();
  68.  
  69. oss.str(""); // Reset the stream and make it empty
  70. //oss << "SELECT förnamn, efternamn, gatuadress, postnr, epost FROM sportsclub.person;";
  71. oss << "SELECT förnamn, epost FROM";
  72. oss << " sportsclub8.person ";
  73. oss << " WHERE person.förnamn = '" << firstname << "' AND person.epost = '" << email << "'AND person.lösenord = md5('" << passWord << "'); ";
  74.  
  75. win1252utf8(oss.str(), utf8QueryStr); // convert to utf8
  76.  
  77. // Start transaction
  78. if (!db.beginTransaction()) {
  79. cerr << "beginTransaction failed" << endl;
  80. db.closeDBconn();
  81. return 1;
  82. }
  83. // Make the query
  84. PGresult* qResult1 = db.makeQuery(utf8QueryStr.c_str());
  85. if (!qResult1) // Failure
  86. return 1;
  87.  
  88. int tuples = PQntuples(qResult1);
  89. int nFields = PQnfields(qResult1);
  90.  
  91. if (tuples == 1)
  92. {
  93. string valueStr1; // utf8 convertered to Windows cp 1252
  94. string subStr1;
  95. string subStr2;
  96. for (int i = 0; i < PQntuples(qResult1); i++)
  97. {
  98. term.pushColor(TerminalColor(COLOR::WHITE, COLOR::BLACK));
  99. for (int j = 0; j < nFields; j++) {
  100.  
  101. utf8win1252(PQgetvalue(qResult1, i, j), valueStr1);
  102. //cout << valueStr1 << "\t";
  103. if (j == 0)
  104. {
  105. string subStr1 = valueStr1;
  106. }
  107. if (j == 1)
  108. {
  109. string subStr2 = valueStr1;
  110. }
  111. cout << subStr1 << endl << subStr2 << endl;
  112. }
  113.  
  114. }
  115.  
  116. if (subStr1 == "Donald" && subStr2 =="d.duck@quacksville.net")
  117. /////////////// HÄR SLUTAR MINA ÄNDRINGAR//////////////////////////////////////
  118. {
  119.  
  120. // ORGINIAL START
  121. oss.str(""); // Reset the stream and make it empty
  122. //oss << "SELECT förnamn, efternamn, gatuadress, postnr, epost FROM sportsclub.person;";
  123. oss << "SELECT förnamn, efternamn, gatuadress, person.postnr, postort, epost FROM";
  124. oss << " sportsclub8.person, sportsclub8.postort WHERE person.postnr = postort.postnr;";
  125.  
  126. win1252utf8(oss.str(), utf8QueryStr); // convert to utf8
  127.  
  128.  
  129. // Start transaction
  130. if (!db.beginTransaction()) {
  131. cerr << "beginTransaction failed" << endl;
  132. db.closeDBconn();
  133. return 1;
  134. }
  135.  
  136. // Make the query
  137. PGresult* qResult = db.makeQuery(utf8QueryStr.c_str());
  138. if (!qResult) // Failure
  139. return 1;
  140.  
  141. // OK, let's get the field names, first get the number of fields
  142. int nFields = PQnfields(qResult);
  143.  
  144. // Prepare the header with employee table field name
  145. cout << "\nAll persons in Sportsclub8:" << endl;
  146. term.pushColor(TerminalColor(COLOR::YELLOW, COLOR::BLACK));
  147. cout << "\n******************************************************************************************************\n";
  148. for (int i = 0; i < nFields; i++) {
  149. string fieldStr;
  150. utf8win1252(PQfname(qResult, i), fieldStr); // convert from utf8
  151. fieldStr[0] = std::toupper(fieldStr[0]);
  152. cout << left << setw(10) << fieldStr << "\t";
  153. }
  154. cout << endl;
  155.  
  156. // Next, print out the record for each row
  157. for (int i = 0; i < PQntuples(qResult); i++)
  158. {
  159. term.pushColor(TerminalColor(COLOR::WHITE, COLOR::BLACK));
  160. for (int j = 0; j < nFields; j++) {
  161. string valueStr; // utf8 convertered to Windows cp 1252
  162. utf8win1252(PQgetvalue(qResult, i, j), valueStr);
  163. cout << left << setw(10) << valueStr << "\t";
  164. }
  165. cout << endl;
  166. term.popColor();
  167. }
  168.  
  169. cout << "\n******************************************************************************************************\n";
  170. term.clearColors();
  171. PQclear(qResult);
  172.  
  173. db.endTransaction();
  174. }
  175. }
  176. /* close the connection to the database and cleanup */
  177. db.closeDBconn();
  178.  
  179. return 0;
  180. }
Add Comment
Please, Sign In to add comment