MySQL 9.1.0
Source Code Documentation
query_builder.h
Go to the documentation of this file.
1#ifndef QUERY_BUILDER_INCLUDED
2#define QUERY_BUILDER_INCLUDED
3/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include "my_config.h"
27
28#include <string>
29#include <vector>
30
33
34/**
35 @file query_builder.h
36
37*/
38
39/**
40 Class that builds the rewritten query by appending literals in the order
41 they appear in the parse tree.
42*/
44 public:
45 Query_builder(const Pattern *pattern, const Replacement *replacement)
46 : m_previous_slot(0),
47 m_replacement(replacement->query_string),
48 m_slots(replacement->slots()),
50 m_pattern_literals(pattern->literals),
52 m_matches_so_far(true) {}
53
54 /**
55 Implementation of the visit() function that bridges to add_next_literal().
56
57 @param item The current literal.
58 */
59 bool visit(MYSQL_ITEM item) override { return add_next_literal(item); }
60
61 /**
62 To be called after visit() has been called for all literals in the parse
63 tree that this Query_builder was visiting. This function finishes the
64 string to yield a complete query.
65 */
66 const std::string &get_built_query() {
67 // Append trailing segment of replacement.
69 return m_built_query;
70 }
71
72 /**
73 Status of the matching of literals that are not parameter markers.
74
75 @retval true The parse tree matches the pattern and it is safe to continue
76 adding literals.
77
78 @retval false Some literal has been found to differ between parse tree and
79 pattern. Execution must end immediately.
80 */
81 bool matches() const { return m_matches_so_far; }
82
83 private:
84 /**
85 The index of the character in 'm_replacement' after the last slot that we
86 filled.
87 */
89
90 /// Query we copy from (replacement string.)
91 std::string m_replacement;
92
93 /// The slots in the replacement string.
94 std::vector<int> m_slots;
95 std::vector<int>::iterator m_slots_iter;
96
97 /// All literals in the pattern, in order of appearance in parse tree.
98 std::vector<std::string> m_pattern_literals;
99 std::vector<std::string>::iterator m_pattern_literals_iter;
100
101 /// The query under construction.
102 std::string m_built_query;
103
104 /**
105 Whether the literals in the parse tree match those of the pattern so
106 far.
107 */
109
110 /**
111 Adds a literal, assumed to be the next in the parse tree, from the query's
112 parse tree to this Query_builder.
113
114 @param item Assumed to be a literal.
115
116 @retval true The builder is finished. Either it has been detected that the
117 current literal does not match the pattern, or no more literals are needed
118 to build the query.
119 */
120 bool add_next_literal(MYSQL_ITEM item);
121};
122
124 std::string query_literal = services::print_item(item);
125 std::string pattern_literal = *m_pattern_literals_iter;
126
127 if (pattern_literal.compare("?") ==
128 0) { // Literal corresponds to a parameter marker in the pattern.
129
130 if (m_slots_iter != m_slots.end()) // There are more slots to fill
131 {
132 // The part of the replacement leading up to its corresponding slot.
135 m_built_query += query_literal;
136
138 }
139 } else if (pattern_literal.compare(query_literal) != 0) {
140 // The literal does not match the pattern nor a parameter marker, we
141 // fail to rewrite.
142 m_matches_so_far = false;
143 return true;
144 }
146}
147
148#endif // QUERY_BUILDER_INCLUDED
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
The in-memory representation of a pattern.
Definition: rule.h:56
Class that builds the rewritten query by appending literals in the order they appear in the parse tre...
Definition: query_builder.h:43
std::vector< int > m_slots
The slots in the replacement string.
Definition: query_builder.h:94
std::vector< std::string > m_pattern_literals
All literals in the pattern, in order of appearance in parse tree.
Definition: query_builder.h:98
const std::string & get_built_query()
To be called after visit() has been called for all literals in the parse tree that this Query_builder...
Definition: query_builder.h:66
std::vector< int >::iterator m_slots_iter
Definition: query_builder.h:95
std::string m_replacement
Query we copy from (replacement string.)
Definition: query_builder.h:91
bool m_matches_so_far
Whether the literals in the parse tree match those of the pattern so far.
Definition: query_builder.h:108
bool visit(MYSQL_ITEM item) override
Implementation of the visit() function that bridges to add_next_literal().
Definition: query_builder.h:59
int m_previous_slot
The index of the character in 'm_replacement' after the last slot that we filled.
Definition: query_builder.h:88
bool add_next_literal(MYSQL_ITEM item)
Adds a literal, assumed to be the next in the parse tree, from the query's parse tree to this Query_b...
Definition: query_builder.h:123
std::string m_built_query
The query under construction.
Definition: query_builder.h:102
Query_builder(const Pattern *pattern, const Replacement *replacement)
Definition: query_builder.h:45
bool matches() const
Status of the matching of literals that are not parameter markers.
Definition: query_builder.h:81
std::vector< std::string >::iterator m_pattern_literals_iter
Definition: query_builder.h:99
Definition: rule.h:96
Definition: services.h:76
const char * begin(const char *const c)
Definition: base64.h:44
string print_item(MYSQL_ITEM item)
Prints an Item as an std::string.
Definition: services.cc:132
Conversion layer between the parser service and this plugin.