Exec_query_range.cpp
上传用户: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. #include <NdbApi.hpp>
  14. #include <common/StmtArea.hpp>
  15. #include <common/ResultArea.hpp>
  16. #include <codegen/Code_expr.hpp>
  17. #include <codegen/Code_query_range.hpp>
  18. #include <codegen/Code_table.hpp>
  19. #define startBuddyTransaction(x) hupp(x)
  20. void
  21. Exec_query_range::execImpl(Ctx& ctx, Ctl& ctl)
  22. {
  23.     const Code& code = getCode();
  24.     Data& data = getData();
  25.     data.m_done = false;
  26.     Ndb* const ndb = ndbObject();
  27.     NdbConnection* const tcon = ndbConnection();
  28.     if (data.m_con != 0) {
  29. data.m_con->stopScan();
  30. ndb->closeTransaction(data.m_con);
  31. data.m_con = 0;
  32. data.m_op = 0;
  33. ctx_log2(("range scan closed at re-execute"));
  34.     }
  35.     data.m_con = ndb->startBuddyTransaction(tcon);
  36.     if (data.m_con == 0) {
  37. ctx.pushStatus(ndb, tcon, 0, "startBuddyTransaction");
  38. return;
  39.     }
  40.     data.m_op = data.m_con->getNdbOperation(code.m_indexName, code.m_tableName);
  41.     if (data.m_op == 0) {
  42. ctx.pushStatus(ndb, data.m_con, 0, "getNdbOperation");
  43. return;
  44.     }
  45.     if (! code.m_exclusive) {
  46. if (data.m_op->openScanReadCommitted(data.m_parallel) == -1) {
  47.     ctx.pushStatus(ndb, data.m_con, data.m_op, "openScanReadCommitted");
  48.     return;
  49. }
  50.     } else {
  51. if (data.m_op->openScanExclusive(data.m_parallel) == -1) {
  52.     ctx.pushStatus(ndb, data.m_con, data.m_op, "openScanExclusive");
  53.     return;
  54. }
  55.     }
  56.     // set bounds
  57.     for (unsigned k = 1; k <= code.m_keyCount; k++) {
  58. Exec_expr* exprMatch = code.m_keyMatch[k];
  59. ctx_assert(exprMatch != 0);
  60. exprMatch->evaluate(ctx, ctl);
  61. if (! ctx.ok())
  62.     return;
  63. const SqlField& keyMatch = exprMatch->getData().sqlField();
  64. SqlField f(code.m_keySpecs.getEntry(k));
  65. if (! keyMatch.cast(ctx, f)) {
  66.     data.m_done = true; // match is not possible
  67.     return;
  68. }
  69. const NdbAttrId keyId = code.m_keyId[k];
  70. const void* addr = f.addr();
  71. const char* value = static_cast<const char*>(addr);
  72. const unsigned len = f.allocSize();
  73. if (data.m_op->setBound(keyId, NdbOperation::BoundEQ, value, len) == -1) {
  74.     ctx.pushStatus(ndb, data.m_con, data.m_op, "setBound attrId=%u", (unsigned)keyId);
  75.     return;
  76. }
  77.     }
  78.     // queried attributes
  79.     const SqlRow& sqlRow = data.sqlRow();
  80.     ctx_assert(sqlRow.count() == code.m_attrCount);
  81.     for (unsigned i = 1; i <= code.m_attrCount; i++) {
  82. const NdbAttrId attrId = code.m_attrId[i];
  83. SqlField& f = sqlRow.getEntry(i);
  84. char* addr = static_cast<char*>(f.addr());
  85. NdbRecAttr* recAttr = data.m_op->getValue(attrId, addr);
  86. if (recAttr == 0) {
  87.     ctx.pushStatus(ndb, data.m_con, data.m_op, "getValue attrId=%u", (unsigned)attrId);
  88.     return;
  89. }
  90. data.m_recAttr[i] = recAttr;
  91.     }
  92.     if (code.m_attrCount == 0) { // NDB requires one
  93. (void)data.m_op->getValue((NdbAttrId)0);
  94.     }
  95.     data.setCount(0);
  96.     if (data.m_con->executeScan() == -1) {
  97. ctx.pushStatus(ndb, data.m_con, data.m_op, "executeScan");
  98. return;
  99.     }
  100.     ctx_log2(("range scan %s [%08x] started", ! code.m_exclusive ? "read" : "exclusive", (unsigned)this));
  101.     ctl.m_scanOp = data.m_op;
  102. }
  103. bool
  104. Exec_query_range::fetchImpl(Ctx &ctx, Ctl& ctl)
  105. {
  106.     const Code& code = getCode();
  107.     Data& data = getData();
  108.     Ndb* const ndb = ndbObject();
  109.     // if never started
  110.     if (data.m_done)
  111. return false;
  112.     int ret = data.m_con->nextScanResult();
  113.     if (ret != 0) {
  114. if (ret == -1) {
  115.     ctx.pushStatus(ndb, data.m_con, data.m_op, "nextScanResult");
  116. }
  117. data.m_con->stopScan();
  118. ndb->closeTransaction(data.m_con);
  119. data.m_con = 0;
  120. data.m_op = 0;
  121. ctx_log2(("range scan [%08x] closed at last row", (unsigned)this));
  122. return false;
  123.     }
  124.     // set null bits
  125.     const SqlRow& sqlRow = data.sqlRow();
  126.     ctx_assert(sqlRow.count() == code.m_attrCount);
  127.     for (unsigned i = 1; i <= code.m_attrCount; i++) {
  128. NdbRecAttr* recAttr = data.m_recAttr[i];
  129. int isNULL = recAttr->isNULL();
  130. SqlField& f = sqlRow.getEntry(i);
  131. ctx_assert(isNULL == 0 || isNULL == 1);
  132. f.sqlNull(isNULL == 1);
  133.     }
  134.     stmtArea().incTuplesFetched();
  135.     return true;
  136. }