9
3

DatabaseResult.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "Log.h"
  19. #include "DatabaseResult.h"
  20. //enforced by MySQL...couldn't find a #define in their headers though
  21. #define FIELD_NAME_MAX 64
  22. //return this instead of NULL for certain functions to prevent crashes from coding errors
  23. static const char *empty_str = "";
  24. DatabaseResult::DatabaseResult(): field_map(), result(0), num_fields(0), row(0) {
  25. }
  26. DatabaseResult::~DatabaseResult() {
  27. unsigned int i;
  28. if (result != NULL)
  29. mysql_free_result(result);
  30. if (field_map.size()) {
  31. field_map.clear();
  32. }
  33. }
  34. bool DatabaseResult::StoreResult(MYSQL_RES* res, uint8 field_count, uint8 row_count) {
  35. //clear any previously stored result
  36. if (result != NULL)
  37. mysql_free_result(result);
  38. //clear any field names from a previous result
  39. if (field_map.size()) {
  40. field_map.clear();
  41. }
  42. result = res;
  43. num_rows = row_count;
  44. num_fields = field_count;
  45. // No rows or fields then we don't care
  46. if (!num_rows || !num_fields) {
  47. mysql_free_result(res);
  48. result = NULL;
  49. return false;
  50. }
  51. const MYSQL_FIELD* fields = mysql_fetch_fields(result);
  52. for (uint8 i = 0; i < num_fields; ++i) {
  53. field_map.emplace(std::make_pair(std::string_view(fields[i].name), i));
  54. }
  55. return true;
  56. }
  57. const char * DatabaseResult::GetFieldValue(unsigned int index) {
  58. if (index >= num_fields) {
  59. LogWrite(DATABASE__ERROR, 0, "Database Result", "Attempt to access field at index %u but there %s only %u field%s", index, num_fields == 1 ? "is" : "are", num_fields, num_fields == 1 ? "" : "s");
  60. return NULL;
  61. }
  62. return row[index];
  63. }
  64. const char * DatabaseResult::GetFieldValueStr(const char *field_name) {
  65. const auto& map_iterator = field_map.find(std::string_view(field_name));
  66. if (map_iterator != field_map.end()) {
  67. return row[map_iterator->second];
  68. }
  69. LogWrite(DATABASE__ERROR, 0, "Database Result", "Unknown field name '%s'", field_name);
  70. return NULL;
  71. }
  72. bool DatabaseResult::Next() {
  73. return (result != NULL && (row = mysql_fetch_row(result)) != NULL);
  74. }
  75. bool DatabaseResult::IsNull(unsigned int index) {
  76. const char *value = GetFieldValue(index);
  77. return value == NULL;
  78. }
  79. bool DatabaseResult::IsNullStr(const char *field_name) {
  80. const char *value = GetFieldValueStr(field_name);
  81. return value == NULL;
  82. }
  83. int8 DatabaseResult::GetInt8(unsigned int index) {
  84. const char *value = GetFieldValue(index);
  85. return value == NULL ? 0 : atoi(value);
  86. }
  87. int8 DatabaseResult::GetInt8Str(const char *field_name) {
  88. const char *value = GetFieldValueStr(field_name);
  89. return value == NULL ? 0 : atoi(value);
  90. }
  91. sint8 DatabaseResult::GetSInt8(unsigned int index) {
  92. const char *value = GetFieldValue(index);
  93. return value == NULL ? 0 : atoi(value);
  94. }
  95. sint8 DatabaseResult::GetSInt8Str(const char *field_name) {
  96. const char *value = GetFieldValueStr(field_name);
  97. return value == NULL ? 0 : atoi(value);
  98. }
  99. int16 DatabaseResult::GetInt16(unsigned int index) {
  100. const char *value = GetFieldValue(index);
  101. return value == NULL ? 0 : atoi(value);
  102. }
  103. int16 DatabaseResult::GetInt16Str(const char *field_name) {
  104. const char *value = GetFieldValueStr(field_name);
  105. return value == NULL ? 0 : atoi(value);
  106. }
  107. sint16 DatabaseResult::GetSInt16(unsigned int index) {
  108. const char *value = GetFieldValue(index);
  109. return value == NULL ? 0 : atoi(value);
  110. }
  111. sint16 DatabaseResult::GetSInt16Str(const char *field_name) {
  112. const char *value = GetFieldValueStr(field_name);
  113. return value == NULL ? 0 : atoi(value);
  114. }
  115. int32 DatabaseResult::GetInt32(unsigned int index) {
  116. const char *value = GetFieldValue(index);
  117. return value == NULL ? 0U : strtoul(value, NULL, 10);
  118. }
  119. int32 DatabaseResult::GetInt32Str(const char *field_name) {
  120. const char *value = GetFieldValueStr(field_name);
  121. return value == NULL ? 0U : strtoul(value, NULL, 10);
  122. }
  123. sint32 DatabaseResult::GetSInt32(unsigned int index) {
  124. const char *value = GetFieldValue(index);
  125. return value == NULL ? 0 : atoi(value);
  126. }
  127. sint32 DatabaseResult::GetSInt32Str(const char *field_name) {
  128. const char *value = GetFieldValueStr(field_name);
  129. return value == NULL ? 0 : atoi(value);
  130. }
  131. uint64 DatabaseResult::GetInt64(unsigned int index) {
  132. const char *value = GetFieldValue(index);
  133. #ifdef _WIN32
  134. return value == NULL ? 0UL : _strtoui64(value, NULL, 10);
  135. #else
  136. return value == NULL ? 0UL : strtoull(value, NULL, 10);
  137. #endif
  138. }
  139. uint64 DatabaseResult::GetInt64Str(const char *field_name) {
  140. const char *value = GetFieldValueStr(field_name);
  141. #ifdef _WIN32
  142. return value == NULL ? 0UL : _strtoui64(value, NULL, 10);
  143. #else
  144. return value == NULL ? 0UL : strtoull(value, NULL, 10);
  145. #endif
  146. }
  147. sint64 DatabaseResult::GetSInt64(unsigned int index) {
  148. const char *value = GetFieldValue(index);
  149. #ifdef _WIN32
  150. return value == NULL ? 0L : _strtoi64(value, NULL, 10);
  151. #else
  152. return value == NULL ? 0L : strtoll(value, NULL, 10);
  153. #endif
  154. }
  155. sint64 DatabaseResult::GetSInt64Str(const char *field_name) {
  156. const char *value = GetFieldValueStr(field_name);
  157. #ifdef _WIN32
  158. return value == NULL ? 0L : _strtoi64(value, NULL, 10);
  159. #else
  160. return value == NULL ? 0L : strtoll(value, NULL, 10);
  161. #endif
  162. }
  163. float DatabaseResult::GetFloat(unsigned int index) {
  164. const char *value = GetFieldValue(index);
  165. return value == NULL ? 0.0F : atof(value);
  166. }
  167. float DatabaseResult::GetFloatStr(const char *field_name) {
  168. const char *value = GetFieldValueStr(field_name);
  169. return value == NULL ? 0.0F : atof(value);
  170. }
  171. char DatabaseResult::GetChar(unsigned int index) {
  172. const char *value = GetFieldValue(index);
  173. return value == NULL ? '\0' : value[0];
  174. }
  175. char DatabaseResult::GetCharStr(const char *field_name) {
  176. const char *value = GetFieldValueStr(field_name);
  177. return value == NULL ? '\0' : value[0];
  178. }
  179. const char * DatabaseResult::GetString(unsigned int index) {
  180. const char *value = GetFieldValue(index);
  181. return value == NULL ? empty_str : value;
  182. }
  183. const char * DatabaseResult::GetStringStr(const char *field_name) {
  184. const char *value = GetFieldValueStr(field_name);
  185. return value == NULL ? empty_str : value;
  186. }