1 /* Copyright (c) 2010, 2015, 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 Foundation,
21   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #ifndef TABLE_EVENTS_STATEMENTS_H
24 #define TABLE_EVENTS_STATEMENTS_H
25 
26 /**
27   @file storage/perfschema/table_events_statements.h
28   Table EVENTS_STATEMENTS_xxx (declarations).
29 */
30 
31 #include "pfs_column_types.h"
32 #include "pfs_engine_table.h"
33 #include "pfs_events_statements.h"
34 #include "table_helper.h"
35 
36 struct PFS_thread;
37 
38 /**
39   @addtogroup Performance_schema_tables
40   @{
41 */
42 
43 /** A row of table_events_statements_common. */
44 struct row_events_statements
45 {
46   /** Column THREAD_ID. */
47   ulonglong m_thread_internal_id;
48   /** Column EVENT_ID. */
49   ulonglong m_event_id;
50   /** Column END_EVENT_ID. */
51   ulonglong m_end_event_id;
52   /** Column NESTING_EVENT_ID. */
53   ulonglong m_nesting_event_id;
54   /** Column NESTING_EVENT_TYPE. */
55   enum_event_type m_nesting_event_type;
56   /** Column EVENT_NAME. */
57   const char *m_name;
58   /** Length in bytes of @c m_name. */
59   uint m_name_length;
60   /** Column TIMER_START. */
61   ulonglong m_timer_start;
62   /** Column TIMER_END. */
63   ulonglong m_timer_end;
64   /** Column TIMER_WAIT. */
65   ulonglong m_timer_wait;
66   /** Column LOCK_TIME. */
67   ulonglong m_lock_time;
68   /** Column SOURCE. */
69   char m_source[COL_SOURCE_SIZE];
70   /** Length in bytes of @c m_source. */
71   uint m_source_length;
72   /** Column SQL_TEXT. */
73   String m_sqltext;
74   /** Column DIGEST and DIGEST_TEXT. */
75   PFS_digest_row m_digest;
76   /** Column CURRENT_SCHEMA. */
77   char m_current_schema_name[NAME_LEN];
78   /** Length in bytes of @c m_current_schema_name. */
79   uint m_current_schema_name_length;
80 
81   /** Column MESSAGE_TEXT. */
82   char m_message_text[MYSQL_ERRMSG_SIZE+1];
83   /** Column MYSQL_ERRNO. */
84   uint m_sql_errno;
85   /** Column RETURNED_SQLSTATE. */
86   char m_sqlstate[SQLSTATE_LENGTH];
87   /** Column ERRORS. */
88   uint m_error_count;
89   /** Column WARNINGS. */
90   uint m_warning_count;
91   /** Column ROWS_AFFECTED. */
92   ulonglong m_rows_affected;
93   /** Column ROWS_SENT. */
94   ulonglong m_rows_sent;
95   /** Column ROWS_EXAMINED. */
96   ulonglong m_rows_examined;
97   /** Column CREATED_TMP_DISK_TABLES. */
98   ulonglong m_created_tmp_disk_tables;
99   /** Column CREATED_TMP_TABLES. */
100   ulonglong m_created_tmp_tables;
101   /** Column SELECT_FULL_JOIN. */
102   ulonglong m_select_full_join;
103   /** Column SELECT_FULL_RANGE_JOIN. */
104   ulonglong m_select_full_range_join;
105   /** Column SELECT_RANGE. */
106   ulonglong m_select_range;
107   /** Column SELECT_RANGE_CHECK. */
108   ulonglong m_select_range_check;
109   /** Column SELECT_SCAN. */
110   ulonglong m_select_scan;
111   /** Column SORT_MERGE_PASSES. */
112   ulonglong m_sort_merge_passes;
113   /** Column SORT_RANGE. */
114   ulonglong m_sort_range;
115   /** Column SORT_ROWS. */
116   ulonglong m_sort_rows;
117   /** Column SORT_SCAN. */
118   ulonglong m_sort_scan;
119   /** Column NO_INDEX_USED. */
120   ulonglong m_no_index_used;
121   /** Column NO_GOOD_INDEX_USED. */
122   ulonglong m_no_good_index_used;
123 };
124 
125 /** Position of a cursor on PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_CURRENT. */
126 struct pos_events_statements_current : public PFS_double_index
127 {
pos_events_statements_currentpos_events_statements_current128   pos_events_statements_current()
129     : PFS_double_index(0, 0)
130   {}
131 
resetpos_events_statements_current132   inline void reset(void)
133   {
134     m_index_1= 0;
135     m_index_2= 0;
136   }
137 
next_threadpos_events_statements_current138   inline void next_thread(void)
139   {
140     m_index_1++;
141     m_index_2= 0;
142   }
143 };
144 
145 /** Position of a cursor on PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_HISTORY. */
146 struct pos_events_statements_history : public PFS_double_index
147 {
pos_events_statements_historypos_events_statements_history148   pos_events_statements_history()
149     : PFS_double_index(0, 0)
150   {}
151 
resetpos_events_statements_history152   inline void reset(void)
153   {
154     m_index_1= 0;
155     m_index_2= 0;
156   }
157 
next_threadpos_events_statements_history158   inline void next_thread(void)
159   {
160     m_index_1++;
161     m_index_2= 0;
162   }
163 };
164 
165 /**
166   Adapter, for table sharing the structure of
167   PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_CURRENT.
168 */
169 class table_events_statements_common : public PFS_engine_table
170 {
171 protected:
172   virtual int read_row_values(TABLE *table,
173                               unsigned char *buf,
174                               Field **fields,
175                               bool read_all);
176 
177   table_events_statements_common(const PFS_engine_table_share *share, void *pos);
178 
~table_events_statements_common()179   ~table_events_statements_common()
180   {}
181 
182   void make_row_part_1(PFS_events_statements *statement,
183                        sql_digest_storage *digest);
184 
185   void make_row_part_2(const sql_digest_storage *digest);
186 
187   /** Current row. */
188   row_events_statements m_row;
189   /** True if the current row exists. */
190   bool m_row_exists;
191   unsigned char m_token_array[MAX_DIGEST_STORAGE_SIZE];
192 };
193 
194 /** Table PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_CURRENT. */
195 class table_events_statements_current : public table_events_statements_common
196 {
197 public:
198   /** Table share */
199   static PFS_engine_table_share m_share;
200   static PFS_engine_table* create();
201   static int delete_all_rows();
202 
203   virtual int rnd_init(bool scan);
204   virtual int rnd_next();
205   virtual int rnd_pos(const void *pos);
206   virtual void reset_position(void);
207 
208 protected:
209   table_events_statements_current();
210 
211 public:
~table_events_statements_current()212   ~table_events_statements_current()
213   {}
214 
215 private:
216   friend class table_events_statements_history;
217   friend class table_events_statements_history_long;
218 
219   /** Table share lock. */
220   static THR_LOCK m_table_lock;
221   /**
222     Fields definition.
223     Also used by table_events_statements_history
224     and table_events_statements_history_long.
225   */
226   static TABLE_FIELD_DEF m_field_def;
227 
228   void make_row(PFS_thread* pfs_thread, PFS_events_statements *statement);
229 
230   /** Current position. */
231   pos_events_statements_current m_pos;
232   /** Next position. */
233   pos_events_statements_current m_next_pos;
234 };
235 
236 /** Table PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_HISTORY. */
237 class table_events_statements_history : public table_events_statements_common
238 {
239 public:
240   /** Table share */
241   static PFS_engine_table_share m_share;
242   static PFS_engine_table* create();
243   static int delete_all_rows();
244 
245   virtual int rnd_init(bool scan);
246   virtual int rnd_next();
247   virtual int rnd_pos(const void *pos);
248   virtual void reset_position(void);
249 
250 protected:
251   table_events_statements_history();
252 
253 public:
~table_events_statements_history()254   ~table_events_statements_history()
255   {}
256 
257 private:
258   /** Table share lock. */
259   static THR_LOCK m_table_lock;
260 
261   void make_row(PFS_thread* pfs_thread, PFS_events_statements *statement);
262 
263   /** Current position. */
264   pos_events_statements_history m_pos;
265   /** Next position. */
266   pos_events_statements_history m_next_pos;
267 };
268 
269 /** Table PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_HISTORY_LONG. */
270 class table_events_statements_history_long : public table_events_statements_common
271 {
272 public:
273   /** Table share */
274   static PFS_engine_table_share m_share;
275   static PFS_engine_table* create();
276   static int delete_all_rows();
277 
278   virtual int rnd_init(bool scan);
279   virtual int rnd_next();
280   virtual int rnd_pos(const void *pos);
281   virtual void reset_position(void);
282 
283 protected:
284   table_events_statements_history_long();
285 
286 public:
~table_events_statements_history_long()287   ~table_events_statements_history_long()
288   {}
289 
290 private:
291   /** Table share lock. */
292   static THR_LOCK m_table_lock;
293 
294   void make_row(PFS_events_statements *statement);
295 
296   /** Current position. */
297   PFS_simple_index m_pos;
298   /** Next position. */
299   PFS_simple_index m_next_pos;
300 };
301 
302 /** @} */
303 #endif
304