Code_delete_index.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 <common/StmtArea.hpp>
  14. #include <dictionary/DictTable.hpp>
  15. #include "Code_expr.hpp"
  16. #include "Code_delete_index.hpp"
  17. #include "Code_table.hpp"
  18. #include "Code_root.hpp"
  19. Plan_delete_index::~Plan_delete_index()
  20. {
  21. }
  22. Plan_base*
  23. Plan_delete_index::analyze(Ctx& ctx, Ctl& ctl)
  24. {
  25.     ctx_assert(m_query != 0);
  26.     m_query->analyze(ctx, ctl);
  27.     if (! ctx.ok())
  28. return 0;
  29.     ctx_assert(m_table != 0);
  30.     m_table->analyze(ctx, ctl);
  31.     if (! ctx.ok())
  32. return 0;
  33.     return this;
  34. }
  35. void
  36. Plan_delete_index::describe(Ctx& ctx)
  37. {
  38.     stmtArea().setFunction(ctx, "DELETE WHERE", SQL_DIAG_DELETE_WHERE);
  39. }
  40. Exec_base*
  41. Plan_delete_index::codegen(Ctx& ctx, Ctl& ctl)
  42. {   
  43.     // create code for the query
  44.     ctx_assert(m_query != 0);
  45.     Exec_query* execQuery = static_cast<Exec_query*>(m_query->codegen(ctx, ctl));
  46.     if (! ctx.ok())
  47. return 0;
  48.     ctx_assert(execQuery != 0);
  49.     // set up
  50.     ctx_assert(m_table != 0 && m_index != 0);
  51.     const BaseString& tableName = m_table->getName();
  52.     ctx_assert(m_index->m_dictIndex != 0);
  53.     const DictIndex& dictIndex = *m_index->m_dictIndex;
  54.     const BaseString& indexName = dictIndex.getName();
  55.     const unsigned keyCount = m_index->m_keyCount;
  56.     // create the code
  57.     Exec_delete_index::Code& code = *new Exec_delete_index::Code(keyCount);
  58.     code.m_tableName = strcpy(new char[tableName.length() + 1], tableName.c_str());
  59.     code.m_indexName = strcpy(new char[indexName.length() + 1], indexName.c_str());
  60.     // key attributes
  61.     code.m_keyId = new NdbAttrId[1 + keyCount];
  62.     code.m_keyId[0] = (NdbAttrId)-1;
  63.     for (unsigned k = 1; k <= keyCount; k++) {
  64. const DictColumn* keyColumn = dictIndex.getColumn(k);
  65. const SqlType& sqlType = keyColumn->sqlType();
  66. SqlSpec sqlSpec(sqlType, SqlSpec::Physical);
  67. code.m_keySpecs.setEntry(k, sqlSpec);
  68. code.m_keyId[k] = k - 1; // index column order
  69.     }
  70.     // matching expressions
  71.     ctx_assert(m_index->m_keyFound);
  72.     const ExprVector& keyEq = m_index->m_keyEq;
  73.     ctx_assert(keyEq.size() == 1 + keyCount);
  74.     code.m_keyMatch = new Exec_expr* [1 + keyCount];
  75.     code.m_keyMatch[0] = 0;
  76.     for (unsigned k = 1; k <= keyCount; k++) {
  77. Plan_expr* expr = keyEq[k];
  78. Exec_expr* execExpr = static_cast<Exec_expr*>(expr->codegen(ctx, ctl));
  79. if (! ctx.ok())
  80.     return 0;
  81. ctx_assert(execExpr != 0);
  82. code.m_keyMatch[k] = execExpr;
  83.     }
  84.     // create the exec
  85.     Exec_delete_index* exec = new Exec_delete_index(ctl.m_execRoot);
  86.     ctl.m_execRoot->saveNode(exec);
  87.     exec->setCode(code);
  88.     exec->setQuery(execQuery);
  89.     return exec;
  90. }    
  91. void
  92. Plan_delete_index::print(Ctx& ctx)
  93. {
  94.     ctx.print(" [delete_index");
  95.     Plan_base* a[] = { m_query, m_table };
  96.     printList(ctx, a, 2);
  97.     ctx.print("]");
  98. }
  99. // Exec_delete_index
  100. Exec_delete_index::Code::~Code()
  101. {
  102.     delete[] m_tableName;
  103.     delete[] m_keyId;
  104.     delete[] m_keyMatch;
  105. }
  106. Exec_delete_index::Data::~Data()
  107. {
  108. }
  109. Exec_delete_index::~Exec_delete_index()
  110. {
  111. }
  112. void
  113. Exec_delete_index::alloc(Ctx& ctx, Ctl& ctl)
  114. {
  115.     const Code& code = getCode();
  116.     // allocate the subquery
  117.     ctx_assert(m_query != 0);
  118.     m_query->alloc(ctx, ctl);
  119.     if (! ctx.ok())
  120.         return;
  121.     // allocate matching expressions
  122.     for (unsigned k = 1; k <= code.m_keyCount; k++) {
  123. Exec_expr* expr = code.m_keyMatch[k];
  124. ctx_assert(expr != 0);
  125. expr->alloc(ctx, ctl);
  126. if (! ctx.ok())
  127.     return;
  128.     }
  129.     // create data
  130.     Data& data = *new Data;
  131.     setData(data);
  132. }
  133. void
  134. Exec_delete_index::close(Ctx& ctx)
  135. {
  136.     ctx_assert(m_query != 0);
  137.     m_query->close(ctx);
  138. }
  139. void
  140. Exec_delete_index::print(Ctx& ctx)
  141. {
  142.     const Code& code = getCode();
  143.     ctx.print(" [delete_index");
  144.     Exec_base* a[] = { m_query };
  145.     printList(ctx, a, 1);
  146.     printList(ctx, (Exec_base**)&code.m_keyMatch[1], code.m_keyCount);
  147.     ctx.print("]");
  148. }