1 /* Copyright (c) 2015, 2018, 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 #ifndef PROTOCOL_CALLBACK_INCLUDED
24 #define PROTOCOL_CALLBACK_INCLUDED
25 
26 /**
27 @file
28   Interface of the Protocol_callback class, which is used by the Command
29   service as proxy protocol.
30 */
31 
32 
33 #include "protocol.h"
34 #include "mysql/service_command.h"
35 
36 class Protocol_callback : public Protocol
37 {
38 public:
Protocol_callback(const struct st_command_service_cbs * cbs,enum cs_text_or_binary t_or_b,void * cbs_ctx)39   Protocol_callback(const struct st_command_service_cbs *cbs,
40                     enum cs_text_or_binary t_or_b, void *cbs_ctx) :
41     callbacks_ctx(cbs_ctx),
42     callbacks(*cbs),
43     client_capabilities(0),
44     client_capabilities_set(false),
45     text_or_binary(t_or_b),
46     in_meta_sending(false)
47     {}
48 
49   /**
50     Forces read of packet from the connection
51 
52     @return
53       bytes read
54       -1 failure
55   */
56   virtual int read_packet();
57 
58   /**
59     Reads from the line and parses the data into union COM_DATA
60 
61     @return
62       bytes read
63       -1 failure
64   */
65   virtual int get_command(COM_DATA *com_data, enum_server_command *cmd);
66 
67   /**
68     Returns the type of the protocol
69 
70     @return
71       false  success
72       true   failure
73   */
type()74   virtual enum enum_protocol_type type() { return PROTOCOL_PLUGIN; }
75 
76 
77   /**
78     Returns the type of the connection
79 
80     @return
81       enum enum_vio_type
82   */
83   virtual enum enum_vio_type connection_type();
84 
85   /**
86     Sends null value
87 
88     @return
89       false  success
90       true   failure
91   */
92   virtual bool store_null();
93 
94   /**
95     Sends TINYINT value
96 
97     @param from value
98 
99     @return
100       false  success
101       true   failure
102   */
103   virtual bool store_tiny(longlong from);
104 
105   /**
106     Sends SMALLINT value
107 
108     @param from value
109 
110     @return
111       false  success
112       true   failure
113   */
114   virtual bool store_short(longlong from);
115 
116   /**
117     Sends INT/INTEGER value
118 
119     @param from value
120 
121     @return
122       false  success
123       true   failure
124   */
125   virtual bool store_long(longlong from);
126 
127   /**
128     Sends BIGINT value
129 
130     @param from         value
131     @param is_unsigned  from is unsigned
132 
133     @return
134       false  success
135       true   failure
136   */
137   virtual bool store_longlong(longlong from, bool is_unsigned);
138 
139   /**
140     Sends DECIMAL value
141 
142     @param d    value
143     @param prec field's precision, unused
144     @param dec  field's decimals, unused
145 
146     @return
147       false  success
148       true   failure
149   */
150   virtual bool store_decimal(const my_decimal * d, uint, uint);
151 
152   /**
153     Sends string (CHAR/VARCHAR/TEXT/BLOB) value
154 
155     @param d value
156 
157     @return
158       false  success
159       true   failure
160   */
161   virtual bool store(const char *from, size_t length,
162                      const CHARSET_INFO *fromcs);
163 
164   /**
165     Sends FLOAT value
166 
167     @param from      value
168     @param decimals
169     @param buffer    auxiliary buffer
170 
171     @return
172       false  success
173       true   failure
174   */
175   virtual bool store(float from, uint32 decimals, String *buffer);
176 
177   /**
178     Sends DOUBLE value
179 
180     @param from      value
181     @param decimals
182     @param buffer    auxiliary buffer
183 
184     @return
185       false  success
186       true   failure
187   */
188   virtual bool store(double from, uint32 decimals, String *buffer);
189 
190   /**
191     Sends DATETIME value
192 
193     @param time      value
194     @param precision
195 
196     @return
197       false  success
198       true   failure
199   */
200   virtual bool store(MYSQL_TIME *time, uint precision);
201 
202   /**
203     Sends DATE value
204 
205     @param time      value
206 
207     @return
208       false  success
209       true   failure
210   */
211   virtual bool store_date(MYSQL_TIME *time);
212 
213   /**
214     Sends TIME value
215 
216     @param time      value
217     @param precision
218 
219     @return
220       false  success
221       true   failure
222   */
223   virtual bool store_time(MYSQL_TIME *time, uint precision);
224 
225   /**
226     Sends Field
227 
228     @param field
229 
230     @return
231       false  success
232       true   failure
233   */
234   virtual bool store(Proto_field *field);
235 
236   /**
237     Returns the capabilities supported by the protocol
238   */
239   virtual ulong get_client_capabilities();
240 
241   /**
242     Checks if the protocol supports a capability
243 
244     @param cap the capability
245 
246     @return
247       true   supports
248       false  does not support
249   */
250   virtual bool has_client_capability(unsigned long capability);
251 
252   /**
253     Called BEFORE sending data row or before field_metadata
254   */
255   virtual void start_row();
256 
257   /**
258     Called AFTER sending data row or before field_metadata
259   */
260   virtual bool end_row();
261 
262   /**
263     Called when a row is aborted
264   */
265   virtual void abort_row();
266 
267   /**
268     Called in case of error while sending data
269   */
270   virtual void end_partial_result_set();
271 
272   /**
273     Called when the server shuts down the connection (THD is being destroyed).
274     In this regard, this is also called when the server shuts down. The callback
275     implementor can differentiate between those 2 events by inspecting the
276     shutdown_type parameter.
277 
278     @param server_shutdown  Whether this is a normal connection shutdown (false)
279                             or a server shutdown (true).
280 
281     @return
282     0   success
283     !0  failure
284   */
285   virtual int shutdown(bool server_shutdown= false);
286 
287   /**
288     This function always returns true as in many places in the server this
289     is a prerequisite for continuing operations.
290 
291     @return
292       true   alive
293   */
294   virtual bool connection_alive();
295 
296   /**
297     Should return protocol's reading/writing status. Returns 0 (idle) as it
298     this is the best guess that can be made as there is no callback for
299     get_rw_status().
300   */
301   virtual uint get_rw_status();
302 
303   /**
304     Checks if compression is enabled
305 
306     @return
307       true  enabled
308       false disabled
309   */
310   virtual bool get_compression();
311 
312   /**
313     Called BEFORE sending metadata
314 
315     @param num_cols Number of columns in the result set
316     @param flags
317     @param resultcs The character set of the results. Can be different from the
318                     one in the field metadata.
319 
320     @return
321       true  failure
322      false success
323   */
324   virtual bool start_result_metadata(uint num_cols, uint flags,
325                                      const CHARSET_INFO *resultcs);
326 
327   /**
328     Sends metadata of one field. Called for every column in the result set.
329 
330     @param field  Field's metadata
331     @param cs     Charset
332 
333     @return
334       true  failure
335       false success
336   */
337   virtual bool send_field_metadata(Send_field *field, const CHARSET_INFO *cs);
338 
339   /**
340     Called AFTER sending metadata
341 
342     @return
343       true  failure
344       false success
345   */
346   virtual bool end_result_metadata();
347 
348   /**
349     Sends OK
350 
351     @param server_status Bit field with different statuses. See SERVER_STATUS_*
352     @param warn_count      Warning count from the execution
353     @param affected_row    Rows changed/deleted during the operation
354     @param last_insert_id  ID of the last insert row, which has AUTO_INCROMENT
355                            column
356     @param message         Textual message from the execution. May be NULL.
357 
358     @return
359       true  failure
360       false success
361   */
362   virtual bool send_ok(uint server_status, uint warn_count,
363                        ulonglong affected_rows, ulonglong last_insert_id,
364                        const char *message);
365 
366   /**
367     Sends end of file.
368 
369 
370     This will be called once all data has been sent.
371 
372     @param server_status Bit field with different statuses. See SERVER_STATUS_*
373     @param warn_count    The warning count generated by the execution of the
374                          statement.
375 
376     @return
377       true  failure
378       false success
379   */
380   virtual bool send_eof(uint server_status, uint warn_count);
381 
382   /**
383     Sends error
384 
385     @param sql_errno  Error number, beginning from 1000
386     @param err_msg    The error message
387     @param sql_state  The SQL state - 5 char string
388 
389     @return
390       true  failure
391       false success
392   */
393   virtual bool send_error(uint sql_errno, const char *err_msg,
394                           const char *sql_state);
395 
396 private:
397   void *callbacks_ctx;
398   struct st_command_service_cbs callbacks;
399   unsigned long client_capabilities;
400   bool client_capabilities_set;
401   enum cs_text_or_binary text_or_binary;
402   bool in_meta_sending;
403 };
404 
405 #endif /* PROTOCOL_CALLBACK_INCLUDED */
406