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 #include "sql/dd/impl/types/entity_object_impl.h"
24 
25 #include <new>
26 
27 #include "my_rapidjson_size_t.h"  // IWYU pragma: keep
28 
29 #include <rapidjson/document.h>
30 #include <rapidjson/prettywriter.h>
31 
32 #include "m_string.h"
33 #include "sql/dd/impl/raw/object_keys.h"  // dd::Primary_id_key
34 #include "sql/dd/impl/raw/raw_record.h"   // dd::Raw_new_record
35 #include "sql/dd/impl/sdi_impl.h"         // sdi read/write functions
36 
37 namespace dd {
38 
39 ///////////////////////////////////////////////////////////////////////////
40 
41 class Object_key;
42 class Sdi_rcontext;
43 class Sdi_wcontext;
44 
set_primary_key_value(const Raw_new_record & r)45 void Entity_object_impl::set_primary_key_value(const Raw_new_record &r) {
46   /*
47     Don't set primary key value if object has one assigned already.
48     Raw_new_record::get_insert_id() doesn't work correctly if value for
49     auto-increment column was explicitly provided.
50 
51     Delay updating of the m_has_new_primary_key flag until end of store()
52     method. This is necessary for children's store() methods to know that
53     that parent entity has new ID which was not used before (and hence
54     children primary keys based on this ID will be new too).
55   */
56   if (m_id == INVALID_OBJECT_ID) m_id = r.get_insert_id();
57 }
58 
59 ///////////////////////////////////////////////////////////////////////////
60 
create_primary_key() const61 Object_key *Entity_object_impl::create_primary_key() const {
62   return new (std::nothrow) Primary_id_key(id());
63 }
64 
65 ///////////////////////////////////////////////////////////////////////////
66 
restore_id(const Raw_record & r,int field_idx)67 void Entity_object_impl::restore_id(const Raw_record &r, int field_idx) {
68   m_id = r.read_int(field_idx);
69   fix_has_new_primary_key();
70 }
71 
72 ///////////////////////////////////////////////////////////////////////////
73 
restore_name(const Raw_record & r,int field_idx)74 void Entity_object_impl::restore_name(const Raw_record &r, int field_idx) {
75   m_name = r.read_str(field_idx);
76 }
77 ///////////////////////////////////////////////////////////////////////////
78 
store_id(Raw_record * r,int field_idx)79 bool Entity_object_impl::store_id(Raw_record *r, int field_idx) {
80   return r->store_pk_id(field_idx, m_id);
81 }
82 
83 ///////////////////////////////////////////////////////////////////////////
84 
store_name(Raw_record * r,int field_idx,bool is_null)85 bool Entity_object_impl::store_name(Raw_record *r, int field_idx,
86                                     bool is_null) {
87   return r->store(field_idx, m_name, is_null);
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////
91 
store_name(Raw_record * r,int field_idx)92 bool Entity_object_impl::store_name(Raw_record *r, int field_idx) {
93   return store_name(r, field_idx, false);
94 }
95 
96 ///////////////////////////////////////////////////////////////////////////
97 
serialize(Sdi_wcontext *,Sdi_writer * w) const98 void Entity_object_impl::serialize(Sdi_wcontext *, Sdi_writer *w) const {
99   write(w, m_name, STRING_WITH_LEN("name"));
100 }
101 
102 ///////////////////////////////////////////////////////////////////////////
103 
deserialize(Sdi_rcontext *,const RJ_Value & val)104 bool Entity_object_impl::deserialize(Sdi_rcontext *, const RJ_Value &val) {
105   return read(&m_name, val, "name");
106 }
107 
108 ///////////////////////////////////////////////////////////////////////////
109 }  // namespace dd
110