File: mysql_command_consumer_imp.cc

package info (click to toggle)
mysql-8.0 8.0.45-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,048 kB
  • sloc: cpp: 4,685,434; ansic: 412,712; pascal: 108,396; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (482 lines) | stat: -rw-r--r-- 17,050 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
/* Copyright (c) 2022, 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/server_component/mysql_command_consumer_imp.h"
#include <mysql/components/minimal_chassis.h>
#include "include/my_byteorder.h"
#include "include/my_sys.h"
#include "include/my_time.h"
#include "include/mysql/service_command.h"
#include "include/mysqld_error.h"
#include "m_ctype.h"
#include "sql/my_decimal.h"
#include "sql/server_component/mysql_command_services_imp.h"
#include "sql_string.h"  // String

PSI_memory_key key_memory_cc_MYSQL_DATA;
PSI_memory_key key_memory_cc_MYSQL;

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::start,
                   (SRV_CTX_H * srv_ctx_h, MYSQL_H *mysql_h)) {
  try {
    Dom_ctx *ctx = (Dom_ctx *)my_malloc(key_memory_cc_MYSQL, sizeof(Dom_ctx),
                                        MYF(MY_WME | MY_ZEROFILL));
    if (ctx == nullptr || mysql_h == nullptr) return true;
    Mysql_handle *mysql_handle = reinterpret_cast<Mysql_handle *>(mysql_h);
    ctx->m_mysql = mysql_handle->mysql;
    *srv_ctx_h = reinterpret_cast<SRV_CTX_H>(ctx);
    auto mcs_extn = MYSQL_COMMAND_SERVICE_EXTN(ctx->m_mysql);
    mcs_extn->consumer_srv_data = reinterpret_cast<SRV_CTX_H *>(ctx);
    ctx->m_result = &mcs_extn->data;
    if (!(*ctx->m_result = (MYSQL_DATA *)my_malloc(
              key_memory_cc_MYSQL_DATA, sizeof(MYSQL_DATA),
              MYF(MY_WME | MY_ZEROFILL))) ||
        !((*ctx->m_result)->alloc = (MEM_ROOT *)my_malloc(
              key_memory_cc_MYSQL_DATA, 8192, /* Assume rowlength < 8192 */
              MYF(MY_WME | MY_ZEROFILL)))) {
      my_error(ER_DA_OOM, MYF(0));
      free_rows(*ctx->m_result);
      my_free(ctx);
      return true;
    }
    ctx->m_message = new std::string();
    ctx->m_err_msg = new std::string();
    ctx->m_sqlstate = new std::string();
    ctx->m_data = *ctx->m_result;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::start_result_metadata,
                   (SRV_CTX_H srv_ctx_h, unsigned int num_cols, unsigned int,
                    const char *const)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    if (ctx->m_mysql->field_alloc == nullptr) {
      ctx->m_mysql->field_alloc = (MEM_ROOT *)my_malloc(
          key_memory_cc_MYSQL, 8192, MYF(MY_WME | MY_ZEROFILL));
      /* Assume rowlength < 8192 */
      if (ctx->m_mysql->field_alloc == nullptr) {
        ctx->m_sql_errno = my_errno();
        *ctx->m_err_msg = "Out of memory";
        return true;
      }
    }

    ctx->m_field_column_count = (protocol_41(ctx->m_mysql) ? 7 : 5);
    size_t size = sizeof(MYSQL_FIELD) * num_cols;

    ctx->m_fields = ctx->m_mysql->fields =
        (MYSQL_FIELD *)ctx->m_mysql->field_alloc->Alloc(size);
    if (!ctx->m_mysql->fields) {
      my_error(ER_DA_OOM, MYF(0));
      my_free(ctx->m_mysql->field_alloc);
      return true;
    }
    memset(ctx->m_fields, 0, sizeof(MYSQL_FIELD) * num_cols);
    ctx->m_data->fields = ctx->m_mysql->field_count = num_cols;

    // Prepare for rows
    ctx->m_prev_ptr = &ctx->m_data->data;
    ctx->m_data->rows = 0;
    ctx->m_mysql->status = MYSQL_STATUS_GET_RESULT;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::field_metadata,
                   (SRV_CTX_H srv_ctx_h, struct Field_metadata *field,
                    const char *const)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    /* The field metadata strings are part of the query context and will be
       freed after query execution. Copy the metadata strings into the result
       set context so they can be accessed upon return to the caller.
    */
    MEM_ROOT *mem_root = (*ctx->m_result)->alloc;
    ctx->m_fields->db =
        strmake_root(mem_root, field->db_name, strlen(field->db_name));
    ctx->m_fields->table =
        strmake_root(mem_root, field->table_name, strlen(field->table_name));
    ctx->m_fields->org_table = strmake_root(mem_root, field->org_table_name,
                                            strlen(field->org_table_name));
    ctx->m_fields->name =
        strmake_root(mem_root, field->col_name, strlen(field->col_name));
    ctx->m_fields->org_name = strmake_root(mem_root, field->org_col_name,
                                           strlen(field->org_col_name));
    ctx->m_fields->length = field->length;
    ctx->m_fields->charsetnr = field->charsetnr;
    ctx->m_fields->flags = field->flags;
    ctx->m_fields->decimals = field->decimals;
    ctx->m_fields->type = static_cast<enum_field_types>(field->type);
    ctx->m_fields++;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::end_result_metadata,
                   (SRV_CTX_H, unsigned int, unsigned int)) {
  return false;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::start_row,
                   (SRV_CTX_H srv_ctx_h)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    ctx->m_cur_row =
        (MYSQL_ROWS *)ctx->m_data->alloc->Alloc(sizeof(MYSQL_ROWS));
    if (ctx->m_cur_row == nullptr) {
      my_error(ER_DA_OOM, MYF(0));
      free_rows(ctx->m_data);
      return true;
    }
    *ctx->m_prev_ptr = ctx->m_cur_row;
    ctx->m_cur_row->data = (MYSQL_ROW)ctx->m_data->alloc->Alloc(
        ctx->m_data->fields * sizeof(char *));
    if (ctx->m_cur_row->data == nullptr) {
      free_rows(ctx->m_data);
      my_error(ER_DA_OOM, MYF(0));
      return true;
    }
    ctx->m_cur_row->length = 0;
    ctx->m_cur_row->next = nullptr;
    ctx->m_cur_field_num = 0;
    ctx->m_data->rows++;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::abort_row,
                   (SRV_CTX_H srv_ctx_h)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    auto count = ctx->m_data->rows;
    MYSQL_ROWS **last_row_hook = &ctx->m_data->data;
    while (--count) last_row_hook = &(*last_row_hook)->next;
    *last_row_hook = nullptr;
    ctx->m_prev_ptr = last_row_hook;
    ctx->m_data->rows--;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::end_row,
                   (SRV_CTX_H srv_ctx_h)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    ctx->m_prev_ptr = &ctx->m_cur_row->next;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_METHOD(void, mysql_command_consumer_dom_imp::handle_ok,
              (SRV_CTX_H srv_ctx_h, unsigned int server_status,
               unsigned int statement_warn_count,
               unsigned long long affected_rows,
               unsigned long long last_insert_id, const char *const message)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return;
    ctx->m_mysql->affected_rows = affected_rows;
    ctx->m_mysql->warning_count = statement_warn_count;
    ctx->m_mysql->server_status = server_status;
    ctx->m_mysql->free_me = true;
    ctx->m_mysql->insert_id = last_insert_id;
    *ctx->m_message = message ? message : "";
    return;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return;
  }
}

