MySQL 9.0.0
Source Code Documentation
system_view_definition_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 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
24#ifndef DD_SYSTEM_VIEWS__SYSTEM_VIEW_DEFINITION_IMPL_INCLUDED
25#define DD_SYSTEM_VIEWS__SYSTEM_VIEW_DEFINITION_IMPL_INCLUDED
26
27#include <map>
28#include <memory>
29#include <vector>
30
31#include "sql/dd/string_type.h" // dd::String_type
32#include "sql/dd/types/system_view_definition.h" // dd::System_view_definition
33#include "sql/mysqld.h" // lower_case_table_names
34
35namespace dd {
36namespace system_views {
37
39 public:
40 /**
41 Get view name.
42
43 @return name of the view.
44 */
45 virtual const String_type &view_name() const { return m_view_name; }
46
47 /**
48 Set view name.
49 */
50 virtual void set_view_name(const String_type &name) { m_view_name = name; }
51
52 /**
53 Get collation clause to append to view definition for some
54 view columns based on lower_case_table_names.
55
56 @return Empty string if lctn=0, other wise " COLLATE utf8mb3_tolower_ci".
57 */
59 if (lower_case_table_names != 0) return " COLLATE utf8mb3_tolower_ci";
60 return "";
61 }
62
63 String_type build_ddl_create_view() const override = 0;
64
65 private:
66 // Name of I_S system view;
68};
69
71 public:
72 /**
73 Add a field definition for the SELECT projection.
74 This function can be called more than once. The call will add a new
75 projection to the SELECT command.
76
77 @param field_number Ordinal position of field in the projection list.
78 @param field_name Field name used for the SELECT's projection.
79 @param field_definition Expression representing the projection.
80 @param add_quotes If true, output single quotes around the
81 field_definition.
82 */
83 virtual void add_field(int field_number, const String_type &field_name,
84 const String_type &field_definition,
85 bool add_quotes = false) {
86 // Make sure the field_number and field_name are not added twise.
87 assert(m_field_numbers.find(field_name) == m_field_numbers.end() &&
89
90 // Store the field number.
91 m_field_numbers[field_name] = field_number;
92
93 // Store the field definition expression.
95 if (field_name == "*") {
96 ss << " * ";
97 } else {
98 if (add_quotes) {
99 assert(field_definition.find('\'') == String_type::npos);
100 ss << '\'' << field_definition << '\'';
101 } else
102 ss << field_definition;
103
104 ss << " AS " << field_name;
105 }
106
108 }
109
110 /**
111 Add FROM clause for the SELECT.
112 This function can be called more than once. The clause will be appended to
113 the previous FROM clause string.
114
115 @param from String representing the FROM clause.
116 */
117 virtual void add_from(const String_type &from) {
118 m_from_clauses.push_back(from);
119 }
120
121 /**
122 Add WHERE clause for the SELECT.
123 This function can be called more than once. The clause will be appended to
124 the previous WHERE clause string.
125
126 @param where String representing the WHERE clause.
127 */
128 virtual void add_where(const String_type &where) {
129 m_where_clauses.push_back(where);
130 }
131
132 /**
133 Add CTE expression before SELECT.
134
135 @param cte String representing the CTE expression.
136 */
137 virtual void add_cte_expression(const String_type &cte) {
138 m_cte_expression = cte;
139 }
140
141 /**
142 Indicates that we should add DISTINCT clause to SELECT.
143 */
144 virtual void add_distinct() { m_is_distinct = true; }
145
146 /**
147 Indicates selection of all field (SELECT '*').
148 */
149 virtual void add_star() { m_add_star = true; }
150 /**
151 Get the field ordinal position number for the given field name.
152
153 @param field_name Column name for which the field number is returned.
154
155 @return Integer representing position of column in projection list.
156 */
157 virtual int field_number(const String_type &field_name) const {
158 assert(m_field_numbers.find(field_name) != m_field_numbers.end());
159 return m_field_numbers.find(field_name)->second;
160 }
161
162 /**
163 Build the SELECT query that is used in the CREATE VIEW command.
164
165 @return The SELECT query string.
166 */
169
170 if (!m_cte_expression.empty()) ss << m_cte_expression << "\n ";
171
172 // Make SELECT [DISTINCT]
173 ss << "SELECT " << (m_is_distinct ? "DISTINCT \n" : "\n");
174
175 if (!m_add_star) {
176 // Output view column definitions
177 for (Field_definitions::const_iterator field =
178 m_field_definitions.begin();
179 field != m_field_definitions.end(); ++field) {
180 if (field != m_field_definitions.begin()) ss << ",\n";
181 ss << " " << field->second;
182 }
183 } else
184 ss << "*";
185
186 // Output FROM clauses
187 for (From_clauses::const_iterator from = m_from_clauses.begin();
188 from != m_from_clauses.end(); ++from) {
189 if (from == m_from_clauses.begin()) ss << " FROM ";
190
191 ss << "\n " << *from;
192 }
193
194 // Output WHERE clauses
195 for (Where_clauses::const_iterator where = m_where_clauses.begin();
196 where != m_where_clauses.end(); ++where) {
197 if (where == m_where_clauses.begin()) ss << " WHERE ";
198
199 ss << "\n " << *where;
200 }
201
202 ss << "\n";
203
204 return ss.str();
205 }
206
209 ss << "CREATE OR REPLACE DEFINER=`mysql.infoschema`@`localhost` VIEW "
210 << "information_schema." << view_name() << " AS " + build_select_query();
211
212 return ss.str();
213 }
214
215 private:
216 // Map of field_names and the ordinal position in SELECT projection.
217 typedef std::map<String_type, int> Field_numbers;
218
219 // Map of field ordinal position and their view column definition.
220 typedef std::map<int, String_type> Field_definitions;
221
222 // List of FROM clause definition in the SELECT
223 typedef std::vector<String_type> From_clauses;
224
225 // List of WHERE clause definition in the SELECT
226 typedef std::vector<String_type> Where_clauses;
227
233 bool m_is_distinct{false};
234 bool m_add_star{false};
235};
236
237// This class is unused and kept here for future use.
239 public:
240 /**
241 Get the object for a SELECT definition to be used in the UNION.
242
243 @return The System_view_select_definition_impl&.
244 */
246 m_selects.push_back(
248 return *(m_selects.back().get());
249 }
250
253 bool first_query_block = true;
254 // Union definition must have minimum two SELECTs.
255 assert(m_selects.size() >= 2);
256
257 for (auto &select : m_selects) {
258 if (first_query_block) {
259 ss << "CREATE OR REPLACE DEFINER=`mysql.infoschema`@`localhost` VIEW "
260 << "information_schema." << view_name() << " AS "
261 << "(" << select->build_select_query() << ")";
262 first_query_block = false;
263 } else {
264 ss << " UNION "
265 << "(" << select->build_select_query() << ")";
266 }
267 }
268
269 return ss.str();
270 }
271
272 private:
273 using Select_definition = std::unique_ptr<System_view_select_definition_impl>;
274
275 // Member holds SELECT's used for the UNION
276 std::vector<Select_definition> m_selects;
277};
278
279} // namespace system_views
280} // namespace dd
281
282#endif // DD_SYSTEM_VIEWS__SYSTEM_VIEW_DEFINITION_IMPL_INCLUDED
Definition: system_view_definition_impl.h:38
virtual void set_view_name(const String_type &name)
Set view name.
Definition: system_view_definition_impl.h:50
String_type build_ddl_create_view() const override=0
Build CREATE VIEW DDL statement for the system view.
static const String_type fs_name_collation()
Get collation clause to append to view definition for some view columns based on lower_case_table_nam...
Definition: system_view_definition_impl.h:58
String_type m_view_name
Definition: system_view_definition_impl.h:67
virtual const String_type & view_name() const
Get view name.
Definition: system_view_definition_impl.h:45
Definition: system_view_definition.h:37
Definition: system_view_definition_impl.h:70
virtual int field_number(const String_type &field_name) const
Get the field ordinal position number for the given field name.
Definition: system_view_definition_impl.h:157
virtual void add_field(int field_number, const String_type &field_name, const String_type &field_definition, bool add_quotes=false)
Add a field definition for the SELECT projection.
Definition: system_view_definition_impl.h:83
virtual void add_where(const String_type &where)
Add WHERE clause for the SELECT.
Definition: system_view_definition_impl.h:128
String_type build_select_query() const
Build the SELECT query that is used in the CREATE VIEW command.
Definition: system_view_definition_impl.h:167
virtual void add_from(const String_type &from)
Add FROM clause for the SELECT.
Definition: system_view_definition_impl.h:117
virtual void add_cte_expression(const String_type &cte)
Add CTE expression before SELECT.
Definition: system_view_definition_impl.h:137
Where_clauses m_where_clauses
Definition: system_view_definition_impl.h:231
std::map< String_type, int > Field_numbers
Definition: system_view_definition_impl.h:217
Field_definitions m_field_definitions
Definition: system_view_definition_impl.h:229
virtual void add_star()
Indicates selection of all field (SELECT '*').
Definition: system_view_definition_impl.h:149
virtual void add_distinct()
Indicates that we should add DISTINCT clause to SELECT.
Definition: system_view_definition_impl.h:144
std::map< int, String_type > Field_definitions
Definition: system_view_definition_impl.h:220
From_clauses m_from_clauses
Definition: system_view_definition_impl.h:230
bool m_is_distinct
Definition: system_view_definition_impl.h:233
dd::String_type m_cte_expression
Definition: system_view_definition_impl.h:232
Field_numbers m_field_numbers
Definition: system_view_definition_impl.h:228
String_type build_ddl_create_view() const override
Build CREATE VIEW DDL statement for the system view.
Definition: system_view_definition_impl.h:207
bool m_add_star
Definition: system_view_definition_impl.h:234
std::vector< String_type > From_clauses
Definition: system_view_definition_impl.h:223
std::vector< String_type > Where_clauses
Definition: system_view_definition_impl.h:226
Definition: system_view_definition_impl.h:238
std::unique_ptr< System_view_select_definition_impl > Select_definition
Definition: system_view_definition_impl.h:273
System_view_select_definition_impl & get_query_block()
Get the object for a SELECT definition to be used in the UNION.
Definition: system_view_definition_impl.h:245
std::vector< Select_definition > m_selects
Definition: system_view_definition_impl.h:276
String_type build_ddl_create_view() const override
Build CREATE VIEW DDL statement for the system view.
Definition: system_view_definition_impl.h:251
uint lower_case_table_names
Definition: mysqld.cc:1340
static char * where
Definition: mysqldump.cc:152
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
Char_stringstream_template< String_type_allocator > Stringstream_type
Instantiation of std::basic_stringstream with the same allocator as String_type.
Definition: string_type.h:72
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
case opt name
Definition: sslopt-case.h:29