forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.cc
More file actions
216 lines (178 loc) · 6.12 KB
/
proc.cc
File metadata and controls
216 lines (178 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*****************************************************************************
Copyright (c) 2025, Alibaba and/or its affiliates. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.
This program is also distributed with certain software (including but not
limited to OpenSSL) that is licensed under separate terms, as designated in a
particular file or component or in included license documentation. The authors
of MySQL hereby grant you an additional permission to link the program and
your derivative works with the separately licensed software that they have
included with MySQL.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
#include "my_dbug.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/package/package_parse.h"
#include "sql/package/proc.h"
#include "sql/protocol.h"
#include "sql/sql_class.h"
namespace im {
/**
Generate the parse tree root.
@param[in] THD Thread context
@param[in] pt_expr_list Parameters
@param[in] proc Native predefined proc
@retval Parse_tree_root
*/
Parse_tree_root *Proc::PT_evoke(THD *thd, PT_item_list *pt_expr_list,
const Proc *proc) const {
return new (thd->mem_root) PT_proc_type(pt_expr_list, proc);
}
/**
Send the result meta data by columns definition.
@param[in] THD Thread context
@retval true Failure
@retval false Success
*/
bool Proc::send_result_metadata(THD *thd) const {
mem_root_deque<Item *> field_list(thd->mem_root);
Item *item;
DBUG_ENTER("Proc::send_result_metadata");
assert(m_columns.size() > 0 && m_result_type == Result_type::RESULT_SET);
for (Columns::const_iterator it = m_columns.begin(); it != m_columns.end();
it++) {
/* TODO: Support more field type */
switch ((*it).type) {
case MYSQL_TYPE_LONGLONG:
field_list.push_back(
item = new Item_int(Name_string((*it).name, (*it).name_len),
(*it).size, MY_INT64_NUM_DECIMAL_DIGITS));
item->set_nullable(true);
break;
case MYSQL_TYPE_VARCHAR:
field_list.push_back(item =
new Item_empty_string((*it).name, (*it).size));
item->set_nullable(true);
break;
case MYSQL_TYPE_TIMESTAMP:
field_list.push_back(item = new Item_temporal(
MYSQL_TYPE_TIMESTAMP,
Name_string((*it).name, strlen((*it).name)), 0,
0));
break;
default:
assert(0);
}
}
if (thd->send_result_metadata(field_list,
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
DBUG_RETURN(true);
DBUG_RETURN(false);
}
/**
Send the ok or error packet defaultly,
Override it if any result set.
*/
void Sql_cmd_proc::send_result(THD *thd, bool error) {
DBUG_ENTER("Sql_cmd_proc::send_result");
if (!error) {
assert(m_proc->get_result_type() == Proc::Result_type::RESULT_OK);
my_ok(thd);
} else {
assert(thd->is_error());
}
DBUG_VOID_RETURN;
}
/**
Check access, require SUPER_ACL defaultly.
Override it if any other requirement or set different
priv type.
*/
bool Sql_cmd_proc::check_access(THD *thd) {
Security_context *sctx = thd->security_context();
if (m_priv_type == Priv_type::PRIV_NONE_ACL) {
return false;
} else if (m_priv_type == Priv_type::PRIV_SUPER_ACL) {
if (!sctx->check_access(SUPER_ACL)) {
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
return true;
}
return false;
}
return false;
}
/**
Check the list of parameters, report error if failed.
Override it if any other requirement.
*/
bool Sql_cmd_proc::check_parameter() {
std::size_t actual_size = (m_list == nullptr ? 0 : m_list->size());
std::size_t define_size = m_proc->get_parameters()->size();
const Proc::Parameters *current_parameters = nullptr;
if (m_proc->get_parameters_list().size() == 0) {
current_parameters = m_proc->get_parameters();
} else {
for (auto parameters : m_proc->get_parameters_list()) {
if (parameters->size() == actual_size) {
define_size = parameters->size();
current_parameters = parameters;
break;
}
}
}
if (current_parameters == nullptr || actual_size != define_size) {
my_error(ER_SP_WRONG_NO_OF_ARGS, MYF(0), "PROCEDURE",
m_proc->qname().c_str(), define_size, actual_size);
return true;
}
if (actual_size > 0) {
std::size_t i = 0;
for (auto item : *m_list) {
if (item->data_type() != current_parameters->at(i)) {
my_error(ER_NATIVE_PROC_PARAMETER_MISMATCH, MYF(0), i + 1,
m_proc->qname().c_str());
return true;
}
i++;
}
}
return false;
}
/**
Prepare the proc before execution.
@param[in] THD Thread context
@retval true Failure
@retval false Success
*/
bool Sql_cmd_proc::prepare(THD *thd) {
if (check_parameter() || check_access(thd))
return true;
set_prepared();
return false;
}
/**
Interface of Proc execution body.
@param[in] THD Thread context
@retval true Failure
@retval false Success
*/
bool Sql_cmd_proc::execute(THD *thd) {
bool error;
/* Step 1 : prepare */
if ((error = prepare(thd))) goto end;
/* Step 2 : execute */
if ((error = pc_execute(thd))) goto end;
end:
/* Step 3 : send result */
send_result(thd, error);
return error;
}
} /* namespace im */