HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ORMField.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_ORMField.h
7  *
8  * COMMENTS:
9  *
10  *
11  */
12 
13 #ifndef __UT_ORMFIELD_H__
14 #define __UT_ORMFIELD_H__
15 
16 #include "UT_API.h"
17 
18 #include "UT_StringHolder.h"
19 #include "UT_ErrorCode.h"
20 #include "UT_DateTimeField.h"
21 #include "UT_WorkBuffer.h"
22 #include "UT_SQL.h"
23 #include "UT_Array.h"
24 #include "UT_Optional.h"
25 #include "UT_SGuid.h"
26 #include "UT_ORMColumn.h"
27 
28 #include <variant>
29 
30 class UT_JSONValue;
31 class UT_SqlStatement;
32 class UT_SqlOrm;
33 class UT_ORMColumn;
34 
36 {
37 public:
38  virtual ~UT_IORMFieldAdapter() = default;
39  UT_IORMFieldAdapter(const UT_IORMFieldAdapter&) = default;
41 
42  virtual UT_ORMColumn::Type sqlType() const = 0;
43  virtual void bind(
44  void* obj,
45  UT_SqlStatement& cursor,
46  int bind_index,
47  UT_ErrorCode& ec)
48  = 0;
49  virtual void load(
50  void* obj,
51  UT_SqlStatement& cursor,
52  int index,
53  UT_ErrorCode& ec)
54  = 0;
55  virtual void onDelete(
56  void* obj,
57  UT_ORMColumn::OnDelete ondelete,
58  UT_ErrorCode& ec)
59  {
60  }
61 
62 protected:
63  UT_IORMFieldAdapter() = default;
64 };
65 
67 {
68 public:
70  const UT_StringHolder& name,
72  unsigned props = UT_ORMColumn::Properties::Empty,
73  UT_ORMColumn::OnDelete ondelete = UT_ORMColumn::OnDelete::DoNothing)
74  : UT_ORMColumn(name, adapter->sqlType(), props, ondelete)
75  , myFieldPtr(std::move(adapter))
76  {
77  }
79  {
80  return myFieldPtr.get();
81  }
83  {
84  return SYSconst_cast(this)->myFieldPtr.get();
85  }
86 protected:
88 };
89 
90 template <typename Cls, typename MemberT>
92 {
93 public:
94  using model_t = Cls;
95  using field_t = MemberT;
96 
97  UT_ORMBasicFieldAdapter(MemberT Cls::*member_ptr) : myMemberPtr(member_ptr)
98  {
99  }
100 
102  const UT_StringHolder& name,
103  MemberT Cls::*member_ptr,
104  unsigned props)
105  {
106  auto adapter = UTmakeUnique<UT_ORMBasicFieldAdapter<Cls, MemberT>>(
107  member_ptr);
108  UT_ORMFieldColumn col(name, std::move(adapter), props);
109  return col;
110  }
111 
112  UT_ORMColumn::Type sqlType() const override
113  {
114  return UTsqlOrmColumnType<MemberT>();
115  }
116  void bind(void* obj, UT_SqlStatement& stmt, int index, UT_ErrorCode& ec)
117  override
118  {
119  MemberT& v = this->field(obj);
120  stmt.bind(index, v);
121  ec = stmt.getError();
122  }
123  void load(void* obj, UT_SqlStatement& stmt, int index, UT_ErrorCode& ec)
124  override
125  {
126  MemberT& v = this->field(obj);
127  v = stmt.get<MemberT>(index);
128  ec = stmt.getError();
129  }
130  void setValue(void* obj, const MemberT& t)
131  {
132  field(obj) = t;
133  }
134  MemberT& getValue(void* obj)
135  {
136  return field(obj);
137  }
138 
139 protected:
140  MemberT& field(void* obj)
141  {
142  Cls* cls = static_cast<Cls*>(obj);
143  return (cls->*myMemberPtr);
144  }
145 
146 private:
147  MemberT Cls::*myMemberPtr;
148 
149 };
150 template <typename Cls>
152 template <typename Cls>
154 template <typename Cls>
156 template <typename Cls>
158 
159 template <typename ModelT>
161 {
162 public:
163  using model_t = ModelT;
164  using foreign_key_t = typename ModelT::Meta::primary_key_t;
165 
166  UT_ORMForeignKeyField() = default;
168  myID(pk)
169  {}
170 
171  bool operator==(const UT_ORMForeignKeyField& rhs) const
172  {
173  return myID == rhs.myID;
174  }
175  bool operator!=(const UT_ORMForeignKeyField& rhs) const
176  {
177  return !(*this == rhs);
178  }
179 
180  ModelT* get()
181  {
182  if (!myValue)
183  {
184  UT_ErrorCode ec;
185  load_(ec);
186  }
187  UT_ASSERT(myValue);
188  return myValue.get();
189  }
190 
192  {
193  return myID;
194  }
195  void setId(const foreign_key_t& id)
196  {
197  myID = id;
198  myValue.reset();
199  }
200  void setValue(const ModelT& value)
201  {
202  using foreign_field_t = UT_ORMBasicFieldAdapter<ModelT, foreign_key_t>;
203 
204  auto&& meta = ModelT::metaInfo();
205  UT_ErrorCode ec;
206  UT_IORMFieldAdapter* adapter = meta.primaryKey()->adapter();
207  if (auto fadp = dynamic_cast<foreign_field_t*>(adapter); fadp)
208  {
209  myID = fadp->getValue(SYSconst_cast(&value));
210  myValue = UTmakeUnique<ModelT>(value);
211  }
212  }
213 
214  void clear()
215  {
216  myID = foreign_key_t();
217  myValue.reset();
218  }
219 
220 private:
221  void load_(UT_ErrorCode &ec)
222  {
223  if (auto&& opt = ModelT::fetch(myID, ec); opt)
224  {
225  myValue = UTmakeUnique<ModelT>(opt.value());
226  }
227  }
228 
229  foreign_key_t myID;
230  UT_UniquePtr<ModelT> myValue;
231 };
232 
233 template <typename Cls, typename ForeignKey>
235 {
236 public:
237  using model_t = typename ForeignKey::model_t;
238  using field_t = ForeignKey;
239  using id_t = typename ForeignKey::foreign_key_t;
240 
241  UT_ORMForeignKeyFieldAdapter(ForeignKey Cls::*member_ptr)
242  : myMemberPtr(member_ptr)
243  {}
244 
246  const UT_StringHolder& name,
247  ForeignKey Cls::*member_ptr,
248  unsigned props,
249  UT_ORMColumn::OnDelete ondelete)
250  {
251  auto&& meta = model_t::metaInfo();
252  const UT_ORMFieldColumn* pk_col = meta.primaryKey();
253 
254  auto adapter
255  = UTmakeUnique<UT_ORMForeignKeyFieldAdapter<Cls, ForeignKey>>(
256  member_ptr);
257  UT_ORMFieldColumn col(name, std::move(adapter), props);
258  col.setAsForeignKey(meta.tableName(), pk_col->name());
259  col.setOnDelete(ondelete);
260 
261  return col;
262  }
263 
264  UT_ORMColumn::Type sqlType() const override
265  {
266  return UTsqlOrmColumnType<id_t>();
267  }
268  void bind(
269  void* obj,
270  UT_SqlStatement& stmt,
271  int bind_index,
272  UT_ErrorCode& ec) override
273  {
274  stmt.bind(bind_index, field(obj).id());
275  ec = stmt.getError();
276  }
277  void load(void* obj, UT_SqlStatement& stmt, int index, UT_ErrorCode& ec)
278  override
279  {
280  field(obj).setId(stmt.get<id_t>(index));
281  ec = stmt.getError();
282  }
283  void onDelete(void* obj, UT_ORMColumn::OnDelete ondelete, UT_ErrorCode& ec)
284  override
285  {
286  if (ondelete != UT_ORMColumn::OnDelete::DoNothing)
287  {
288  ForeignKey& f = field(obj);
289  f.clear();
290  }
291  }
292 
293 protected:
294  ForeignKey& field(void* obj)
295  {
296  Cls* cls = static_cast<Cls*>(obj);
297  return (cls->*myMemberPtr);
298  }
299 
300 private:
301  ForeignKey Cls::*myMemberPtr;
302 };
303 
304 #endif // __UT_ORMFIELD_H__
305 
ForeignKey & field(void *obj)
Definition: UT_ORMField.h:294
foreign_key_t id()
Definition: UT_ORMField.h:191
UT_IORMFieldAdapter * adapter() const
Definition: UT_ORMField.h:82
const UT_StringHolder & name() const
Definition: UT_ORMColumn.h:83
const UT_ErrorCode & getError() const
Definition: UT_SQL.h:615
UT_ORMColumn::Type sqlType() const override
Definition: UT_ORMField.h:112
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
typename ModelT::Meta::primary_key_t foreign_key_t
Definition: UT_ORMField.h:164
bool bind(int idx, null_tag_t)
Definition: UT_SQL.h:490
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
void bind(void *obj, UT_SqlStatement &stmt, int bind_index, UT_ErrorCode &ec) override
Definition: UT_ORMField.h:268
virtual void onDelete(void *obj, UT_ORMColumn::OnDelete ondelete, UT_ErrorCode &ec)
Definition: UT_ORMField.h:55
#define UT_API
Definition: UT_API.h:14
UT_ORMForeignKeyField()=default
typename ForeignKey::foreign_key_t id_t
Definition: UT_ORMField.h:239
void setValue(const ModelT &value)
Definition: UT_ORMField.h:200
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
bool operator==(const UT_ORMForeignKeyField &rhs) const
Definition: UT_ORMField.h:171
UT_ORMColumn::Type sqlType() const override
Definition: UT_ORMField.h:264
void load(void *obj, UT_SqlStatement &stmt, int index, UT_ErrorCode &ec) override
Definition: UT_ORMField.h:277
UT_ORMForeignKeyField(foreign_key_t pk)
Definition: UT_ORMField.h:167
GLfloat f
Definition: glcorearb.h:1926
typename ForeignKey::model_t model_t
Definition: UT_ORMField.h:237
UT_IORMFieldAdapter * adapter()
Definition: UT_ORMField.h:78
MemberT & field(void *obj)
Definition: UT_ORMField.h:140
void bind(void *obj, UT_SqlStatement &stmt, int index, UT_ErrorCode &ec) override
Definition: UT_ORMField.h:116
static UT_ORMFieldColumn createColumn(const UT_StringHolder &name, ForeignKey Cls::*member_ptr, unsigned props, UT_ORMColumn::OnDelete ondelete)
Definition: UT_ORMField.h:245
MemberT & getValue(void *obj)
Definition: UT_ORMField.h:134
GLuint const GLchar * name
Definition: glcorearb.h:786
static UT_ORMFieldColumn createColumn(const UT_StringHolder &name, MemberT Cls::*member_ptr, unsigned props)
Definition: UT_ORMField.h:101
UT_ORMFieldColumn(const UT_StringHolder &name, UT_UniquePtr< UT_IORMFieldAdapter > adapter, unsigned props=UT_ORMColumn::Properties::Empty, UT_ORMColumn::OnDelete ondelete=UT_ORMColumn::OnDelete::DoNothing)
Definition: UT_ORMField.h:69
GLdouble t
Definition: glad.h:2397
std::error_code UT_ErrorCode
Definition: UT_ErrorCode.h:20
UT_ORMBasicFieldAdapter(MemberT Cls::*member_ptr)
Definition: UT_ORMField.h:97
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
UT_UniquePtr< UT_IORMFieldAdapter > myFieldPtr
Definition: UT_ORMField.h:87
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: core.h:1131
void setValue(void *obj, const MemberT &t)
Definition: UT_ORMField.h:130
bool operator!=(const UT_ORMForeignKeyField &rhs) const
Definition: UT_ORMField.h:175
void onDelete(void *obj, UT_ORMColumn::OnDelete ondelete, UT_ErrorCode &ec) override
Definition: UT_ORMField.h:283
T get(int idx) const
Definition: UT_SQL.h:576
UT_ORMForeignKeyFieldAdapter(ForeignKey Cls::*member_ptr)
Definition: UT_ORMField.h:241
void setId(const foreign_key_t &id)
Definition: UT_ORMField.h:195
void load(void *obj, UT_SqlStatement &stmt, int index, UT_ErrorCode &ec) override
Definition: UT_ORMField.h:123
GLenum GLuint GLsizei const GLenum * props
Definition: glcorearb.h:2525