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