File: protocol_callback.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (535 lines) | stat: -rw-r--r-- 15,561 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License, version 2.0,
 as published by the Free Software Foundation.

 This program is designed to work with certain software (including
 but not limited to OpenSSL) that is licensed under separate terms,
 as designated in a particular file or component or in included license
 documentation.  The authors of MySQL hereby grant you an additional
 permission to link the program and your derivative works with the
 separately licensed software that they have either included with
 the program or referenced in the documentation.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License, version 2.0, for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include "sql/protocol_callback.h"

#include <assert.h>
#include <stddef.h>
#include <algorithm>

#include "m_ctype.h"
#include "mysql_com.h"
#include "sql/current_thd.h"
#include "sql/debug_sync.h"
#include "sql/field.h"
#include "sql/item.h"
#include "sql/item_func.h"
#include "sql/my_decimal.h"
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql_string.h"

class String;
union COM_DATA;

/**
  @file sql/protocol_callback.cc
  Implementation of the Protocol_callback class, which is used by the Command
  service as proxy protocol.
*/

/**
  Practically does nothing.
  Returns -1, error, for the case this will be called. It should happen.
  read_packet() is called by get_command() which in turn is called by
  do_command() in sql_parse. After that COM_DATA is filled with proper info
  that in turn is passed to dispatch_command().
  The Command service doesn't use do_command() but dispatch_command() and
  passes COM_DATA directly from the user(plugin).

  @return
    -1 failure
*/
int Protocol_callback::read_packet() { return -1; }

/**
  Practically does nothing. See the comment of #read_packet().
  Always returns -1.

  @return
    -1
*/
int Protocol_callback::get_command(COM_DATA *, enum_server_command *) {
  return read_packet();
}

/**
  Returns the type of the connection.

  @return
    VIO_TYPE_PLUGIN
*/
enum enum_vio_type Protocol_callback::connection_type() const {
  return VIO_TYPE_PLUGIN;
}

bool Protocol_callback::store_null() {
  if (callbacks.get_null) return callbacks.get_null(callbacks_ctx);

  return false;
}

bool Protocol_callback::store_tiny(longlong from, uint32) {
  if (callbacks.get_integer) return callbacks.get_integer(callbacks_ctx, from);
  return false;
}

bool Protocol_callback::store_short(longlong from, uint32) {
  if (callbacks.get_integer) return callbacks.get_integer(callbacks_ctx, from);
  return false;
}

bool Protocol_callback::store_long(longlong from, uint32) {
  if (callbacks.get_integer) return callbacks.get_integer(callbacks_ctx, from);
  return false;
}

bool Protocol_callback::store_longlong(longlong from, bool is_unsigned,
                                       uint32) {
  if (callbacks.get_longlong)
    return callbacks.get_longlong(callbacks_ctx, from, is_unsigned);
  return false;
}

/**
  Sends DECIMAL value

  @param d    value

  @return
    false  success
    true   failure
*/
bool Protocol_callback::store_decimal(const my_decimal *d, uint, uint) {
  if (callbacks.get_decimal) return callbacks.get_decimal(callbacks_ctx, d);
  return false;
}

bool Protocol_callback::store_string(const char *from, size_t length,
                                     const CHARSET_INFO *fromcs) {
  if (callbacks.get_string)
    return callbacks.get_string(callbacks_ctx, from, length, fromcs);
  return false;
}

bool Protocol_callback::store_float(float from, uint32 decimals, uint32) {
  if (callbacks.get_double)
    return callbacks.get_double(callbacks_ctx, from, decimals);
  return false;
}

bool Protocol_callback::store_double(double from, uint32 decimals, uint32) {
  if (callbacks.get_double)
    return callbacks.get_double(callbacks_ctx, from, decimals);
  return false;
}

bool Protocol_callback::store_datetime(const MYSQL_TIME &time, uint precision) {
  if (callbacks.get_datetime)
    return callbacks.get_datetime(callbacks_ctx, &time, precision);
  return false;
}

bool Protocol_callback::store_date(const MYSQL_TIME &time) {
  if (callbacks.get_date) return callbacks.get_date(callbacks_ctx, &time);
  return false;
}

bool Protocol_callback::store_time(const MYSQL_TIME &time, uint precision) {
  if (callbacks.get_time)
    return callbacks.get_time(callbacks_ctx, &time, precision);
  return false;
}

bool Protocol_callback::store_field(const Field *field) {
  switch (text_or_binary) {
    case CS_TEXT_REPRESENTATION: {
      if (field->is_null()) return store_null();
      StringBuffer<MAX_FIELD_WIDTH> buffer;
      return store(field->val_str(&buffer));
    }
    case CS_BINARY_REPRESENTATION:
      return field->send_to_protocol(this);
  }
  return true;
}

/**
  Returns the capabilities supported by the protocol
*/
ulong Protocol_callback::get_client_capabilities() {
  if (!client_capabilities_set && callbacks.get_client_capabilities) {
    client_capabilities_set = true;
    client_capabilities = callbacks.get_client_capabilities(callbacks_ctx);
  }
  return client_capabilities;
}

