MySQL 8.3.0
Source Code Documentation
rule.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2023, Oracle and/or its affiliates.
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*/
55class 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 copied in deep-copy way. This is
71 not done in the CTOR because 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 */
90
91 private:
93};
94
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 */
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*/
126class Rule {
127 public:
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;
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 */
#define MYSQL_THD
Definition: backup_page_tracker.h:37
The in-memory representation of a pattern.
Definition: rule.h:55
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads the pattern.
Definition: rule.cc:113
std::vector< std::string > literals
Definition: rule.h:67
services::Digest digest
The digest obtained from the pattern.
Definition: rule.h:65
std::string m_parse_error_message
Definition: rule.h:92
Load_status
Definition: rule.h:57
@ PARSE_ERROR
Definition: rule.h:57
@ NO_DIGEST
Definition: rule.h:57
@ OK
Definition: rule.h:57
@ NOT_SUPPORTED_STATEMENT
Definition: rule.h:57
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:89
int number_parameters
Definition: rule.h:59
std::string normalized_pattern
The pattern in normalized form.
Definition: rule.h:62
A rule as persisted on disk.
Definition: persisted_rule.h:41
std::optional< std::string > replacement
The rewrite rule's replacement string.
Definition: persisted_rule.h:50
Definition: rule.h:95
std::vector< int > m_param_slots
The positions in query_string of each parameter ('?')
Definition: rule.h:117
std::string m_parse_error_message
Definition: rule.h:119
int number_parameters
The number of parameters (and the size of m_param_slots and parameter_positions.)
Definition: rule.h:104
bool load(MYSQL_THD thd, const std::string replacement)
Load the replacement query string.
Definition: rule.cc:149
std::vector< int > slots() const
Definition: rule.h:113
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:111
std::string query_string
The query string of the replacement.
Definition: rule.h:98
Internal representation of a rewrite rule.
Definition: rule.h:126
Pattern m_pattern
Definition: rule.h:195
Rewrite_result create_new_query(MYSQL_THD thd)
Applies the rule on a query, thereby creating a new one.
Definition: rule.cc:165
std::string normalized_pattern()
The pattern in normalized form.
Definition: rule.h:141
Replacement m_replacement
Definition: rule.h:196
std::string pattern_parse_error_message()
Definition: rule.h:186
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads and parses the rule and replacement.
Definition: rule.h:144
Load_status
Definition: rule.h:128
@ PATTERN_NOT_SUPPORTED_STATEMENT
Definition: rule.h:131
@ REPLACEMENT_PARSE_ERROR
Definition: rule.h:133
@ PATTERN_GOT_NO_DIGEST
Definition: rule.h:132
@ PATTERN_PARSE_ERROR
Definition: rule.h:130
@ OK
Definition: rule.h:129
@ REPLACEMENT_HAS_MORE_MARKERS
Definition: rule.h:134
std::string replacement_parse_error_message()
Definition: rule.h:190
const uchar * digest_buffer() const
The digest buffer.
Definition: rule.h:138
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
Definition: services.h:59
const uchar * c_ptr() const
Needed because we use a C hash table to store digests.
Definition: services.h:72
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:51
The facilities for easily manipulating nullable values from a rules_table_service::Cursor.
Conversion layer between the parser service and this plugin.
The results of an attempt to rewrite a query parse tree.
Definition: rule.h:36
bool digest_matched
Means that at lest one matching digest was found in the hash table.
Definition: rule.h:44
std::string new_query
If was_rewritten is true, a new query, otherwise an empty string.
Definition: rule.h:47
Rewrite_result()
Definition: rule.h:49
bool was_rewritten
Means the query was successfully rewritten, and the new query is available in new_query.
Definition: rule.h:41