MySQL  8.0.18
Source Code Documentation
sql_cmd.h
Go to the documentation of this file.
1 /* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
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 "my_dbug.h"
32 #include "my_sqlcommand.h"
33 #include "sql/select_lex_visitor.h"
34 
35 class THD;
36 class Prepared_statement;
37 struct handlerton;
38 struct MYSQL_LEX_STRING;
39 struct MYSQL_LEX_CSTRING;
40 
41 /**
42  Representation of an SQL command.
43 
44  This class is an interface between the parser and the runtime.
45  The parser builds the appropriate derived classes of Sql_cmd
46  to represent a SQL statement in the parsed tree.
47  The execute() method in the derived classes of Sql_cmd contain the runtime
48  implementation.
49  Note that this interface is used for SQL statements recently implemented,
50  the code for older statements tend to load the LEX structure with more
51  attributes instead.
52  Implement new statements by sub-classing Sql_cmd, as this improves
53  code modularity (see the 'big switch' in dispatch_command()), and decreases
54  the total size of the LEX structure (therefore saving memory in stored
55  programs).
56  The recommended name of a derived class of Sql_cmd is Sql_cmd_<derived>.
57 
58  Notice that the Sql_cmd class should not be confused with the Statement class.
59  Statement is a class that is used to manage an SQL command or a set
60  of SQL commands. When the SQL statement text is analyzed, the parser will
61  create one or more Sql_cmd objects to represent the actual SQL commands.
62 */
63 class Sql_cmd {
64  private:
65  Sql_cmd(const Sql_cmd &); // No copy constructor wanted
66  void operator=(Sql_cmd &); // No assignment operator wanted
67 
68  public:
69  /**
70  @brief Return the command code for this statement
71  */
72  virtual enum_sql_command sql_command_code() const = 0;
73 
74  /// @return true if this statement is prepared
75  bool is_prepared() const { return m_prepared; }
76 
77  /**
78  Prepare this SQL statement.
79 
80  @param thd the current thread
81 
82  @returns false if success, true if error
83  */
84  virtual bool prepare(THD *thd MY_ATTRIBUTE((unused))) {
85  // Default behavior for a statement is to have no preparation code.
86  /* purecov: begin inspected */
88  set_prepared();
89  return false;
90  /* purecov: end */
91  }
92 
93  /**
94  Execute this SQL statement.
95  @param thd the current thread.
96  @returns false if success, true if error
97  */
98  virtual bool execute(THD *thd) = 0;
99 
100  /**
101  Command-specific reinitialization before execution of prepared statement
102 
103  @see reinit_stmt_before_use()
104 
105  @note Currently this function is overloaded for INSERT/REPLACE stmts only.
106 
107  @param thd Current THD.
108  */
109  virtual void cleanup(THD *thd MY_ATTRIBUTE((unused))) {
110  m_secondary_engine = nullptr;
111  }
112 
113  /// Set the owning prepared statement
114  void set_owner(Prepared_statement *stmt) { m_owner = stmt; }
115 
116  /// Get the owning prepared statement
118 
119  /// @return true if SQL command is a DML statement
120  virtual bool is_dml() const { return false; }
121 
122  /// @return true if implemented as single table plan, DML statement only
123  virtual bool is_single_table_plan() const {
124  /* purecov: begin inspected */
125  DBUG_ASSERT(is_dml());
126  return false;
127  /* purecov: end */
128  }
129 
130  /**
131  Temporary function used to "unprepare" a prepared statement after
132  preparation, so that a subsequent execute statement will reprepare it.
133  This is done because UNIT::cleanup() will un-resolve all resolved QBs.
134  */
135  virtual void unprepare(THD *thd MY_ATTRIBUTE((unused))) {
137  m_prepared = false;
138  }
139 
140  virtual bool accept(THD *thd MY_ATTRIBUTE((unused)),
141  Select_lex_visitor *visitor MY_ATTRIBUTE((unused))) {
142  return false;
143  }
144 
145  /**
146  Is this statement of a type and on a form that makes it eligible
147  for execution in a secondary storage engine?
148 
149  @return the name of the secondary storage engine, or nullptr if
150  the statement is not eligible for execution in a secondary storage
151  engine
152  */
154  return nullptr;
155  }
156 
157  /**
158  Disable use of secondary storage engines in this statement. After
159  a call to this function, the statement will not try to use a
160  secondary storage engine until it is reprepared.
161  */
163  DBUG_ASSERT(m_secondary_engine == nullptr);
165  }
166 
167  /**
168  Has use of secondary storage engines been disabled for this statement?
169  */
172  }
173 
174  /**
175  Mark the current statement as using a secondary storage engine.
176  This function must be called before the statement starts opening
177  tables in a secondary engine.
178  */
181  m_secondary_engine = hton;
182  }
183 
184  /**
185  Is this statement using a secondary storage engine?
186  */
188  return m_secondary_engine != nullptr;
189  }
190 
191  /**
192  Get the handlerton of the secondary engine that is used for
193  executing this statement, or nullptr if a secondary engine is not
194  used.
195  */
196  const handlerton *secondary_engine() const { return m_secondary_engine; }
197 
198  protected:
200 
201  virtual ~Sql_cmd() {
202  /*
203  Sql_cmd objects are allocated in thd->mem_root.
204  In MySQL, the C++ destructor is never called, the underlying MEM_ROOT is
205  simply destroyed instead.
206  Do not rely on the destructor for any cleanup.
207  */
208  DBUG_ASSERT(false);
209  }
210 
211  /**
212  @return true if object represents a preparable statement, ie. a query
213  that is prepared with a PREPARE statement and executed with an EXECUTE
214  statement. False is returned for regular statements (non-preparable
215  statements) that are executed directly.
216  @todo replace with "m_owner != nullptr" when prepare-once is implemented
217  */
218  bool needs_explicit_preparation() const { return prepare_only; }
219 
220  /// Set this statement as prepared
221  void set_prepared() { m_prepared = true; }
222 
223  private:
225  *m_owner; /// Owning prepared statement, nullptr if non-prep.
226  bool m_prepared; /// True when statement has been prepared
227 
228  /**
229  Tells if a secondary storage engine can be used for this
230  statement. If it is false, use of a secondary storage engine will
231  not be considered for executing this statement.
232  */
234 
235  /**
236  The secondary storage engine to use for execution of this
237  statement, if any, or nullptr if the primary engine is used.
238  This property is reset at the start of each execution.
239  */
241 
242  protected:
243  bool prepare_only; /// @see needs_explicit_preparation
244  /// @todo remove when prepare-once is implemented
245 };
246 
247 #endif // SQL_CMD_INCLUDED
virtual bool is_single_table_plan() const
Definition: sql_cmd.h:123
Representation of an SQL command.
Definition: sql_cmd.h:63
void use_secondary_storage_engine(const handlerton *hton)
Mark the current statement as using a secondary storage engine.
Definition: sql_cmd.h:179
Sql_cmd()
Definition: sql_cmd.h:199
Visitor interface for parse trees.
Prepared_statement: a statement that can contain placeholders.
Definition: sql_prepare.h:321
Definition: mysql_lex_string.h:34
bool m_prepared
Owning prepared statement, nullptr if non-prep.
Definition: sql_cmd.h:226
virtual ~Sql_cmd()
Definition: sql_cmd.h:201
virtual void unprepare(THD *thd)
Temporary function used to "unprepare" a prepared statement after preparation, so that a subsequent e...
Definition: sql_cmd.h:135
void set_owner(Prepared_statement *stmt)
Set the owning prepared statement.
Definition: sql_cmd.h:114
bool m_secondary_engine_enabled
True when statement has been prepared.
Definition: sql_cmd.h:233
Abstract base class for traversing the SELECT_LEX tree.
Definition: select_lex_visitor.h:39
enum_sql_command
Definition: my_sqlcommand.h:45
virtual bool prepare(THD *thd)
Prepare this SQL statement.
Definition: sql_cmd.h:84
Definition: mysql_lex_string.h:39
virtual bool accept(THD *thd, Select_lex_visitor *visitor)
Definition: sql_cmd.h:140
bool needs_explicit_preparation() const
Definition: sql_cmd.h:218
void operator=(Sql_cmd &)
virtual enum_sql_command sql_command_code() const =0
Return the command code for this statement.
void disable_secondary_storage_engine()
Disable use of secondary storage engines in this statement.
Definition: sql_cmd.h:162
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
const handlerton * secondary_engine() const
Get the handlerton of the secondary engine that is used for executing this statement, or nullptr if a secondary engine is not used.
Definition: sql_cmd.h:196
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:240
void set_prepared()
Set this statement as prepared.
Definition: sql_cmd.h:221
bool secondary_storage_engine_disabled() const
Has use of secondary storage engines been disabled for this statement?
Definition: sql_cmd.h:170
#define true
Definition: config_static.h:44
bool is_prepared() const
Definition: sql_cmd.h:75
virtual bool is_dml() const
Definition: sql_cmd.h:120
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2272
bool using_secondary_storage_engine() const
Is this statement using a secondary storage engine?
Definition: sql_cmd.h:187
Prepared_statement * m_owner
Definition: sql_cmd.h:225
Prepared_statement * get_owner()
Get the owning prepared statement.
Definition: sql_cmd.h:117
bool prepare_only
Definition: sql_cmd.h:243
virtual bool execute(THD *thd)=0
Execute this SQL statement.
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:153
virtual void cleanup(THD *thd)
Command-specific reinitialization before execution of prepared statement.
Definition: sql_cmd.h:109
#define false
Definition: config_static.h:43
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778