File: rule.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 (184 lines) | stat: -rw-r--r-- 5,925 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
/*  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 "plugin/rewriter/rule.h"

#include "my_config.h"

#include <assert.h>
#include <stddef.h>
#include <string>
#include <vector>

#include "mysqld_error.h"
#include "plugin/rewriter/query_builder.h"
#include "plugin/rewriter/services.h"

using std::string;
using std::vector;

/**
  @file plugin/rewriter/rule.cc
  Implementation of rewrite rule execution.
  Details on parameter extraction:
  It is important to understand that in the case of a rewrite the tree of the
  original query and of the pattern have been found to be similar (by
  comparing the normalized strings,) except that instead of parameter markers
  there may be actual literals. Therefore, the parameters to extract are at the
  exact same positions as the parameter markers in the pattern. Example with
  the where clause of a query:

@verbatim
          Pattern                 Original Query

            AND                       AND
          /     \                   /     \
        EQ      NEQ               EQ      NEQ
      /   \   /     \           /   \    /    \
     A     ? C       ?         A     3  C      5

@endverbatim

  When loading a rule, we will traverse the tree and keep each literal we
  encounter. We later reuse these literals to do the third phase of matching:
  either a literal in the query matches a parameter marker in the pattern, or
  an identical literal.
*/

/// A Condition_handler that silences and records parse errors.
class Parse_error_recorder : public services::Condition_handler {
 public:
  /**
    Handle a condition.
    @param sql_errno The sql error number.
    @param message The sql error text.

    @retval true If the error number is a parser error, we claim we handle the
    error.

    @retval false We don't handle the error.
  */
  bool handle(int sql_errno, const char *, const char *message) override {
    assert(message != nullptr);
    if (m_message.empty()) m_message.assign(message);
    switch (sql_errno) {
      case ER_PARSE_ERROR:
      case ER_EMPTY_QUERY:
      case ER_WARN_LEGACY_SYNTAX_CONVERTED:
      case ER_NO_DB_ERROR:
        return true;
      default:
        return false;
    };
  }

  string first_parse_error_message() { return m_message; }

 private:
  string m_message;
};

/// Class that collects literals from a parse tree in an std::vector.
class Literal_collector : public services::Literal_visitor {
  vector<string> m_literals;

 public:
  bool visit(MYSQL_ITEM item) override {
    m_literals.push_back(services::print_item(item));
    return false;
  }

  vector<string> get_literals() { return m_literals; }
};

Pattern::Load_status Pattern::load(MYSQL_THD thd,
                                   const Persisted_rule *diskrule) {
  Parse_error_recorder recorder;

  if (diskrule->pattern_db.has_value())
    services::set_current_database(thd, diskrule->pattern_db.value());
  else
    services::set_current_database(thd, "");

  if (services::parse(thd, diskrule->pattern.value(), true, &recorder)) {
    m_parse_error_message = recorder.first_parse_error_message();
    return PARSE_ERROR;
  }

  if (!services::is_supported_statement(thd)) return NOT_SUPPORTED_STATEMENT;

  // We copy the normalized_pattern to the plugin's memory.
  normalized_pattern = services::get_current_query_normalized(thd);
  number_parameters = services::get_number_params(thd);

  Literal_collector collector;
  services::visit_parse_tree(thd, &collector);
  literals = collector.get_literals();

  if (digest.load(thd)) return NO_DIGEST;

  return OK;
}

/**
  Load the replacement query string.
  It means:
    - extract the number of parameters
    - extract the position of the parameters in the query string
    - copy the replacement in the rewrite rule
*/
bool Replacement::load(MYSQL_THD thd, const string replacement) {
  Parse_error_recorder recorder;
  if (services::parse(thd, replacement, true, &recorder)) {
    m_parse_error_message = recorder.first_parse_error_message();
    return true;
  }

  number_parameters = services::get_number_params(thd);
  if (number_parameters > 0)
    m_param_slots = services::get_parameter_positions(thd);

  query_string = replacement;

  return false;
}

Rewrite_result Rule::create_new_query(MYSQL_THD thd) {
  Query_builder builder(&m_pattern, &m_replacement);

  services::visit_parse_tree(thd, &builder);

  Rewrite_result result;
  if (builder.matches()) {
    result.new_query = builder.get_built_query();
    result.was_rewritten = true;
  } else
    result.was_rewritten = false;

  return result;
}

bool Rule::matches(MYSQL_THD thd) const {
  string normalized_query = services::get_current_query_normalized(thd);
  return normalized_query.compare(m_pattern.normalized_pattern) == 0;
}