File: sp.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (420 lines) | stat: -rw-r--r-- 14,595 bytes parent folder | download
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* Copyright (c) 2002, 2025, Oracle and/or its affiliates.

   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 designed to work 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 either included with
   the program or referenced in the documentation.

   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 _SP_H_
#define _SP_H_

#include <assert.h>
#include <stddef.h>
#include <sys/types.h>

#include <string>

#include "field_types.h"
#include "lex_string.h"
#include "map_helpers.h"
#include "my_inttypes.h"
#include "mysql/udf_registration_types.h"
#include "sql/item.h"     // Item::Type
#include "sql/sp_head.h"  // Stored_program_creation_ctx
#include "sql/sql_lex.h"

class Object_creation_ctx;
class Query_arena;
class THD;
struct CHARSET_INFO;
struct LEX_USER;
struct MEM_ROOT;

namespace dd {
class Routine;
class Schema;
}  // namespace dd

class Field;
class Sroutine_hash_entry;
class String;
class sp_cache;
struct TABLE;
class Table_ref;

typedef ulonglong sql_mode_t;
template <typename T>
class SQL_I_List;

enum class enum_sp_type;

/* Tells what SP_DEFAULT_ACCESS should be mapped to */
#define SP_DEFAULT_ACCESS_MAPPING SP_CONTAINS_SQL

/* Tells what SP_IS_DEFAULT_SUID should be mapped to */
#define SP_DEFAULT_SUID_MAPPING SP_IS_SUID

/* Max length(LONGBLOB field type length) of stored routine body */
static const uint MYSQL_STORED_ROUTINE_BODY_LENGTH = 4294967295U;

/* Max length(TEXT field type length) of stored routine comment */
static const int MYSQL_STORED_ROUTINE_COMMENT_LENGTH = 65535;

enum enum_sp_return_code {
  SP_OK = 0,

  // Schema does not exists
  SP_NO_DB_ERROR,

  // Routine does not exists
  SP_DOES_NOT_EXISTS,

  // Routine already exists
  SP_ALREADY_EXISTS,

  // Create routine failed
  SP_STORE_FAILED,

  // Drop routine failed
  SP_DROP_FAILED,

  // Routine load failed
  SP_LOAD_FAILED,

  // Routine parse failed
  SP_PARSE_ERROR,

  // Internal errors
  SP_INTERNAL_ERROR
};

/*
  Fields in mysql.proc table in 5.7. This enum is used to read and
  update mysql.routines dictionary table during upgrade scenario.

  Note:  This enum should not be used for other purpose
         as it will be removed eventually.
*/
enum {
  MYSQL_PROC_FIELD_DB = 0,
  MYSQL_PROC_FIELD_NAME,
  MYSQL_PROC_MYSQL_TYPE,
  MYSQL_PROC_FIELD_SPECIFIC_NAME,
  MYSQL_PROC_FIELD_LANGUAGE,
  MYSQL_PROC_FIELD_ACCESS,
  MYSQL_PROC_FIELD_DETERMINISTIC,
  MYSQL_PROC_FIELD_SECURITY_TYPE,
  MYSQL_PROC_FIELD_PARAM_LIST,
  MYSQL_PROC_FIELD_RETURNS,
  MYSQL_PROC_FIELD_BODY,
  MYSQL_PROC_FIELD_DEFINER,
  MYSQL_PROC_FIELD_CREATED,
  MYSQL_PROC_FIELD_MODIFIED,
  MYSQL_PROC_FIELD_SQL_MODE,
  MYSQL_PROC_FIELD_COMMENT,
  MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT,
  MYSQL_PROC_FIELD_COLLATION_CONNECTION,
  MYSQL_PROC_FIELD_DB_COLLATION,
  MYSQL_PROC_FIELD_BODY_UTF8,
  MYSQL_PROC_FIELD_COUNT
};

/*************************************************************************/

/**
  Stored_routine_creation_ctx -- creation context of stored routines
  (stored procedures and functions).
*/

class Stored_routine_creation_ctx : public Stored_program_creation_ctx {
 public:
  static Stored_routine_creation_ctx *create_routine_creation_ctx(
      const dd::Routine *routine);

  static Stored_routine_creation_ctx *load_from_db(THD *thd,
                                                   const sp_name *name,
                                                   TABLE *proc_tbl);

 public:
  Stored_program_creation_ctx *clone(MEM_ROOT *mem_root) override;

 protected:
  Object_creation_ctx *create_backup_ctx(THD *thd) const override;
  void delete_backup_ctx() override;

 private:
  explicit Stored_routine_creation_ctx(THD *thd)
      : Stored_program_creation_ctx(thd) {}

  Stored_routine_creation_ctx(const CHARSET_INFO *client_cs,
                              const CHARSET_INFO *connection_cl,
                              const CHARSET_INFO *db_cl)
      : Stored_program_creation_ctx(client_cs, connection_cl, db_cl) {}
};

