1 /* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
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 #include "sql/dd/impl/tables/foreign_key_column_usage.h"
24 
25 #include <new>
26 
27 #include "sql/dd/impl/raw/object_keys.h"       // Parent_id_range_key
28 #include "sql/dd/impl/tables/dd_properties.h"  // TARGET_DD_VERSION
29 #include "sql/dd/impl/types/object_table_definition_impl.h"
30 
31 namespace dd {
32 namespace tables {
33 
instance()34 const Foreign_key_column_usage &Foreign_key_column_usage::instance() {
35   static Foreign_key_column_usage *s_instance = new Foreign_key_column_usage();
36   return *s_instance;
37 }
38 
39 ///////////////////////////////////////////////////////////////////////////
40 
Foreign_key_column_usage()41 Foreign_key_column_usage::Foreign_key_column_usage() {
42   m_target_def.set_table_name("foreign_key_column_usage");
43 
44   m_target_def.add_field(FIELD_FOREIGN_KEY_ID, "FIELD_FOREIGN_KEY_ID",
45                          "foreign_key_id BIGINT UNSIGNED NOT NULL");
46   m_target_def.add_field(FIELD_ORDINAL_POSITION, "FIELD_ORDINAL_POSITION",
47                          "ordinal_position INT UNSIGNED NOT NULL");
48   m_target_def.add_field(FIELD_COLUMN_ID, "FIELD_COLUMN_ID",
49                          "column_id BIGINT UNSIGNED NOT NULL");
50   m_target_def.add_field(FIELD_REFERENCED_COLUMN_NAME,
51                          "FIELD_REFERENCED_COLUMN_NAME",
52                          "referenced_column_name VARCHAR(64) NOT NULL "
53                          "COLLATE utf8_tolower_ci");
54 
55   m_target_def.add_index(INDEX_PK_FOREIGN_KEY_ID_ORDINAL_POSITION,
56                          "INDEX_PK_FOREIGN_KEY_ID_ORDINAL_POSITION",
57                          "PRIMARY KEY(foreign_key_id, ordinal_position)");
58   m_target_def.add_index(INDEX_UK_FOREIGN_KEY_ID_COLUMN_ID,
59                          "INDEX_UK_FOREIGN_KEY_ID_COLUMN_ID",
60                          "UNIQUE KEY(foreign_key_id, column_id, "
61                          "referenced_column_name)");
62   m_target_def.add_index(INDEX_K_COLUMN_ID, "INDEX_K_COLUMN_ID",
63                          "KEY(column_id)");
64 
65   m_target_def.add_foreign_key(FK_FOREIGN_KEY_ID, "FK_FOREIGN_KEY_ID",
66                                "FOREIGN KEY (foreign_key_id) REFERENCES "
67                                "foreign_keys(id)");
68   m_target_def.add_foreign_key(FK_COLUMN_ID, "FK_COLUMN_ID",
69                                "FOREIGN KEY (column_id) REFERENCES "
70                                "columns(id)");
71 }
72 
73 ///////////////////////////////////////////////////////////////////////////
74 
create_key_by_foreign_key_id(Object_id fk_id)75 Object_key *Foreign_key_column_usage::create_key_by_foreign_key_id(
76     Object_id fk_id) {
77   return new (std::nothrow) Parent_id_range_key(
78       INDEX_PK_FOREIGN_KEY_ID_ORDINAL_POSITION, FIELD_FOREIGN_KEY_ID, fk_id);
79 }
80 
81 ///////////////////////////////////////////////////////////////////////////
82 
create_primary_key(Object_id fk_id,int ordinal_position)83 Object_key *Foreign_key_column_usage::create_primary_key(Object_id fk_id,
84                                                          int ordinal_position) {
85   return new (std::nothrow) Composite_pk(
86       INDEX_PK_FOREIGN_KEY_ID_ORDINAL_POSITION, FIELD_FOREIGN_KEY_ID, fk_id,
87       FIELD_ORDINAL_POSITION, ordinal_position);
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////
91 
92 }  // namespace tables
93 }  // namespace dd
94