MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sp_pcontext.h
1 /* -*- C++ -*- */
2 /* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software Foundation,
15  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
16 
17 #ifndef _SP_PCONTEXT_H_
18 #define _SP_PCONTEXT_H_
19 
20 #include "sql_string.h" // LEX_STRING
21 #include "mysql_com.h" // enum_field_types
22 #include "field.h" // Create_field
23 #include "sql_array.h" // Dynamic_array
24 
25 
28 
29 class sp_variable : public Sql_alloc
30 {
31 public:
32  enum enum_mode
33  {
34  MODE_IN,
35  MODE_OUT,
36  MODE_INOUT
37  };
38 
41 
43  enum enum_field_types type;
44 
46  enum_mode mode;
47 
53  uint offset;
54 
57 
60 
61 public:
62  sp_variable(LEX_STRING _name, enum_field_types _type, enum_mode _mode,
63  uint _offset)
64  :Sql_alloc(),
65  name(_name),
66  type(_type),
67  mode(_mode),
68  offset(_offset),
69  default_value(NULL)
70  { }
71 };
72 
74 
81 
82 class sp_label : public Sql_alloc
83 {
84 public:
85  enum enum_type
86  {
89 
92 
95  };
96 
99 
101  uint ip;
102 
105 
107  class sp_pcontext *ctx;
108 
109 public:
110  sp_label(LEX_STRING _name, uint _ip, enum_type _type, sp_pcontext *_ctx)
111  :Sql_alloc(),
112  name(_name),
113  ip(_ip),
114  type(_type),
115  ctx(_ctx)
116  { }
117 };
118 
120 
127 
129 {
130 public:
131  enum enum_type
132  {
133  ERROR_CODE,
134  SQLSTATE,
135  WARNING,
136  NOT_FOUND,
137  EXCEPTION
138  };
139 
141  enum_type type;
142 
144  char sql_state[SQLSTATE_LENGTH+1];
145 
147  uint mysqlerr;
148 
149 public:
150  sp_condition_value(uint _mysqlerr)
151  :Sql_alloc(),
152  type(ERROR_CODE),
153  mysqlerr(_mysqlerr)
154  { }
155 
156  sp_condition_value(const char *_sql_state)
157  :Sql_alloc(),
158  type(SQLSTATE)
159  {
160  memcpy(sql_state, _sql_state, SQLSTATE_LENGTH);
161  sql_state[SQLSTATE_LENGTH]= 0;
162  }
163 
164  sp_condition_value(enum_type _type)
165  :Sql_alloc(),
166  type(_type)
167  {
168  DBUG_ASSERT(type != ERROR_CODE && type != SQLSTATE);
169  }
170 
176  bool equals(const sp_condition_value *cv) const;
177 };
178 
180 
183 
184 class sp_condition : public Sql_alloc
185 {
186 public:
189 
192 
193 public:
195  :Sql_alloc(),
196  name(_name),
197  value(_value)
198  { }
199 };
200 
202 
204 
205 class sp_handler : public Sql_alloc
206 {
207 public:
211  {
212  EXIT,
213  CONTINUE
214  };
215 
218 
221 
224 
225 public:
231  :Sql_alloc(),
232  type(_type),
233  scope(_scope)
234  { }
235 };
236 
238 
261 
262 class sp_pcontext : public Sql_alloc
263 {
264 public:
266  {
269 
272  };
273 
274 public:
275  sp_pcontext();
276  ~sp_pcontext();
277 
278 
280 
284  sp_pcontext *push_context(THD *thd, enum_scope scope);
285 
289 
290  sp_pcontext *parent_context() const
291  { return m_parent; }
292 
293  int get_level() const
294  { return m_level; }
295 
306  uint diff_handlers(const sp_pcontext *ctx, bool exclusive) const;
307 
318  uint diff_cursors(const sp_pcontext *ctx, bool exclusive) const;
319 
321  // SP-variables (parameters and variables).
323 
327  uint max_var_index() const
328  { return m_max_var_index; }
329 
332  uint current_var_count() const
333  { return m_var_offset + m_vars.elements(); }
334 
336  uint context_var_count() const
337  { return m_vars.elements(); }
338 
340  uint var_context2runtime(uint i) const
341  { return m_var_offset + i; }
342 
351  sp_variable *add_variable(THD *thd,
353  enum enum_field_types type,
354  sp_variable::enum_mode mode);
355 
360  void retrieve_field_definitions(List<Create_field> *field_def_lst) const;
361 
373  sp_variable *find_variable(LEX_STRING name, bool current_scope_only) const;
374 
385  sp_variable *find_variable(uint offset) const;
386 
391  { m_pboundary= n; }
392 
394  // CASE expressions.
396 
397  int get_num_case_exprs() const
398  { return m_num_case_exprs; }
399 
400  int push_case_expr_id()
401  {
402  if (m_case_expr_ids.append(m_num_case_exprs))
403  return -1;
404 
405  return m_num_case_exprs++;
406  }
407 
408  void pop_case_expr_id()
409  { m_case_expr_ids.pop(); }
410 
411  int get_current_case_expr_id() const
412  { return *m_case_expr_ids.back(); }
413 
415  // Labels.
417 
418  sp_label *push_label(THD *thd, LEX_STRING name, uint ip);
419 
420  sp_label *find_label(LEX_STRING name);
421 
422  sp_label *last_label()
423  {
424  sp_label *label= m_labels.head();
425 
426  if (!label && m_parent)
427  label= m_parent->last_label();
428 
429  return label;
430  }
431 
432  sp_label *pop_label()
433  { return m_labels.pop(); }
434 
436  // Conditions.
438 
439  bool add_condition(THD *thd, LEX_STRING name, sp_condition_value *value);
440 
443  bool current_scope_only) const;
444 
446  // Handlers.
448 
449  sp_handler *add_handler(THD* thd, sp_handler::enum_type type);
450 
466  bool check_duplicate_handler(const sp_condition_value *cond_value) const;
467 
476  sp_handler *find_handler(const char *sql_state,
477  uint sql_errno,
478  Sql_condition::enum_warning_level level) const;
479 
481  // Cursors.
483 
484  bool add_cursor(LEX_STRING name);
485 
487  bool find_cursor(LEX_STRING name, uint *poff, bool current_scope_only) const;
488 
490  const LEX_STRING *find_cursor(uint offset) const;
491 
492  uint max_cursor_index() const
493  { return m_max_cursor_index + m_cursors.elements(); }
494 
495  uint current_cursor_count() const
496  { return m_cursor_offset + m_cursors.elements(); }
497 
498 private:
502  sp_pcontext(sp_pcontext *prev, enum_scope scope);
503 
504  void init(uint var_offset, uint cursor_offset, int num_case_expressions);
505 
506  /* Prevent use of these */
507  sp_pcontext(const sp_pcontext &);
508  void operator=(sp_pcontext &);
509 
510 private:
512  int m_level;
513 
521  uint m_max_var_index;
522 
524  uint m_max_cursor_index;
525 
527  sp_pcontext *m_parent;
528 
535  uint m_var_offset;
536 
538  uint m_cursor_offset;
539 
544  uint m_pboundary;
545 
546  int m_num_case_exprs;
547 
550 
552  Dynamic_array<int> m_case_expr_ids;
553 
555  Dynamic_array<sp_condition *> m_conditions;
556 
558  Dynamic_array<LEX_STRING> m_cursors;
559 
561  Dynamic_array<sp_handler *> m_handlers;
562 
564  List<sp_label> m_labels;
565 
567  Dynamic_array<sp_pcontext *> m_children;
568 
570  enum_scope m_scope;
571 }; // class sp_pcontext : public Sql_alloc
572 
573 
574 #endif /* _SP_PCONTEXT_H_ */