Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. /***********************************************************************
  2. result.cpp - Implements the ResultBase, StoreQueryResult and
  3. UseQuery Result classes.
  4.  
  5. Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
  6. (c) 2004-2007 by Educational Technology Resources, Inc. Others may
  7. also hold copyrights on code in this file. See the CREDITS.txt file
  8. in the top directory of the distribution for details.
  9.  
  10. This file is part of MySQL++.
  11.  
  12. MySQL++ is free software; you can redistribute it and/or modify it
  13. under the terms of the GNU Lesser General Public License as published
  14. by the Free Software Foundation; either version 2.1 of the License, or
  15. (at your option) any later version.
  16.  
  17. MySQL++ is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21.  
  22. You should have received a copy of the GNU Lesser General Public
  23. License along with MySQL++; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  25. USA
  26. ***********************************************************************/
  27.  
  28. #include "result.h"
  29.  
  30. #include "dbdriver.h"
  31.  
  32.  
  33. namespace mysqlpp {
  34.  
  35.  
  36. ResultBase::ResultBase(MYSQL_RES* res, DBDriver* dbd, bool te) :
  37. OptionalExceptions(te),
  38. driver_(res ? dbd : 0),
  39. fields_(Fields::size_type(res ? dbd->num_fields(res) : 0)),
  40. current_field_(0)
  41. {
  42. if (res) {
  43. Fields::size_type i = 0;
  44. const MYSQL_FIELD* pf;
  45. while ((i < fields_.size()) && (pf = dbd->fetch_field(res))) {
  46. fields_[i++] = pf;
  47. }
  48. dbd->field_seek(res, 0); // semantics break otherwise!
  49.  
  50. names_ = new FieldNames(this);
  51. types_ = new FieldTypes(this);
  52. }
  53. }
  54.  
  55.  
  56. ResultBase&
  57. ResultBase::copy(const ResultBase& other)
  58. {
  59. if (this != &other) {
  60. set_exceptions(other.throw_exceptions());
  61.  
  62. if (other.driver_) {
  63. driver_ = other.driver_;
  64. fields_ = other.fields_;
  65. names_ = other.names_;
  66. types_ = other.types_;
  67. current_field_ = other.current_field_;
  68. }
  69. else {
  70. driver_ = 0;
  71. fields_.clear();
  72. names_ = 0;
  73. types_ = 0;
  74. current_field_ = 0;
  75. }
  76. }
  77.  
  78. return *this;
  79. }
  80.  
  81.  
  82. int
  83. ResultBase::field_num(const std::string& i) const
  84. {
  85. size_t index = (*names_)[i];
  86. if ((index >= names_->size()) && throw_exceptions()) {
  87. if (throw_exceptions()) {
  88. throw BadFieldName(i.c_str());
  89. }
  90. else {
  91. return -1;
  92. }
  93. }
  94.  
  95. return int(index);
  96. }
  97.  
  98.  
  99. StoreQueryResult::StoreQueryResult(MYSQL_RES* res, DBDriver* dbd,
  100. bool te) :
  101. ResultBase(res, dbd, te),
  102. list_type(list_type::size_type(res && dbd ? dbd->num_rows(res) : 0)),
  103. copacetic_(res && dbd)
  104. {
  105. if (copacetic_) {
  106. iterator it = begin();
  107. while (MYSQL_ROW row = dbd->fetch_row(res)) {
  108. if (const unsigned long* lengths = dbd->fetch_lengths(res)) {
  109. *it = Row(row, this, lengths, throw_exceptions());
  110. ++it;
  111. }
  112. }
  113.  
  114. dbd->free_result(res);
  115. }
  116. }
  117.  
  118.  
  119. StoreQueryResult&
  120. StoreQueryResult::copy(const StoreQueryResult& other)
  121. {
  122. if (this != &other) {
  123. ResultBase::copy(other);
  124. assign(other.begin(), other.end());
  125. copacetic_ = other.copacetic_;
  126. }
  127.  
  128. return *this;
  129. }
  130.  
  131.  
  132. UseQueryResult::UseQueryResult(MYSQL_RES* res, DBDriver* dbd, bool te) :
  133. ResultBase(res, dbd, te)
  134. {
  135. if (res) {
  136. result_ = res;
  137. }
  138. }
  139.  
  140.  
  141. UseQueryResult&
  142. UseQueryResult::copy(const UseQueryResult& other)
  143. {
  144. if (this != &other) {
  145. ResultBase::copy(other);
  146. if (other.result_) {
  147. result_ = other.result_;
  148. }
  149. else {
  150. result_ = 0;
  151. }
  152. }
  153.  
  154. return *this;
  155. }
  156.  
  157.  
  158. const unsigned long*
  159. UseQueryResult::fetch_lengths() const
  160. {
  161. return driver_->fetch_lengths(result_.raw());
  162. }
  163.  
  164.  
  165. Row
  166. UseQueryResult::fetch_row() const
  167. {
  168. if (!result_) {
  169. if (throw_exceptions()) {
  170. throw UseQueryError("Results not fetched");
  171. }
  172. else {
  173. return Row();
  174. }
  175. }
  176.  
  177. MYSQL_ROW row = driver_->fetch_row(result_.raw());
  178. if (row) {
  179. const unsigned long* lengths = fetch_lengths();
  180. if (lengths) {
  181. return Row(row, this, lengths, throw_exceptions());
  182. }
  183. else {
  184. if (throw_exceptions()) {
  185. throw UseQueryError("Failed to get field lengths");
  186. }
  187. else {
  188. return Row();
  189. }
  190. }
  191. }
  192. else {
  193. // Prior to v3, this was considered an error, but it just means
  194. // we've fallen off the end of a "use" query's result set. You
  195. // can't predict when this will happen, but it isn't an error.
  196. // Just return a falsy row object so caller's loop terminates.
  197. return Row();
  198. }
  199. }
  200.  
  201.  
  202. MYSQL_ROW
  203. UseQueryResult::fetch_raw_row() const
  204. {
  205. return driver_->fetch_row(result_.raw());
  206. }
  207.  
  208.  
  209. } // end namespace mysqlpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement