MySQL  8.0.27
Source Code Documentation
sql_cmd.h
Go to the documentation of this file.
1 /* Copyright (c) 2009, 2021, Oracle and/or its affiliates.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 /**
24  @file sql/sql_cmd.h
25  Representation of an SQL command.
26 */
27 
28 #ifndef SQL_CMD_INCLUDED
29 #define SQL_CMD_INCLUDED
30 
31 #include <assert.h>
32 
33 #include "my_sqlcommand.h"
34 #include "sql/select_lex_visitor.h"
35 
36 class THD;
37 class Prepared_statement;
38 struct handlerton;
39 struct MYSQL_LEX_STRING;
40 struct MYSQL_LEX_CSTRING;
41 
42 /**
43  Representation of an SQL command.
44 
45  This class is an interface between the parser and the runtime.
46  The parser builds the appropriate derived classes of Sql_cmd
47  to represent a SQL statement in the parsed tree.
48  The execute() method in the derived classes of Sql_cmd contain the runtime
49  implementation.
50  Note that this interface is used for SQL statements recently implemented,
51  the code for older statements tend to load the LEX structure with more
52  attributes instead.
53  Implement new statements by sub-classing Sql_cmd, as this improves
54  code modularity (see the 'big switch' in dispatch_command()), and decreases
55  the total size of the LEX structure (therefore saving memory in stored
56  programs).
57  The recommended name of a derived class of Sql_cmd is Sql_cmd_<derived>.
58 
59  Notice that the Sql_cmd class should not be confused with the Statement class.
60  Statement is a class that is used to manage an SQL command or a set
61  of SQL commands. When the SQL statement text is analyzed, the parser will
62  create one or more Sql_cmd objects to represent the actual SQL commands.
63 */
64 class Sql_cmd {
65  private:
66  Sql_cmd(const Sql_cmd &); // No copy constructor wanted
67  void operator=(Sql_cmd &); // No assignment operator wanted
68 
69  public:
70  /**
71  @brief Return the command code for this statement
72  */
73  virtual enum_sql_command sql_command_code() const = 0;
74 
75  /**
76  @return true if object represents a preparable statement, ie. a query
77  that is prepared with a PREPARE statement and executed with an EXECUTE
78  statement. False is returned for regular statements (non-preparable
79  statements) that are executed directly. Also false if statement is part
80  of a stored procedure.
81  */
83  return m_owner != nullptr && !m_part_of_sp;
84  }
85  /**
86  @return true if statement is regular, ie not prepared statement and not
87  part of stored procedure.
88  */
89  bool is_regular() const { return m_owner == nullptr && !m_part_of_sp; }
90 
91  /// @return true if this statement is prepared
92  bool is_prepared() const { return m_prepared; }
93 
94  /**
95  Prepare this SQL statement.
96 
97  param thd the current thread
98 
99  @returns false if success, true if error
100  */
101  virtual bool prepare(THD *) {
102  // Default behavior for a statement is to have no preparation code.
103  /* purecov: begin inspected */
104  assert(!is_prepared());
105  set_prepared();
106  return false;
107  /* purecov: end */
108  }
109 
110  /**
111  Execute this SQL statement.
112  @param thd the current thread.
113  @returns false if success, true if error
114  */
115  virtual bool execute(THD *thd) = 0;
116 
117  /**
118  Command-specific reinitialization before execution of prepared statement
119 
120  param thd Current THD.
121  */
122  virtual void cleanup(THD *) { m_secondary_engine = nullptr; }
123 
124  /// Set the owning prepared statement
126  assert(!m_part_of_sp);
127  m_owner = stmt;
128  }
129 
130  /// Get the owning prepared statement
131  Prepared_statement *owner() const { return m_owner; }
132 
133  /**
134  Mark statement as part of procedure. Such statements can be executed
135  multiple times, the first execute() call will also prepare it.
136  */
138  assert(!m_part_of_sp && m_owner == nullptr);
139  m_part_of_sp = true;
140  }
141  /// @returns true if statement is part of a stored procedure
142  bool is_part_of_sp() const { return m_part_of_sp; }
143 
144  /// @return true if SQL command is a DML statement
145  virtual bool is_dml() const { return false; }
146 
147  /// @return true if implemented as single table plan, DML statement only
148  virtual bool is_single_table_plan() const {
149  /* purecov: begin inspected */
150  assert(is_dml());
151  return false;
152  /* purecov: end */
153  }
154 
155  virtual bool accept(THD *, Select_lex_visitor *) { return false; }
156 
157  /**
158  Is this statement of a type and on a form that makes it eligible
159  for execution in a secondary storage engine?
160 
161  @return the name of the secondary storage engine, or nullptr if
162  the statement is not eligible for execution in a secondary storage
163  engine
164  */
166  return nullptr;
167  }
168 
169  /**
170  Disable use of secondary storage engines in this statement. After
171  a call to this function, the statement will not try to use a
172  secondary storage engine until it is reprepared.
173  */
175  assert(m_secondary_engine == nullptr);
177  }
178 
179  /**
180  Has use of secondary storage engines been disabled for this statement?
181  */
184  }
185 
186  /**
187  Mark the current statement as using a secondary storage engine.
188  This function must be called before the statement starts opening
189  tables in a secondary engine.
190  */
193  m_secondary_engine = hton;
194  }
195 
196  /**
197  Is this statement using a secondary storage engine?
198  @note that this is reliable during optimization and afterwards; during
199  preparation, if this is an explicit preparation (SQL PREPARE, C API
200  PREPARE, and automatic repreparation), it may be false as RAPID tables have
201  not yet been opened. Therefore, during preparation, it is safer to test
202  THD::secondary_engine_optimization().
203  */
205  return m_secondary_engine != nullptr;
206  }
207 
208  /**
209  Get the handlerton of the secondary engine that is used for
210  executing this statement, or nullptr if a secondary engine is not
211  used.
212  */
213  const handlerton *secondary_engine() const { return m_secondary_engine; }
214 
217  }
218 
221  }
222 
223  protected:
225 
226  virtual ~Sql_cmd() {
227  /*
228  Sql_cmd objects are allocated in thd->mem_root.
229  In MySQL, the C++ destructor is never called, the underlying MEM_ROOT is
230  simply destroyed instead.
231  Do not rely on the destructor for any cleanup.
232  */
233  assert(false);
234  }
235 
236  /// Set this statement as prepared
237  void set_prepared() { m_prepared = true; }
238 
239  private:
240  Prepared_statement *m_owner; /// Owning prepared statement, NULL if non-prep.
241  bool m_part_of_sp; /// True when statement is part of stored proc.
242  bool m_prepared; /// True when statement has been prepared
243 
244  /**
245  Tells if a secondary storage engine can be used for this
246  statement. If it is false, use of a secondary storage engine will
247  not be considered for executing this statement.
248  */
250 
251  /**
252  Keeps track of whether the statement was prepared optional
253  transformation.
254  */
256 
257  /**
258  The secondary storage engine to use for execution of this
259  statement, if any, or nullptr if the primary engine is used.
260  This property is reset at the start of each execution.
261  */
263 };
264 
265 #endif // SQL_CMD_INCLUDED
Prepared_statement: a statement that can contain placeholders.
Definition: sql_prepare.h:345
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:39
Representation of an SQL command.
Definition: sql_cmd.h:64
bool m_part_of_sp
Owning prepared statement, NULL if non-prep.
Definition: sql_cmd.h:241
virtual bool prepare(THD *)
Prepare this SQL statement.
Definition: sql_cmd.h:101
virtual const MYSQL_LEX_CSTRING * eligible_secondary_storage_engine() const
Is this statement of a type and on a form that makes it eligible for execution in a secondary storage...
Definition: sql_cmd.h:165
void use_secondary_storage_engine(const handlerton *hton)
Mark the current statement as using a secondary storage engine.
Definition: sql_cmd.h:191
virtual bool execute(THD *thd)=0
Execute this SQL statement.
const handlerton * m_secondary_engine
The secondary storage engine to use for execution of this statement, if any, or nullptr if the primar...
Definition: sql_cmd.h:262
void set_as_part_of_sp()
Mark statement as part of procedure.
Definition: sql_cmd.h:137
Sql_cmd(const Sql_cmd &)
bool using_secondary_storage_engine() const
Is this statement using a secondary storage engine?
Definition: sql_cmd.h:204
void operator=(Sql_cmd &)
virtual void cleanup(THD *)
Command-specific reinitialization before execution of prepared statement.
Definition: sql_cmd.h:122
const handlerton * secondary_engine() const
Get the handlerton of the secondary engine that is used for executing this statement,...
Definition: sql_cmd.h:213
bool is_optional_transform_prepared()
Definition: sql_cmd.h:219
bool m_prepared_with_optional_transform
Keeps track of whether the statement was prepared optional transformation.
Definition: sql_cmd.h:255
bool is_part_of_sp() const
Definition: sql_cmd.h:142
virtual bool is_dml() const
Definition: sql_cmd.h:145
Sql_cmd()
Definition: sql_cmd.h:224
bool is_regular() const
Definition: sql_cmd.h:89
virtual ~Sql_cmd()
Definition: sql_cmd.h:226
void set_prepared()
Set this statement as prepared.
Definition: sql_cmd.h:237
virtual bool is_single_table_plan() const
Definition: sql_cmd.h:148
bool m_secondary_engine_enabled
True when statement has been prepared.
Definition: sql_cmd.h:249
virtual bool accept(THD *, Select_lex_visitor *)
Definition: sql_cmd.h:155
void disable_secondary_storage_engine()
Disable use of secondary storage engines in this statement.
Definition: sql_cmd.h:174
bool is_prepared() const
Definition: sql_cmd.h:92
Prepared_statement * owner() const
Get the owning prepared statement.
Definition: sql_cmd.h:131
virtual enum_sql_command sql_command_code() const =0
Return the command code for this statement.
void set_optional_transform_prepared(bool value)
Definition: sql_cmd.h:215
bool needs_explicit_preparation() const
Definition: sql_cmd.h:82
bool m_prepared
True when statement is part of stored proc.
Definition: sql_cmd.h:242
bool secondary_storage_engine_disabled() const
Has use of secondary storage engines been disabled for this statement?
Definition: sql_cmd.h:182
Prepared_statement * m_owner
Definition: sql_cmd.h:240
void set_owner(Prepared_statement *stmt)
Set the owning prepared statement.
Definition: sql_cmd.h:125
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
Dialog Client Authentication nullptr
Definition: dialog.cc:352
enum_sql_command
Definition: my_sqlcommand.h:45
const string value("\"Value\"")
Visitor interface for parse trees.
Definition: mysql_lex_string.h:39
Definition: mysql_lex_string.h:34
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2391