1 /* Copyright (c) 2014, 2020, 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 #ifndef DD__FOREIGN_KEY_INCLUDED 24 #define DD__FOREIGN_KEY_INCLUDED 25 26 #include "sql/dd/collection.h" // dd::Collection 27 #include "sql/dd/sdi_fwd.h" // dd::Sdi_wcontext 28 #include "sql/dd/types/entity_object.h" // dd::Entity_object 29 #include "sql/dd/types/object_table.h" 30 31 namespace dd { 32 33 /////////////////////////////////////////////////////////////////////////// 34 35 class Foreign_key_element; 36 class Foreign_key_impl; 37 class Index; 38 class Table; 39 40 namespace tables { 41 class Foreign_keys; 42 } 43 44 /////////////////////////////////////////////////////////////////////////// 45 46 class Foreign_key : virtual public Entity_object { 47 public: 48 typedef Collection<Foreign_key_element *> Foreign_key_elements; 49 typedef Foreign_key_impl Impl; 50 typedef tables::Foreign_keys DD_table; 51 52 public: 53 enum enum_rule { 54 RULE_NO_ACTION = 1, 55 RULE_RESTRICT, 56 RULE_CASCADE, 57 RULE_SET_NULL, 58 RULE_SET_DEFAULT 59 }; 60 61 enum enum_match_option { 62 OPTION_NONE = 1, 63 OPTION_PARTIAL, 64 OPTION_FULL, 65 }; 66 67 public: ~Foreign_key()68 virtual ~Foreign_key() {} 69 70 ///////////////////////////////////////////////////////////////////////// 71 // parent table. 72 ///////////////////////////////////////////////////////////////////////// 73 74 virtual const Table &table() const = 0; 75 76 virtual Table &table() = 0; 77 78 ///////////////////////////////////////////////////////////////////////// 79 // unique_constraint 80 ///////////////////////////////////////////////////////////////////////// 81 82 // Note that setting "" as unique constraint name is interpreted as NULL 83 // when storing this to the DD tables. And correspondingly, if NULL is 84 // stored, and the dd::Foreign_key is fetched from the tables, then 85 // unique_constraint_name() will return "". 86 87 virtual const String_type &unique_constraint_name() const = 0; 88 virtual void set_unique_constraint_name(const String_type &name) = 0; 89 90 ///////////////////////////////////////////////////////////////////////// 91 // match_option. 92 ///////////////////////////////////////////////////////////////////////// 93 94 virtual enum_match_option match_option() const = 0; 95 virtual void set_match_option(enum_match_option match_option) = 0; 96 97 ///////////////////////////////////////////////////////////////////////// 98 // update_rule. 99 ///////////////////////////////////////////////////////////////////////// 100 101 virtual enum_rule update_rule() const = 0; 102 virtual void set_update_rule(enum_rule update_rule) = 0; 103 104 ///////////////////////////////////////////////////////////////////////// 105 // delete_rule. 106 ///////////////////////////////////////////////////////////////////////// 107 108 virtual enum_rule delete_rule() const = 0; 109 virtual void set_delete_rule(enum_rule delete_rule) = 0; 110 111 ///////////////////////////////////////////////////////////////////////// 112 // the catalog name of the referenced table. 113 ///////////////////////////////////////////////////////////////////////// 114 115 virtual const String_type &referenced_table_catalog_name() const = 0; 116 virtual void set_referenced_table_catalog_name(const String_type &name) = 0; 117 118 ///////////////////////////////////////////////////////////////////////// 119 // the schema name of the referenced table. 120 ///////////////////////////////////////////////////////////////////////// 121 122 virtual const String_type &referenced_table_schema_name() const = 0; 123 virtual void set_referenced_table_schema_name(const String_type &name) = 0; 124 125 ///////////////////////////////////////////////////////////////////////// 126 // the name of the referenced table. 127 ///////////////////////////////////////////////////////////////////////// 128 129 virtual const String_type &referenced_table_name() const = 0; 130 virtual void set_referenced_table_name(const String_type &name) = 0; 131 132 ///////////////////////////////////////////////////////////////////////// 133 // Foreign key element collection. 134 ///////////////////////////////////////////////////////////////////////// 135 136 virtual Foreign_key_element *add_element() = 0; 137 138 virtual const Foreign_key_elements &elements() const = 0; 139 140 virtual Foreign_key_elements *elements() = 0; 141 142 /** 143 Converts *this into json. 144 145 Converts all member variables that are to be included in the sdi 146 into json by transforming them appropriately and passing them to 147 the rapidjson writer provided. 148 149 @param wctx opaque context for data needed by serialization 150 @param w rapidjson writer which will perform conversion to json 151 152 */ 153 154 virtual void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const = 0; 155 156 /** 157 Re-establishes the state of *this by reading sdi information from 158 the rapidjson DOM subobject provided. 159 160 Cross-references encountered within this object are tracked in 161 sdictx, so that they can be updated when the entire object graph 162 has been established. 163 164 @param rctx stores book-keeping information for the 165 deserialization process 166 @param val subobject of rapidjson DOM containing json 167 representation of this object 168 @retval false success 169 @retval true failure 170 */ 171 172 virtual bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val) = 0; 173 }; 174 175 /////////////////////////////////////////////////////////////////////////// 176 177 // Class to hold de-normalized information about FKs to be added 178 // to dd::Table objects representing FK parents. 179 180 class Foreign_key_parent { 181 public: Foreign_key_parent()182 Foreign_key_parent() 183 : m_child_schema_name(), 184 m_child_table_name(), 185 m_fk_name(), 186 m_update_rule(Foreign_key::enum_rule::RULE_NO_ACTION), 187 m_delete_rule(Foreign_key::enum_rule::RULE_NO_ACTION) {} 188 child_schema_name()189 const String_type &child_schema_name() const { return m_child_schema_name; } 190 set_child_schema_name(const String_type & child_schema_name)191 void set_child_schema_name(const String_type &child_schema_name) { 192 m_child_schema_name = child_schema_name; 193 } 194 child_table_name()195 const String_type &child_table_name() const { return m_child_table_name; } 196 set_child_table_name(const String_type & child_table_name)197 void set_child_table_name(const String_type &child_table_name) { 198 m_child_table_name = child_table_name; 199 } 200 fk_name()201 const String_type &fk_name() const { return m_fk_name; } 202 set_fk_name(const String_type & fk_name)203 void set_fk_name(const String_type &fk_name) { m_fk_name = fk_name; } 204 update_rule()205 Foreign_key::enum_rule update_rule() const { return m_update_rule; } 206 set_update_rule(Foreign_key::enum_rule update_rule)207 void set_update_rule(Foreign_key::enum_rule update_rule) { 208 m_update_rule = update_rule; 209 } 210 delete_rule()211 Foreign_key::enum_rule delete_rule() const { return m_delete_rule; } 212 set_delete_rule(Foreign_key::enum_rule delete_rule)213 void set_delete_rule(Foreign_key::enum_rule delete_rule) { 214 m_delete_rule = delete_rule; 215 } 216 217 private: 218 String_type m_child_schema_name; 219 String_type m_child_table_name; 220 String_type m_fk_name; 221 Foreign_key::enum_rule m_update_rule; 222 Foreign_key::enum_rule m_delete_rule; 223 }; 224 225 /////////////////////////////////////////////////////////////////////////// 226 227 } // namespace dd 228 229 #endif // DD__FOREIGN_KEY_INCLUDED 230