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