Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. nByte 84 size_t
  2. query "SELECT username,password FROM users WHERE username='dilsizk' AND password='12345678'" std::string
  3. return_matrix <not accessible> std::vector<std::vector<std::string>>
  4. return_value 32745 int
  5. this @0x7fe90ab23f5f DBHandler
  6. columns_ -1282717696 int
  7. db_connection_ 0x740000800003f7da sqlite3 * *
  8. log @0x7fe90ab23f5f Log
  9. count -1064971727 int
  10. query_ 0x401f0fc35bd08908 sqlite3_stmt * *
  11. return_matrix_ 0xc748eb7501046e83 std::vector<std::vector<std::string>> *
  12. unusedSQL "12345678" char *
  13.  
  14. //query must be nul-terminated (ending in )
  15. //if an error code is returned, method returns what it has processed to that point
  16. std::vector<std::vector<std::string>>* DBHandler::runQuery(std::string query) {
  17. //get byte size of query
  18. size_t nByte = std::strlen(query.c_str());
  19. //log
  20. log << "DBHandler: Query size is " + std::to_string((int)nByte) +" bytes";
  21. //make junk variable for potential unused sql
  22. const char **unusedSQL;
  23. //prepare the statement
  24. sqlite3_prepare_v2(*db_connection_,query.c_str(),(int)nByte,query_,unusedSQL);
  25. //log
  26. log << "DBHandler: Query formatted into prepared statement";
  27. //perform first step, grab return value
  28. int return_value = sqlite3_step(*query_);
  29. //log
  30. log << "DBHandler: First evaluation step completed";
  31. //check if done, and if it is because of an error or that there is only 1 row
  32. if(return_value != SQLITE_ROW) {
  33. //log
  34. log << "DBHandler: Query returns only 1 row";
  35. if(return_value != SQLITE_DONE) {
  36. //log
  37. log << "DBHandler: Error handling query, code 1";
  38. return (new std::vector<std::vector<std::string>>);
  39. }
  40. }
  41.  
  42. sqlite3_prepare_v2(*db_connection_,query.c_str(),(int)nByte,query_,unusedSQL);
  43.  
  44. #include <algorithm>
  45. #include <sqlite3.h>
  46. #include <vector>
  47. #include <cstring>
  48. #include "log.h"
  49.  
  50. #include <iostream>
  51.  
  52. /* Class to handle interaction with the local test database in the /db_tools directory. Run ./newDB.sh in that directory prior to use. */
  53. class DBHandler {
  54.  
  55. public:
  56. /* Constructor to create DBHandler object. */
  57. /*!
  58. * This object should ALWAYS be destroyed by its destructor when use is done. The constructor only opens a connection to the local database.
  59. */
  60. DBHandler();
  61. /* Destructor to prevent memory leaks from this object. */
  62. /*!
  63. * Important that this is called, as it is otherwise vulnerable to causing multiple memory leaks.
  64. */
  65. ~DBHandler();
  66. /* Runs a query to the database and returns the output in a formatted vector. */
  67. /*!
  68. * This method formats the query, passed as a string, into a prepared statement to be processed.
  69. * param query The query to the database. Must be nul-terminated (ending in ) with no whitespace between the end of the query and the zero-terminator.
  70. * return Returns a vector, where each index holds a vector per column, whose 0 index is the column name and all following indices are entries in order of their row.
  71. */
  72. std::vector<std::vector<std::string>>* runQuery(std::string query);
  73.  
  74. private:
  75. Log log;
  76. sqlite3** db_connection_; /** Database connection object. **/
  77. sqlite3_stmt** query_; /** Prepared statement object. **/
  78. int columns_; /** Number of columns in the prepared statement after 1 step. **/
  79. std::vector<std::vector<std::string>>* return_matrix_; /** Pointer to the matrix containing the evaluation of the prepared statement. **/
  80. /* Fills the return matrix with the names of the columns in the prepared statement. */
  81. void fillColName();
  82. /* Fills each column vector in the return matrix with the entries from the most recent step of the prepared statement. */
  83. void fillRow();
  84. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement