1 /* Copyright (c) 2010, 2021, Oracle and/or its affiliates.
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 NESTING_EVENT_LEVEL. */
57   uint m_nesting_event_level;
58   /** Column EVENT_NAME. */
59   const char *m_name;
60   /** Length in bytes of @c m_name. */
61   uint m_name_length;
62   /** Column TIMER_START. */
63   ulonglong m_timer_start;
64   /** Column TIMER_END. */
65   ulonglong m_timer_end;
66   /** Column TIMER_WAIT. */
67   ulonglong m_timer_wait;
68   /** Column LOCK_TIME. */
69   ulonglong m_lock_time;
70   /** Column SOURCE. */
71   char m_source[COL_SOURCE_SIZE];
72   /** Length in bytes of @c m_source. */
73   uint m_source_length;
74   /** Column SQL_TEXT. */
75   String m_sqltext;
76   /** Column DIGEST and DIGEST_TEXT. */
77   PFS_digest_row m_digest;
78     /** Column CURRENT_SCHEMA. */
79   char m_current_schema_name[NAME_LEN];
80   /** Length in bytes of @c m_current_schema_name. */
81   uint m_current_schema_name_length;
82 
83   /** Column OBJECT_TYPE. */
84   enum_object_type m_object_type;
85   /** Column OBJECT_SCHEMA. */
86   char m_schema_name[NAME_LEN];
87   /** Length in bytes of @c m_schema_name. */
88   uint m_schema_name_length;
89   /** Column OBJECT_NAME. */
90   char m_object_name[NAME_LEN];
91   /** Length in bytes of @c m_object_name. */
92   uint m_object_name_length;
93 
94 
95   /** Column MESSAGE_TEXT. */
96   char m_message_text[MYSQL_ERRMSG_SIZE+1];
97   /** Column MYSQL_ERRNO. */
98   uint m_sql_errno;
99   /** Column RETURNED_SQLSTATE. */
100   char m_sqlstate[SQLSTATE_LENGTH];
101   /** Column ERRORS. */
102   uint m_error_count;
103   /** Column WARNINGS. */
104   uint m_warning_count;
105   /** Column ROWS_AFFECTED. */
106   ulonglong m_rows_affected;
107   /** Column ROWS_SENT. */
108   ulonglong m_rows_sent;
109   /** Column ROWS_EXAMINED. */
110   ulonglong m_rows_examined;
111   /** Column CREATED_TMP_DISK_TABLES. */
112   ulonglong m_created_tmp_disk_tables;
113   /** Column CREATED_TMP_TABLES. */
114   ulonglong m_created_tmp_tables;
115   /** Column SELECT_FULL_JOIN. */
116   ulonglong m_select_full_join;
117   /** Column SELECT_FULL_RANGE_JOIN. */
118   ulonglong m_select_full_range_join;
119   /** Column SELECT_RANGE. */
120   ulonglong m_select_range;
121   /** Column SELECT_RANGE_CHECK. */
122   ulonglong m_select_range_check;
123   /** Column SELECT_SCAN. */
124   ulonglong m_select_scan;
125   /** Column SORT_MERGE_PASSES. */
126   ulonglong m_sort_merge_passes;
127   /** Column SORT_RANGE. */
128   ulonglong m_sort_range;
129   /** Column SORT_ROWS. */
130   ulonglong m_sort_rows;
131   /** Column SORT_SCAN. */
132   ulonglong m_sort_scan;
133   /** Column NO_INDEX_USED. */
134   ulonglong m_no_index_used;
135   /** Column NO_GOOD_INDEX_USED. */
136   ulonglong m_no_good_index_used;
137 };
138 
139 /** Position of a cursor on PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_CURRENT. */
140 struct pos_events_statements_current : public PFS_double_index
141 {
pos_events_statements_currentpos_events_statements_current142   pos_events_statements_current()
143     : PFS_double_index(0, 0)
144   {}
145 
resetpos_events_statements_current146   inline void reset(void)
147   {
148     m_index_1= 0;
149     m_index_2= 0;
150   }
151 
next_threadpos_events_statements_current152   inline void next_thread(void)
153   {
154     m_index_1++;
155     m_index_2= 0;
156   }
157 };
158 
159 /** Position of a cursor on PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_HISTORY. */
160 struct pos_events_statements_history : public PFS_double_index
161 {
pos_events_statements_historypos_events_statements_history162   pos_events_statements_history()
163     : PFS_double_index(0, 0)
164   {}
165 
resetpos_events_statements_history166   inline void reset(void)
167   {
168     m_index_1= 0;
169     m_index_2= 0;
170   }
171 
next_threadpos_events_statements_history172   inline void next_thread(void)
173   {
174     m_index_1++;
175     m_index_2= 0;
176   }
177 };
178 
179 /**
180   Adapter, for table sharing the structure of
181   PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_CURRENT.
182 */
183 class table_events_statements_common : public PFS_engine_table
184 {
185 protected:
186   virtual int read_row_values(TABLE *table,
187                               unsigned char *buf,
188                               Field **fields,
189                               bool read_all);
190 
191   table_events_statements_common(const PFS_engine_table_share *share, void *pos);
192 
~table_events_statements_common()193   ~table_events_statements_common()
194   {}
195 
196   void make_row_part_1(PFS_events_statements *statement,
197                        sql_digest_storage *digest);
198 
199   void make_row_part_2(const sql_digest_storage *digest);
200 
201   /** Current row. */
202   row_events_statements m_row;
203   /** True if the current row exists. */
204   bool m_row_exists;
205   unsigned char m_token_array[MAX_DIGEST_STORAGE_SIZE];
206 };
207 
208 /** Table PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_CURRENT. */
209 class table_events_statements_current : public table_events_statements_common
210 {
211 public:
212   /** Table share */
213   static PFS_engine_table_share m_share;
214   static PFS_engine_table* create();
215   static int delete_all_rows();
216   static ha_rows get_row_count();
217 
218   virtual int rnd_init(bool scan);
219   virtual int rnd_next();
220   virtual int rnd_pos(const void *pos);
221   virtual void reset_position(void);
222 
223 protected:
224   table_events_statements_current();
225 
226 public:
~table_events_statements_current()227   ~table_events_statements_current()
228   {}
229 
230 private:
231   friend class table_events_statements_history;
232   friend class table_events_statements_history_long;
233 
234   /** Table share lock. */
235   static THR_LOCK m_table_lock;
236   /**
237     Fields definition.
238     Also used by table_events_statements_history
239     and table_events_statements_history_long.
240   */
241   static TABLE_FIELD_DEF m_field_def;
242 
243   void make_row(PFS_thread* pfs_thread, PFS_events_statements *statement);
244 
245   /** Current position. */
246   pos_events_statements_current m_pos;
247   /** Next position. */
248   pos_events_statements_current m_next_pos;
249 };
250 
251 /** Table PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_HISTORY. */
252 class table_events_statements_history : public table_events_statements_common
253 {
254 public:
255   /** Table share */
256   static PFS_engine_table_share m_share;
257   static PFS_engine_table* create();
258   static int delete_all_rows();
259   static ha_rows get_row_count();
260 
261   virtual int rnd_init(bool scan);
262   virtual int rnd_next();
263   virtual int rnd_pos(const void *pos);
264   virtual void reset_position(void);
265 
266 protected:
267   table_events_statements_history();
268 
269 public:
~table_events_statements_history()270   ~table_events_statements_history()
271   {}
272 
273 private:
274   /** Table share lock. */
275   static THR_LOCK m_table_lock;
276 
277   void make_row(PFS_thread* pfs_thread, PFS_events_statements *statement);
278 
279   /** Current position. */
280   pos_events_statements_history m_pos;
281   /** Next position. */
282   pos_events_statements_history m_next_pos;
283 };
284 
285 /** Table PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_HISTORY_LONG. */
286 class table_events_statements_history_long : public table_events_statements_common
287 {
288 public:
289   /** Table share */
290   static PFS_engine_table_share m_share;
291   static PFS_engine_table* create();
292   static int delete_all_rows();
293   static ha_rows get_row_count();
294 
295   virtual int rnd_init(bool scan);
296   virtual int rnd_next();
297   virtual int rnd_pos(const void *pos);
298   virtual void reset_position(void);
299 
300 protected:
301   table_events_statements_history_long();
302 
303 public:
~table_events_statements_history_long()304   ~table_events_statements_history_long()
305   {}
306 
307 private:
308   /** Table share lock. */
309   static THR_LOCK m_table_lock;
310 
311   void make_row(PFS_events_statements *statement);
312 
313   /** Current position. */
314   PFS_simple_index m_pos;
315   /** Next position. */
316   PFS_simple_index m_next_pos;
317 };
318 
319 /** @} */
320 #endif
321