bool Protocol_callback::has_client_capability(unsigned long capability) {
  if (!client_capabilities_set) (void)get_client_capabilities();
  return client_capabilities & capability;
}

/**
  Called BEFORE sending data row or before field_metadata
*/
void Protocol_callback::start_row() {
  /*
    start_row() is called for metadata for convenience in Protocol_classic
    This is not wanted for protocol plugins as otherwise we get more calls
    and the plugin has to track state internally if it is in meta or not.
    start_meta(),
      start_row(), field_meta(), end_row(),
      ...
    end_meta()
    Calling start_row() is a left over from the era where metadata was
    sent with only one call send_metadata, and start_row() + end_row()
    were usable hooks in this send_metadata. Now it doesn't make any more
    sense as start_row() is called just before field_meta() and end_row()
    just after. The same code can go in to field_meta().
  */
  if (!in_meta_sending && callbacks.start_row)
    callbacks.start_row(callbacks_ctx);
}

/**
  Called AFTER sending all fields of a row, or after field_metadata().
  Please read the big comment in start_row() for explanation why
  in_meta_sending is used.

  @return
    true   ok
    false  not ok
*/
bool Protocol_callback::end_row() {
  /* See start_row() */
  if (!in_meta_sending && callbacks.end_row)
    return callbacks.end_row(callbacks_ctx);
  return false;
}

/**
  Called when a row is aborted
*/
void Protocol_callback::abort_row() {
  if (callbacks.end_row) callbacks.abort_row(callbacks_ctx);
}

/**
  Called in case of error while sending data
*/
void Protocol_callback::end_partial_result_set() {
  /* Protocol_callback shouldn't be used in this context */
  assert(0);
}

/**
  Called when the server shuts down the connection (THD is being destroyed).
  In this regard, this is also called when the server shuts down. The callback
  implementor can differentiate between those 2 events by inspecting the
  server_shutdown parameter.

  @param server_shutdown  Whether this is a normal connection shutdown (false)
                          or a server shutdown (true).

  @return
  0   success
  !0  failure
*/
int Protocol_callback::shutdown(bool server_shutdown) {
  if (callbacks.shutdown)
    callbacks.shutdown(callbacks_ctx, server_shutdown ? 1 : 0);
  return 0;
}

/**
  Returns if the connection is alive or dead.

  @note If there's no callback provided, this function always returns true
  because, in many places in the server, this is a prerequisite for continuing
  operations. If the user is connection bound, a callback function should be
  provided, to have a possibility to stop execution of operations when the user
  disconnected.

  @return
    true   connected
    false  disconnected
*/
bool Protocol_callback::connection_alive() const {
  if (callbacks.connection_alive) {
    DEBUG_SYNC(current_thd, "wait_before_checking_alive");
    return callbacks.connection_alive(callbacks_ctx);
  }

  return true;
}

/**
  Should return protocol's reading/writing status. Returns 0 (idle) as it this
  is the best guess that can be made as there is no callback for
  get_rw_status().

  @return
    0
*/
uint Protocol_callback::get_rw_status() { return 0; }

/**
  Should check if compression is enabled. Returns always false (no compression)

  @return
    false disabled
*/
bool Protocol_callback::get_compression() { return false; }

/**
  Always returns null.

  @return
     null compression not supported
*/
char *Protocol_callback::get_compression_algorithm() { return nullptr; }

/**
  Always returns 0.

  @return
   compression not supported
*/
uint Protocol_callback::get_compression_level() { return 0; }

/**
  Called BEFORE sending metadata

  @return
    true  failure
    false success
*/
bool Protocol_callback::start_result_metadata(uint num_cols, uint flags,
                                              const CHARSET_INFO *resultcs) {
  in_meta_sending = true;
  if (callbacks.start_result_metadata)
    return callbacks.start_result_metadata(callbacks_ctx, num_cols, flags,
                                           resultcs);
  return false;
}

/**
  Sends metadata of one field. Called for every column in the result set.

  @return
    true  failure
    false success
*/
bool Protocol_callback::send_field_metadata(Send_field *field,
                                            const CHARSET_INFO *cs) {
  if (callbacks.field_metadata) {
    struct st_send_field f;

    f.db_name = field->db_name;
    f.table_name = field->table_name;
    f.org_table_name = field->org_table_name;
    f.col_name = field->col_name;
    f.org_col_name = field->org_col_name;
    f.length = field->length;
    f.charsetnr = field->charsetnr;
    f.flags = field->flags;
    f.decimals = field->decimals;
    f.type = field->type;

    return callbacks.field_metadata(callbacks_ctx, &f, cs);
  }
  return true;
}

/**
  Called AFTER sending metadata

  @return
    true  failure
    false success
*/
bool Protocol_callback::end_result_metadata() {
  in_meta_sending = false;

  if (callbacks.end_result_metadata) {
    THD *t = current_thd;
    uint status = t->server_status;
    uint warn_count = t->get_stmt_da()->current_statement_cond_count();

    return callbacks.end_result_metadata(callbacks_ctx, status, warn_count);
  }
  return false;
}

