File: parser_service.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 (362 lines) | stat: -rw-r--r-- 11,070 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
/*  Copyright (c) 2015, 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 <stddef.h>
#include <sys/types.h>
#include <algorithm>
#include <new>

#include "lex_string.h"
#include "m_string.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_sqlcommand.h"
#include "my_thread.h"
#include "mysql/mysql_lex_string.h"
#include "mysql/psi/mysql_thread.h"
#include "mysql/service_parser.h"
#include "sql/auth/sql_security_ctx.h"
#include "sql/current_thd.h"
#include "sql/enum_query_type.h"
#include "sql/error_handler.h"
#include "sql/item.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"  // my_localhost
#include "sql/mysqld_thd_manager.h"
#include "sql/protocol_classic.h"
#include "sql/select_lex_visitor.h"
#include "sql/sql_base.h"  // close_thread_tables
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_digest.h"
#include "sql/sql_digest_stream.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h"
#include "sql/system_variables.h"
#include "sql/transaction.h"  // trans_commit_stmt
#include "sql_string.h"
#include "template_utils.h"

/**
  This class implements the parse tree visiting service.
  @see mysql_parser_visit_tree
*/
class Service_visitor : public Select_lex_visitor {
  parse_node_visit_function m_processor;
  uchar *m_arg;

 public:
  /**
    @param processor This function will be called for each literal (Item) in
    the parse tree, along with the Item's type and arg. If the function
    returns non-zero, parse tree traversal will terminate.

    @param arg Will be passed to processor on every call.
  */
  Service_visitor(parse_node_visit_function processor, uchar *arg)
      : m_processor(processor), m_arg(arg) {}

 protected:
  bool visit_item(Item *item) override {
    switch (item->type()) {
        // These are all the literals.
      case Item::PARAM_ITEM:
      case Item::STRING_ITEM:
      case Item::INT_ITEM:
      case Item::DECIMAL_ITEM:
      case Item::REAL_ITEM:
      case Item::NULL_ITEM:
      case Item::VARBIN_ITEM:
      case Item::CACHE_ITEM:
        return m_processor(item, m_arg);
      default:
        break;
    }
    return false;
  }
};

/**
  This class implements the framework needed for the callback function that
  handles conditions that may arise during parsing via the parser service.

  @see mysql_parser_visit_tree
*/
class Plugin_error_handler : public Internal_error_handler {
  THD *m_thd;
  const char *m_message;
  const sql_condition_handler_function m_handle_error;
  void *m_state;

 public:
  Plugin_error_handler(THD *thd, sql_condition_handler_function handle_error,
                       void *state)
      : m_thd(thd),
        m_message(nullptr),
        m_handle_error(handle_error),
        m_state(state) {
    if (handle_error != nullptr) thd->push_internal_handler(this);
  }

  bool handle_condition(THD *, uint sql_errno_u, const char *sqlstate,
                        Sql_condition::enum_severity_level *,
                        const char *msg) override {
    int sql_errno = static_cast<int>(sql_errno_u);
    if (m_handle_error != nullptr)
      return m_handle_error(sql_errno, sqlstate, msg, m_state) != 0;
    return false;
  }

  const char *get_message() { return m_message; }

  ~Plugin_error_handler() override {
    if (m_handle_error != nullptr) m_thd->pop_internal_handler();
  }
};

MYSQL_THD mysql_parser_current_session() { return current_thd; }

MYSQL_THD mysql_parser_open_session() {
  THD *old_thd = current_thd;

  // See create_thd()
  THD *thd = new (std::nothrow) THD;
  if (thd == nullptr) return nullptr;

  thd->security_context()->set_host_ptr(STRING_WITH_LEN(my_localhost));
  thd->lex = new LEX;
  thd->lex->set_current_query_block(nullptr);

  thd->variables.character_set_client = old_thd->variables.character_set_client;

  return thd;
}

namespace {

struct thread_args {
  thread_args(MYSQL_THD thd, callback_function fun, void *arg)
      : m_thd(thd), m_fun(fun), m_arg(arg) {}
  MYSQL_THD m_thd;
  callback_function m_fun;
  void *m_arg;
};

void *parser_service_start_routine(void *arg) {
  thread_args *tt = pointer_cast<thread_args *>(arg);
  THD *thd = tt->m_thd;
  my_thread_init();

  {
    DBUG_TRACE;

    Global_THD_manager *thd_manager = Global_THD_manager::get_instance();
    thd->thread_stack = reinterpret_cast<char *>(&thd);
    thd->set_new_thread_id();
    mysql_thread_set_psi_id(thd->thread_id());
    thd->store_globals();
    thd->set_time();

    thd_manager->add_thd(thd);
    (tt->m_fun)(tt->m_arg);

    trans_commit_stmt(thd);
    trans_commit(thd);
    close_thread_tables(thd);
    thd->mdl_context.release_transactional_locks();
    close_mysql_tables(thd);

    thd->release_resources();
    thd->restore_globals();
    thd_manager->remove_thd(thd);

    LEX *lex = thd->lex;
    delete thd;
    delete lex;
    delete tt;
  }
  my_thread_end();
  my_thread_exit(nullptr);
  return nullptr;
}

}  // namespace

void mysql_parser_start_thread(THD *thd, callback_function fun, void *arg,
                               my_thread_handle *thread_handle) {
  my_thread_handle handle;
  my_thread_attr_t attr;
  my_thread_attr_init(&attr);

  thread_args *args = new thread_args(thd, fun, arg);
  mysql_thread_create(key_thread_parser_service, &handle, &attr,
                      parser_service_start_routine, args);
  *thread_handle = handle;
}

