1 /* Copyright (c) 2014, 2018, 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__ENTITY_OBJECT_IMPL_INCLUDED 24 #define DD__ENTITY_OBJECT_IMPL_INCLUDED 25 26 #include "sql/dd/impl/raw/raw_record.h" 27 #include "sql/dd/impl/types/weak_object_impl.h" // Weak_object_impl 28 #include "sql/dd/object_id.h" 29 #include "sql/dd/sdi_fwd.h" 30 #include "sql/dd/string_type.h" 31 #include "sql/dd/types/entity_object.h" // Entity_object 32 #include "sql/dd/types/weak_object.h" 33 34 namespace dd { 35 36 /////////////////////////////////////////////////////////////////////////// 37 38 class Object_key; 39 class Sdi_rcontext; 40 class Sdi_wcontext; 41 42 class Entity_object_impl : virtual public Entity_object, 43 public Weak_object_impl { 44 public: Entity_object_impl()45 Entity_object_impl() : m_id(INVALID_OBJECT_ID), m_has_new_primary_key(true) {} 46 47 public: id()48 virtual Object_id id() const override { return m_id; } 49 set_id(Object_id id)50 /* non-virtual */ void set_id(Object_id id) { 51 m_id = id; 52 fix_has_new_primary_key(); 53 } 54 55 /* purecov: begin deadcode */ is_persistent()56 virtual bool is_persistent() const override { 57 return (m_id != INVALID_OBJECT_ID); 58 } 59 /* purecov: end */ 60 name()61 virtual const String_type &name() const override { return m_name; } 62 set_name(const String_type & name)63 virtual void set_name(const String_type &name) override { m_name = name; } 64 65 virtual Object_key *create_primary_key() const override; 66 has_new_primary_key()67 virtual bool has_new_primary_key() const override { 68 return m_has_new_primary_key; 69 } 70 impl()71 virtual Entity_object_impl *impl() override { return this; } impl()72 virtual const Entity_object_impl *impl() const override { return this; } 73 74 protected: 75 virtual void set_primary_key_value(const Raw_new_record &r) override; 76 fix_has_new_primary_key()77 virtual void fix_has_new_primary_key() override { 78 m_has_new_primary_key = (m_id == INVALID_OBJECT_ID); 79 } 80 81 void restore_id(const Raw_record &r, int field_idx); 82 void restore_name(const Raw_record &r, int field_idx); 83 84 bool store_id(Raw_record *r, int field_idx); 85 bool store_name(Raw_record *r, int field_idx); 86 bool store_name(Raw_record *r, int field_idx, bool is_null); 87 88 void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const; 89 bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val); 90 91 private: 92 // NOTE: ID and Name attributes *must* remain private so that we can track 93 // changes in them and prevent abuse. 94 95 Object_id m_id; 96 97 String_type m_name; 98 99 /** 100 Indicates that object is guaranteed to have ID which doesn't exist in 101 database because it will be or just was generated using auto-increment. 102 Main difference of this member from result of m_id == INVALID_OBJECT_ID 103 check is that we delay resetting of this flag until end of store() method 104 while m_id is updated right after object was inserted into the table. 105 This is necessary to let entity's children figure out that their parent 106 has new ID which was not used before (and hence their primary keys based 107 on this ID will be new too) while still giving access to the exact value 108 of new ID. 109 */ 110 bool m_has_new_primary_key; 111 112 protected: 113 // The generated copy constructor could have been used, 114 // but by adding this we force derived classes which define 115 // their own copy constructor to also invoke the Entity_object_impl 116 // copy constructor in the initializer list. 117 // Note that we must copy the m_has_new_primary_key property to make sure 118 // the clone is handled correctly if storing it persistently as part of 119 // updating a DD object. Entity_object_impl(const Entity_object_impl & src)120 Entity_object_impl(const Entity_object_impl &src) 121 : Weak_object(src), 122 m_id(src.m_id), 123 m_name(src.m_name), 124 m_has_new_primary_key(src.m_has_new_primary_key) {} 125 }; 126 127 /////////////////////////////////////////////////////////////////////////// 128 129 } // namespace dd 130 131 #endif // DD__ENTITY_OBJECT_IMPL_INCLUDED 132