Guest User

Untitled

a guest
Sep 26th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.30 KB | None | 0 0
  1. #include "mysqlquery.h"
  2.  
  3. MySQLConnection::MySQLConnection()
  4. {
  5.     m_bIsConnected = false;
  6.     m_MySQLConn = NULL;
  7.     m_sHostname = "";
  8.     m_sUsername = "";
  9.     m_sPassword = "";
  10.     m_wPort = 0;
  11.     m_sSchemaName = "";
  12. }
  13.  
  14. MySQLConnection::~MySQLConnection()
  15. {
  16.     if (m_MySQLConn != NULL)
  17.     {
  18.         std::clog << "Closing MySQL Connection" << std::endl;
  19.         mysql_close(m_MySQLConn);
  20.     }
  21. }
  22.  
  23. bool MySQLConnection::Connect(const std::string &sHostname, const uint16_t &wPort, const std::string &sUsername, const std::string &sPassword, const std::string &sDB = NULL)
  24. {
  25.     // If we're already connected, we should close the first connection
  26.     Disconnect();
  27.  
  28.     m_sHostname = sHostname;
  29.     m_sUsername = sUsername;
  30.     m_sPassword = sPassword;
  31.     m_wPort = wPort;
  32.     m_sSchemaName = sDB;
  33.     m_bIsConnected = false;
  34.  
  35.     MYSQL *MySQLConnRet = NULL;
  36.     m_MySQLConn = mysql_init(m_MySQLConn);
  37.  
  38.     std::clog << "Connection to " << m_sUsername << "@" << m_sHostname << ":" << wPort << "..." << std::endl;
  39.  
  40.     MySQLConnRet = mysql_real_connect(m_MySQLConn, m_sHostname.c_str(), m_sUsername.c_str(), m_sPassword.c_str(), m_sSchemaName.c_str(), m_wPort, NULL, 0);
  41.  
  42.     if (MySQLConnRet == NULL)
  43.     {
  44.         m_bIsConnected = false;
  45.         std::cerr << "Connection failed: " << mysql_error(m_MySQLConn);
  46.     }
  47.     else {
  48.         m_bIsConnected = true;
  49.         std::clog << "Connected!" << std::endl;
  50.     }
  51.  
  52.     return m_bIsConnected;
  53. }
  54.  
  55. void MySQLConnection::Disconnect()
  56. {
  57.     if (m_bIsConnected)
  58.     {
  59.         mysql_close(m_MySQLConn);
  60.         std::clog << "Disconnected from MySQL DB!" << std::endl;
  61.     }
  62.  
  63.  
  64.     m_bIsConnected = false;
  65.  
  66. }
  67.  
  68. bool MySQLConnection::SelectDB(const std::string &sSchemaName)
  69. {
  70.     if (!m_bIsConnected)
  71.     {
  72.         std::cerr << "Not connected to MySQL DB!" << std::endl;
  73.         return false;
  74.     }
  75.  
  76.     if (mysql_select_db(m_MySQLConn, sSchemaName.c_str()) != 0)
  77.     {
  78.         std::cerr << "Failed to select DB! Error: " << mysql_error(m_MySQLConn) << std::endl;
  79.         return false;
  80.     }
  81.     else {
  82.         m_sSchemaName = sSchemaName.c_str();
  83.         std::clog << "Selected database \"" << sSchemaName << "\"" << std::endl;
  84.         return true;
  85.     }
  86. }
  87.  
  88. const std::string MySQLConnection::GetLastError() const
  89. {
  90.     if (!m_bIsConnected)
  91.     {
  92.         std::cerr << "Not connected to MySQL DB!" << std::endl;
  93.         return "Not connected";
  94.     }
  95.  
  96.     return (char*)mysql_error(m_MySQLConn);
  97. }
  98.  
  99. MYSQL *MySQLConnection::getConn()
  100. {
  101.     return m_MySQLConn;
  102. }
  103.  
  104. bool MySQLConnection::IsConnected()
  105. {
  106.     return m_bIsConnected;
  107. }
  108.  
  109. const std::string MySQLConnection::EscapeString(const std::string &value) const
  110. {
  111.     if (!m_bIsConnected)
  112.     {
  113.         std::cerr << "Not connected to MySQL DB!" << std::endl;
  114.         return "";
  115.     }
  116.  
  117.     char *cValue = new char[(value.length() * 2) + 1];
  118.     mysql_real_escape_string(m_MySQLConn, cValue, value.c_str(), value.length());
  119.  
  120.     std::string sRet = cValue;
  121.     delete[] cValue;
  122.  
  123.     return sRet;
  124. }
  125.  
  126. MySQLQuery::MySQLQuery(MySQLConnection *mConn, const std::string &sStatement)
  127. {
  128.     m_sqlConn = mConn;
  129.     m_sStatement = sStatement;
  130.     m_iResultRowCount = 0;
  131.  
  132.     int argCount = std::count(m_sStatement.begin(), m_sStatement.end(), '?');
  133.     for (int i = 1; i <= argCount; i++)
  134.     {
  135.         m_mArgMap.insert(std::pair<int, std::string>(i, ""));
  136.     }
  137. }
  138.  
  139. bool MySQLQuery::setString(const unsigned int &idx, const std::string &value)
  140. {
  141.  
  142.     if (idx > m_mArgMap.size())
  143.     {
  144.         std::cerr << "Index exceeds total arg count in statement" << std::endl;
  145.         return false;
  146.     }
  147.  
  148.     std::stringstream ss;
  149.     std::string escapedValue = m_sqlConn->EscapeString(value);
  150.     ss << "\"" << escapedValue << "\"";
  151.     m_mArgMap[idx] = ss.str();
  152.  
  153.     return true;
  154. }
  155.  
  156. bool MySQLQuery::setInt(const unsigned int &idx, const int &value)
  157. {
  158.     if (idx > m_mArgMap.size())
  159.     {
  160.         std::cerr << "Index exceeds total arg count in statement" << std::endl;
  161.         return false;
  162.     }
  163.  
  164.     std::stringstream ss;
  165.     ss << value;
  166.     m_mArgMap[idx] = ss.str();
  167.  
  168.     return true;
  169. }
  170.  
  171. bool MySQLQuery::setDouble(const unsigned int &idx, const double &value)
  172. {
  173.     if (idx > m_mArgMap.size())
  174.     {
  175.         std::cerr << "Index exceeds total arg count in statement" << std::endl;
  176.         return false;
  177.     }
  178.  
  179.     std::stringstream ss;
  180.     ss << value;
  181.     m_mArgMap[idx] = ss.str();
  182.  
  183.     return true;
  184. }
  185.  
  186. bool MySQLQuery::setTime(const unsigned int &idx, const time_t &value)
  187. {
  188.     if (idx > m_mArgMap.size())
  189.     {
  190.         std::cerr << "Index exceeds total arg count in statement" << std::endl;
  191.         return false;
  192.     }
  193.  
  194.     std::stringstream ss;
  195.     ss << value;
  196.     m_mArgMap[idx] = ss.str();
  197.  
  198.     return true;
  199. }
  200.  
  201. bool MySQLQuery::setNull(const unsigned int &idx)
  202. {
  203.     if (idx > m_mArgMap.size())
  204.     {
  205.         std::cerr << "Index exceeds total arg count in statement" << std::endl;
  206.         return false;
  207.     }
  208.  
  209.     m_mArgMap[idx] = "NULL";
  210.     return true;
  211. }
  212.  
  213. const std::string MySQLQuery::getFieldName(const unsigned int &field)
  214. {
  215.     if (field < 1)
  216.     {
  217.         std::cerr << "The field index has to be over 1!" << std::endl;
  218.         return NULL;
  219.     }
  220.     else if (m_mFieldMap.size() < field) {
  221.         std::cerr << "There are only " << m_mFieldMap.size() << " fields available!" << std::endl;
  222.         return NULL;
  223.     }
  224.  
  225.     std::string sFieldName = m_mFieldMap[field];
  226.     return sFieldName;
  227. }
  228.  
  229. const std::string MySQLQuery::getString(const unsigned int &row, const unsigned int &field)
  230. {
  231.     if (GetResultRowCount() < 1)
  232.     {
  233.         std::cerr << "The query didn't return any rows!" << std::endl;
  234.         return NULL;
  235.     }
  236.     else if (GetResultRowCount() < row)
  237.     {
  238.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  239.         return NULL;
  240.     }
  241.     else if (row < 1)
  242.     {
  243.         std::cerr << "The selected row has to be > 1" << std::endl;
  244.         return NULL;
  245.     }
  246.  
  247.     TResultRow rSelectedRow;
  248.     rSelectedRow = m_mResultMap[row - 1];
  249.  
  250.     std::string sValue = rSelectedRow[field];
  251.  
  252.     return sValue;
  253. }
  254.  
  255. const std::string MySQLQuery::getString(const unsigned int &row, const std::string &field)
  256. {
  257.     if (GetResultRowCount() < 1)
  258.     {
  259.         std::cerr << "The query didn't return any rows!" << std::endl;
  260.         return "";
  261.     }
  262.     else if (GetResultRowCount() < row)
  263.     {
  264.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  265.         return "";
  266.     }
  267.     else if (row < 1)
  268.     {
  269.         std::cerr << "The selected row has to be > 1" << std::endl;
  270.         return "";
  271.     }
  272.  
  273.     TResultRow rSelectedRow;
  274.     rSelectedRow = m_mResultMap[row - 1];
  275.  
  276.     int iFieldID = m_mFieldStringToIntMap[field];
  277.     std::string sValue = rSelectedRow[iFieldID];
  278.  
  279.     return sValue;
  280. }
  281.  
  282. int MySQLQuery::getInt(const unsigned int &row, const unsigned int &field)
  283. {
  284.     if (GetResultRowCount() < 1)
  285.     {
  286.         std::cerr << "The query didn't return any rows!" << std::endl;
  287.         return -1;
  288.     }
  289.     else if (GetResultRowCount() < row)
  290.     {
  291.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  292.         return -1;
  293.     }
  294.     else if (row < 1)
  295.     {
  296.         std::cerr << "The selected row has to be > 1" << std::endl;
  297.         return -1;
  298.     }
  299.  
  300.     TResultRow rSelectedRow;
  301.     rSelectedRow = m_mResultMap[row - 1];
  302.  
  303.     int iValue = atoi(rSelectedRow[field].c_str());
  304.  
  305.     return iValue;
  306. }
  307.  
  308. int MySQLQuery::getInt(const unsigned int &row, const std::string &field)
  309. {
  310.     if (GetResultRowCount() < 1)
  311.     {
  312.         std::cerr << "The query didn't return any rows!" << std::endl;
  313.         return -1;
  314.     }
  315.     else if (GetResultRowCount() < row)
  316.     {
  317.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  318.         return -1;
  319.     }
  320.     else if (row < 1)
  321.     {
  322.         std::cerr << "The selected row has to be > 1" << std::endl;
  323.         return -1;
  324.     }
  325.  
  326.     TResultRow rSelectedRow;
  327.     rSelectedRow = m_mResultMap[row - 1];
  328.  
  329.     int iFieldID = m_mFieldStringToIntMap[field];
  330.     int iValue = atoi(rSelectedRow[iFieldID].c_str());
  331.  
  332.     return iValue;
  333. }
  334.  
  335. double MySQLQuery::getDouble(const unsigned int &row, const unsigned int &field)
  336. {
  337.     if (GetResultRowCount() < 1)
  338.     {
  339.         std::cerr << "The query didn't return any rows!" << std::endl;
  340.         return -1.0;
  341.     }
  342.     else if (GetResultRowCount() < row)
  343.     {
  344.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  345.         return -1.0;
  346.     }
  347.     else if (row < 1)
  348.     {
  349.         std::cerr << "The selected row has to be > 1" << std::endl;
  350.         return -1.0;
  351.     }
  352.  
  353.     TResultRow rSelectedRow;
  354.     rSelectedRow = m_mResultMap[row - 1];
  355.  
  356.     double dValue = atof(rSelectedRow[field].c_str());
  357.  
  358.     return dValue;
  359. }
  360.  
  361. double MySQLQuery::getDouble(const unsigned int &row, const std::string &field)
  362. {
  363.     if (GetResultRowCount() < 1)
  364.     {
  365.         std::cerr << "The query didn't return any rows!" << std::endl;
  366.         return -1.0;
  367.     }
  368.     else if (GetResultRowCount() < row)
  369.     {
  370.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  371.         return -1.0;
  372.     }
  373.     else if (row < 1)
  374.     {
  375.         std::cerr << "The selected row has to be > 1" << std::endl;
  376.         return -1.0;
  377.     }
  378.  
  379.     TResultRow rSelectedRow;
  380.     rSelectedRow = m_mResultMap[row - 1];
  381.  
  382.     int iFieldID = m_mFieldStringToIntMap[field];
  383.     double dValue = atof(rSelectedRow[iFieldID].c_str());
  384.  
  385.     return dValue;
  386. }
  387.  
  388. time_t MySQLQuery::getTime(const unsigned int &row, const unsigned int &field)
  389. {
  390.     if (GetResultRowCount() < 1)
  391.     {
  392.         std::cerr << "The query didn't return any rows!" << std::endl;
  393.         return -1;
  394.     }
  395.     else if (GetResultRowCount() < row)
  396.     {
  397.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  398.         return -1;
  399.     }
  400.     else if (row < 1)
  401.     {
  402.         std::cerr << "The selected row has to be > 1" << std::endl;
  403.         return -1;
  404.     }
  405.  
  406.     TResultRow rSelectedRow;
  407.     rSelectedRow = m_mResultMap[row - 1];
  408.  
  409.     time_t tValue = atoi(rSelectedRow[field].c_str());
  410.  
  411.     return tValue;
  412. }
  413.  
  414. time_t MySQLQuery::getTime(const unsigned int &row, const std::string &field)
  415. {
  416.     if (GetResultRowCount() < 1)
  417.     {
  418.         std::cerr << "The query didn't return any rows!" << std::endl;
  419.         return -1;
  420.     }
  421.     else if (GetResultRowCount() < row)
  422.     {
  423.         std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
  424.         return -1;
  425.     }
  426.     else if (row < 1)
  427.     {
  428.         std::cerr << "The selected row has to be > 1" << std::endl;
  429.         return -1;
  430.     }
  431.  
  432.     TResultRow rSelectedRow;
  433.     rSelectedRow = m_mResultMap[row - 1];
  434.  
  435.     int iFieldID = m_mFieldStringToIntMap[field];
  436.     time_t tValue = atoi(rSelectedRow[iFieldID].c_str());
  437.  
  438.     return tValue;
  439. }
  440.  
  441.  
  442. unsigned int MySQLQuery::GetResultRowCount()
  443. {
  444.     const int iRowCount = m_mResultMap.size();
  445.     return iRowCount;
  446. }
  447.  
  448. unsigned int MySQLQuery::GetFieldCount()
  449. {
  450.     const int iFieldCount = m_mFieldMap.size();
  451.     return iFieldCount;
  452. }
  453.  
  454. const std::string MySQLQuery::BuildQueryString()
  455. {
  456.     // replace each '?' with the corresponding value
  457.     int iLastFoundPos = 0;
  458.     std::string sPreparedStatement;
  459.     sPreparedStatement = m_sStatement;
  460.     for (unsigned int i = 1; i <= m_mArgMap.size(); i++)
  461.     {
  462.         iLastFoundPos = sPreparedStatement.find('?');
  463.         sPreparedStatement.replace(iLastFoundPos, 1, "");
  464.         sPreparedStatement.insert(iLastFoundPos, m_mArgMap[i]);
  465.     }
  466.  
  467.     return sPreparedStatement;
  468. }
  469.  
  470. bool MySQLQuery::ExecuteQuery()
  471. {
  472.     std::string sStatement = BuildQueryString();
  473.  
  474.     if (mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
  475.     {
  476.         std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
  477.         return false;
  478.     }
  479.  
  480.     MYSQL_RES *result = mysql_store_result(m_sqlConn->getConn());
  481.  
  482.     if (result == NULL)
  483.     {
  484.         std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
  485.         return false;
  486.     }
  487.  
  488.  
  489.     int iNumFields = mysql_num_fields(result);
  490.     MYSQL_ROW row;
  491.     MYSQL_FIELD *field;
  492.  
  493.     // Get field names and store it in the map
  494.     int i = 0;
  495.     while ((field = mysql_fetch_field(result)))
  496.     {
  497.         m_mFieldMap.insert(std::pair<int, std::string>(i, field->name));
  498.         m_mFieldStringToIntMap.insert(std::pair<std::string, int>(field->name, i));
  499.         i++;
  500.     }
  501.  
  502.     // Get Rows
  503.     i = 0;
  504.     while ((row = mysql_fetch_row(result)))
  505.     {
  506.         TResultRow resRow;
  507.         for (int n = 0; n < iNumFields; n++)
  508.         {
  509.             resRow.insert(std::pair<int, std::string>(n, row[n] ? row[n] : "NULL"));
  510.         }
  511.  
  512.         m_mResultMap.insert(std::pair<int, TResultRow>(i, resRow));
  513.  
  514.         i++;
  515.     }
  516.  
  517.     return true;
  518. }
  519.  
  520. bool MySQLQuery::ExecuteUpdate()
  521. {
  522.     std::string sStatement = BuildQueryString();
  523.  
  524.     if (mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
  525.     {
  526.         std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
  527.         return false;
  528.     }
  529.  
  530.     return true;
  531. }
  532.  
  533. int MySQLQuery::ExecuteInsert()
  534. {
  535.     std::string sStatement = BuildQueryString();
  536.  
  537.     if (mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
  538.     {
  539.         std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
  540.         return 0;
  541.     }
  542.  
  543.     int iLastInsertID = mysql_insert_id(m_sqlConn->getConn());
  544.  
  545.     return iLastInsertID;
  546. }
Add Comment
Please, Sign In to add comment