void mysql_parser_join_thread(my_thread_handle *thread_id) {
  my_thread_join(thread_id, nullptr);
}

void mysql_parser_set_current_database(MYSQL_THD thd,
                                       const MYSQL_LEX_STRING db) {
  if (db.length == 0) {
    LEX_CSTRING db_const = {nullptr, 0};
    thd->set_db(db_const);
  } else {
    LEX_CSTRING db_const = {db.str, db.length};
    thd->set_db(db_const);
  }
}

int mysql_parser_parse(MYSQL_THD thd, const MYSQL_LEX_STRING query,
                       unsigned char is_prepared,
                       sql_condition_handler_function handle_condition,
                       void *condition_handler_state) {
  /*
    Clean up parse- and item trees in case this function was called before for
    the same THD.
  */
  if (thd->lex->is_lex_started) {
    thd->end_statement();
    thd->cleanup_after_query();
  }

  lex_start(thd);

  if (alloc_query(thd, query.str, query.length))
    return 1;  // Fatal error flag set

  Parser_state parser_state;
  if (parser_state.init(thd, query.str, query.length)) return 1;

  parser_state.m_input.m_has_digest = true;
  parser_state.m_input.m_compute_digest = true;
  thd->m_digest = &thd->m_digest_state;
  thd->m_digest->reset(thd->m_token_array, max_digest_length);

  if (is_prepared) {
    parser_state.m_lip.stmt_prepare_mode = true;
    parser_state.m_lip.multi_statements = false;
    thd->lex->context_analysis_only |= CONTEXT_ANALYSIS_ONLY_PREPARE;
  }

  Plugin_error_handler error_handler(thd, handle_condition,
                                     condition_handler_state);

  int parse_status = parse_sql(thd, &parser_state, nullptr);

  /*
    Handled conditions are thrown away at this point - they are supposedly
    handled by handle_condition. If there was no handler supplied, the
    diagnostics area is not touched. It will contain any errors thrown by the
    parser.
  */
  if (handle_condition != nullptr) {
    thd->get_stmt_da()->reset_diagnostics_area();
    thd->get_stmt_da()->reset_condition_info(thd);
  }

  return parse_status;
}

int mysql_parser_get_statement_type(MYSQL_THD thd) {
  LEX *lex = thd->lex;
  switch (lex->sql_command) {
    case SQLCOM_SELECT:
      return STATEMENT_TYPE_SELECT;
    case SQLCOM_UPDATE:
      [[fallthrough]];
    case SQLCOM_UPDATE_MULTI:
      return STATEMENT_TYPE_UPDATE;
    case SQLCOM_INSERT:
      [[fallthrough]];
    case SQLCOM_INSERT_SELECT:
      return STATEMENT_TYPE_INSERT;
    case SQLCOM_REPLACE:
      [[fallthrough]];
    case SQLCOM_REPLACE_SELECT:
      return STATEMENT_TYPE_REPLACE;
    case SQLCOM_DELETE:
      [[fallthrough]];
    case SQLCOM_DELETE_MULTI:
      return STATEMENT_TYPE_DELETE;
    default:
      return STATEMENT_TYPE_OTHER;
  }
}

int mysql_parser_get_statement_digest(MYSQL_THD thd, uchar *digest) {
  static_assert(PARSER_SERVICE_DIGEST_LENGTH == DIGEST_HASH_SIZE,
                "If you change the digest hash, PARSER_SERVICE_DIGEST_LENGTH "
                "needs to adjust");

  if (thd->m_digest == nullptr) return true;
  compute_digest_hash(&thd->m_digest->m_digest_storage, digest);
  return false;
}

int mysql_parser_get_number_params(MYSQL_THD thd) {
  return thd->lex->param_list.elements;
}

int mysql_parser_extract_prepared_params(MYSQL_THD thd, int *positions) {
  LEX *lex = thd->lex;
  List_iterator_fast<Item_param> it(lex->param_list);
  for (uint i = 0; i < lex->param_list.elements; i++)
    positions[i] = it++->pos_in_query;
  return lex->param_list.elements;
}

int mysql_parser_visit_tree(MYSQL_THD thd, parse_node_visit_function processor,
                            unsigned char *arg) {
  Service_visitor visitor(processor, arg);
  return thd->lex->accept(&visitor);
}

MYSQL_LEX_STRING mysql_parser_item_string(MYSQL_ITEM item) {
  String str;
  static_cast<Item *>(item)->print(mysql_parser_current_session(), &str,
                                   QT_ORDINARY);
  MYSQL_LEX_STRING res = {new char[str.length()], 0};
  if (res.str != nullptr) {
    res.length = str.length();
    std::copy(str.ptr(), str.ptr() + str.length(), res.str);
  }
  return res;
}

void mysql_parser_free_string(MYSQL_LEX_STRING string) { delete[] string.str; }

MYSQL_LEX_STRING mysql_parser_get_query(MYSQL_THD thd) {
  MYSQL_LEX_STRING str = {const_cast<char *>(thd->query().str),
                          thd->query().length};
  return str;
}

MYSQL_LEX_STRING mysql_parser_get_normalized_query(MYSQL_THD thd) {
  String normalized_query = thd->normalized_query();
  return normalized_query.lex_string();
}