1/* Copyright (c) 2009, 2010, 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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/**
17 @file Representation of an SQL command.
18*/
19
20#ifndef SQL_CMD_INCLUDED
21#define SQL_CMD_INCLUDED
22
23/*
24 When a command is added here, be sure it's also added in mysqld.cc
25 in "struct show_var_st status_vars[]= {" ...
26
27 If the command returns a result set or is not allowed in stored
28 functions or triggers, please also make sure that
29 sp_get_flags_for_command (sp_head.cc) returns proper flags for the
30 added SQLCOM_.
31*/
32
33enum enum_sql_command {
34 SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
35 SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
36 SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
37
38 SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
39 SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
40 SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
41 SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
42 SQLCOM_SHOW_GRANTS, SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
43 SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
44 SQLCOM_SHOW_TRIGGERS,
45
46 SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
47 SQLCOM_GRANT,
48 SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
49 SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
50 SQLCOM_CREATE_FUNCTION, SQLCOM_DROP_FUNCTION,
51 SQLCOM_REVOKE,SQLCOM_OPTIMIZE, SQLCOM_CHECK,
52 SQLCOM_ASSIGN_TO_KEYCACHE, SQLCOM_PRELOAD_KEYS,
53 SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
54 SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
55 SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
56 SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
57 SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
58 SQLCOM_RENAME_TABLE,
59 SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
60 SQLCOM_SHOW_OPEN_TABLES,
61 SQLCOM_HA_OPEN, SQLCOM_HA_CLOSE, SQLCOM_HA_READ,
62 SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
63 SQLCOM_SHOW_BINLOG_EVENTS, SQLCOM_DO,
64 SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
65 SQLCOM_SHOW_STORAGE_ENGINES, SQLCOM_SHOW_PRIVILEGES,
66 SQLCOM_HELP, SQLCOM_CREATE_USER, SQLCOM_DROP_USER, SQLCOM_RENAME_USER,
67 SQLCOM_REVOKE_ALL, SQLCOM_CHECKSUM,
68 SQLCOM_CREATE_PROCEDURE, SQLCOM_CREATE_SPFUNCTION, SQLCOM_CALL,
69 SQLCOM_DROP_PROCEDURE, SQLCOM_ALTER_PROCEDURE,SQLCOM_ALTER_FUNCTION,
70 SQLCOM_SHOW_CREATE_PROC, SQLCOM_SHOW_CREATE_FUNC,
71 SQLCOM_SHOW_STATUS_PROC, SQLCOM_SHOW_STATUS_FUNC,
72 SQLCOM_PREPARE, SQLCOM_EXECUTE, SQLCOM_DEALLOCATE_PREPARE,
73 SQLCOM_CREATE_VIEW, SQLCOM_DROP_VIEW,
74 SQLCOM_CREATE_TRIGGER, SQLCOM_DROP_TRIGGER,
75 SQLCOM_XA_START, SQLCOM_XA_END, SQLCOM_XA_PREPARE,
76 SQLCOM_XA_COMMIT, SQLCOM_XA_ROLLBACK, SQLCOM_XA_RECOVER,
77 SQLCOM_SHOW_PROC_CODE, SQLCOM_SHOW_FUNC_CODE,
78 SQLCOM_ALTER_TABLESPACE,
79 SQLCOM_INSTALL_PLUGIN, SQLCOM_UNINSTALL_PLUGIN,
80 SQLCOM_SHOW_AUTHORS, SQLCOM_BINLOG_BASE64_EVENT,
81 SQLCOM_SHOW_PLUGINS, SQLCOM_SHOW_CONTRIBUTORS,
82 SQLCOM_CREATE_SERVER, SQLCOM_DROP_SERVER, SQLCOM_ALTER_SERVER,
83 SQLCOM_CREATE_EVENT, SQLCOM_ALTER_EVENT, SQLCOM_DROP_EVENT,
84 SQLCOM_SHOW_CREATE_EVENT, SQLCOM_SHOW_EVENTS,
85 SQLCOM_SHOW_CREATE_TRIGGER,
86 SQLCOM_ALTER_DB_UPGRADE,
87 SQLCOM_SHOW_PROFILE, SQLCOM_SHOW_PROFILES,
88 SQLCOM_SIGNAL, SQLCOM_RESIGNAL,
89 SQLCOM_SHOW_RELAYLOG_EVENTS,
90 SQLCOM_GET_DIAGNOSTICS,
91 SQLCOM_SLAVE_ALL_START, SQLCOM_SLAVE_ALL_STOP,
92 SQLCOM_SHOW_EXPLAIN, SQLCOM_SHUTDOWN,
93 SQLCOM_CREATE_ROLE, SQLCOM_DROP_ROLE, SQLCOM_GRANT_ROLE, SQLCOM_REVOKE_ROLE,
94 SQLCOM_COMPOUND,
95 SQLCOM_SHOW_GENERIC,
96 SQLCOM_ALTER_USER,
97 SQLCOM_SHOW_CREATE_USER,
98 SQLCOM_EXECUTE_IMMEDIATE,
99 SQLCOM_CREATE_SEQUENCE,
100 SQLCOM_DROP_SEQUENCE,
101 SQLCOM_ALTER_SEQUENCE,
102 SQLCOM_CREATE_PACKAGE,
103 SQLCOM_DROP_PACKAGE,
104 SQLCOM_CREATE_PACKAGE_BODY,
105 SQLCOM_DROP_PACKAGE_BODY,
106 SQLCOM_SHOW_CREATE_PACKAGE,
107 SQLCOM_SHOW_CREATE_PACKAGE_BODY,
108 SQLCOM_SHOW_STATUS_PACKAGE,
109 SQLCOM_SHOW_STATUS_PACKAGE_BODY,
110 SQLCOM_SHOW_PACKAGE_BODY_CODE,
111
112 /*
113 When a command is added here, be sure it's also added in mysqld.cc
114 in "struct show_var_st com_status_vars[]= {" ...
115 */
116 /* This should be the last !!! */
117 SQLCOM_END
118};
119
120/**
121 @class Sql_cmd - Representation of an SQL command.
122
123 This class is an interface between the parser and the runtime.
124 The parser builds the appropriate derived classes of Sql_cmd
125 to represent a SQL statement in the parsed tree.
126 The execute() method in the derived classes of Sql_cmd contain the runtime
127 implementation.
128 Note that this interface is used for SQL statements recently implemented,
129 the code for older statements tend to load the LEX structure with more
130 attributes instead.
131 Implement new statements by sub-classing Sql_cmd, as this improves
132 code modularity (see the 'big switch' in dispatch_command()), and decreases
133 the total size of the LEX structure (therefore saving memory in stored
134 programs).
135 The recommended name of a derived class of Sql_cmd is Sql_cmd_<derived>.
136
137 Notice that the Sql_cmd class should not be confused with the
138 Statement class. Statement is a class that is used to manage an SQL
139 command or a set of SQL commands. When the SQL statement text is
140 analyzed, the parser will create one or more Sql_cmd objects to
141 represent the actual SQL commands.
142*/
143class Sql_cmd : public Sql_alloc
144{
145private:
146 Sql_cmd(const Sql_cmd &); // No copy constructor wanted
147 void operator=(Sql_cmd &); // No assignment operator wanted
148
149public:
150 /**
151 @brief Return the command code for this statement
152 */
153 virtual enum_sql_command sql_command_code() const = 0;
154
155 /**
156 Execute this SQL statement.
157 @param thd the current thread.
158 @retval false on success.
159 @retval true on error
160 */
161 virtual bool execute(THD *thd) = 0;
162
163protected:
164 Sql_cmd()
165 {}
166
167 virtual ~Sql_cmd()
168 {
169 /*
170 Sql_cmd objects are allocated in thd->mem_root.
171 In MySQL, the C++ destructor is never called, the underlying MEM_ROOT is
172 simply destroyed instead.
173 Do not rely on the destructor for any cleanup.
174 */
175 DBUG_ASSERT(FALSE);
176 }
177};
178
179
180/**
181 Sql_cmd_call represents the CALL statement.
182*/
183class Sql_cmd_call : public Sql_cmd
184{
185public:
186 class sp_name *m_name;
187 const class Sp_handler *m_handler;
188 Sql_cmd_call(class sp_name *name, const class Sp_handler *handler)
189 :m_name(name),
190 m_handler(handler)
191 {}
192
193 virtual ~Sql_cmd_call()
194 {}
195
196 /**
197 Execute a CALL statement at runtime.
198 @param thd the current thread.
199 @return false on success.
200 */
201 bool execute(THD *thd);
202
203 virtual enum_sql_command sql_command_code() const
204 {
205 return SQLCOM_CALL;
206 }
207};
208
209
210#endif // SQL_CMD_INCLUDED
211