1 /* Copyright (c) 2015, 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 SQL_SERVICE_CONTEXT_BASE_INCLUDE
24 #define SQL_SERVICE_CONTEXT_BASE_INCLUDE
25 
26 #include <mysql/plugin.h>
27 #include "sql_resultset.h"
28 
29 class Sql_service_context_base
30 {
31 public:
32 
33   /** The sql service callbacks that will call the below virtual methods*/
34   static const st_command_service_cbs sql_service_callbacks;
35 
36   /**
37     Sql_service_context_base constructor
38     resets all variables
39   */
Sql_service_context_base()40   Sql_service_context_base() {}
41 
~Sql_service_context_base()42   virtual ~Sql_service_context_base() {}
43 
44   /** Getting metadata **/
45   /**
46     Indicates start of metadata for the result set
47 
48     @param num_cols Number of fields being sent
49     @param flags    Flags to alter the metadata sending
50     @param resultcs Charset of the result set
51 
52     @return
53       @retval 1  Error
54       @retval 0  OK
55   */
56   virtual int start_result_metadata(uint num_cols, uint flags,
57                                     const CHARSET_INFO *resultcs) = 0;
58 
59   /**
60     Field metadata is provided via this callback
61 
62     @param field   Field's metadata (see field.h)
63     @param charset Field's charset
64 
65     @return
66       @retval 1  Error
67       @retval 0  OK
68   */
69   virtual int field_metadata(struct st_send_field *field,
70                              const CHARSET_INFO *charset) = 0;
71 
72   /**
73     Indicates end of metadata for the result set
74 
75     @param server_status   Status of server (see mysql_com.h SERVER_STATUS_*)
76     @param warn_count      Number of warnings thrown during execution
77 
78     @return
79       @retval 1  Error
80       @retval 0  OK
81   */
82   virtual int end_result_metadata(uint server_status,
83                                   uint warn_count) = 0;
84 
85   /**
86     Indicates the beginning of a new row in the result set/metadata
87 
88     @return
89       @retval 1  Error
90       @retval 0  OK
91   */
92   virtual int start_row() = 0;
93 
94   /**
95     Indicates end of the row in the result set/metadata
96 
97     @return
98       @retval 1  Error
99       @retval 0  OK
100   */
101   virtual int end_row() = 0;
102 
103   /**
104     An error occured during execution
105 
106     @details This callback indicates that an error occureded during command
107     execution and the partial row should be dropped. Server will raise error
108     and return.
109   */
110   virtual void abort_row() = 0;
111 
112 
113   /**
114     Return client's capabilities (see mysql_com.h, CLIENT_*)
115 
116     @return Bitmap of client's capabilities
117   */
118   virtual ulong get_client_capabilities() = 0;
119 
120   /** Getting data **/
121   /**
122     Receive NULL value from server
123 
124     @return status
125       @retval 1  Error
126       @retval 0  OK
127   */
128   virtual int get_null() = 0;
129 
130 
131   /**
132     Get TINY/SHORT/LONG value from server
133 
134     @param value         Value received
135 
136     @note In order to know which type exactly was received, the plugin must
137     track the metadata that was sent just prior to the result set.
138 
139     @return status
140       @retval 1  Error
141       @retval 0  OK
142   */
143   virtual int get_integer(longlong value) = 0;
144 
145   /**
146     Get LONGLONG value from server
147 
148     @param value         Value received
149     @param is_unsigned   TRUE <=> value is unsigned
150 
151     @return status
152       @retval 1  Error
153       @retval 0  OK
154   */
155   virtual int get_longlong(longlong value, uint is_unsigned) = 0;
156 
157   /**
158     Receive DECIMAL value from server
159 
160     @param value Value received
161 
162     @return status
163       @retval 1  Error
164       @retval 0  OK
165    */
166   virtual int get_decimal(const decimal_t * value) = 0;
167 
168   /**
169 
170     @return status
171       @retval 1  Error
172       @retval 0  OK
173   */
174   virtual int get_double(double value, uint32 decimals) = 0;
175 
176   /**
177     Get DATE value from server
178 
179     @param value    Value received
180 
181     @return status
182       @retval 1  Error
183       @retval 0  OK
184   */
185   virtual int get_date(const MYSQL_TIME * value) = 0;
186 
187   /**
188     Get TIME value from server
189 
190     @param value    Value received
191     @param decimals Number of decimals
192 
193     @return status
194       @retval 1  Error
195       @retval 0  OK
196   */
197   virtual int get_time(const MYSQL_TIME * value, uint decimals) = 0;
198 
199   /**
200     Get DATETIME value from server
201 
202     @param value    Value received
203     @param decimals Number of decimals
204 
205     @return status
206       @retval 1  Error
207       @retval 0  OK
208   */
209   virtual int get_datetime(const MYSQL_TIME * value,
210                            uint decimals) = 0;
211 
212   /**
213     Get STRING value from server
214 
215     @param value   Value received
216     @param length  Value's length
217     @param valuecs Value's charset
218 
219     @return status
220       @retval 1  Error
221       @retval 0  OK
222   */
223   virtual int get_string(const char * const value,
224                          size_t length, const CHARSET_INFO * const valuecs) = 0;
225 
226 
227   /** Getting execution status **/
228   /**
229     Command ended with success
230 
231     @param server_status        Status of server (see mysql_com.h,
232                                 SERVER_STATUS_*)
233     @param statement_warn_count Number of warnings thrown during execution
234     @param affected_rows        Number of rows affected by the command
235     @param last_insert_id       Last insert id being assigned during execution
236     @param message              A message from server
237   */
238   virtual void handle_ok(uint server_status, uint statement_warn_count,
239                          ulonglong affected_rows, ulonglong last_insert_id,
240                          const char * const message) = 0;
241 
242 
243   /**
244     Command ended with ERROR
245 
246     @param sql_errno Error code
247     @param err_msg   Error message
248     @param sqlstate  SQL state corresponding to the error code
249   */
250   virtual void handle_error(uint sql_errno,
251                             const char * const err_msg,
252                             const char * const sqlstate) = 0;
253 
254   /**
255    Session was shutdown while command was running
256   */
257   virtual void shutdown(int flag) = 0;
258 
259 private:
sql_start_result_metadata(void * ctx,uint num_cols,uint flags,const CHARSET_INFO * resultcs)260   static int sql_start_result_metadata(void *ctx, uint num_cols, uint flags,
261                                        const CHARSET_INFO *resultcs)
262   {
263     return ((Sql_service_context_base *) ctx)->start_result_metadata(num_cols,
264                                                                  flags,
265                                                                  resultcs);
266   }
267 
sql_field_metadata(void * ctx,struct st_send_field * field,const CHARSET_INFO * charset)268   static int sql_field_metadata(void *ctx, struct st_send_field *field,
269                                 const CHARSET_INFO *charset)
270   {
271     return ((Sql_service_context_base *) ctx)->field_metadata(field,
272                                                           charset);
273   }
274 
sql_end_result_metadata(void * ctx,uint server_status,uint warn_count)275   static int sql_end_result_metadata(void *ctx, uint server_status,
276                                      uint warn_count)
277   {
278     return ((Sql_service_context_base *) ctx)->end_result_metadata(server_status,
279                                                                warn_count);
280   }
281 
sql_start_row(void * ctx)282   static int sql_start_row(void *ctx)
283   {
284     return ((Sql_service_context_base *) ctx)->start_row();
285   }
286 
sql_end_row(void * ctx)287   static int sql_end_row(void *ctx)
288   {
289     return ((Sql_service_context_base *) ctx)->end_row();
290   }
291 
sql_abort_row(void * ctx)292   static void sql_abort_row(void *ctx)
293   {
294     return ((Sql_service_context_base *) ctx)->abort_row(); /* purecov: inspected */
295   }
296 
sql_get_client_capabilities(void * ctx)297   static ulong sql_get_client_capabilities(void *ctx)
298   {
299     return ((Sql_service_context_base *) ctx)->get_client_capabilities();
300   }
301 
sql_get_null(void * ctx)302   static int sql_get_null(void *ctx)
303   {
304     return ((Sql_service_context_base *) ctx)->get_null();
305   }
306 
sql_get_integer(void * ctx,longlong value)307   static int sql_get_integer(void * ctx, longlong value)
308   {
309     return ((Sql_service_context_base *) ctx)->get_integer(value);
310   }
311 
sql_get_longlong(void * ctx,longlong value,uint is_unsigned)312   static int sql_get_longlong(void * ctx, longlong value, uint is_unsigned)
313   {
314     return ((Sql_service_context_base *) ctx)->get_longlong(value, is_unsigned);
315   }
316 
sql_get_decimal(void * ctx,const decimal_t * value)317   static int sql_get_decimal(void * ctx, const decimal_t * value)
318   {
319     return ((Sql_service_context_base *) ctx)->get_decimal(value);
320   }
321 
sql_get_double(void * ctx,double value,uint32 decimals)322   static int sql_get_double(void * ctx, double value, uint32 decimals)
323   {
324     return ((Sql_service_context_base *) ctx)->get_double(value, decimals);
325   }
326 
sql_get_date(void * ctx,const MYSQL_TIME * value)327   static int sql_get_date(void * ctx, const MYSQL_TIME * value)
328   {
329     return ((Sql_service_context_base *) ctx)->get_date(value);
330   }
331 
sql_get_time(void * ctx,const MYSQL_TIME * value,uint decimals)332   static int sql_get_time(void * ctx, const MYSQL_TIME * value, uint decimals)
333   {
334     return ((Sql_service_context_base *) ctx)->get_time(value, decimals);
335   }
336 
sql_get_datetime(void * ctx,const MYSQL_TIME * value,uint decimals)337   static int sql_get_datetime(void * ctx, const MYSQL_TIME * value,
338                               uint decimals)
339   {
340     return ((Sql_service_context_base *) ctx)->get_datetime(value, decimals);
341   }
342 
sql_get_string(void * ctx,const char * const value,size_t length,const CHARSET_INFO * const valuecs)343   static int sql_get_string(void * ctx, const char * const value,
344                             size_t length, const CHARSET_INFO * const valuecs)
345   {
346     return ((Sql_service_context_base *) ctx)->get_string(value, length, valuecs);
347   }
348 
sql_handle_ok(void * ctx,uint server_status,uint statement_warn_count,ulonglong affected_rows,ulonglong last_insert_id,const char * const message)349   static void sql_handle_ok(void * ctx,
350                             uint server_status, uint statement_warn_count,
351                             ulonglong affected_rows, ulonglong last_insert_id,
352                             const char * const message)
353   {
354     return ((Sql_service_context_base *) ctx)->handle_ok(server_status,
355                                                      statement_warn_count,
356                                                      affected_rows,
357                                                      last_insert_id,
358                                                      message);
359   }
360 
361 
sql_handle_error(void * ctx,uint sql_errno,const char * const err_msg,const char * const sqlstate)362   static void sql_handle_error(void * ctx, uint sql_errno,
363                                const char * const err_msg,
364                                const char * const sqlstate)
365   {
366     return ((Sql_service_context_base *) ctx)->handle_error(sql_errno,
367                                                         err_msg,
368                                                         sqlstate);
369   }
370 
371 
sql_shutdown(void * ctx,int flag)372   static void sql_shutdown(void *ctx, int flag)
373   {
374     return ((Sql_service_context_base *) ctx)->shutdown(flag);
375   }
376 };
377 
378 
379 #endif //SQL_SERVICE_CONTEXT_BASE_INCLUDE
380