DEFINE_METHOD(void, mysql_command_consumer_dom_imp::handle_error,
              (SRV_CTX_H srv_ctx_h, unsigned int sql_errno,
               const char *const err_msg, const char *const sqlstate)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return;
    *ctx->m_err_msg = err_msg ? err_msg : "";
    ctx->m_sql_errno = sql_errno;
    *ctx->m_sqlstate = sqlstate ? sqlstate : "";
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return;
  }
  return;
}

/* get_null */
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get, (SRV_CTX_H srv_ctx_h)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    char buff[5] = {"NULL"}; /* Store 'NULL' as null value into the data */
    bool ret = store_data(srv_ctx_h, buff, 4);
    ++ctx->m_cur_field_num;
    return ret;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}

/* get_integer */
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get,
                   (SRV_CTX_H srv_ctx_h, long long value)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    char buff[MY_INT64_NUM_DECIMAL_DIGITS + 1];
    const char *end = longlong10_to_str(value, buff, -10);
    const size_t int_length = end - buff;
    bool ret = store_data(srv_ctx_h, buff, int_length);
    ++ctx->m_cur_field_num;
    return ret;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}

/* get_longlong */
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get,
                   (SRV_CTX_H srv_ctx_h, long long value,
                    unsigned int unsigned_flag)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    char buff[MY_INT64_NUM_DECIMAL_DIGITS + 1];
    const char *end = longlong10_to_str(value, buff, unsigned_flag ? 10 : -10);
    const size_t length = end - buff;
    bool ret = store_data(srv_ctx_h, buff, length);
    ++ctx->m_cur_field_num;
    return ret;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}