/* Drop all routines in database 'db' */
bool sp_drop_db_routines(THD *thd, const dd::Schema &schema);

/**
   Acquires exclusive metadata lock on all stored routines in the
   given database.

   @param  thd     Thread handler
   @param  schema  Schema object

   @retval  false  Success
   @retval  true   Failure
 */
bool lock_db_routines(THD *thd, const dd::Schema &schema);

sp_head *sp_find_routine(THD *thd, enum_sp_type type, sp_name *name,
                         sp_cache **cp, bool cache_only);

sp_head *sp_setup_routine(THD *thd, enum_sp_type type, sp_name *name,
                          sp_cache **cp);

enum_sp_return_code sp_cache_routine(THD *thd, Sroutine_hash_entry *rt,
                                     bool lookup_only, sp_head **sp);

enum_sp_return_code sp_cache_routine(THD *thd, enum_sp_type type,
                                     const sp_name *name, bool lookup_only,
                                     sp_head **sp);

bool sp_exist_routines(THD *thd, Table_ref *procs, bool is_proc);

bool sp_show_create_routine(THD *thd, enum_sp_type type, sp_name *name);

enum_sp_return_code db_load_routine(
    THD *thd, enum_sp_type type, const char *sp_db, size_t sp_db_len,
    const char *sp_name, size_t sp_name_len, sp_head **sphp,
    sql_mode_t sql_mode, const char *params, const char *returns,
    const char *body, st_sp_chistics *chistics, const char *definer_user,
    const char *definer_host, longlong created, longlong modified,
    Stored_program_creation_ctx *creation_ctx);

bool sp_create_routine(THD *thd, sp_head *sp, const LEX_USER *definer,
                       bool if_not_exists, bool &sp_already_exists);

bool sp_update_routine(THD *thd, enum_sp_type type, sp_name *name,
                       st_sp_chistics *chistics);

enum_sp_return_code sp_drop_routine(THD *thd, enum_sp_type type, sp_name *name);

/**
  Structure that represents element in the set of stored routines
  used by statement or routine.
*/

class Sroutine_hash_entry {
 public:
  /**
    Key identifying routine or other object added to the set.

    Key format: "@<1-byte entry type@>@<db name@>\0@<routine/object name@>\0".

    @note We use binary comparison for these keys as the @<db name@> component
          requires case-sensitive comparison on --lower-case-table-names=0
          systems. On systems where --lower-case-table-names > 0 database
          names which passed to functions working with this set are already
          lowercased. So binary comparison is equivalent to case-insensitive
          comparison for them.
          Routine names are case and accent insensitive. To achieve such
          comparison we normalize routine names by converting their characters
          to their sort weights (according to case and accent insensitive
          collation). In this case, the actual routine name is also stored in
          the member m_object_name.

    @note For Foreign Key objects, '@<db name@>\0@<object name@>\0' part of the
          key is compatible with keys used by MDL. So one can easily construct
          MDL_key from this key.
  */
  char *m_key;
  LEX_CSTRING m_object_name;
  uint16 m_key_length;
  uint16 m_db_length;

  enum entry_type {
    FUNCTION,
    PROCEDURE,
    TRIGGER,
    /**
      Parent table in a foreign key on which child table there was insert
      or update. We will lookup new values in parent, so need to acquire
      SR lock on it.
    */
    FK_TABLE_ROLE_PARENT_CHECK,
    /**
      Child table in a foreign key with RESTRICT/NO ACTION as corresponding
      rule and on which parent table there was delete or update.
      We will check if old parent key is referenced by child table,
      so need to acquire SR lock on it.
    */
    FK_TABLE_ROLE_CHILD_CHECK,
    /**
      Child table in a foreign key with CASCADE/SET NULL/SET DEFAULT as
      'on update' rule, on which parent there was update, or with SET NULL/
      SET DEFAULT as 'on delete' rule, on which parent there was delete.
      We might need to update rows in child table, so we need to acquire
      SW lock on it. We also need to take into account that child table
      might be parent for some other FKs, so such update needs
      to be handled recursively.
    */
    FK_TABLE_ROLE_CHILD_UPDATE,
    /**
      Child table in a foreign key with CASCADE as 'on delete' rule for
      which there was delete from the parent table.
      We might need to delete rows from the child table, so we need to
      acquire SW lock on it.
      We also need to take into account that child table might be parent
      for some other FKs, so such delete needs to be handled recursively
      (and even might result in updates).
    */
    FK_TABLE_ROLE_CHILD_DELETE
  };

