row.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef MYSQLX_ROW_H
  31. #define MYSQLX_ROW_H
  32. /**
  33. @file
  34. TODO
  35. */
  36. #include "common.h"
  37. #include "document.h"
  38. #include "detail/row.h"
  39. #include <memory>
  40. namespace mysqlx {
  41. /**
  42. Represents a single row from a result that contains rows.
  43. Such a row consists of a number of fields, each storing single
  44. value. The number of fields and types of values stored in each
  45. field are described by `RowResult` instance that produced this
  46. row.
  47. Values of fields can be accessed with `get()` method or using
  48. `row[pos]` expression. Fields are identified by 0-based position.
  49. It is also possible to get raw bytes representing value of a
  50. given field with `getBytes()` method.
  51. @sa `Value` class.
  52. @todo Support for iterating over row fields with range-for loop.
  53. @ingroup devapi_res
  54. */
  55. class Row
  56. : private internal::Row_detail
  57. {
  58. Row(internal::Row_detail &&other)
  59. try
  60. : Row_detail(std::move(other))
  61. {}
  62. CATCH_AND_WRAP
  63. public:
  64. Row() {}
  65. template<typename T, typename... Types>
  66. explicit Row(T val, Types... vals)
  67. {
  68. try {
  69. Row_detail::set_values(0, val, vals...);
  70. }
  71. CATCH_AND_WRAP
  72. }
  73. col_count_t colCount() const
  74. {
  75. try {
  76. return Row_detail::col_count();
  77. }
  78. CATCH_AND_WRAP
  79. }
  80. /**
  81. Get raw bytes representing value of row field at position `pos`.
  82. @returns null bytes range if given field is NULL.
  83. @throws out_of_range if given row was not fetched from server.
  84. */
  85. bytes getBytes(col_count_t pos) const
  86. {
  87. try {
  88. return Row_detail::get_bytes(pos);
  89. }
  90. CATCH_AND_WRAP
  91. }
  92. /**
  93. Get reference to row field at position `pos`.
  94. @throws out_of_range if given field does not exist in the row.
  95. */
  96. Value& get(col_count_t pos)
  97. {
  98. try {
  99. return Row_detail::get_val(pos);
  100. }
  101. CATCH_AND_WRAP
  102. }
  103. /**
  104. Set value of row field at position `pos`.
  105. Creates new field if it does not exist.
  106. @returns Reference to the field that was set.
  107. */
  108. Value& set(col_count_t pos, const Value &val)
  109. {
  110. try {
  111. Row_detail::set_values(pos, val);
  112. return Row_detail::get_val(pos);
  113. }
  114. CATCH_AND_WRAP
  115. }
  116. /**
  117. Get const reference to row field at position `pos`.
  118. This is const version of method `get()`.
  119. @throws out_of_range if given field does not exist in the row.
  120. */
  121. const Value& operator[](col_count_t pos) const
  122. {
  123. return const_cast<Row*>(this)->get(pos);
  124. }
  125. /**
  126. Get modifiable reference to row field at position `pos`.
  127. The field is created if it does not exist. In this case
  128. the initial value of the field is NULL.
  129. */
  130. Value& operator[](col_count_t pos)
  131. {
  132. ensure_impl();
  133. try {
  134. return get(pos);
  135. }
  136. catch (const out_of_range&)
  137. {
  138. return set(pos, Value());
  139. }
  140. }
  141. /// Check if this row contains fields or is null.
  142. bool isNull() const { return NULL == m_impl; }
  143. operator bool() const { return !isNull(); }
  144. void clear()
  145. {
  146. try {
  147. Row_detail::clear();
  148. }
  149. CATCH_AND_WRAP
  150. }
  151. private:
  152. using internal::Row_detail::m_impl;
  153. /// @cond IGNORED
  154. friend internal::Row_result_detail<Columns>;
  155. friend internal::Table_insert_detail;
  156. /// @endcond
  157. };
  158. } // mysqlx
  159. #endif