1 /* Copyright (c) 2011, 2012, 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 #ifndef SQL_GET_DIAGNOSTICS_H
24 #define SQL_GET_DIAGNOSTICS_H
25 
26 /** Diagnostics information forward reference. */
27 class Diagnostics_information;
28 
29 
30 /**
31   Sql_cmd_get_diagnostics represents a GET DIAGNOSTICS statement.
32 
33   The GET DIAGNOSTICS statement retrieves exception or completion
34   condition information from a Diagnostics Area, usually pertaining
35   to the last non-diagnostic SQL statement that was executed.
36 */
37 class Sql_cmd_get_diagnostics : public Sql_cmd
38 {
39 public:
40   /**
41     Constructor, used to represent a GET DIAGNOSTICS statement.
42 
43     @param info Diagnostics information to be obtained.
44   */
Sql_cmd_get_diagnostics(Diagnostics_information * info)45   Sql_cmd_get_diagnostics(Diagnostics_information *info)
46     : m_info(info)
47   {}
48 
sql_command_code()49   virtual enum_sql_command sql_command_code() const
50   {
51     return SQLCOM_GET_DIAGNOSTICS;
52   }
53 
54   virtual bool execute(THD *thd);
55 
56 private:
57   /** The information to be obtained. */
58   Diagnostics_information *m_info;
59 };
60 
61 
62 /**
63   Represents the diagnostics information to be obtained.
64 
65   Diagnostic information is made available through statement
66   information and condition information items.
67 */
68 class Diagnostics_information : public Sql_alloc
69 {
70 public:
71   /**
72     Which Diagnostics Area to access.
73   */
74   enum Which_area
75   {
76     /** Access the first Diagnostics Area. */
77     CURRENT_AREA,
78     /** Access the second Diagnostics Area. */
79     STACKED_AREA
80   };
81 
82   /** Set which Diagnostics Area to access. */
set_which_da(Which_area area)83   void set_which_da(Which_area area)
84   { m_area= area; }
85 
86   /** Get which Diagnostics Area to access. */
get_which_da(void)87   Which_area get_which_da(void) const
88   { return m_area; }
89 
90   /**
91     Aggregate diagnostics information.
92 
93     @param thd  The current thread.
94     @param da   The Diagnostics Area.
95 
96     @retval false on success.
97     @retval true on error
98   */
99   virtual bool aggregate(THD *thd, const Diagnostics_area *da) = 0;
100 
101 protected:
102   /**
103     Diagnostics_information objects are allocated in thd->mem_root.
104     Do not rely on the destructor for any cleanup.
105   */
~Diagnostics_information()106   virtual ~Diagnostics_information()
107   {
108     DBUG_ASSERT(false);
109   }
110 
111   /**
112     Evaluate a diagnostics information item in a specific context.
113 
114     @param thd        The current thread.
115     @param diag_item  The diagnostics information item.
116     @param ctx        The context to evaluate the item.
117 
118     @retval false on success.
119     @retval true on error.
120   */
121   template <typename Diag_item, typename Context>
evaluate(THD * thd,Diag_item * diag_item,Context ctx)122   bool evaluate(THD *thd, Diag_item *diag_item, Context ctx)
123   {
124     Item *value;
125 
126     /* Get this item's value. */
127     if (! (value= diag_item->get_value(thd, ctx)))
128       return true;
129 
130     /* Set variable/parameter value. */
131     return diag_item->set_value(thd, &value);
132   }
133 
134 private:
135   /** Which Diagnostics Area to access. */
136   Which_area m_area;
137 };
138 
139 
140 /**
141   A diagnostics information item. Used to associate a specific
142   diagnostics information item to a target variable.
143 */
144 class Diagnostics_information_item : public Sql_alloc
145 {
146 public:
147   /**
148     Set a value for this item.
149 
150     @param thd    The current thread.
151     @param value  The obtained value.
152 
153     @retval false on success.
154     @retval true on error.
155   */
156   bool set_value(THD *thd, Item **value);
157 
158 protected:
159   /**
160     Constructor, used to represent a diagnostics information item.
161 
162     @param target A target that gets the value of this item.
163   */
Diagnostics_information_item(Item * target)164   Diagnostics_information_item(Item *target)
165     : m_target(target)
166   {}
167 
168   /**
169     Diagnostics_information_item objects are allocated in thd->mem_root.
170     Do not rely on the destructor for any cleanup.
171   */
~Diagnostics_information_item()172   virtual ~Diagnostics_information_item()
173   {
174     DBUG_ASSERT(false);
175   }
176 
177 private:
178   /** The target variable that will receive the value of this item. */
179   Item *m_target;
180 };
181 
182 
183 /**
184   A statement information item.
185 */
186 class Statement_information_item : public Diagnostics_information_item
187 {
188 public:
189   /** The name of a statement information item. */
190   enum Name
191   {
192     NUMBER,
193     ROW_COUNT
194   };
195 
196   /**
197     Constructor, used to represent a statement information item.
198 
199     @param name   The name of this item.
200     @param target A target that gets the value of this item.
201   */
Statement_information_item(Name name,Item * target)202   Statement_information_item(Name name, Item *target)
203     : Diagnostics_information_item(target), m_name(name)
204   {}
205 
206   /** Obtain value of this statement information item. */
207   Item *get_value(THD *thd, const Diagnostics_area *da);
208 
209 private:
210   /** The name of this statement information item. */
211   Name m_name;
212 };
213 
214 
215 /**
216   Statement information.
217 
218   @remark Provides information about the execution of a statement.
219 */
220 class Statement_information : public Diagnostics_information
221 {
222 public:
223   /**
224     Constructor, used to represent the statement information of a
225     GET DIAGNOSTICS statement.
226 
227     @param items  List of requested statement information items.
228   */
Statement_information(List<Statement_information_item> * items)229   Statement_information(List<Statement_information_item> *items)
230     : m_items(items)
231   {}
232 
233   /** Obtain statement information in the context of a Diagnostics Area. */
234   bool aggregate(THD *thd, const Diagnostics_area *da);
235 
236 private:
237   /* List of statement information items. */
238   List<Statement_information_item> *m_items;
239 };
240 
241 
242 /**
243   A condition information item.
244 */
245 class Condition_information_item : public Diagnostics_information_item
246 {
247 public:
248   /**
249     The name of a condition information item.
250   */
251   enum Name
252   {
253     CLASS_ORIGIN,
254     SUBCLASS_ORIGIN,
255     CONSTRAINT_CATALOG,
256     CONSTRAINT_SCHEMA,
257     CONSTRAINT_NAME,
258     CATALOG_NAME,
259     SCHEMA_NAME,
260     TABLE_NAME,
261     COLUMN_NAME,
262     CURSOR_NAME,
263     MESSAGE_TEXT,
264     MYSQL_ERRNO,
265     RETURNED_SQLSTATE
266   };
267 
268   /**
269     Constructor, used to represent a condition information item.
270 
271     @param name   The name of this item.
272     @param target A target that gets the value of this item.
273   */
Condition_information_item(Name name,Item * target)274   Condition_information_item(Name name, Item *target)
275     : Diagnostics_information_item(target), m_name(name)
276   {}
277 
278   /** Obtain value of this condition information item. */
279   Item *get_value(THD *thd, const Sql_condition *cond);
280 
281 private:
282   /** The name of this condition information item. */
283   Name m_name;
284 
285   /** Create an string item to represent a condition item string. */
286   Item *make_utf8_string_item(THD *thd, const String *str);
287 };
288 
289 
290 /**
291   Condition information.
292 
293   @remark Provides information about conditions raised during the
294           execution of a statement.
295 */
296 class Condition_information : public Diagnostics_information
297 {
298 public:
299   /**
300     Constructor, used to represent the condition information of a
301     GET DIAGNOSTICS statement.
302 
303     @param cond_number_expr Number that identifies the diagnostic condition.
304     @param items List of requested condition information items.
305   */
Condition_information(Item * cond_number_expr,List<Condition_information_item> * items)306   Condition_information(Item *cond_number_expr,
307                         List<Condition_information_item> *items)
308     : m_cond_number_expr(cond_number_expr), m_items(items)
309   {}
310 
311   /** Obtain condition information in the context of a Diagnostics Area. */
312   bool aggregate(THD *thd, const Diagnostics_area *da);
313 
314 private:
315   /**
316     Number that identifies the diagnostic condition for which
317     information is to be obtained.
318   */
319   Item *m_cond_number_expr;
320 
321   /** List of condition information items. */
322   List<Condition_information_item> *m_items;
323 };
324 
325 #endif
326 
327