result.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __CH_RESUL_H__
  17. #define __CH_RESUL_H__
  18. #include "field/field.h"
  19. class ResultSet : public DTCFieldSet {
  20. private:
  21. int err;
  22. DTCBinary init;
  23. DTCBinary curr;
  24. int numRows;
  25. int rowno;
  26. RowValue row;
  27. public:
  28. /*fieldset at largest size*/
  29. ResultSet(const uint8_t *idtab, int num, int total,
  30. DTCTableDefinition *t)
  31. : DTCFieldSet(idtab, num, total), err(0),
  32. init(DTCBinary::Make(NULL, 0)),
  33. curr(DTCBinary::Make(NULL, 0)), numRows(0), rowno(0), row(t)
  34. {
  35. }
  36. ResultSet(const DTCFieldSet &f, DTCTableDefinition *t)
  37. : DTCFieldSet(f), err(0), init(DTCBinary::Make(NULL, 0)),
  38. curr(DTCBinary::Make(NULL, 0)), numRows(0), rowno(0), row(t)
  39. {
  40. }
  41. ~ResultSet(void)
  42. {
  43. }
  44. inline void Clean()
  45. {
  46. DTCFieldSet::Clean();
  47. err = 0;
  48. init = DTCBinary::Make(NULL, 0);
  49. curr = DTCBinary::Make(NULL, 0);
  50. numRows = 0;
  51. rowno = 0;
  52. row.Clean();
  53. }
  54. inline int field_set_max_fields()
  55. {
  56. return DTCFieldSet::max_fields();
  57. }
  58. inline void realloc_field_set(int total)
  59. {
  60. DTCFieldSet::Realloc(total);
  61. }
  62. /* clean before set*/
  63. inline void Set(const uint8_t *idtab, int num)
  64. {
  65. DTCFieldSet::Set(idtab, num);
  66. //row.set_table_definition(t);
  67. }
  68. inline void set_value_data(int nr, DTCBinary b)
  69. {
  70. rowno = 0;
  71. numRows = nr;
  72. init = b;
  73. curr = b;
  74. }
  75. int total_rows(void) const
  76. {
  77. return numRows;
  78. }
  79. const DTCValue &operator[](int id) const
  80. {
  81. return row[id];
  82. }
  83. const DTCValue *field_value(int id) const
  84. {
  85. return row.field_value(id);
  86. }
  87. const DTCValue *field_value(const char *id) const
  88. {
  89. return row.field_value(id);
  90. }
  91. int decode_row(void);
  92. const RowValue *row_value(void) const
  93. {
  94. if (err)
  95. return NULL;
  96. return &row;
  97. }
  98. const RowValue *fetch_row(void)
  99. {
  100. if (decode_row())
  101. return NULL;
  102. return &row;
  103. }
  104. RowValue *_fetch_row(void)
  105. {
  106. if (decode_row())
  107. return NULL;
  108. return &row;
  109. }
  110. int error_num(void) const
  111. {
  112. return err;
  113. }
  114. int rewind(void)
  115. {
  116. curr = init;
  117. err = 0;
  118. rowno = 0;
  119. return 0;
  120. }
  121. int data_len(void) const
  122. {
  123. return init.len;
  124. }
  125. char* data(void) const
  126. {
  127. return init.ptr;
  128. }
  129. };
  130. class ResultWriter {
  131. ResultWriter(const ResultWriter &); // NOT IMPLEMENTED
  132. public:
  133. const DTCFieldSet *fieldSet;
  134. unsigned int limitStart, limitNext;
  135. unsigned int totalRows;
  136. unsigned int numRows;
  137. ResultWriter(const DTCFieldSet *, unsigned int, unsigned int);
  138. virtual ~ResultWriter(void)
  139. {
  140. }
  141. inline virtual void Clean()
  142. {
  143. totalRows = 0;
  144. numRows = 0;
  145. }
  146. virtual void detach_result() = 0;
  147. inline virtual int Set(const DTCFieldSet *fs, unsigned int st,
  148. unsigned int cnt)
  149. {
  150. if (cnt == 0)
  151. st = 0;
  152. limitStart = st;
  153. limitNext = st + cnt;
  154. const int nf = fs == NULL ? 0 : fs->num_fields();
  155. fieldSet = nf ? fs : NULL;
  156. return 0;
  157. }
  158. virtual int append_row(const RowValue &) = 0;
  159. virtual int merge_no_limit(const ResultWriter *rp) = 0;
  160. int set_rows(unsigned int rows);
  161. void set_total_rows(unsigned int totalrows)
  162. {
  163. totalRows = totalrows;
  164. }
  165. void add_total_rows(int n)
  166. {
  167. totalRows += n;
  168. }
  169. int is_full(void) const
  170. {
  171. return limitNext > 0 && totalRows >= limitNext;
  172. }
  173. int in_range(unsigned int total, unsigned int begin = 0) const
  174. {
  175. if (limitNext == 0)
  176. return (1);
  177. if (total <= limitStart || begin >= limitNext)
  178. return (0);
  179. return (1);
  180. }
  181. };
  182. class ResultPacket : public ResultWriter {
  183. ResultPacket(const ResultPacket &); // NOT IMPLEMENTED
  184. public:
  185. /* acctually just one buff */
  186. BufferChain *bc;
  187. unsigned int rowDataBegin;
  188. ResultPacket(const DTCFieldSet *, unsigned int, unsigned int);
  189. virtual ~ResultPacket(void);
  190. void Clean();
  191. inline virtual void detach_result()
  192. {
  193. bc = NULL;
  194. }
  195. virtual int Set(const DTCFieldSet *fieldList, unsigned int st,
  196. unsigned int ct);
  197. virtual int append_row(const RowValue &);
  198. virtual int merge_no_limit(const ResultWriter *rp);
  199. };
  200. class ResultBuffer : public ResultWriter {
  201. ResultBuffer(const ResultBuffer &); // NOT IMPLEMENTED
  202. public:
  203. ResultBuffer(const DTCFieldSet *, unsigned int, unsigned int);
  204. virtual ~ResultBuffer(void);
  205. virtual int append_row(const RowValue &);
  206. virtual int merge_no_limit(const ResultWriter *rp);
  207. };
  208. #endif