1 /* Copyright (c) 2014, 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 /**
24   @file storage/perfschema/pfs_prepared_stmt.cc
25   Prepared Statement data structures (implementation).
26 */
27 
28 /*
29   This code needs extra visibility in the lexer structures
30 */
31 
32 #include "my_global.h"
33 #include "my_sys.h"
34 #include "pfs_instr.h"
35 #include "pfs_prepared_stmt.h"
36 #include "pfs_global.h"
37 #include "sql_string.h"
38 #include "pfs_buffer_container.h"
39 #include <string.h>
40 
41 /**
42   Initialize table PREPARED_STATEMENTS_INSTANCE.
43   @param param performance schema sizing
44 */
init_prepared_stmt(const PFS_global_param * param)45 int init_prepared_stmt(const PFS_global_param *param)
46 {
47   if (global_prepared_stmt_container.init(param->m_prepared_stmt_sizing))
48     return 1;
49 
50   reset_prepared_stmt_instances();
51   return 0;
52 }
53 
54 /** Cleanup table PREPARED_STATEMENTS_INSTANCE. */
cleanup_prepared_stmt(void)55 void cleanup_prepared_stmt(void)
56 {
57   global_prepared_stmt_container.cleanup();
58 }
59 
reset_data()60 void PFS_prepared_stmt::reset_data()
61 {
62   m_prepare_stat.reset();
63   m_reprepare_stat.reset();
64   m_execute_stat.reset();
65 }
66 
fct_reset_prepared_stmt_instances(PFS_prepared_stmt * pfs)67 static void fct_reset_prepared_stmt_instances(PFS_prepared_stmt *pfs)
68 {
69   pfs->reset_data();
70 }
71 
reset_prepared_stmt_instances()72 void reset_prepared_stmt_instances()
73 {
74   global_prepared_stmt_container.apply_all(fct_reset_prepared_stmt_instances);
75 }
76 
77 PFS_prepared_stmt*
create_prepared_stmt(void * identity,PFS_thread * thread,PFS_program * pfs_program,PFS_events_statements * pfs_stmt,uint stmt_id,const char * stmt_name,uint stmt_name_length,const char * sqltext,uint sqltext_length)78 create_prepared_stmt(void *identity,
79                      PFS_thread *thread, PFS_program *pfs_program,
80                      PFS_events_statements *pfs_stmt, uint stmt_id,
81                      const char* stmt_name, uint stmt_name_length,
82                      const char* sqltext, uint sqltext_length)
83 {
84   PFS_prepared_stmt *pfs= NULL;
85   pfs_dirty_state dirty_state;
86 
87   /* Create a new record in prepared stmt stat array. */
88   pfs= global_prepared_stmt_container.allocate(& dirty_state);
89   if (pfs != NULL)
90   {
91     /* Reset the stats. */
92     pfs->reset_data();
93     /* Do the assignments. */
94     pfs->m_identity= identity;
95     /* Set query text if available, else it will be set later. */
96     if (sqltext_length > 0)
97       strncpy(pfs->m_sqltext, sqltext, sqltext_length);
98 
99     pfs->m_sqltext_length= sqltext_length;
100 
101     if (stmt_name != NULL)
102     {
103       pfs->m_stmt_name_length= stmt_name_length;
104       if (pfs->m_stmt_name_length > PS_NAME_LENGTH)
105         pfs->m_stmt_name_length= PS_NAME_LENGTH;
106       strncpy(pfs->m_stmt_name, stmt_name, pfs->m_stmt_name_length);
107     }
108     else
109       pfs->m_stmt_name_length= 0;
110 
111     pfs->m_stmt_id= stmt_id;
112     pfs->m_owner_thread_id= thread->m_thread_internal_id;
113 
114     /* If this statement prepare is called from a SP. */
115     if (pfs_program)
116     {
117       pfs->m_owner_object_type= pfs_program->m_type;
118       strncpy(pfs->m_owner_object_schema, pfs_program->m_schema_name, pfs_program->m_schema_name_length);
119       pfs->m_owner_object_schema_length= pfs_program->m_schema_name_length;
120       strncpy(pfs->m_owner_object_name, pfs_program->m_object_name, pfs_program->m_object_name_length);
121       pfs->m_owner_object_name_length= pfs_program->m_object_name_length;
122     }
123     else
124     {
125       pfs->m_owner_object_type= NO_OBJECT_TYPE;
126       pfs->m_owner_object_schema_length= 0;
127       pfs->m_owner_object_name_length= 0;
128     }
129 
130     if (pfs_stmt)
131     {
132       if (pfs_program)
133         pfs->m_owner_event_id= pfs_stmt->m_nesting_event_id;
134       else
135         pfs->m_owner_event_id= pfs_stmt->m_event_id;
136     }
137 
138     /* Insert this record. */
139     pfs->m_lock.dirty_to_allocated(& dirty_state);
140   }
141 
142   return pfs;
143 }
144 
delete_prepared_stmt(PFS_prepared_stmt * pfs)145 void delete_prepared_stmt(PFS_prepared_stmt *pfs)
146 {
147   global_prepared_stmt_container.deallocate(pfs);
148   return;
149 }
150