Code_query_group.hpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. #ifndef ODBC_CODEGEN_Code_query_group_hpp
  14. #define ODBC_CODEGEN_Code_query_group_hpp
  15. #include <functional>
  16. #include <common/common.hpp>
  17. #include "Code_query.hpp"
  18. #include "Code_expr_row.hpp"
  19. #include "Code_pred.hpp"
  20. /**
  21.  * @class Plan_query_group
  22.  * @brief Group-by node in PlanTree
  23.  */
  24. class Plan_query_group : public Plan_query {
  25. public:
  26.     Plan_query_group(Plan_root* root);
  27.     virtual ~Plan_query_group();
  28.     Plan_base* analyze(Ctx& ctx, Ctl& ctl);
  29.     Exec_base* codegen(Ctx& ctx, Ctl& ctl);
  30.     void print(Ctx& ctx);
  31.     // children
  32.     void setQuery(Plan_query* query);
  33.     void setDataRow(Plan_expr_row* dataRow);
  34.     void setGroupRow(Plan_expr_row* groupRow);
  35.     void setHavingPred(Plan_pred* havingPred);
  36.     Plan_expr_row* getRow();
  37. protected:
  38.     Plan_query* m_query;
  39.     Plan_expr_row* m_dataRow;
  40.     Plan_expr_row* m_groupRow;
  41.     Plan_pred* m_havingPred;
  42. };
  43. inline
  44. Plan_query_group::Plan_query_group(Plan_root* root) :
  45.     Plan_query(root),
  46.     m_query(0),
  47.     m_dataRow(0),
  48.     m_groupRow(0),
  49.     m_havingPred(0)
  50. {
  51. }
  52. // children
  53. inline void
  54. Plan_query_group::setQuery(Plan_query* query)
  55. {
  56.     ctx_assert(query != 0);
  57.     m_query = query;
  58. }
  59. inline void
  60. Plan_query_group::setDataRow(Plan_expr_row* dataRow)
  61. {
  62.     ctx_assert(dataRow != 0);
  63.     m_dataRow = dataRow;
  64. }
  65. inline void
  66. Plan_query_group::setGroupRow(Plan_expr_row* groupRow)
  67. {
  68.     ctx_assert(groupRow != 0);
  69.     m_groupRow = groupRow;
  70. }
  71. inline void
  72. Plan_query_group::setHavingPred(Plan_pred* havingPred)
  73. {
  74.     ctx_assert(havingPred != 0);
  75.     m_havingPred = havingPred;
  76. }
  77. /**
  78.  * Group-by uses a std::map.  Key is values grouped by.  Data is unique index
  79.  * (starting at 1) into arrays in expression data.
  80.  */
  81. class Exec_query_group;
  82. struct GroupLess : std::binary_function<const SqlRow*, const SqlRow*, bool> {
  83.     bool operator()(const SqlRow* s1, const SqlRow* s2) const;
  84. };
  85. typedef std::map<const SqlRow*, unsigned, GroupLess> GroupList;
  86. /**
  87.  * @class Exec_query_group
  88.  * @brief Group-by node in ExecTree
  89.  */
  90. class Exec_query_group : public Exec_query {
  91. public:
  92.     class Code : public Exec_query::Code {
  93.     public:
  94. Code(const SqlSpecs& sqlSpecs);
  95. virtual ~Code();
  96.     protected:
  97. friend class Exec_query_group;
  98. // sets reference to Sqlspecs from subquery
  99.     };
  100.     class Data : public Exec_query::Data {
  101.     public:
  102. Data(Exec_query_group* node, const SqlSpecs& sqlSpecs);
  103. virtual ~Data();
  104.     protected:
  105. friend class Exec_query_group;
  106. SqlRow m_sqlRow; // current row
  107. bool m_grouped; // fetch and group-by done
  108. unsigned m_count;
  109. GroupList m_groupList;
  110. GroupList::iterator m_iterator;
  111.     };
  112.     Exec_query_group(Exec_root* root);
  113.     virtual ~Exec_query_group();
  114.     void alloc(Ctx& ctx, Ctl& ctl);
  115.     void execImpl(Ctx& ctx, Ctl& ctl);
  116.     bool fetchImpl(Ctx& ctx, Ctl& ctl);
  117.     void close(Ctx& ctx);
  118.     void print(Ctx& ctx);
  119.     // children
  120.     const Code& getCode() const;
  121.     Data& getData() const;
  122.     void setQuery(Exec_query* query);
  123.     void setDataRow(Exec_expr_row* dataRow);
  124.     void setGroupRow(Exec_expr_row* groupRow);
  125.     void setHavingPred(Exec_pred* havingPred);
  126.     const Exec_query* getRawQuery() const;
  127. protected:
  128.     friend class Exec_query;
  129.     Exec_query* m_query;
  130.     Exec_expr_row* m_dataRow;
  131.     Exec_expr_row* m_groupRow;
  132.     Exec_pred* m_havingPred;
  133. };
  134. inline
  135. Exec_query_group::Code::Code(const SqlSpecs& sqlSpecs) :
  136.     Exec_query::Code(sqlSpecs)
  137. {
  138. }
  139. inline
  140. Exec_query_group::Data::Data(Exec_query_group* node, const SqlSpecs& sqlSpecs) :
  141.     Exec_query::Data(node, m_sqlRow),
  142.     m_sqlRow(sqlSpecs),
  143.     m_grouped(false),
  144.     m_count(0)
  145. {
  146. }
  147. inline
  148. Exec_query_group::Exec_query_group(Exec_root* root) :
  149.     Exec_query(root),
  150.     m_query(0),
  151.     m_dataRow(0),
  152.     m_groupRow(0),
  153.     m_havingPred(0)
  154. {
  155. }
  156. // children
  157. inline const Exec_query_group::Code&
  158. Exec_query_group::getCode() const
  159. {
  160.     const Code* code = static_cast<const Code*>(m_code);
  161.     return *code;
  162. }
  163. inline Exec_query_group::Data&
  164. Exec_query_group::getData() const
  165. {
  166.     Data* data = static_cast<Data*>(m_data);
  167.     return *data;
  168. }
  169. inline void
  170. Exec_query_group::setQuery(Exec_query* query)
  171. {
  172.     ctx_assert(m_query == 0 && query != 0);
  173.     m_query = query;
  174. }
  175. inline void
  176. Exec_query_group::setDataRow(Exec_expr_row* dataRow)
  177. {
  178.     ctx_assert(m_dataRow == 0 && dataRow != 0);
  179.     m_dataRow = dataRow;
  180. }
  181. inline void
  182. Exec_query_group::setGroupRow(Exec_expr_row* groupRow)
  183. {
  184.     ctx_assert(m_groupRow == 0 && groupRow != 0);
  185.     m_groupRow = groupRow;
  186. }
  187. inline void
  188. Exec_query_group::setHavingPred(Exec_pred* havingPred)
  189. {
  190.     ctx_assert(m_havingPred == 0 && havingPred != 0);
  191.     m_havingPred = havingPred;
  192. }
  193. #endif