MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
set_variable_executor.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Wangyunlai on 2023/6/14.
13//
14
15#pragma once
16
17#include "common/rc.h"
18#include "sql/operator/string_list_physical_operator.h"
19#include "event/sql_event.h"
20#include "event/session_event.h"
21#include "sql/executor/sql_result.h"
22#include "session/session.h"
23#include "sql/stmt/set_variable_stmt.h"
24
30{
31public:
32 SetVariableExecutor() = default;
33 virtual ~SetVariableExecutor() = default;
34
35 RC execute(SQLStageEvent *sql_event)
36 {
37 RC rc = RC::SUCCESS;
38 Session *session = sql_event->session_event()->session();
39 SetVariableStmt *stmt = (SetVariableStmt *)sql_event->stmt();
40
41 const char *var_name = stmt->var_name();
42 const Value &var_value = stmt->var_value();
43 if (strcasecmp(var_name, "sql_debug") == 0) {
44 bool bool_value = false;
45 rc = var_value_to_boolean(var_value, bool_value);
46 if (rc != RC::SUCCESS) {
47 return rc;
48 }
49
50 session->set_sql_debug(bool_value);
51 LOG_TRACE("set sql_debug to %d", bool_value);
52 } else {
53 rc = RC::VARIABLE_NOT_EXISTS;
54 }
55
56 return RC::SUCCESS;
57 }
58
59private:
60 RC var_value_to_boolean(const Value &var_value, bool &bool_value) const
61 {
62 RC rc = RC::SUCCESS;
63
64 if (var_value.attr_type() == AttrType::BOOLEANS) {
65 bool_value = var_value.get_boolean();
66 } else if (var_value.attr_type() == AttrType::INTS) {
67 bool_value = var_value.get_int() != 0;
68 } else if (var_value.attr_type() == AttrType::FLOATS) {
69 bool_value = var_value.get_float() != 0.0;
70 } else if (var_value.attr_type() == AttrType::CHARS) {
71
72 std::string true_strings[] = {
73 "true",
74 "on",
75 "yes",
76 "t",
77 "1"
78 };
79
80 std::string false_strings[] = {
81 "false",
82 "off",
83 "no",
84 "f",
85 "0"
86 };
87
88 for (size_t i = 0; i < sizeof(true_strings) / sizeof(true_strings[0]); i++) {
89 if (strcasecmp(var_value.get_string().c_str(), true_strings[i].c_str()) == 0) {
90 bool_value = true;
91 return rc;
92 }
93 }
94
95 for (size_t i = 0; i < sizeof(false_strings) / sizeof(false_strings[0]); i++) {
96 if (strcasecmp(var_value.get_string().c_str(), false_strings[i].c_str()) == 0) {
97 bool_value = false;
98 return rc;
99 }
100 }
101 rc = RC::VARIABLE_NOT_VALID;
102 }
103
104 return rc;
105 }
106};
与SessionEvent类似,也是处理SQL请求的事件,只是用在SQL的不同阶段
Definition: sql_event.h:30
表示会话
Definition: session.h:28
SetVariable语句执行器
Definition: set_variable_executor.h:30
SetVairable 语句,设置变量,当前是会话变量,但是只有会话变量,没有全局变量
Definition: set_variable_stmt.h:27
属性的值
Definition: value.h:40
int get_int() const
Definition: value.cpp:205