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