delete_stmt.cpp 2.0 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
W
wangyunlai.wyl 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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/5/22.
//

#include "common/log/log.h"
#include "sql/stmt/delete_stmt.h"
#include "sql/stmt/filter_stmt.h"
#include "storage/common/db.h"
#include "storage/common/table.h"

L
Longda Feng 已提交
21
DeleteStmt::DeleteStmt(Table *table, FilterStmt *filter_stmt) : table_(table), filter_stmt_(filter_stmt)
W
wangyunlai.wyl 已提交
22 23 24 25 26 27 28
{}

DeleteStmt::~DeleteStmt()
{
  if (nullptr != filter_stmt_) {
    delete filter_stmt_;
    filter_stmt_ = nullptr;
L
Longda Feng 已提交
29
  }
W
wangyunlai.wyl 已提交
30 31 32 33
}

RC DeleteStmt::create(Db *db, const Deletes &delete_sql, Stmt *&stmt)
{
羽飞's avatar
羽飞 已提交
34
  const char *table_name = delete_sql.relation_name.c_str();
W
wangyunlai.wyl 已提交
35
  if (nullptr == db || nullptr == table_name) {
L
Longda Feng 已提交
36
    LOG_WARN("invalid argument. db=%p, table_name=%p", db, table_name);
W
wangyunlai.wyl 已提交
37 38 39 40 41 42 43 44 45 46
    return RC::INVALID_ARGUMENT;
  }

  // check whether the table exists
  Table *table = db->find_table(table_name);
  if (nullptr == table) {
    LOG_WARN("no such table. db=%s, table_name=%s", db->name(), table_name);
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }

羽飞's avatar
羽飞 已提交
47
  std::unordered_map<std::string, Table *> table_map;
羽飞's avatar
羽飞 已提交
48
  table_map.insert(std::pair<std::string, Table *>(std::string(table_name), table));
羽飞's avatar
羽飞 已提交
49

W
wangyunlai.wyl 已提交
50
  FilterStmt *filter_stmt = nullptr;
L
Longda Feng 已提交
51 52
  RC rc = FilterStmt::create(
      db, table, &table_map, delete_sql.conditions.data(), static_cast<int>(delete_sql.conditions.size()), filter_stmt);
W
wangyunlai.wyl 已提交
53 54 55 56 57 58 59 60
  if (rc != RC::SUCCESS) {
    LOG_WARN("failed to create filter statement. rc=%d:%s", rc, strrc(rc));
    return rc;
  }

  stmt = new DeleteStmt(table, filter_stmt);
  return rc;
}