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
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef MYSQL_PS_H
24 #define MYSQL_PS_H
25 
26 /**
27   @file mysql/psi/mysql_ps.h
28   Instrumentation helpers for prepared statements.
29 */
30 
31 #include "mysql/psi/psi.h"
32 
33 #ifndef PSI_PS_CALL
34 #define PSI_PS_CALL(M) PSI_DYNAMIC_CALL(M)
35 #endif
36 
37 #ifdef HAVE_PSI_PS_INTERFACE
38   #define MYSQL_CREATE_PS(IDENTITY, ID, LOCKER, NAME, NAME_LENGTH, SQLTEXT, SQLTEXT_LENGTH) \
39     inline_mysql_create_prepared_stmt(IDENTITY, ID, LOCKER, NAME, NAME_LENGTH, SQLTEXT, SQLTEXT_LENGTH)
40   #define MYSQL_EXECUTE_PS(LOCKER, PREPARED_STMT) \
41     inline_mysql_execute_prepared_stmt(LOCKER, PREPARED_STMT)
42   #define MYSQL_DESTROY_PS(PREPARED_STMT) \
43     inline_mysql_destroy_prepared_stmt(PREPARED_STMT)
44   #define MYSQL_REPREPARE_PS(PREPARED_STMT) \
45     inline_mysql_reprepare_prepared_stmt(PREPARED_STMT)
46   #define MYSQL_SET_PS_TEXT(PREPARED_STMT, SQLTEXT, SQLTEXT_LENGTH) \
47     inline_mysql_set_prepared_stmt_text(PREPARED_STMT, SQLTEXT, SQLTEXT_LENGTH)
48 #else
49   #define MYSQL_CREATE_PS(IDENTITY, ID, LOCKER, NAME, NAME_LENGTH, SQLTEXT, SQLTEXT_LENGTH) \
50     NULL
51   #define MYSQL_EXECUTE_PS(LOCKER, PREPARED_STMT) \
52     do {} while (0)
53   #define MYSQL_DESTROY_PS(PREPARED_STMT) \
54     do {} while (0)
55   #define MYSQL_REPREPARE_PS(PREPARED_STMT) \
56     do {} while (0)
57   #define MYSQL_SET_PS_TEXT(PREPARED_STMT, SQLTEXT, SQLTEXT_LENGTH) \
58     do {} while (0)
59 #endif
60 
61 #ifdef HAVE_PSI_PS_INTERFACE
62 static inline struct PSI_prepared_stmt*
inline_mysql_create_prepared_stmt(void * identity,uint stmt_id,PSI_statement_locker * locker,const char * stmt_name,size_t stmt_name_length,const char * sqltext,size_t sqltext_length)63 inline_mysql_create_prepared_stmt(void *identity, uint stmt_id,
64                                   PSI_statement_locker *locker,
65                                   const char *stmt_name, size_t stmt_name_length,
66                                   const char *sqltext, size_t sqltext_length)
67 {
68   if (locker == NULL)
69     return NULL;
70   return PSI_PS_CALL(create_prepared_stmt)(identity, stmt_id,
71                                            locker,
72                                            stmt_name, stmt_name_length,
73                                            sqltext, sqltext_length);
74 }
75 
76 static inline void
inline_mysql_execute_prepared_stmt(PSI_statement_locker * locker,PSI_prepared_stmt * prepared_stmt)77 inline_mysql_execute_prepared_stmt(PSI_statement_locker *locker,
78                                    PSI_prepared_stmt* prepared_stmt)
79 {
80   if (prepared_stmt != NULL && locker != NULL)
81     PSI_PS_CALL(execute_prepared_stmt)(locker, prepared_stmt);
82 }
83 
84 static inline void
inline_mysql_destroy_prepared_stmt(PSI_prepared_stmt * prepared_stmt)85 inline_mysql_destroy_prepared_stmt(PSI_prepared_stmt *prepared_stmt)
86 {
87   if (prepared_stmt != NULL)
88     PSI_PS_CALL(destroy_prepared_stmt)(prepared_stmt);
89 }
90 
91 static inline void
inline_mysql_reprepare_prepared_stmt(PSI_prepared_stmt * prepared_stmt)92 inline_mysql_reprepare_prepared_stmt(PSI_prepared_stmt *prepared_stmt)
93 {
94   if (prepared_stmt != NULL)
95     PSI_PS_CALL(reprepare_prepared_stmt)(prepared_stmt);
96 }
97 
98 static inline void
inline_mysql_set_prepared_stmt_text(PSI_prepared_stmt * prepared_stmt,const char * text,uint text_len)99 inline_mysql_set_prepared_stmt_text(PSI_prepared_stmt *prepared_stmt,
100                                     const char *text,
101                                     uint text_len)
102 {
103   if (prepared_stmt != NULL)
104   {
105     PSI_PS_CALL(set_prepared_stmt_text)(prepared_stmt, text, text_len);
106   }
107 }
108 #endif
109 
110 #endif
111