MySQL  8.0.19
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 
50 };
51 
52 /**
53  The in-memory representation of a pattern.
54 */
55 class Pattern {
56  public:
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.
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 
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  */
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 
188  }
189 
192  }
193 
194  private:
197 };
198 
199 #endif /* RULE_INCLUDED */
Pattern::PARSE_ERROR
@ PARSE_ERROR
Definition: rule.h:57
Rule
Internal representation of a rewrite rule.
Definition: rule.h:126
Replacement::m_parse_error_message
std::string m_parse_error_message
Definition: rule.h:119
Rule::matches
bool matches(MYSQL_THD thd) const
Asks the parser service for the current query in normalized form and compares it to the normalized pa...
Definition: rule.cc:180
Replacement::parse_error_message
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:111
my_dbug.h
Rewrite_result::was_rewritten
bool was_rewritten
Means the query was successfully rewritten, and the new query is available in new_query.
Definition: rule.h:41
services.h
Rule::PATTERN_NOT_SUPPORTED_STATEMENT
@ PATTERN_NOT_SUPPORTED_STATEMENT
Definition: rule.h:131
Rule::digest_buffer
const uchar * digest_buffer() const
The digest buffer.
Definition: rule.h:138
Rule::OK
@ OK
Definition: rule.h:129
Rule::normalized_pattern
std::string normalized_pattern()
The pattern in normalized form.
Definition: rule.h:141
Rewrite_result
The results of an attempt to rewrite a query parse tree.
Definition: rule.h:36
Persisted_rule::replacement
Mysql::Nullable< std::string > replacement
The rewrite rule's replacement string.
Definition: persisted_rule.h:50
Pattern::literals
std::vector< std::string > literals
Definition: rule.h:67
Pattern::parse_error_message
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:89
Replacement::load
bool load(MYSQL_THD thd, const std::string replacement)
Load the replacement query string.
Definition: rule.cc:149
services::Digest::c_ptr
const uchar * c_ptr() const
Needed because we use a C hash table to store digests.
Definition: services.h:72
Mysql::Nullable::value
const T_value & value() const
Returns actual value of object.
Definition: nullable.h:88
Replacement::number_parameters
int number_parameters
The number of parameters (and the size of m_param_slots and parameter_positions.)
Definition: rule.h:104
my_inttypes.h
Rewrite_result::digest_matched
bool digest_matched
Means that at lest one matching digest was found in the hash table.
Definition: rule.h:44
Pattern::load
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads the pattern.
Definition: rule.cc:113
Pattern::OK
@ OK
Definition: rule.h:57
Rule::create_new_query
Rewrite_result create_new_query(MYSQL_THD thd)
Applies the rule on a query, thereby creating a new one.
Definition: rule.cc:165
Rule::m_pattern
Pattern m_pattern
Definition: rule.h:195
Rule::load
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads and parses the rule and replacement.
Definition: rule.h:144
uchar
unsigned char uchar
Definition: my_inttypes.h:51
Rule::pattern_parse_error_message
std::string pattern_parse_error_message()
Definition: rule.h:186
persisted_rule.h
Persisted_rule
A rule as persisted on disk.
Definition: persisted_rule.h:41
Replacement::m_param_slots
std::vector< int > m_param_slots
The positions in query_string of each parameter ('?')
Definition: rule.h:117
Pattern::NO_DIGEST
@ NO_DIGEST
Definition: rule.h:57
services::Digest
Definition: services.h:59
MYSQL_THD
#define MYSQL_THD
Definition: backup_page_tracker.h:15
Replacement::slots
std::vector< int > slots() const
Definition: rule.h:113
Pattern::normalized_pattern
std::string normalized_pattern
The pattern in normalized form.
Definition: rule.h:62
Pattern::digest
services::Digest digest
The digest obtained from the pattern.
Definition: rule.h:65
Rule::replacement_parse_error_message
std::string replacement_parse_error_message()
Definition: rule.h:190
Rule::PATTERN_PARSE_ERROR
@ PATTERN_PARSE_ERROR
Definition: rule.h:130
Pattern
The in-memory representation of a pattern.
Definition: rule.h:55
Rule::REPLACEMENT_HAS_MORE_MARKERS
@ REPLACEMENT_HAS_MORE_MARKERS
Definition: rule.h:134
Rewrite_result::Rewrite_result
Rewrite_result()
Definition: rule.h:49
Pattern::Load_status
Load_status
Definition: rule.h:57
Replacement
Definition: rule.h:95
Rule::REPLACEMENT_PARSE_ERROR
@ REPLACEMENT_PARSE_ERROR
Definition: rule.h:133
Pattern::m_parse_error_message
std::string m_parse_error_message
Definition: rule.h:92
Pattern::NOT_SUPPORTED_STATEMENT
@ NOT_SUPPORTED_STATEMENT
Definition: rule.h:57
Rule::m_replacement
Replacement m_replacement
Definition: rule.h:196
Replacement::query_string
std::string query_string
The query string of the replacement.
Definition: rule.h:98
Pattern::number_parameters
int number_parameters
Definition: rule.h:59
Rule::Load_status
Load_status
Definition: rule.h:128
Rule::PATTERN_GOT_NO_DIGEST
@ PATTERN_GOT_NO_DIGEST
Definition: rule.h:132
Rewrite_result::new_query
std::string new_query
If was_rewritten is true, a new query, otherwise an empty string.
Definition: rule.h:47
false
#define false
Definition: config_static.h:43