MySQLInterface.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "MySQLInterface.h"
  11. #include "RakAssert.h"
  12. #include "BitStream.h"
  13. #include "FormatString.h"
  14. #include "LinuxStrings.h"
  15. #include <errmsg.h>
  16. #ifdef _WIN32
  17. #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
  18. #else
  19. #include <stdlib.h>//atoi
  20. #endif
  21. #ifdef _WIN32
  22. #include <Winsock2.h>
  23. #endif
  24. #include "mysql.h"
  25. MySQLInterface::MySQLInterface()
  26. {
  27. mySqlConnection=0;
  28. lastError[0]=0;
  29. }
  30. MySQLInterface::~MySQLInterface()
  31. {
  32. Disconnect();
  33. }
  34. bool MySQLInterface::Connect(const char *host,
  35. const char *user,
  36. const char *passwd,
  37. const char *db,
  38. unsigned int port,
  39. const char *unix_socket,
  40. unsigned long clientflag)
  41. {
  42. if (IsConnected())
  43. return false;
  44. _host=host;
  45. _user=user;
  46. _passwd=passwd;
  47. _db=db;
  48. _port=port;
  49. _unix_socket=unix_socket;
  50. _clientflag=clientflag;
  51. mySqlConnection = mysql_init(0);
  52. return mysql_real_connect (mySqlConnection, host, user, passwd, db, port, unix_socket, clientflag) != 0;
  53. }
  54. void MySQLInterface::Disconnect(void)
  55. {
  56. if (IsConnected())
  57. {
  58. mysql_close(mySqlConnection);
  59. mySqlConnection = 0;
  60. }
  61. }
  62. bool MySQLInterface::IsConnected(void) const
  63. {
  64. return mySqlConnection != 0;
  65. }
  66. void MySQLInterface::Commit(void)
  67. {
  68. mysql_query (mySqlConnection, "COMMIT;");
  69. }
  70. void MySQLInterface::Rollback(void)
  71. {
  72. mysql_query (mySqlConnection, "ROLLBACK");
  73. }
  74. bool MySQLInterface::ExecuteBlockingCommand(const char *command)
  75. {
  76. int queryResult;
  77. if ((queryResult=mysql_query(mySqlConnection, command)) != 0)
  78. {
  79. strcpy (lastError, mysql_error (mySqlConnection));
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool MySQLInterface::ExecuteBlockingCommand(const char *command, MYSQL_RES **result, bool rollbackOnFailure)
  85. {
  86. if (!ExecuteBlockingCommand (command))
  87. return false;
  88. *result = mysql_store_result (mySqlConnection);
  89. return *result != 0;
  90. }
  91. char *MySQLInterface::GetLocalTimestamp(void)
  92. {
  93. static char resultString[512];
  94. MYSQL_RES *result;
  95. if (ExecuteBlockingCommand("SELECT LOCALTIMESTAMP", &result, false))
  96. {
  97. MYSQL_ROW row = mysql_fetch_row (result);
  98. if (row [0])
  99. sprintf(resultString,"%s\n", row [0]);
  100. else
  101. resultString[0]=0;
  102. mysql_free_result(result);
  103. }
  104. else
  105. resultString[0]=0;
  106. return (char*)resultString;
  107. }
  108. bool MySQLInterface::ExecuteQueryReadInt (const char * query, int *value)
  109. {
  110. MYSQL_RES * result=0;
  111. if (!ExecuteBlockingCommand(query, &result, false))
  112. {
  113. mysql_free_result(result);
  114. return false;
  115. }
  116. MYSQL_ROW row = mysql_fetch_row (result);
  117. if (row == 0 || mysql_num_fields (result) != 1)
  118. {
  119. mysql_free_result(result);
  120. return false;
  121. }
  122. *value = atoi(row [0]);
  123. mysql_free_result(result);
  124. return true;
  125. }
  126. const char* MySQLInterface::GetLastError(void) const
  127. {
  128. return lastError;
  129. }
  130. RakNet::RakString MySQLInterface::GetEscapedString(const char *input) const
  131. {
  132. unsigned long len = (unsigned long) strlen(input);
  133. char *fn = new char [len*2+1];
  134. mysql_real_escape_string(mySqlConnection, fn, input, len);
  135. RakNet::RakString output;
  136. // Use assignment so it doesn't parse printf escape strings
  137. output = fn;
  138. delete [] fn;
  139. return output;
  140. }
粤ICP备19079148号