session.cpp 2.2 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"
羽飞's avatar
羽飞 已提交
17
#include "storage/db/db.h"
W
wangyunlai.wyl 已提交
18
#include "storage/default/default_handler.h"
羽飞's avatar
羽飞 已提交
19
#include "common/global_context.h"
羽飞's avatar
羽飞 已提交
20

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

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

30 31
Session::~Session()
{
32 33 34 35
  if (nullptr != trx_) {
    GCTX.trx_kit_->destroy_trx(trx_);
    trx_ = nullptr;
  }
羽飞's avatar
羽飞 已提交
36 37
}

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

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

51 52
void Session::set_current_db(const std::string &dbname)
{
W
wangyunlai.wyl 已提交
53 54 55 56 57 58 59 60 61
  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
羽飞 已提交
62 63
}

64 65
void Session::set_trx_multi_operation_mode(bool multi_operation_mode)
{
羽飞's avatar
羽飞 已提交
66 67 68
  trx_multi_operation_mode_ = multi_operation_mode;
}

69 70
bool Session::is_trx_multi_operation_mode() const
{
羽飞's avatar
羽飞 已提交
71 72 73
  return trx_multi_operation_mode_;
}

74 75
Trx *Session::current_trx()
{
羽飞's avatar
羽飞 已提交
76
  if (trx_ == nullptr) {
77
    trx_ = GCTX.trx_kit_->create_trx(db_->clog_manager());
羽飞's avatar
羽飞 已提交
78 79 80
  }
  return trx_;
}
81 82 83 84 85 86 87 88 89 90 91 92

thread_local Session *thread_session = nullptr;

void Session::set_current_session(Session *session)
{
  thread_session = session;
}

Session *Session::current_session()
{
  return thread_session;
}
羽飞's avatar
羽飞 已提交
93 94 95 96 97 98 99 100 101 102

void Session::set_current_request(SessionEvent *request)
{
  current_request_ = request;
}

SessionEvent *Session::current_request() const
{
  return current_request_;
}