MySQL  8.0.21
Source Code Documentation
rule.h
Go to the documentation of this file.
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 #ifndef RULE_INCLUDED
23 #define RULE_INCLUDED
24 
25 #include "my_config.h"
26 
27 #include <string>
28 #include <vector>
29 
30 #include "my_dbug.h"
31 #include "my_inttypes.h"
34 
35 /// The results of an attempt to rewrite a query parse tree.
37  /**
38  Means the query was successfully rewritten, and the new query is available
39  in new_query.
40  */
42 
43  /// Means that at lest one matching digest was found in the hash table.
45 
46  /// If was_rewritten is true, a new query, otherwise an empty string.
47  std::string new_query;
48 
49  Rewrite_result() : was_rewritten(false), digest_matched(false) {}
50 };
51 
52 /**
53  The in-memory representation of a pattern.
54 */
55 class Pattern {
56  public:
57  enum Load_status { OK, PARSE_ERROR, NOT_SUPPORTED_STATEMENT, NO_DIGEST };
58 
60 
61  /// The pattern in normalized form.
62  std::string normalized_pattern;
63 
64  /// The digest obtained from the pattern
66 
67  std::vector<std::string> literals;
68 
69  /**
70  Loads the pattern. The pattern string is deep copied. This is not done in
71  the CTOR becuase of the memory allocation.
72 
73  This function does the following:
74  - Parse the pattern string.
75  - Print a normalized version.
76  - Extract the position of the parameter markers.
77  - Compute the digest
78 
79  @retval false Success.
80 
81  @retval true Either parse error, the pattern is not a select statement or
82  out of memory.
83  */
84  Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule);
85 
86  /**
87  If any errors were raised during parsing, the first one is available here.
88  */
89  std::string parse_error_message() { return m_parse_error_message; }
90 
91  private:
92  std::string m_parse_error_message;
93 };
94 
95 class Replacement {
96  public:
97  /// The query string of the replacement
98  std::string query_string;
99 
100  /**
101  The number of parameters (and the size of m_param_slots and
102  parameter_positions.)
103  */
105 
106  bool load(MYSQL_THD thd, const std::string replacement);
107 
108  /**
109  If any errors were raised during parsing, the first one is available here.
110  */
111  std::string parse_error_message() { return m_parse_error_message; }
112 
113  std::vector<int> slots() const { return m_param_slots; }
114 
115  private:
116  /// The positions in query_string of each parameter ('?')
117  std::vector<int> m_param_slots;
118 
120 };
121 
122 /**
123  Internal representation of a rewrite rule.
124  A rewrite rule consists of a pattern and a replacement.
125 */
126 class Rule {
127  public:
128  enum Load_status {
129  OK,
135  };
136 
137  /// The digest buffer.
138  const uchar *digest_buffer() const { return m_pattern.digest.c_ptr(); }
139 
140  /// The pattern in normalized form.
141  std::string normalized_pattern() { return m_pattern.normalized_pattern; }
142 
143  /// Loads and parses the rule and replacement.
144  Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule) {
145  switch (m_pattern.load(thd, diskrule)) {
146  case Pattern::OK:
147  break;
149  return PATTERN_PARSE_ERROR;
152  case Pattern::NO_DIGEST:
153  return PATTERN_GOT_NO_DIGEST;
154  }
155 
156  if (m_replacement.load(thd, diskrule->replacement.value()))
158 
159  if (m_replacement.number_parameters > m_pattern.number_parameters)
161 
162  return OK;
163  }
164 
165  /**
166  Applies the rule on a query, thereby creating a new one. This is done by
167  merging the replacement and literals from the query.
168 
169  @param thd Pointer to the query string.
170 
171  @retval false Everything worked, the new query is pointed to by 'query'.
172  @retval true The query did not match the pattern, nothing is allocated.
173  */
174  Rewrite_result create_new_query(MYSQL_THD thd);
175 
176  /**
177  Asks the parser service for the current query in normalized form and
178  compares it to the normalized pattern. This is the equivalent of comparing
179  the structure of two parse trees.
180 
181  @return True if the normalized pattern matches the current normalized
182  query, otherwise false.
183  */
184  bool matches(MYSQL_THD thd) const;
185 
187  return m_pattern.parse_error_message();
188  }
189 
191  return m_replacement.parse_error_message();
192  }
193 
194  private:
197 };
198 
199 #endif /* RULE_INCLUDED */
unsigned char uchar
Definition: my_inttypes.h:51
std::string new_query
If was_rewritten is true, a new query, otherwise an empty string.
Definition: rule.h:47
std::string replacement_parse_error_message()
Definition: rule.h:190
Load_status
Definition: rule.h:57
bool load(THD *, const dd::String_type &fname, dd::String_type *buf)
Read an sdi file from disk and store in a buffer.
Definition: sdi_file.cc:306
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:111
Definition: rule.h:132
bool was_rewritten
Means the query was successfully rewritten, and the new query is available in new_query.
Definition: rule.h:41
Some integer typedefs for easier portability.
services::Digest digest
The digest obtained from the pattern.
Definition: rule.h:65
std::string pattern_parse_error_message()
Definition: rule.h:186
The in-memory representation of a pattern.
Definition: rule.h:55
const char * PATTERN_GOT_NO_DIGEST
Definition: messages.h:37
bool digest_matched
Means that at lest one matching digest was found in the hash table.
Definition: rule.h:44
#define MYSQL_THD
Definition: backup_page_tracker.h:35
std::string m_parse_error_message
Definition: rule.h:119
int number_parameters
Definition: rule.h:59
Load_status
Definition: rule.h:128
Definition: rule.h:129
Replacement m_replacement
Definition: rule.h:196
Definition: rule.h:130
Definition: rule.h:57
A rule as persisted on disk.
Definition: persisted_rule.h:41
Definition: rule.h:95
Definition: rule.h:133
Conversion layer between the parser service and this plugin.
int number_parameters
The number of parameters (and the size of m_param_slots and parameter_positions.) ...
Definition: rule.h:104
Definition: rule.h:57
Mysql::Nullable< std::string > replacement
The rewrite rule&#39;s replacement string.
Definition: persisted_rule.h:50
Internal representation of a rewrite rule.
Definition: rule.h:126
std::vector< int > slots() const
Definition: rule.h:113
const T_value & value() const
Returns actual value of object.
Definition: nullable.h:88
The results of an attempt to rewrite a query parse tree.
Definition: rule.h:36
The facilities for easily manipulating nullable values from a rules_table_service::Cursor.
std::vector< int > m_param_slots
The positions in query_string of each parameter (&#39;?&#39;)
Definition: rule.h:117
const uchar * digest_buffer() const
The digest buffer.
Definition: rule.h:138
Rewrite_result()
Definition: rule.h:49
const char * REPLACEMENT_HAS_MORE_MARKERS
Definition: messages.h:39
const char * REPLACEMENT_PARSE_ERROR
Definition: messages.h:38
std::vector< std::string > literals
Definition: rule.h:67
Definition: services.h:59
std::string query_string
The query string of the replacement.
Definition: rule.h:98
Definition: rule.h:57
const char * PATTERN_PARSE_ERROR
Definition: messages.h:34
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads and parses the rule and replacement.
Definition: rule.h:144
Pattern m_pattern
Definition: rule.h:195
const char * PATTERN_NOT_SUPPORTED_STATEMENT
Definition: messages.h:35
std::string normalized_pattern()
The pattern in normalized form.
Definition: rule.h:141
#define false
Definition: config_static.h:43
std::string m_parse_error_message
Definition: rule.h:92
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:89
std::string normalized_pattern
The pattern in normalized form.
Definition: rule.h:62