  entry_type type() const { return (entry_type)m_key[0]; }
  const char *db() const { return (char *)m_key + 1; }
  size_t db_length() const { return m_db_length; }
  const char *name() const {
    return use_normalized_key() ? m_object_name.str
                                : (char *)m_key + 1 + m_db_length + 1;
  }
  size_t name_length() const {
    return use_normalized_key() ? m_object_name.length
                                : m_key_length - 1U - m_db_length - 1U - 1U;
  }
  bool use_normalized_key() const {
    return (type() == FUNCTION || type() == PROCEDURE || type() == TRIGGER);
  }

  const char *part_mdl_key() {
    assert(!use_normalized_key());
    return (char *)m_key + 1;
  }
  size_t part_mdl_key_length() {
    assert(!use_normalized_key());
    return m_key_length - 1U;
  }

  /**
    Next element in list linking all routines in set. See also comments
    for LEX::sroutine/sroutine_list and sp_head::m_sroutines.
  */
  Sroutine_hash_entry *next;
  /**
    Uppermost view which directly or indirectly uses this routine.
    0 if routine is not used in view. Note that it also can be 0 if
    statement uses routine both via view and directly.
  */
  Table_ref *belong_to_view;
  /**
    This is for prepared statement validation purposes.
    A statement looks up and pre-loads all its stored functions
    at prepare. Later on, if a function is gone from the cache,
    execute may fail. Similarly, tables involved in referential
    constraints are also prelocked.
    Remember the version of the cached item at prepare to be able to
    invalidate the prepared statement at execute if it
    changes.
  */
  int64 m_cache_version;
};

/*
  Enum to indicate SP name normalization required when constructing a key
  in sp_add_used_routine method.
*/
enum class Sp_name_normalize_type {
  LEAVE_AS_IS = 0,              // No normalization needed.
  LOWERCASE_NAME,               // Lower case SP name.
  UNACCENT_AND_LOWERCASE_NAME,  // Lower case SP name and remove accent.
};

/*
  Procedures for handling sets of stored routines used by statement or routine.
*/
bool sp_add_used_routine(Query_tables_list *prelocking_ctx, Query_arena *arena,
                         Sroutine_hash_entry::entry_type type, const char *db,
                         size_t db_length, const char *name, size_t name_length,
                         bool lowercase_db,
                         Sp_name_normalize_type name_normalize_type,
                         bool own_routine, Table_ref *belong_to_view);

/**
  Convenience wrapper around sp_add_used_routine() for most common case -
  stored procedure or function which are explicitly used by the statement.
*/

inline bool sp_add_own_used_routine(Query_tables_list *prelocking_ctx,
                                    Query_arena *arena,
                                    Sroutine_hash_entry::entry_type type,
                                    sp_name *sp_name) {
  assert(type == Sroutine_hash_entry::FUNCTION ||
         type == Sroutine_hash_entry::PROCEDURE);

  return sp_add_used_routine(
      prelocking_ctx, arena, type, sp_name->m_db.str, sp_name->m_db.length,
      sp_name->m_name.str, sp_name->m_name.length, false,
      Sp_name_normalize_type::UNACCENT_AND_LOWERCASE_NAME, true, nullptr);
}

void sp_remove_not_own_routines(Query_tables_list *prelocking_ctx);
void sp_update_stmt_used_routines(
    THD *thd, Query_tables_list *prelocking_ctx,
    malloc_unordered_map<std::string, Sroutine_hash_entry *> *src,
    Table_ref *belong_to_view);
void sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx,
                                  SQL_I_List<Sroutine_hash_entry> *src,
                                  Table_ref *belong_to_view);

const uchar *sp_sroutine_key(const uchar *ptr, size_t *plen);

bool load_charset(MEM_ROOT *mem_root, Field *field, const CHARSET_INFO *dflt_cs,
                  const CHARSET_INFO **cs);

bool load_collation(MEM_ROOT *mem_root, Field *field,
                    const CHARSET_INFO *dflt_cl, const CHARSET_INFO **cl);

///////////////////////////////////////////////////////////////////////////

sp_head *sp_start_parsing(THD *thd, enum_sp_type sp_type, sp_name *sp_name);

void sp_finish_parsing(THD *thd);

///////////////////////////////////////////////////////////////////////////

Item_result sp_map_result_type(enum enum_field_types type);
Item::Type sp_map_item_type(enum enum_field_types type);
uint sp_get_flags_for_command(LEX *lex);

bool sp_check_name(LEX_STRING *ident);

Table_ref *sp_add_to_query_tables(THD *thd, LEX *lex, const char *db,
                                  const char *name);

Item *sp_prepare_func_item(THD *thd, bool standalone, Item **it_addr);

bool sp_eval_expr(THD *thd, bool standalone, Field *result_field,
                  Item **expr_item_ptr);

String *sp_get_item_value(THD *thd, Item *item, String *str);

///////////////////////////////////////////////////////////////////////////

#endif /* _SP_H_ */