MySQL 9.1.0
Source Code Documentation
object_table_definition_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 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__OBJECT_TABLE_DEFINITION_IMPL_INCLUDED
25#define DD__OBJECT_TABLE_DEFINITION_IMPL_INCLUDED
26
27#include <assert.h>
28#include <map>
29#include <memory>
30#include <vector>
31
32#include "m_string.h"
34#include "sql/dd/string_type.h" // dd::String_type
35#include "sql/dd/types/object_table_definition.h" // dd::Object_table_definition
36#include "sql/mysqld.h" // lower_case_table_names
37
38namespace dd {
39
40class Properties;
41
42///////////////////////////////////////////////////////////////////////////
43
45 public:
46 typedef std::map<String_type, int> Element_numbers;
47 typedef std::map<int, String_type> Element_definitions;
48
49 private:
50 enum class Label {
51 NAME,
52 FIELDS,
53 INDEXES,
55 OPTIONS,
56 LABEL,
60 };
61
62 static const char *key(Label label) {
63 switch (label) {
64 case Label::NAME:
65 return "name";
66 case Label::FIELDS:
67 return "fields";
68 case Label::INDEXES:
69 return "indexes";
71 return "foreign_keys";
72 case Label::OPTIONS:
73 return "options";
74 case Label::LABEL:
75 return "lbl";
76 case Label::POSITION:
77 return "pos";
79 return "def";
80 case Label::ELEMENT:
81 return "elem";
82 default:
83 assert(false);
84 return "";
85 }
86 }
87
89
92
94
97
100
103
106
107 std::vector<String_type> m_dml_statements;
108
109 void add_element(int element_number, const String_type &element_name,
110 const String_type &element_definition,
111 Element_numbers *element_numbers,
112 Element_definitions *element_definitions) {
113 assert(element_numbers != nullptr && element_definitions != nullptr &&
114 element_numbers->find(element_name) == element_numbers->end() &&
115 element_definitions->find(element_number) ==
116 element_definitions->end());
117
118 (*element_numbers)[element_name] = element_number;
119 (*element_definitions)[element_number] = element_definition;
120 }
121
122 int element_number(const String_type &element_name,
123 const Element_numbers &element_numbers) const {
124 assert(element_numbers.find(element_name) != element_numbers.end());
125 return element_numbers.find(element_name)->second;
126 }
127
128 void get_element_properties(dd::Properties *properties,
129 const Element_numbers &element_numbers,
130 const Element_definitions &element_defs) const;
131
132 bool set_element_properties(const String_type &prop_str,
133 Element_numbers *element_numbers,
134 Element_definitions *element_defs);
135
136 public:
138
140 const String_type &table_name,
141 const String_type &ddl_statement)
142 : m_schema_name(schema_name),
144 m_ddl_statement(ddl_statement) {}
145
146 ~Object_table_definition_impl() override = default;
147
150 }
151
153
154 /**
155 Get the collation which is used for names related to the file
156 system (e.g. a schema name or table name). This collation is
157 case sensitive or not, depending on the setting of lower_case-
158 table_names.
159
160 @return Pointer to CHARSET_INFO.
161 */
162
166 }
167
168 /**
169 Get the collation which is used for the name field in the table.
170 Table collation UTF8_BIN is used when collation for the name field
171 is not specified. Tables using different collation must override this
172 method.
173
174 TODO: Changing table collation is not supporting during upgrade as of now.
175 To support this, static definition of this method should be avoided
176 and should provide a possibility to have different collations for
177 actual and target table definition.
178
179 @return Pointer to CHARSET_INFO.
180 */
181 static const CHARSET_INFO *name_collation() {
183 }
184
185 /**
186 Convert to lowercase if lower_case_table_names == 2. This is needed
187 e.g when reconstructing name keys from a dictionary object in order
188 to remove the object.
189
190 @param src String to possibly convert to lowercase.
191 @param [in,out] buf Buffer for storing lowercase'd string. Supplied
192 by the caller.
193
194 @returns A pointer to the src string if l_c_t_n != 2
195 @returns A pointer to the buf supplied by the caller, into which
196 the src string has been copied and lowercase'd, if l_c_t_n == 2
197 */
198
199 static const char *fs_name_case(const String_type &src, char *buf) {
200 const char *tmp_name = src.c_str();
201 if (lower_case_table_names == 2) {
202 // Lower case table names == 2 is tested on OSX.
203 /* purecov: begin tested */
204 my_stpcpy(buf, tmp_name);
206 tmp_name = buf;
207 /* purecov: end */
208 }
209 return tmp_name;
210 }
211
212 const String_type &get_table_name() const { return m_table_name; }
213
214 void set_table_name(const String_type &name) override { m_table_name = name; }
215
217
218 void add_field(int field_number, const String_type &field_name,
219 const String_type field_definition) override {
220 add_element(field_number, field_name, field_definition, &m_field_numbers,
222 }
223
224 void add_sql_mode_field(int field_number, const String_type &field_name);
225
226 void add_index(int index_number, const String_type &index_name,
227 const String_type &index_definition) override {
228 add_element(index_number, index_name, index_definition, &m_index_numbers,
230 }
231
232 virtual void add_foreign_key(int foreign_key_number,
233 const String_type &foreign_key_name,
234 const String_type &foreign_key_definition) {
235 add_element(foreign_key_number, foreign_key_name, foreign_key_definition,
237 }
238
239 virtual void add_option(int option_number, const String_type &option_name,
240 const String_type &option_definition) {
241 add_element(option_number, option_name, option_definition,
243 }
244
246 m_dml_statements.push_back(statement);
247 }
248
249 virtual int field_number(const String_type &field_name) const {
250 return element_number(field_name, m_field_numbers);
251 }
252
253 virtual int index_number(const String_type &index_name) const {
254 return element_number(index_name, m_index_numbers);
255 }
256
257 virtual int option_number(const String_type &option_name) const {
258 return element_number(option_name, m_option_numbers);
259 }
260
261 String_type get_ddl() const override;
262
263 const std::vector<String_type> &get_dml() const override {
264 return m_dml_statements;
265 }
266
267 void store_into_properties(Properties *table_def_properties) const override;
268
269 virtual bool restore_from_string(const String_type &ddl_statement) {
270 m_ddl_statement = ddl_statement;
271 return false;
272 }
273
274 bool restore_from_properties(const Properties &table_def_properties) override;
275};
276
277///////////////////////////////////////////////////////////////////////////
278
279} // namespace dd
280
281#endif // DD__OBJECT_TABLE_DEFINITION_IMPL_INCLUDED
Definition: object_table_definition_impl.h:44
const String_type & get_table_name() const
Definition: object_table_definition_impl.h:212
std::map< String_type, int > Element_numbers
Definition: object_table_definition_impl.h:46
static bool is_dd_tablespace_encrypted()
Definition: object_table_definition_impl.h:152
void store_into_properties(Properties *table_def_properties) const override
Store the elements of the object table definition into a property object.
Definition: object_table_definition_impl.cc:141
virtual int index_number(const String_type &index_name) const
Definition: object_table_definition_impl.h:253
Element_definitions m_field_definitions
Definition: object_table_definition_impl.h:96
virtual bool restore_from_string(const String_type &ddl_statement)
Definition: object_table_definition_impl.h:269
Label
Definition: object_table_definition_impl.h:50
String_type get_ddl() const override
Get the SQL DDL statement for creating the dictionary table.
Definition: object_table_definition_impl.cc:100
void add_field(int field_number, const String_type &field_name, const String_type field_definition) override
Add a field to the object table definition.
Definition: object_table_definition_impl.h:218
static const char * key(Label label)
Definition: object_table_definition_impl.h:62
std::map< int, String_type > Element_definitions
Definition: object_table_definition_impl.h:47
Element_definitions m_foreign_key_definitions
Definition: object_table_definition_impl.h:102
int element_number(const String_type &element_name, const Element_numbers &element_numbers) const
Definition: object_table_definition_impl.h:122
Element_numbers m_option_numbers
Definition: object_table_definition_impl.h:104
bool set_element_properties(const String_type &prop_str, Element_numbers *element_numbers, Element_definitions *element_defs)
Definition: object_table_definition_impl.cc:59
Element_definitions m_option_definitions
Definition: object_table_definition_impl.h:105
virtual void add_option(int option_number, const String_type &option_name, const String_type &option_definition)
Definition: object_table_definition_impl.h:239
void set_table_name(const String_type &name) override
Set the name of the table.
Definition: object_table_definition_impl.h:214
virtual void add_populate_statement(const String_type &statement)
Definition: object_table_definition_impl.h:245
void set_schema_name(const String_type &name)
Definition: object_table_definition_impl.h:216
static const CHARSET_INFO * fs_name_collation()
Get the collation which is used for names related to the file system (e.g.
Definition: object_table_definition_impl.h:163
virtual int option_number(const String_type &option_name) const
Definition: object_table_definition_impl.h:257
void get_element_properties(dd::Properties *properties, const Element_numbers &element_numbers, const Element_definitions &element_defs) const
Definition: object_table_definition_impl.cc:38
void add_index(int index_number, const String_type &index_name, const String_type &index_definition) override
Add an index to the object table definition.
Definition: object_table_definition_impl.h:226
void add_sql_mode_field(int field_number, const String_type &field_name)
Definition: object_table_definition_impl.cc:88
String_type m_table_name
Definition: object_table_definition_impl.h:91
virtual void add_foreign_key(int foreign_key_number, const String_type &foreign_key_name, const String_type &foreign_key_definition)
Definition: object_table_definition_impl.h:232
static const char * fs_name_case(const String_type &src, char *buf)
Convert to lowercase if lower_case_table_names == 2.
Definition: object_table_definition_impl.h:199
Object_table_definition_impl(const String_type &schema_name, const String_type &table_name, const String_type &ddl_statement)
Definition: object_table_definition_impl.h:139
const std::vector< String_type > & get_dml() const override
Get the SQL DML statements for populating the table.
Definition: object_table_definition_impl.h:263
void add_element(int element_number, const String_type &element_name, const String_type &element_definition, Element_numbers *element_numbers, Element_definitions *element_definitions)
Definition: object_table_definition_impl.h:109
Element_numbers m_index_numbers
Definition: object_table_definition_impl.h:98
std::vector< String_type > m_dml_statements
Definition: object_table_definition_impl.h:107
~Object_table_definition_impl() override=default
static bool s_dd_tablespace_encrypted
Definition: object_table_definition_impl.h:88
bool restore_from_properties(const Properties &table_def_properties) override
Restore the elements of the object table definition from a property object.
Definition: object_table_definition_impl.cc:168
String_type m_ddl_statement
Definition: object_table_definition_impl.h:93
Element_definitions m_index_definitions
Definition: object_table_definition_impl.h:99
Element_numbers m_field_numbers
Definition: object_table_definition_impl.h:95
Element_numbers m_foreign_key_numbers
Definition: object_table_definition_impl.h:101
static const CHARSET_INFO * name_collation()
Get the collation which is used for the name field in the table.
Definition: object_table_definition_impl.h:181
virtual int field_number(const String_type &field_name) const
Definition: object_table_definition_impl.h:249
static void set_dd_tablespace_encrypted(bool is_encrypted)
Definition: object_table_definition_impl.h:148
String_type m_schema_name
Definition: object_table_definition_impl.h:90
The purpose of this interface is to enable retrieving the SQL statements necessary to create and popu...
Definition: object_table_definition.h:48
The Properties class defines an interface for storing key=value pairs, where both key and value may b...
Definition: properties.h:74
static char buf[MAX_BUF]
Definition: conf_to_src.cc:73
A better implementation of the UNIX ctype(3) library.
MYSQL_STRINGS_EXPORT CHARSET_INFO my_charset_utf8mb3_tolower_ci
Definition: ctype-utf8.cc:5829
size_t my_casedn_str(const CHARSET_INFO *cs, char *str)
Definition: m_ctype.h:732
MYSQL_STRINGS_EXPORT CHARSET_INFO my_charset_utf8mb3_bin
Definition: ctype-utf8.cc:5899
static char * my_stpcpy(char *dst, const char *src)
Copy a string from src to dst until (and including) terminating null byte.
Definition: m_string.h:142
uint lower_case_table_names
Definition: mysqld.cc:1345
Definition: buf0block_hint.cc:30
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
bool is_encrypted(const String_type &type)
Definition: dd_table.h:422
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
const char * table_name
Definition: rules_table_service.cc:56
case opt name
Definition: sslopt-case.h:29
Definition: m_ctype.h:421
Definition: mysqlslap.cc:219