Advertisement
Guest User

arikmelech

a guest
Jun 28th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. #include "DataBase.h"
  2.  
  3. DataBase::DataBase()
  4. {
  5.  
  6. }
  7.  
  8. DataBase::DataBase(const DataBase & other)
  9. {
  10. *this = other;
  11. }
  12.  
  13. bool DataBase::isUserExist(string username)
  14. {
  15. sqlite3* db;
  16. if (sqlite3_open(FILE_NAME, &db)) //TRY to open db
  17. {
  18. sqlite3_close(db);
  19. throw std::exception(sqlite3_errmsg(db));
  20. }
  21.  
  22. char* errMsg = 0;
  23. string query = "select username from t_users";
  24. vector<vector<string>> values;
  25.  
  26. if (sqlite3_exec(db, query.c_str(), DataBase::callbackCount, &values, &errMsg) != SQLITE_OK) //run the query, store output values in values vector
  27. {
  28. sqlite3_close(db);
  29. cout << "SQL ERROR: " << errMsg << endl;
  30. delete errMsg;
  31. throw std::exception(errMsg);
  32. }
  33.  
  34. for (unsigned int i = 0; i < values.size(); i++)
  35. {
  36. //check if the usename matches any username from the DB
  37. if (username == string(values[i][USERNAME]))
  38. {
  39. sqlite3_close(db);
  40. delete errMsg;
  41. return true;
  42. }
  43. }
  44.  
  45. sqlite3_close(db);
  46. delete errMsg;
  47. return false;
  48. }
  49.  
  50. bool DataBase::addNewUser(string name, string pass, string email)
  51. {
  52. if (this->isUserExist(name)) return false; //if the user exists return false (failed to add new User)
  53.  
  54. sqlite3* db;
  55. if (sqlite3_open(FILE_NAME, &db)) //TRY to open db
  56. {
  57. sqlite3_close(db);
  58. return false; //return false if failed
  59. }
  60.  
  61. //the SQL query in the right format
  62. string query = "insert into t_users(username, password, email) values(";
  63. query += '"' + name + '"' + ", ";
  64. query += '"' + pass + '"' + ", ";
  65. query += '"' + email + '"' + ')';
  66.  
  67. char* errMsg = 0;
  68.  
  69. if (sqlite3_exec(db, query.c_str(), DataBase::callbackCount, 0, &errMsg) != SQLITE_OK) //run the query, store output values in values vector
  70. {
  71. sqlite3_close(db);
  72. cout << "SQL ERROR: " << errMsg << endl;
  73. delete errMsg;
  74. return false; //return false if failed
  75.  
  76. }
  77. return true;
  78. }
  79.  
  80. bool DataBase::doUserAndPassMatch(string username, string password)
  81. {
  82. sqlite3* db;
  83. if (sqlite3_open(FILE_NAME, &db)) //TRY to open db
  84. {
  85. sqlite3_close(db);
  86. throw std::exception(sqlite3_errmsg(db));
  87. }
  88.  
  89. char* errMsg = 0;
  90. string query = "select * from t_users";
  91. vector<vector<string>> values;
  92.  
  93. if (sqlite3_exec(db, query.c_str(), DataBase::callbackCount, &values, &errMsg) != SQLITE_OK) //run the query, store output values in values vector
  94. {
  95. sqlite3_close(db);
  96. cout << "SQL ERROR: " << errMsg << endl;
  97. delete errMsg;
  98. throw std::exception(errMsg);
  99. }
  100.  
  101. for (unsigned int i = 0; i < values.size(); i++)
  102. {
  103. //check if the usename & password combination matches any username & password combination from the DB
  104. if (username == string(values[i][USERNAME]) && password == string(values[i][PASSWORD]))
  105. {
  106. sqlite3_close(db);
  107. delete errMsg;
  108. return true;
  109. }
  110. }
  111.  
  112. sqlite3_close(db);
  113. delete errMsg;
  114. return false;
  115. }
  116.  
  117. vector<Question*> DataBase::initQuestions(int)
  118. {
  119. return vector<Question*>();
  120. }
  121.  
  122. vector<string> DataBase::getBestScores()
  123. {
  124. return vector<string>();
  125. }
  126.  
  127. vector<string> DataBase::getPersonalStatus(string)
  128. {
  129. return vector<string>();
  130. }
  131.  
  132. int DataBase::insertNewGame()
  133. {
  134. {
  135. if (!checkIfColumnExist("t_games"))
  136. try
  137. {
  138. insertToTable("BEGIN TRANSACTION;");
  139. string addGame = "insert into t_games(status,start_time) values(0,date(" + insertQouteForEscapeCharacters("now") + "));";
  140. insertToTable(addGame.c_str());
  141. insertToTable("COMMIT;");
  142. updateDetails("t_games");
  143. return stoi(_details["game_id"].back());
  144. }
  145. catch (invalid_argument& e)
  146. {
  147. std::cout << e.what() << '\n';
  148. return 0;
  149. }
  150. }
  151. }
  152.  
  153. bool DataBase::updateGameStatus(int gameID)
  154. {
  155. return false;
  156. }
  157.  
  158. bool DataBase::addAnswerToPlayer(int, string, int, string, bool, int)
  159. {
  160. return false;
  161. }
  162.  
  163. int DataBase::callbackCount(void* output, int argc, char** argv, char** azCol)
  164. {
  165. vector<string> vals;
  166. for (int i = 0; i < argc; i++)
  167. {
  168. vals.push_back(argv[i]); //save the output from the sql query in a vector of strings
  169. }
  170.  
  171. ((vector<vector<string>>*)output)->push_back(vals); //add only the usernames as we only need the names, nobody cares for the password and email
  172.  
  173. return 0;
  174. }
  175.  
  176. int DataBase::callbackQuestions(void* notUsed, int argc, char** argv, char** azCol)
  177. {
  178. int i;
  179.  
  180. for (i = 0; i < argc; i++)
  181. {
  182. auto it = _questions.find(azCol[i]);
  183. if (it != _questions.end())
  184. {
  185. it->second.push_back(argv[i]);
  186. }
  187. else
  188. {
  189. pair<string, vector<string>> p;
  190. p.first = azCol[i];
  191. p.second.push_back(argv[i]);
  192. _questions.insert(p);
  193. }
  194. }
  195.  
  196. return 0;
  197. }
  198.  
  199. int DataBase::callbackBestScores(void* notUsed, int argc, char** argv, char** azCol)
  200. {
  201. int i;
  202.  
  203. for (i = 0; i < argc; i++)
  204. {
  205. auto it = _bestScores.find(azCol[i]);
  206. if (it != _bestScores.end())
  207. {
  208. it->second.push_back(argv[i]);
  209. }
  210. else
  211. {
  212. pair<string, vector<string>> p;
  213. p.first = azCol[i];
  214. p.second.push_back(argv[i]);
  215. _bestScores.insert(p);
  216. }
  217. }
  218.  
  219. return 0;
  220. }
  221.  
  222. int DataBase::callbackPersonalStatus(void* notUsed, int argc, char** argv, char** azCol)
  223. {
  224. int i;
  225.  
  226. for (i = 0; i < argc; i++)
  227. {
  228. auto it = _status.find(azCol[i]);
  229. if (it != _status.end())
  230. {
  231. it->second.push_back(argv[i]);
  232. }
  233. else
  234. {
  235. pair<string, vector<string>> p;
  236. p.first = azCol[i];
  237. p.second.push_back(argv[i]);
  238. _status.insert(p);
  239. }
  240. }
  241.  
  242. return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement