1 #ifndef SQL_TRUNCATE_INCLUDED
2 #define SQL_TRUNCATE_INCLUDED
3 
4 /* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
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 */
43 class Sql_cmd_truncate_table : public Sql_cmd {
44   /** Set if a lock must be downgraded after truncate is done. */
45   MDL_ticket *m_ticket_downgrade = nullptr;
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   */
Sql_cmd_truncate_table()54   Sql_cmd_truncate_table() {}
55 
~Sql_cmd_truncate_table()56   virtual ~Sql_cmd_truncate_table() {}
57 
58   bool execute(THD *);
59 
sql_command_code()60   virtual enum_sql_command sql_command_code() const { 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 *);
72   void truncate_temporary(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
81