forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.h
More file actions
315 lines (242 loc) · 8.27 KB
/
proc.h
File metadata and controls
315 lines (242 loc) · 8.27 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*****************************************************************************
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
*****************************************************************************/
#ifndef SQL_PACKAGE_PROC_INCLUDED
#define SQL_PACKAGE_PROC_INCLUDED
#include "field_types.h"
#include "prealloced_array.h"
#include "sql/package/package_common.h"
#include "sql/package/package_parse.h"
#include "sql/sql_cmd.h"
class THD;
class sp_name;
class PT_item_list;
/**
Interface of native procedure.
Any native procedure should implement these two interface:
1) Proc
It's abstract class declaration.
- Parser structure
All subclass of proc will has the same parse tree root,
PT_package_proc(sp_name, item_list)
- Sql command
All subclass of proc need to implement itself command class.
- Procedure name
All subclass should define itself name.
2) Sql_cmd_proc
It's abstract class declaration.
- Execute logic (pc_execute())
All subclass need to implement it.
- SQL command type
All subclass has the same command type (SQLCOM_PROC).
- Default behaviour (pls override these if individualization)
1. send_result
2. check_access
3. check_parameter
4. prepare
Revision History:
=================
R1. Proc will be classified into two categories, administrator proc and
transactional proc. They has the same base class PROC interface, but
ADMIN_PROC and TRANS_PROC will have different SQL command, ADMIN_PROC will
trigger implicit commit, and TRANS_PROC will inherit the transaction state
context.
*/
namespace im {
extern const LEX_CSTRING ADMIN_PROC_SCHEMA;
/**
Native procedure interface
*/
class Proc : public PSI_memory_base {
static constexpr unsigned int PROC_PREALLOC_SIZE = 10;
public:
/**
All the native procedures have uniform parse tree root.
It includes:
- sp_name
- pt_expr_list
from sql_yacc.yy
- Proc
from searching native proc map
*/
typedef PT_package_proc PT_proc_type;
/* Container of proc parameters */
typedef Prealloced_array<enum_field_types, PROC_PREALLOC_SIZE>
Parameters;
/* Column element */
typedef struct st_column_element {
enum enum_field_types type;
const char *name;
std::size_t name_len;
std::size_t size;
} Column_element;
/* Container of proc columns */
typedef Prealloced_array<Column_element, PROC_PREALLOC_SIZE> Columns;
typedef std::vector<const Parameters *> Parameters_list;
/**
*/
enum class Result_type {
RESULT_NONE, // Initiail state
RESULT_OK, // Only OK or ERROR protocal
RESULT_SET // Send result set
};
public:
explicit Proc(PSI_memory_key key)
: PSI_memory_base(key),
m_result_type(Result_type::RESULT_NONE),
m_parameters(key),
m_parameters_list(),
m_columns(key) {}
virtual ~Proc() {}
/**
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 *PT_evoke(THD *thd, PT_item_list *pt_expr_list,
const Proc *proc) const;
/**
Interface of generating proc execution logic.
@param[in] THD Thread context
@param[in] pt_expr_list Parameters
@retval Sql cmd
*/
virtual Sql_cmd *evoke_cmd(THD *thd, mem_root_deque<Item *> *list) const = 0;
Result_type get_result_type() const { return m_result_type; }
const Parameters *get_parameters() const { return &m_parameters; }
const Parameters_list &get_parameters_list() const {
return m_parameters_list;
}
const Columns &get_columns() const { return m_columns; }
/**
Send the result meta data by columns definition.
@param[in] THD Thread context
@retval true Failure
@retval false Success
*/
bool send_result_metadata(THD *thd) const;
/**
Interface of proc name.
@retval string Proc name
*/
virtual const std::string str() const = 0;
/**
Interface of sp name.
@retval string sp name
*/
virtual const std::string qname() const = 0;
/* Disable copy and assign function */
Proc(const Proc &) = delete;
Proc(const Proc &&) = delete;
Proc &operator=(const Proc &) = delete;
protected:
/* The type of result packet */
Result_type m_result_type;
/* The list of proc parameters, the default parameter format */
Parameters m_parameters;
/* Now support multiple parameter formats */
Parameters_list m_parameters_list;
/* The list of proc columns */
Columns m_columns;
};
/**
Interface of proc execution.
Should implement pc_execute() function at least!
*/
class Sql_cmd_proc : public Sql_cmd {
protected:
enum class Priv_type { PRIV_NONE_ACL = 0, PRIV_SUPER_ACL };
public:
explicit Sql_cmd_proc(THD *thd, mem_root_deque<Item *> *list,
const Proc *proc, Priv_type priv_type)
: m_thd(thd), m_list(list), m_proc(proc), m_priv_type(priv_type) {}
/**
Interface of Proc execution body.
@param[in] THD Thread context
@retval true Failure
@retval false Success
*/
virtual bool execute(THD *thd) override;
/**
Implementation of Proc execution body.
@param[in] THD Thread context
@retval true Failure
@retval false Success
*/
virtual bool pc_execute(THD *thd) = 0;
/**
SQLCOM_ADMIN_PROC or SQLCOM_TRANS_PROC.
*/
virtual enum_sql_command sql_command_code() const override = 0;
/**
Send the ok or error packet defaultly,
Override it if any result set.
*/
virtual void send_result(THD *thd, bool error);
/**
Check access, require SUPER_ACL defaultly.
Override it if any other requirement.
*/
virtual bool check_access(THD *thd);
/**
Check the parameters
Override it if any other requirement.
*/
virtual bool check_parameter();
/**
Prepare the proc before execution.
*/
virtual bool prepare(THD *thd) override;
protected:
void set_priv_type(Priv_type priv_type) { m_priv_type = priv_type; }
protected:
THD *m_thd;
mem_root_deque<Item *> *m_list;
const Proc *m_proc;
Priv_type m_priv_type;
};
/**
Base class for administrator procedure.
Require SUPER_ACL default.
*/
class Sql_cmd_admin_proc : public Sql_cmd_proc {
public:
explicit Sql_cmd_admin_proc(THD *thd, mem_root_deque<Item *> *list,
const Proc *proc)
: Sql_cmd_proc(thd, list, proc, Priv_type::PRIV_SUPER_ACL) {}
virtual enum_sql_command sql_command_code() const override {
return SQLCOM_ADMIN_PROC;
}
};
/**
Base class for transactional procedure.
*/
class Sql_cmd_trans_proc : public Sql_cmd_proc {
public:
explicit Sql_cmd_trans_proc(THD *thd, mem_root_deque<Item *> *list,
const Proc *proc)
: Sql_cmd_proc(thd, list, proc, Priv_type::PRIV_NONE_ACL) {}
virtual enum_sql_command sql_command_code() const override {
return SQLCOM_TRANS_PROC;
}
};
} /* namespace im */
#endif