MySQL 8.3.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, 2023, 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 also distributed 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 included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#include "my_config.h"
26
27#include <string>
28#include <vector>
29
32
33/**
34 @file query_builder.h
35
36*/
37
38/**
39 Class that builds the rewritten query by appending literals in the order
40 they appear in the parse tree.
41*/
43 public:
44 Query_builder(const Pattern *pattern, const Replacement *replacement)
45 : m_previous_slot(0),
46 m_replacement(replacement->query_string),
47 m_slots(replacement->slots()),
49 m_pattern_literals(pattern->literals),
51 m_matches_so_far(true) {}
52
53 /**
54 Implementation of the visit() function that bridges to add_next_literal().
55
56 @param item The current literal.
57 */
58 bool visit(MYSQL_ITEM item) override { return add_next_literal(item); }
59
60 /**
61 To be called after visit() has been called for all literals in the parse
62 tree that this Query_builder was visiting. This function finishes the
63 string to yield a complete query.
64 */
65 const std::string &get_built_query() {
66 // Append trailing segment of replacement.
68 return m_built_query;
69 }
70
71 /**
72 Status of the matching of literals that are not parameter markers.
73
74 @retval true The parse tree matches the pattern and it is safe to continue
75 adding literals.
76
77 @retval false Some literal has been found to differ between parse tree and
78 pattern. Execution must end immediately.
79 */
80 bool matches() const { return m_matches_so_far; }
81
82 private:
83 /**
84 The index of the character in 'm_replacement' after the last slot that we
85 filled.
86 */
88
89 /// Query we copy from (replacement string.)
90 std::string m_replacement;
91
92 /// The slots in the replacement string.
93 std::vector<int> m_slots;
94 std::vector<int>::iterator m_slots_iter;
95
96 /// All literals in the pattern, in order of appearance in parse tree.
97 std::vector<std::string> m_pattern_literals;
98 std::vector<std::string>::iterator m_pattern_literals_iter;
99
100 /// The query under construction.
101 std::string m_built_query;
102
103 /**
104 Whether the literals in the parse tree match those of the pattern so
105 far.
106 */
108
109 /**
110 Adds a literal, assumed to be the next in the parse tree, from the query's
111 parse tree to this Query_builder.
112
113 @param item Assumed to be a literal.
114
115 @retval true The builder is finished. Either it has been detected that the
116 current literal does not match the pattern, or no more literals are needed
117 to build the query.
118 */
119 bool add_next_literal(MYSQL_ITEM item);
120};
121
123 std::string query_literal = services::print_item(item);
124 std::string pattern_literal = *m_pattern_literals_iter;
125
126 if (pattern_literal.compare("?") ==
127 0) { // Literal corresponds to a parameter marker in the pattern.
128
129 if (m_slots_iter != m_slots.end()) // There are more slots to fill
130 {
131 // The part of the replacement leading up to its corresponding slot.
134 m_built_query += query_literal;
135
136 m_previous_slot = *m_slots_iter++ + sizeof('?');
137 }
138 } else if (pattern_literal.compare(query_literal) != 0) {
139 // The literal does not match the pattern nor a parameter marker, we
140 // fail to rewrite.
141 m_matches_so_far = false;
142 return true;
143 }
145}
146
147#endif // QUERY_BUILDER_INCLUDED
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:933
The in-memory representation of a pattern.
Definition: rule.h:55
Class that builds the rewritten query by appending literals in the order they appear in the parse tre...
Definition: query_builder.h:42
std::vector< int > m_slots
The slots in the replacement string.
Definition: query_builder.h:93
std::vector< std::string > m_pattern_literals
All literals in the pattern, in order of appearance in parse tree.
Definition: query_builder.h:97
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:65
std::vector< int >::iterator m_slots_iter
Definition: query_builder.h:94
std::string m_replacement
Query we copy from (replacement string.)
Definition: query_builder.h:90
bool m_matches_so_far
Whether the literals in the parse tree match those of the pattern so far.
Definition: query_builder.h:107
bool visit(MYSQL_ITEM item) override
Implementation of the visit() function that bridges to add_next_literal().
Definition: query_builder.h:58
int m_previous_slot
The index of the character in 'm_replacement' after the last slot that we filled.
Definition: query_builder.h:87
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:122
std::string m_built_query
The query under construction.
Definition: query_builder.h:101
Query_builder(const Pattern *pattern, const Replacement *replacement)
Definition: query_builder.h:44
bool matches() const
Status of the matching of literals that are not parameter markers.
Definition: query_builder.h:80
std::vector< std::string >::iterator m_pattern_literals_iter
Definition: query_builder.h:98
Definition: rule.h:95
Definition: services.h:75
void * begin(THD *thd, const TABLE *table, size_t data_size, size_t memory, size_t num_threads) noexcept
Definition: bulk_data_service.cc:1533
string print_item(MYSQL_ITEM item)
Prints an Item as an std::string.
Definition: services.cc:131
Conversion layer between the parser service and this plugin.