MySQL  8.0.27
Source Code Documentation
sql_truncate.h
Go to the documentation of this file.
1 #ifndef SQL_TRUNCATE_INCLUDED
2 #define SQL_TRUNCATE_INCLUDED
3 
4 /* Copyright (c) 2010, 2021, Oracle and/or its affiliates.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 #include <memory>
27 #include <string>
28 
29 #include "my_sqlcommand.h"
30 #include "sql/dd/types/table.h"
31 #include "sql/sql_cmd.h"
32 
33 class MDL_ticket;
34 class THD;
35 struct TABLE_LIST;
36 struct handlerton;
37 
38 using Up_table = std::unique_ptr<dd::Table>;
39 
40 /**
41  Sql_cmd_truncate_table represents the TRUNCATE statement.
42 */
44  /** Set if a lock must be downgraded after truncate is done. */
46 
47  /** Track error status from functions called. */
48  bool m_error = true;
49 
50  public:
51  /**
52  Constructor, used to represent a TRUNCATE statement.
53  */
55 
56  ~Sql_cmd_truncate_table() override = default;
57 
58  bool execute(THD *) override;
59 
60  enum_sql_command sql_command_code() const override { return SQLCOM_TRUNCATE; }
61 
62  private:
63  /* Handle locking a base table for truncate. */
64  bool lock_table(THD *, TABLE_LIST *);
65 
66  /*
67  Optimized delete of all rows by doing a full regenerate of the table.
68  Depending on the storage engine, it can be accomplished through a
69  drop and recreate or via the handler truncate method.
70  */
71  void truncate_base(THD *, TABLE_LIST *);
73 
74  void end_transaction(THD *, bool, bool);
75  void cleanup_base(THD *, const handlerton *);
76  void cleanup_temporary(THD *, handlerton *, const TABLE_LIST &, Up_table *,
77  const std::string &);
78 };
79 
80 #endif
A granted metadata lock.
Definition: mdl.h:980
Sql_cmd_truncate_table represents the TRUNCATE statement.
Definition: sql_truncate.h:43
void cleanup_base(THD *, const handlerton *)
Performs cleanup actions after truncate of a normal (non-temporary) table.
Definition: sql_truncate.cc:398
Sql_cmd_truncate_table()=default
Constructor, used to represent a TRUNCATE statement.
void truncate_temporary(THD *, TABLE_LIST *)
Optimized delete of all rows by doing a full generate of the temporary table.
Definition: sql_truncate.cc:623
bool execute(THD *) override
Execute a TRUNCATE statement at runtime.
Definition: sql_truncate.cc:737
MDL_ticket * m_ticket_downgrade
Set if a lock must be downgraded after truncate is done.
Definition: sql_truncate.h:45
~Sql_cmd_truncate_table() override=default
void end_transaction(THD *, bool, bool)
Completes transaction by attempting to binlog and commit if truncate has been successful so far.
Definition: sql_truncate.cc:371
bool m_error
Track error status from functions called.
Definition: sql_truncate.h:48
void cleanup_temporary(THD *, handlerton *, const TABLE_LIST &, Up_table *, const std::string &)
Reopens a temporary table after truncate if supported.
Definition: sql_truncate.cc:421
bool lock_table(THD *, TABLE_LIST *)
Definition: sql_truncate.cc:303
void truncate_base(THD *, TABLE_LIST *)
Optimized delete of all rows by doing a full generate of the base (non-temporary) table.
Definition: sql_truncate.cc:470
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_truncate.h:60
Representation of an SQL command.
Definition: sql_cmd.h:64
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
enum_sql_command
Definition: my_sqlcommand.h:45
@ SQLCOM_TRUNCATE
Definition: my_sqlcommand.h:54
Representation of an SQL command.
std::unique_ptr< dd::Table > Up_table
Definition: sql_truncate.h:38
Definition: table.h:2694
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2391