MySQL 9.4.0
Source Code Documentation
system_view_definition_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2025, 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 /**
64 Get collation clause to append to view definition for some
65 utf8mb4 view columns that depend on lower_case_table_names.
66
67 Copying the behavior of fs_name_collation(), but wondering
68 why we don't use the the ci collation only for LCTN=2, that
69 would be more in line with the behavior oF the DD tables,
70 see Object_table_definition_impl::fs_name_collation().
71
72 @return utf8mb4_bin if lctn=0, otherwise utf8mb4_0900_as_ci.
73 */
75 if (lower_case_table_names != 0) return " COLLATE utf8mb4_0900_as_ci";
76 return " COLLATE utf8mb4_bin";
77 }
78
79 String_type build_ddl_create_view() const override = 0;
80
81 private:
82 // Name of I_S system view;
84};
85
87 public:
88 /**
89 Add a field definition for the SELECT projection.
90 This function can be called more than once. The call will add a new
91 projection to the SELECT command.
92
93 @param field_number Ordinal position of field in the projection list.
94 @param field_name Field name used for the SELECT's projection.
95 @param field_definition Expression representing the projection.
96 @param add_quotes If true, output single quotes around the
97 field_definition.
98 */
99 virtual void add_field(int field_number, const String_type &field_name,
100 const String_type &field_definition,
101 bool add_quotes = false) {
102 // Make sure the field_number and field_name are not added twise.
103 assert(m_field_numbers.find(field_name) == m_field_numbers.end() &&
105
106 // Store the field number.
107 m_field_numbers[field_name] = field_number;
108
109 // Store the field definition expression.
111 if (field_name == "*") {
112 ss << " * ";
113 } else {
114 if (add_quotes) {
115 assert(field_definition.find('\'') == String_type::npos);
116 ss << '\'' << field_definition << '\'';
117 } else
118 ss << field_definition;
119
120 ss << " AS " << field_name;
121 }
122
124 }
125
126 /**
127 Add FROM clause for the SELECT.
128 This function can be called more than once. The clause will be appended to
129 the previous FROM clause string.
130
131 @param from String representing the FROM clause.
132 */
133 virtual void add_from(const String_type &from) {
134 m_from_clauses.push_back(from);
135 }
136
137 /**
138 Add WHERE clause for the SELECT.
139 This function can be called more than once. The clause will be appended to
140 the previous WHERE clause string.
141
142 @param where String representing the WHERE clause.
143 */
144 virtual void add_where(const String_type &where) {
145 m_where_clauses.push_back(where);
146 }
147
148 /**
149 Add CTE expression before SELECT.
150
151 @param cte String representing the CTE expression.
152 */
153 virtual void add_cte_expression(const String_type &cte) {
154 m_cte_expression = cte;
155 }
156
157 /**
158 Indicates that we should add DISTINCT clause to SELECT.
159 */
160 virtual void add_distinct() { m_is_distinct = true; }
161
162 /**
163 Indicates selection of all field (SELECT '*').
164 */
165 virtual void add_star() { m_add_star = true; }
166 /**
167 Get the field ordinal position number for the given field name.
168
169 @param field_name Column name for which the field number is returned.
170
171 @return Integer representing position of column in projection list.
172 */
173 virtual int field_number(const String_type &field_name) const {
174 assert(m_field_numbers.find(field_name) != m_field_numbers.end());
175 return m_field_numbers.find(field_name)->second;
176 }
177
178 /**
179 Build the SELECT query that is used in the CREATE VIEW command.
180
181 @return The SELECT query string.
182 */
185
186 if (!m_cte_expression.empty()) ss << m_cte_expression << "\n ";
187
188 // Make SELECT [DISTINCT]
189 ss << "SELECT " << (m_is_distinct ? "DISTINCT \n" : "\n");
190
191 if (!m_add_star) {
192 // Output view column definitions
193 for (Field_definitions::const_iterator field =
194 m_field_definitions.begin();
195 field != m_field_definitions.end(); ++field) {
196 if (field != m_field_definitions.begin()) ss << ",\n";
197 ss << " " << field->second;
198 }
199 } else
200 ss << "*";
201
202 // Output FROM clauses
203 for (From_clauses::const_iterator from = m_from_clauses.begin();
204 from != m_from_clauses.end(); ++from) {
205 if (from == m_from_clauses.begin()) ss << " FROM ";
206
207 ss << "\n " << *from;
208 }
209
210 // Output WHERE clauses
211 for (Where_clauses::const_iterator where = m_where_clauses.begin();
212 where != m_where_clauses.end(); ++where) {
213 if (where == m_where_clauses.begin()) ss << " WHERE ";
214
215 ss << "\n " << *where;
216 }
217
218 ss << "\n";
219
220 return ss.str();
221 }
222
225 ss << "CREATE OR REPLACE DEFINER=`mysql.infoschema`@`localhost` VIEW "
226 << "information_schema." << view_name() << " AS " + build_select_query();
227
228 return ss.str();
229 }
230
231 private:
232 // Map of field_names and the ordinal position in SELECT projection.
233 typedef std::map<String_type, int> Field_numbers;
234
235 // Map of field ordinal position and their view column definition.
236 typedef std::map<int, String_type> Field_definitions;
237
238 // List of FROM clause definition in the SELECT
239 typedef std::vector<String_type> From_clauses;
240
241 // List of WHERE clause definition in the SELECT
242 typedef std::vector<String_type> Where_clauses;
243
249 bool m_is_distinct{false};
250 bool m_add_star{false};
251};
252
253// This class is unused and kept here for future use.
255 public:
256 /**
257 Get the object for a SELECT definition to be used in the UNION.
258
259 @return The System_view_select_definition_impl&.
260 */
262 m_selects.push_back(
264 return *(m_selects.back().get());
265 }
266
269 bool first_query_block = true;
270 // Union definition must have minimum two SELECTs.
271 assert(m_selects.size() >= 2);
272
273 for (auto &select : m_selects) {
274 if (first_query_block) {
275 ss << "CREATE OR REPLACE DEFINER=`mysql.infoschema`@`localhost` VIEW "
276 << "information_schema." << view_name() << " AS "
277 << "(" << select->build_select_query() << ")";
278 first_query_block = false;
279 } else {
280 ss << " UNION "
281 << "(" << select->build_select_query() << ")";
282 }
283 }
284
285 return ss.str();
286 }
287
288 private:
289 using Select_definition = std::unique_ptr<System_view_select_definition_impl>;
290
291 // Member holds SELECT's used for the UNION
292 std::vector<Select_definition> m_selects;
293};
294
295} // namespace system_views
296} // namespace dd
297
298#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
static const String_type fs_name_collation_utf8mb4()
Get collation clause to append to view definition for some utf8mb4 view columns that depend on lower_...
Definition: system_view_definition_impl.h:74
String_type m_view_name
Definition: system_view_definition_impl.h:83
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:86
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:173
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:99
virtual void add_where(const String_type &where)
Add WHERE clause for the SELECT.
Definition: system_view_definition_impl.h:144
String_type build_select_query() const
Build the SELECT query that is used in the CREATE VIEW command.
Definition: system_view_definition_impl.h:183
virtual void add_from(const String_type &from)
Add FROM clause for the SELECT.
Definition: system_view_definition_impl.h:133
virtual void add_cte_expression(const String_type &cte)
Add CTE expression before SELECT.
Definition: system_view_definition_impl.h:153
Where_clauses m_where_clauses
Definition: system_view_definition_impl.h:247
std::map< String_type, int > Field_numbers
Definition: system_view_definition_impl.h:233
Field_definitions m_field_definitions
Definition: system_view_definition_impl.h:245
virtual void add_star()
Indicates selection of all field (SELECT '*').
Definition: system_view_definition_impl.h:165
virtual void add_distinct()
Indicates that we should add DISTINCT clause to SELECT.
Definition: system_view_definition_impl.h:160
std::map< int, String_type > Field_definitions
Definition: system_view_definition_impl.h:236
From_clauses m_from_clauses
Definition: system_view_definition_impl.h:246
bool m_is_distinct
Definition: system_view_definition_impl.h:249
dd::String_type m_cte_expression
Definition: system_view_definition_impl.h:248
Field_numbers m_field_numbers
Definition: system_view_definition_impl.h:244
String_type build_ddl_create_view() const override
Build CREATE VIEW DDL statement for the system view.
Definition: system_view_definition_impl.h:223
bool m_add_star
Definition: system_view_definition_impl.h:250
std::vector< String_type > From_clauses
Definition: system_view_definition_impl.h:239
std::vector< String_type > Where_clauses
Definition: system_view_definition_impl.h:242
Definition: system_view_definition_impl.h:254
std::unique_ptr< System_view_select_definition_impl > Select_definition
Definition: system_view_definition_impl.h:289
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:261
std::vector< Select_definition > m_selects
Definition: system_view_definition_impl.h:292
String_type build_ddl_create_view() const override
Build CREATE VIEW DDL statement for the system view.
Definition: system_view_definition_impl.h:267
uint lower_case_table_names
Definition: mysqld.cc:1354
static char * where
Definition: mysqldump.cc:153
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