/**
  Sends OK

  @return
    true  failure
    false success
*/
bool Protocol_callback::send_ok(uint server_status, uint warn_count,
                                ulonglong affected_rows,
                                ulonglong last_insert_id, const char *message) {
  if (callbacks.handle_ok)
    callbacks.handle_ok(callbacks_ctx, server_status, warn_count, affected_rows,
                        last_insert_id, message);
  return false;
}

bool Protocol_callback::flush() { return false; }

/**
  Sends end of file

  @return
    true  failure
    false success
*/
bool Protocol_callback::send_eof(uint server_status, uint warn_count) {
  if (callbacks.handle_ok)
    callbacks.handle_ok(callbacks_ctx, server_status, warn_count, 0, 0,
                        nullptr);
  return false;
}

/**
  Sends error

  @return
    true  failure
    false success
*/
bool Protocol_callback::send_error(uint sql_errno, const char *err_msg,
                                   const char *sql_state) {
  if (callbacks.handle_error)
    callbacks.handle_error(callbacks_ctx, sql_errno, err_msg, sql_state);
  return false;
}

bool Protocol_callback::store_ps_status(ulong stmt_id, uint column_count,
                                        uint param_count, ulong cond_count) {
  THD *thd = current_thd;
  mem_root_deque<Item *> field_list(thd->mem_root);
  field_list.push_back(new Item_empty_string("stmt_id", MY_CS_NAME_SIZE));
  field_list.push_back(new Item_empty_string("column_no", MY_CS_NAME_SIZE));
  field_list.push_back(new Item_empty_string("param_no", MY_CS_NAME_SIZE));
  field_list.push_back(new Item_empty_string("warning_no", MY_CS_NAME_SIZE));

  if (thd->send_result_metadata(field_list,
                                Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
    return true;

  start_row();
  if (store_long(stmt_id) || store_short(column_count) ||
      store_short(param_count) || store_short(std::min(cond_count, 65535UL)) ||
      end_row()) {
    return true; /* purecov: inspected */
  }
  return false;
}

bool Protocol_callback::send_parameters(List<Item_param> *parameters,
                                        bool is_sql_prep) {
  if (is_sql_prep) return set_variables_from_parameters(parameters);

  if (!has_client_capability(CLIENT_PS_MULTI_RESULTS))
    // The client does not support OUT-parameters.
    return false;

  List_iterator_fast<Item_param> item_param_it(*parameters);

  mem_root_deque<Item *> out_param_lst(current_thd->mem_root);
  Item_param *item_param;
  while ((item_param = item_param_it++)) {
    // Skip it as it's just an IN-parameter.
    if (!item_param->get_out_param_info()) continue;

    out_param_lst.push_back(item_param);
  }

  // Empty list
  if (out_param_lst.empty()) return false;

  THD *thd = current_thd;
  /*
    We have to set SERVER_PS_OUT_PARAMS in THD::server_status, because it
    is used in send_result_metadata().
  */
  thd->server_status |= SERVER_PS_OUT_PARAMS | SERVER_MORE_RESULTS_EXISTS;

  // Send meta-data.
  if (thd->send_result_metadata(out_param_lst,
                                Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
    return true; /* purecov: inspected */

  // Send data.
  start_row();
  if (thd->send_result_set_row(out_param_lst) || end_row())
    return true; /* purecov: inspected */

  // Restore THD::server_status.
  thd->server_status &= ~SERVER_PS_OUT_PARAMS;
  thd->server_status &= ~SERVER_MORE_RESULTS_EXISTS;

  return send_ok(
      (thd->server_status | SERVER_PS_OUT_PARAMS | SERVER_MORE_RESULTS_EXISTS),
      thd->get_stmt_da()->current_statement_cond_count(), 0, 0, nullptr);
}

bool Protocol_callback::set_variables_from_parameters(
    List<Item_param> *parameters) {
  THD *thd = current_thd;
  List_iterator_fast<Item_param> item_param_it(*parameters);
  List_iterator_fast<LEX_STRING> user_var_name_it(
      thd->lex->prepared_stmt_params);

  Item_param *item_param;
  LEX_STRING *user_var_name;
  while ((item_param = item_param_it++) &&
         (user_var_name = user_var_name_it++)) {
    // Skip it as it's just an IN-parameter.
    if (!item_param->get_out_param_info()) continue;

    /*
      Delete call not needed, the object appends itself
      to THDs free_list.
    */
    Item_func_set_user_var *suv =
        new Item_func_set_user_var(*user_var_name, item_param);
    /*
      Item_func_set_user_var is not fixed after construction,
      call fix_fields().
    */
    if (suv->fix_fields(thd, nullptr)) return true;

    if (suv->check(false)) return true;

    if (suv->update()) return true;
  }

  return false;
}