session.cpp 1.7 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 16
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 2021/5/12.
//

#include "session/session.h"
#include "storage/trx/trx.h"
W
wangyunlai.wyl 已提交
17 18
#include "storage/common/db.h"
#include "storage/default/default_handler.h"
羽飞's avatar
羽飞 已提交
19

20 21
Session &Session::default_session()
{
羽飞's avatar
羽飞 已提交
22 23 24 25
  static Session session;
  return session;
}

W
wangyunlai.wyl 已提交
26
Session::Session(const Session &other) : db_(other.db_)
27
{}
羽飞's avatar
羽飞 已提交
28

29 30
Session::~Session()
{
羽飞's avatar
羽飞 已提交
31 32 33 34
  delete trx_;
  trx_ = nullptr;
}

W
wangyunlai.wyl 已提交
35
const char *Session::get_current_db_name() const
36
{
W
wangyunlai.wyl 已提交
37 38 39 40
  if (db_ != nullptr)
    return db_->name();
  else
    return "";
羽飞's avatar
羽飞 已提交
41
}
W
wangyunlai.wyl 已提交
42 43 44 45 46 47

Db *Session::get_current_db() const
{
  return db_;
}

48 49
void Session::set_current_db(const std::string &dbname)
{
W
wangyunlai.wyl 已提交
50 51 52 53 54 55 56 57 58
  DefaultHandler &handler = DefaultHandler::get_default();
  Db *db = handler.find_db(dbname.c_str());
  if (db == nullptr) {
    LOG_WARN("no such database: %s", dbname.c_str());
    return;
  }

  LOG_TRACE("change db to %s", dbname.c_str());
  db_ = db;
羽飞's avatar
羽飞 已提交
59 60
}

61 62
void Session::set_trx_multi_operation_mode(bool multi_operation_mode)
{
羽飞's avatar
羽飞 已提交
63 64 65
  trx_multi_operation_mode_ = multi_operation_mode;
}

66 67
bool Session::is_trx_multi_operation_mode() const
{
羽飞's avatar
羽飞 已提交
68 69 70
  return trx_multi_operation_mode_;
}

71 72
Trx *Session::current_trx()
{
羽飞's avatar
羽飞 已提交
73
  if (trx_ == nullptr) {
羽飞's avatar
羽飞 已提交
74
    trx_ = TrxKit::instance()->create_trx();
羽飞's avatar
羽飞 已提交
75 76 77
  }
  return trx_;
}