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

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. #include <NdbApi.hpp>
  14. #include <common/StmtArea.hpp>
  15. #include <dictionary/DictSchema.hpp>
  16. #include <dictionary/DictColumn.hpp>
  17. #include "Code_query.hpp"
  18. #include "Code_table.hpp"
  19. #include "Code_expr_column.hpp"
  20. #include "Code_root.hpp"
  21. // Plan_expr_column
  22. Plan_expr_column::~Plan_expr_column()
  23. {
  24. }
  25. Plan_base*
  26. Plan_expr_column::analyze(Ctx& ctx, Ctl& ctl)
  27. {
  28.     m_exec = 0;
  29.     analyzeColumn(ctx, ctl);
  30.     if (! ctx.ok())
  31. return 0;
  32.     Plan_expr::m_sqlType = Plan_column::m_sqlType;
  33.     // depends on one table
  34.     m_tableSet.insert(m_resTable);
  35.     // not constant as set-value
  36.     ctl.m_const = false;
  37.     // set alias name
  38.     m_alias = m_name;
  39.     return this;
  40. }
  41. Exec_base*
  42. Plan_expr_column::codegen(Ctx& ctx, Ctl& ctl)
  43. {
  44.     if (m_exec != 0)
  45. return m_exec;
  46.     // connect column to query column
  47.     const Exec_query* execQuery = ctl.m_execQuery;
  48.     ctx_assert(execQuery != 0);
  49.     const Exec_query::Code& codeQuery = execQuery->getCode();
  50.     const SqlSpec sqlSpec(Plan_expr::m_sqlType, SqlSpec::Reference);
  51.     // offset in final output row
  52.     ctx_assert(m_resTable != 0 && m_resTable->m_resOff != 0 && m_resPos != 0);
  53.     unsigned resOff = m_resTable->m_resOff + (m_resPos - 1);
  54.     // create the code
  55.     Exec_expr_column* exec = new Exec_expr_column(ctl.m_execRoot);
  56.     ctl.m_execRoot->saveNode(exec);
  57.     Exec_expr_column::Code& code = *new Exec_expr_column::Code(sqlSpec, resOff);
  58.     exec->setCode(code);
  59.     m_exec = exec;
  60.     return exec;
  61. }
  62. bool
  63. Plan_expr_column::resolveEq(Ctx& ctx, Plan_expr* expr)
  64. {
  65.     ctx_assert(m_resTable != 0 && expr != 0);
  66.     return m_resTable->resolveEq(ctx, this, expr);
  67. }
  68. void
  69. Plan_expr_column::print(Ctx& ctx)
  70. {
  71.     ctx.print(" [expr_column %s]", getPrintName());
  72. }
  73. bool
  74. Plan_expr_column::isEqual(const Plan_expr* expr) const
  75. {
  76.     ctx_assert(expr != 0);
  77.     if (expr->type() != Plan_expr::TypeColumn)
  78. return false;
  79.     const Plan_expr_column* expr2 = static_cast<const Plan_expr_column*>(expr);
  80.     ctx_assert(m_resTable != 0 && expr2->m_resTable != 0);
  81.     if (m_resTable != expr2->m_resTable)
  82. return false;
  83.     ctx_assert(m_dictColumn != 0 && expr2->m_dictColumn != 0);
  84.     if (m_dictColumn != expr2->m_dictColumn)
  85. return false;
  86.     return true;
  87. }
  88. bool
  89. Plan_expr_column::isGroupBy(const Plan_expr_row* row) const
  90. {
  91.     if (isAnyEqual(row))
  92. return true;
  93.     return false;
  94. }
  95. // Exec_expr_column
  96. Exec_expr_column::Code::~Code()
  97. {
  98. }
  99. Exec_expr_column::Data::~Data()
  100. {
  101. }
  102. Exec_expr_column::~Exec_expr_column()
  103. {
  104. }
  105. void
  106. Exec_expr_column::alloc(Ctx& ctx, Ctl& ctl)
  107. {
  108.     if (m_data != 0)
  109. return;
  110.     const Code& code = getCode();
  111.     // connect column to query column
  112.     ctx_assert(ctl.m_query != 0);
  113.     const SqlRow& sqlRow = ctl.m_query->getData().sqlRow();
  114.     SqlField& sqlField = sqlRow.getEntry(code.m_resOff);
  115.     // create the data
  116.     Data& data = *new Data(sqlField);
  117.     setData(data);
  118. }
  119. void
  120. Exec_expr_column::evaluate(Ctx& ctx, Ctl& ctl)
  121. {
  122.     const Code& code = getCode();
  123.     Data& data = getData();
  124.     if (ctl.m_groupIndex != 0) {
  125. SqlField& out = data.groupField(code.sqlSpec().sqlType(), ctl.m_groupIndex, ctl.m_groupInit);
  126. data.sqlField().copy(ctx, out);
  127.     }
  128. }
  129. void
  130. Exec_expr_column::close(Ctx& ctx)
  131. {
  132.     Data& data = getData();
  133.     data.m_groupField.clear();
  134. }
  135. void
  136. Exec_expr_column::print(Ctx& ctx)
  137. {
  138.     const Code& code = getCode();
  139.     ctx.print(" [column %d]", code.m_resOff);
  140. }