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