sql_cmd.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. /**
  19. @file sql/sql_cmd.h
  20. Representation of an SQL command.
  21. */
  22. #ifndef SQL_CMD_INCLUDED
  23. #define SQL_CMD_INCLUDED
  24. #include "my_dbug.h"
  25. #include "my_sqlcommand.h"
  26. #include "sql/select_lex_visitor.h"
  27. class THD;
  28. class Prepared_statement;
  29. struct handlerton;
  30. struct MYSQL_LEX_STRING;
  31. struct MYSQL_LEX_CSTRING;
  32. /**
  33. Representation of an SQL command.
  34. This class is an interface between the parser and the runtime.
  35. The parser builds the appropriate derived classes of Sql_cmd
  36. to represent a SQL statement in the parsed tree.
  37. The execute() method in the derived classes of Sql_cmd contain the runtime
  38. implementation.
  39. Note that this interface is used for SQL statements recently implemented,
  40. the code for older statements tend to load the LEX structure with more
  41. attributes instead.
  42. Implement new statements by sub-classing Sql_cmd, as this improves
  43. code modularity (see the 'big switch' in dispatch_command()), and decreases
  44. the total size of the LEX structure (therefore saving memory in stored
  45. programs).
  46. The recommended name of a derived class of Sql_cmd is Sql_cmd_<derived>.
  47. Notice that the Sql_cmd class should not be confused with the Statement class.
  48. Statement is a class that is used to manage an SQL command or a set
  49. of SQL commands. When the SQL statement text is analyzed, the parser will
  50. create one or more Sql_cmd objects to represent the actual SQL commands.
  51. */
  52. class Sql_cmd {
  53. private:
  54. Sql_cmd(const Sql_cmd &); // No copy constructor wanted
  55. void operator=(Sql_cmd &); // No assignment operator wanted
  56. public:
  57. /**
  58. @brief Return the command code for this statement
  59. */
  60. virtual enum_sql_command sql_command_code() const = 0;
  61. /// @return true if this statement is prepared
  62. bool is_prepared() const { return m_prepared; }
  63. /**
  64. Prepare this SQL statement.
  65. @param thd the current thread
  66. @returns false if success, true if error
  67. */
  68. virtual bool prepare(THD *thd MY_ATTRIBUTE((unused))) {
  69. // Default behavior for a statement is to have no preparation code.
  70. /* purecov: begin inspected */
  71. DBUG_ASSERT(!is_prepared());
  72. set_prepared();
  73. return false;
  74. /* purecov: end */
  75. }
  76. /**
  77. Execute this SQL statement.
  78. @param thd the current thread.
  79. @returns false if success, true if error
  80. */
  81. virtual bool execute(THD *thd) = 0;
  82. /**
  83. Command-specific reinitialization before execution of prepared statement
  84. @see reinit_stmt_before_use()
  85. @note Currently this function is overloaded for INSERT/REPLACE stmts only.
  86. @param thd Current THD.
  87. */
  88. virtual void cleanup(THD *thd MY_ATTRIBUTE((unused))) {
  89. m_secondary_engine = nullptr;
  90. }
  91. /// Set the owning prepared statement
  92. void set_owner(Prepared_statement *stmt) { m_owner = stmt; }
  93. /// Get the owning prepared statement
  94. Prepared_statement *get_owner() { return m_owner; }
  95. /// @return true if SQL command is a DML statement
  96. virtual bool is_dml() const { return false; }
  97. /// @return true if implemented as single table plan, DML statement only
  98. virtual bool is_single_table_plan() const {
  99. /* purecov: begin inspected */
  100. DBUG_ASSERT(is_dml());
  101. return false;
  102. /* purecov: end */
  103. }
  104. /**
  105. Temporary function used to "unprepare" a prepared statement after
  106. preparation, so that a subsequent execute statement will reprepare it.
  107. This is done because UNIT::cleanup() will un-resolve all resolved QBs.
  108. */
  109. virtual void unprepare(THD *thd MY_ATTRIBUTE((unused))) {
  110. DBUG_ASSERT(is_prepared());
  111. m_prepared = false;
  112. }
  113. virtual bool accept(THD *thd MY_ATTRIBUTE((unused)),
  114. Select_lex_visitor *visitor MY_ATTRIBUTE((unused))) {
  115. return false;
  116. }
  117. /**
  118. Is this statement of a type and on a form that makes it eligible
  119. for execution in a secondary storage engine?
  120. @return the name of the secondary storage engine, or nullptr if
  121. the statement is not eligible for execution in a secondary storage
  122. engine
  123. */
  124. virtual const MYSQL_LEX_CSTRING *eligible_secondary_storage_engine() const {
  125. return nullptr;
  126. }
  127. /**
  128. Disable use of secondary storage engines in this statement. After
  129. a call to this function, the statement will not try to use a
  130. secondary storage engine until it is reprepared.
  131. */
  132. void disable_secondary_storage_engine() {
  133. DBUG_ASSERT(m_secondary_engine == nullptr);
  134. m_secondary_engine_enabled = false;
  135. }
  136. /**
  137. Has use of secondary storage engines been disabled for this statement?
  138. */
  139. bool secondary_storage_engine_disabled() const {
  140. return !m_secondary_engine_enabled;
  141. }
  142. /**
  143. Mark the current statement as using a secondary storage engine.
  144. This function must be called before the statement starts opening
  145. tables in a secondary engine.
  146. */
  147. void use_secondary_storage_engine(const handlerton *hton) {
  148. DBUG_ASSERT(m_secondary_engine_enabled);
  149. m_secondary_engine = hton;
  150. }
  151. /**
  152. Is this statement using a secondary storage engine?
  153. */
  154. bool using_secondary_storage_engine() const {
  155. return m_secondary_engine != nullptr;
  156. }
  157. /**
  158. Get the handlerton of the secondary engine that is used for
  159. executing this statement, or nullptr if a secondary engine is not
  160. used.
  161. */
  162. const handlerton *secondary_engine() const { return m_secondary_engine; }
  163. protected:
  164. Sql_cmd() : m_owner(nullptr), m_prepared(false), prepare_only(true) {}
  165. virtual ~Sql_cmd() {
  166. /*
  167. Sql_cmd objects are allocated in thd->mem_root.
  168. In MySQL, the C++ destructor is never called, the underlying MEM_ROOT is
  169. simply destroyed instead.
  170. Do not rely on the destructor for any cleanup.
  171. */
  172. DBUG_ASSERT(false);
  173. }
  174. /**
  175. @return true if object represents a preparable statement, ie. a query
  176. that is prepared with a PREPARE statement and executed with an EXECUTE
  177. statement. False is returned for regular statements (non-preparable
  178. statements) that are executed directly.
  179. @todo replace with "m_owner != nullptr" when prepare-once is implemented
  180. */
  181. bool needs_explicit_preparation() const { return prepare_only; }
  182. /// Set this statement as prepared
  183. void set_prepared() { m_prepared = true; }
  184. private:
  185. Prepared_statement
  186. *m_owner; /// Owning prepared statement, nullptr if non-prep.
  187. bool m_prepared; /// True when statement has been prepared
  188. /**
  189. Tells if a secondary storage engine can be used for this
  190. statement. If it is false, use of a secondary storage engine will
  191. not be considered for executing this statement.
  192. */
  193. bool m_secondary_engine_enabled{true};
  194. /**
  195. The secondary storage engine to use for execution of this
  196. statement, if any, or nullptr if the primary engine is used.
  197. This property is reset at the start of each execution.
  198. */
  199. const handlerton *m_secondary_engine{nullptr};
  200. protected:
  201. bool prepare_only; /// @see needs_explicit_preparation
  202. /// @todo remove when prepare-once is implemented
  203. };
  204. #endif // SQL_CMD_INCLUDED