MySQL 9.0.0
Source Code Documentation
rule.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23#ifndef RULE_INCLUDED
24#define RULE_INCLUDED
25
26#include "my_config.h"
27
28#include <string>
29#include <vector>
30
31#include "my_dbug.h"
32#include "my_inttypes.h"
35
36/// The results of an attempt to rewrite a query parse tree.
38 /**
39 Means the query was successfully rewritten, and the new query is available
40 in new_query.
41 */
43
44 /// Means that at lest one matching digest was found in the hash table.
46
47 /// If was_rewritten is true, a new query, otherwise an empty string.
48 std::string new_query;
49
51};
52
53/**
54 The in-memory representation of a pattern.
55*/
56class Pattern {
57 public:
59
61
62 /// The pattern in normalized form.
63 std::string normalized_pattern;
64
65 /// The digest obtained from the pattern
67
68 std::vector<std::string> literals;
69
70 /**
71 Loads the pattern. The pattern string is copied in deep-copy way. This is
72 not done in the CTOR because of the memory allocation.
73
74 This function does the following:
75 - Parse the pattern string.
76 - Print a normalized version.
77 - Extract the position of the parameter markers.
78 - Compute the digest
79
80 @retval false Success.
81
82 @retval true Either parse error, the pattern is not a select statement or
83 out of memory.
84 */
85 Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule);
86
87 /**
88 If any errors were raised during parsing, the first one is available here.
89 */
91
92 private:
94};
95
97 public:
98 /// The query string of the replacement
99 std::string query_string;
100
101 /**
102 The number of parameters (and the size of m_param_slots and
103 parameter_positions.)
104 */
106
107 bool load(MYSQL_THD thd, const std::string replacement);
108
109 /**
110 If any errors were raised during parsing, the first one is available here.
111 */
113
114 std::vector<int> slots() const { return m_param_slots; }
115
116 private:
117 /// The positions in query_string of each parameter ('?')
118 std::vector<int> m_param_slots;
119
121};
122
123/**
124 Internal representation of a rewrite rule.
125 A rewrite rule consists of a pattern and a replacement.
126*/
127class Rule {
128 public:
136 };
137
138 /// The digest buffer.
139 const uchar *digest_buffer() const { return m_pattern.digest.c_ptr(); }
140
141 /// The pattern in normalized form.
143
144 /// Loads and parses the rule and replacement.
145 Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule) {
146 switch (m_pattern.load(thd, diskrule)) {
147 case Pattern::OK:
148 break;
150 return PATTERN_PARSE_ERROR;
155 }
156
157 if (m_replacement.load(thd, diskrule->replacement.value()))
159
162
163 return OK;
164 }
165
166 /**
167 Applies the rule on a query, thereby creating a new one. This is done by
168 merging the replacement and literals from the query.
169
170 @param thd Pointer to the query string.
171
172 @retval false Everything worked, the new query is pointed to by 'query'.
173 @retval true The query did not match the pattern, nothing is allocated.
174 */
176
177 /**
178 Asks the parser service for the current query in normalized form and
179 compares it to the normalized pattern. This is the equivalent of comparing
180 the structure of two parse trees.
181
182 @return True if the normalized pattern matches the current normalized
183 query, otherwise false.
184 */
185 bool matches(MYSQL_THD thd) const;
186
189 }
190
193 }
194
195 private:
198};
199
200#endif /* RULE_INCLUDED */
#define MYSQL_THD
Definition: backup_page_tracker.h:38
The in-memory representation of a pattern.
Definition: rule.h:56
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads the pattern.
Definition: rule.cc:114
std::vector< std::string > literals
Definition: rule.h:68
services::Digest digest
The digest obtained from the pattern.
Definition: rule.h:66
std::string m_parse_error_message
Definition: rule.h:93
Load_status
Definition: rule.h:58
@ PARSE_ERROR
Definition: rule.h:58
@ NO_DIGEST
Definition: rule.h:58
@ OK
Definition: rule.h:58
@ NOT_SUPPORTED_STATEMENT
Definition: rule.h:58
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:90
int number_parameters
Definition: rule.h:60
std::string normalized_pattern
The pattern in normalized form.
Definition: rule.h:63
A rule as persisted on disk.
Definition: persisted_rule.h:42
std::optional< std::string > replacement
The rewrite rule's replacement string.
Definition: persisted_rule.h:51
Definition: rule.h:96
std::vector< int > m_param_slots
The positions in query_string of each parameter ('?')
Definition: rule.h:118
std::string m_parse_error_message
Definition: rule.h:120
int number_parameters
The number of parameters (and the size of m_param_slots and parameter_positions.)
Definition: rule.h:105
bool load(MYSQL_THD thd, const std::string replacement)
Load the replacement query string.
Definition: rule.cc:150
std::vector< int > slots() const
Definition: rule.h:114
std::string parse_error_message()
If any errors were raised during parsing, the first one is available here.
Definition: rule.h:112
std::string query_string
The query string of the replacement.
Definition: rule.h:99
Internal representation of a rewrite rule.
Definition: rule.h:127
Pattern m_pattern
Definition: rule.h:196
Rewrite_result create_new_query(MYSQL_THD thd)
Applies the rule on a query, thereby creating a new one.
Definition: rule.cc:166
std::string normalized_pattern()
The pattern in normalized form.
Definition: rule.h:142
Replacement m_replacement
Definition: rule.h:197
std::string pattern_parse_error_message()
Definition: rule.h:187
Load_status load(MYSQL_THD thd, const Persisted_rule *diskrule)
Loads and parses the rule and replacement.
Definition: rule.h:145
Load_status
Definition: rule.h:129
@ PATTERN_NOT_SUPPORTED_STATEMENT
Definition: rule.h:132
@ REPLACEMENT_PARSE_ERROR
Definition: rule.h:134
@ PATTERN_GOT_NO_DIGEST
Definition: rule.h:133
@ PATTERN_PARSE_ERROR
Definition: rule.h:131
@ OK
Definition: rule.h:130
@ REPLACEMENT_HAS_MORE_MARKERS
Definition: rule.h:135
std::string replacement_parse_error_message()
Definition: rule.h:191
const uchar * digest_buffer() const
The digest buffer.
Definition: rule.h:139
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:181
Definition: services.h:60
const uchar * c_ptr() const
Needed because we use a C hash table to store digests.
Definition: services.h:73
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
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:37
bool digest_matched
Means that at lest one matching digest was found in the hash table.
Definition: rule.h:45
std::string new_query
If was_rewritten is true, a new query, otherwise an empty string.
Definition: rule.h:48
Rewrite_result()
Definition: rule.h:50
bool was_rewritten
Means the query was successfully rewritten, and the new query is available in new_query.
Definition: rule.h:42