delete_physical_operator.cpp 1.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by WangYunlai on 2022/6/27.
//

#include "common/log/log.h"
羽飞's avatar
羽飞 已提交
16
#include "sql/operator/delete_physical_operator.h"
羽飞's avatar
羽飞 已提交
17
#include "storage/record/record.h"
羽飞's avatar
羽飞 已提交
18
#include "storage/table/table.h"
羽飞's avatar
羽飞 已提交
19
#include "storage/trx/trx.h"
羽飞's avatar
羽飞 已提交
20 21
#include "sql/stmt/delete_stmt.h"

羽飞's avatar
羽飞 已提交
22
RC DeletePhysicalOperator::open(Trx *trx)
羽飞's avatar
羽飞 已提交
23
{
羽飞's avatar
羽飞 已提交
24 25
  if (children_.empty()) {
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
26 27
  }

羽飞's avatar
羽飞 已提交
28
  std::unique_ptr<PhysicalOperator> &child = children_[0];
羽飞's avatar
羽飞 已提交
29
  RC rc = child->open(trx);
羽飞's avatar
羽飞 已提交
30 31 32 33 34
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to open child operator: %s", strrc(rc));
    return rc;
  }

羽飞's avatar
羽飞 已提交
35 36
  trx_ = trx;

羽飞's avatar
羽飞 已提交
37 38 39 40 41 42 43 44 45
  return RC::SUCCESS;
}

RC DeletePhysicalOperator::next()
{
  RC rc = RC::SUCCESS;
  if (children_.empty()) {
    return RC::RECORD_EOF;
  }
L
Longda Feng 已提交
46

羽飞's avatar
羽飞 已提交
47
  PhysicalOperator *child = children_[0].get();
羽飞's avatar
羽飞 已提交
48
  while (RC::SUCCESS == (rc = child->next())) {
羽飞's avatar
羽飞 已提交
49 50
    Tuple *tuple = child->current_tuple();
    if (nullptr == tuple) {
羽飞's avatar
羽飞 已提交
51 52 53 54
      LOG_WARN("failed to get current record: %s", strrc(rc));
      return rc;
    }

羽飞's avatar
羽飞 已提交
55 56
    RowTuple *row_tuple = static_cast<RowTuple *>(tuple);
    Record &record = row_tuple->record();
羽飞's avatar
羽飞 已提交
57
    rc = trx_->delete_record(table_, record);
羽飞's avatar
羽飞 已提交
58 59 60 61 62 63 64 65 66
    if (rc != RC::SUCCESS) {
      LOG_WARN("failed to delete record: %s", strrc(rc));
      return rc;
    }
  }

  return RC::RECORD_EOF;
}

羽飞's avatar
羽飞 已提交
67
RC DeletePhysicalOperator::close()
羽飞's avatar
羽飞 已提交
68
{
羽飞's avatar
羽飞 已提交
69 70 71
  if (!children_.empty()) {
    children_[0]->close();
  }
羽飞's avatar
羽飞 已提交
72 73
  return RC::SUCCESS;
}