/* get_decimal */
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get,
                   (SRV_CTX_H srv_ctx_h, const DECIMAL_T_H decimal)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    auto value = (const decimal_t *)(decimal);
    if (ctx == nullptr) return true;
    char buff[DECIMAL_MAX_STR_LENGTH + 1];
    int string_length = DECIMAL_MAX_STR_LENGTH + 1;
    String str(buff, sizeof(buff), &my_charset_bin);
    decimal2string(value, buff, &string_length);
    bool ret_val = store_data(srv_ctx_h, str.ptr(), str.length());
    ++ctx->m_cur_field_num;
    return ret_val;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}

/* get_double */
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get,
                   (SRV_CTX_H srv_ctx_h, double value, unsigned int decimals)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    char buffer[FLOATING_POINT_BUFFER + 1];
    size_t length;
    if (decimals < DECIMAL_NOT_SPECIFIED)
      length = my_fcvt(value, decimals, buffer, nullptr);
    else
      length = my_gcvt(value, MY_GCVT_ARG_FLOAT, FLOATING_POINT_BUFFER, buffer,
                       nullptr);
    bool ret_val = store_data(srv_ctx_h, buffer, length);
    ++ctx->m_cur_field_num;
    return ret_val;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get_date,
                   (SRV_CTX_H srv_ctx_h, const MYSQL_TIME_H time)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    auto value = (const MYSQL_TIME *)(time);
    char buff[MAX_DATE_STRING_REP_LENGTH];
    size_t length = my_date_to_str(*value, buff);
    bool ret_val = store_data(srv_ctx_h, buff, length);
    ++ctx->m_cur_field_num;
    return ret_val;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get_time,
                   (SRV_CTX_H srv_ctx_h, const MYSQL_TIME_H time,
                    unsigned int precision)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    auto value = (const MYSQL_TIME *)(time);
    char buff[MAX_DATE_STRING_REP_LENGTH];
    size_t length = my_time_to_str(*value, buff, precision);
    bool ret_val = store_data(srv_ctx_h, buff, length);
    ++ctx->m_cur_field_num;
    return ret_val;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get_datetime,
                   (SRV_CTX_H srv_ctx_h, const MYSQL_TIME_H time,
                    unsigned int precision)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    auto value = (const MYSQL_TIME *)(time);
    char buff[MAX_DATE_STRING_REP_LENGTH];
    size_t length = my_datetime_to_str(*value, buff, precision);
    bool ret_val = store_data(srv_ctx_h, buff, length);
    ++ctx->m_cur_field_num;
    return ret_val;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::get_string,
                   (SRV_CTX_H srv_ctx_h, const char *const value, size_t length,
                    const char *const)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return true;
    bool ret = store_data(srv_ctx_h, value, length);
    ++(ctx->m_cur_field_num);
    return ret;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
}

DEFINE_METHOD(void, mysql_command_consumer_dom_imp::client_capabilities,
              (SRV_CTX_H srv_ctx_h, unsigned long *capabilities)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return;
    *capabilities = ctx->m_mysql->server_capabilities;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return;
  }
  return;
}
DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::error,
                   (SRV_CTX_H srv_ctx_h, unsigned int *err_num,
                    const char **error_msg)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr || err_num == nullptr || error_msg == nullptr)
      return true;
    *err_num = ctx->m_sql_errno;
    *error_msg = ctx->m_err_msg->data();
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}

DEFINE_METHOD(void, mysql_command_consumer_dom_imp::end,
              (SRV_CTX_H srv_ctx_h)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    if (ctx == nullptr) return;
    /* The m_result is freed by
       free_result->mysql_free_result()->free_rows() api.
       In non result cases, it has to be freed here. */
    if (*ctx->m_result) {
      (*ctx->m_result)->alloc->Clear();
      my_free((*ctx->m_result)->alloc);
      my_free(*ctx->m_result);
    }
    delete ctx->m_message;
    delete ctx->m_err_msg;
    delete ctx->m_sqlstate;
    my_free(ctx);
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return;
  }
  return;
}

DEFINE_BOOL_METHOD(mysql_command_consumer_dom_imp::store_data,
                   (SRV_CTX_H srv_ctx_h, const char *data, size_t length)) {
  try {
    Dom_ctx *ctx = reinterpret_cast<Dom_ctx *>(srv_ctx_h);
    assert(ctx);
    char *&field_buf = ctx->m_cur_row->data[ctx->m_cur_field_num];
    field_buf = (char *)ctx->m_data->alloc->Alloc(length + 1);
    if (field_buf == nullptr) {
      my_error(ER_DA_OOM, MYF(0));
      return true;
    }
    memcpy(field_buf, (const uchar *)data, length);
    field_buf[length] = '\0';
    ctx->m_cur_row->length += length;
  } catch (...) {
    mysql_components_handle_std_exception(__func__);
    return true;
  }
  return false;
}