HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ORMColumn.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_ORMColumn.h
7  *
8  * COMMENTS:
9  *
10  *
11  */
12 
13 #ifndef __UT_ORMCOLUMN_H__
14 #define __UT_ORMCOLUMN_H__
15 
16 #include "UT_API.h"
17 
18 #include "UT_StringArray.h"
19 #include "UT_StringHolder.h"
20 #include "UT_DateTimeField.h"
21 #include "UT_ORMColumnType.h"
22 #include "UT_SharedPtr.h"
23 
24 class UT_SqlStatement;
25 class UT_ORMModelMeta;
26 
28 {
29 public:
31 
33  {
34  Empty = 0,
35  PrimaryKey = 1 << 1,
36  Unique = 1 << 2,
37  NotNull = 1 << 3,
38  ForeignKey = 1 << 4,
39  AutoIncrement = 1 << 5,
40  ManyToMany = 1 << 6
41  };
42 
43  enum OnDelete
44  {
45  DoNothing = 0,
47  SetNull
48  };
49 
50  UT_ORMColumn() = default;
52  const UT_StringHolder& name,
53  Type type,
54  unsigned props = Properties::Empty,
55  OnDelete on_delete = OnDelete::DoNothing)
56  : myName(name), myType(type), myProperties(props), myOnDelete(on_delete)
57  {
58  }
59 
60  bool operator==(const UT_ORMColumn& rhs) const
61  {
62  return myName == rhs.myName && myType == rhs.myType
63  && myProperties == rhs.myProperties &&
64  myOnDelete == rhs.myOnDelete;
65  }
66  bool operator!=(const UT_ORMColumn& rhs) const { return !(*this == rhs); }
67 
68  void sql(const UT_SqlStatement& stmt, UT_WorkBuffer& wbuf);
69 
70  Type type() const { return myType; }
72  {
73  myName = name;
74  return *this;
75  }
76  const UT_StringHolder& name() const { return myName; }
77  UT_ORMColumn& setNotNull(bool not_null)
78  {
79  if (not_null)
80  myProperties |= Properties::NotNull;
81  else
82  myProperties &= ~Properties::NotNull;
83  return *this;
84  }
85  bool isNotNull() const { return myProperties & Properties::NotNull; }
87  {
88  if (pk)
89  myProperties |= Properties::PrimaryKey;
90  else
91  myProperties &= ~Properties::PrimaryKey;
92  return *this;
93  }
94  bool isPrimaryKey() const
95  {
96  return myProperties & Properties::PrimaryKey;
97  }
99  {
100  if (upk)
101  myProperties |= Properties::Unique;
102  else
103  myProperties &= ~Properties::Unique;
104  return *this;
105  }
106  bool isUnique() const { return myProperties & Properties::Unique; }
108  {
109  if (inc)
110  myProperties |= Properties::AutoIncrement;
111  else
112  myProperties &= ~Properties::AutoIncrement;
113  return *this;
114  }
115  bool isAutoIncrement() const
116  {
117  return myProperties & Properties::AutoIncrement;
118  }
119  bool isTableColumn() const { return !isManyToMany(); }
120  bool isLocal() const { return !isManyToMany() && !isForeignKey(); }
121 
122  unsigned properties() const { return myProperties; }
123 
124  OnDelete onDelete() const { return myOnDelete; }
125  void setOnDelete(OnDelete ondelete) { myOnDelete = ondelete; }
126 
127  const UT_StringHolder& typeString(const UT_SqlStatement& cursor) const;
128  /// This is only a humar readable label. If you want the actual typename
129  /// you have to use UT_SqlStatement as the underlying typename may be
130  /// different for various sql backends.
131  static const UT_StringHolder& typeToDisplayName(Type type);
132 
133  bool isForeignKey() const { return myProperties & Properties::ForeignKey; }
134  bool isManyToMany() const { return myProperties & Properties::ManyToMany; }
136  const UT_StringHolder& foreign_model,
137  const UT_StringHolder& table_name,
138  const UT_StringHolder& col,
139  UT_ORMColumn::OnDelete ondelete = UT_ORMColumn::OnDelete::Cascade,
140  const UT_StringHolder& related_name = UT_StringHolder::theEmptyString)
141  {
142  myProperties |= ForeignKey;
143  myForeignModelName = foreign_model;
144  myForeignTable = table_name;
145  myForeignColumns.clear();
146  myForeignColumns.emplace_back(col);
147  myOnDelete = ondelete;
148  setRelatedName(related_name);
149  }
151  const UT_StringHolder& foreign_model,
152  const UT_StringHolder& table_name,
153  const UT_StringArray& cols,
154  UT_ORMColumn::OnDelete ondelete = UT_ORMColumn::OnDelete::Cascade,
155  const UT_StringHolder& related_name = UT_StringHolder::theEmptyString)
156  {
157  myProperties |= ForeignKey;
158  myForeignModelName = foreign_model;
159  myForeignTable = table_name;
160  myForeignColumns = cols;
161  myOnDelete = ondelete;
162  setRelatedName(related_name);
163  }
164  void setAsForeignKey(
165  const UT_ORMModelMeta& meta,
167  UT_ORMColumn::OnDelete = UT_ORMColumn::OnDelete::Cascade,
168  const UT_StringHolder& related_name = UT_StringHolder::theEmptyString);
169  const UT_StringHolder& foreignTable() const { return myForeignTable; }
171  {
172  return myForeignModelName;
173  }
175  {
176  return myForeignColumns[0];
177  }
178  const UT_StringArray& foreignColumns() const { return myForeignColumns; }
179  bool isForeignKeyToModel(const UT_ORMModelMeta& model) const;
180  void setRelatedName(const UT_StringHolder& related);
181  const UT_StringHolder& relatedName() const { return myRelated; }
182 
183 protected:
184  unsigned myProperties = Properties::Empty;
185  OnDelete myOnDelete = OnDelete::DoNothing;
188 
193 };
194 
195 template <>
196 inline UT_ORMColumn::Type
198 {
200 }
201 template <>
202 inline UT_ORMColumn::Type
204 {
206 }
207 template <>
208 inline UT_ORMColumn::Type
210 {
212 }
213 template <>
214 inline UT_ORMColumn::Type
216 {
218 }
219 
220 template<>
221 inline UT_ORMColumn::Type
223 {
225 }
226 
227 template<>
228 inline UT_ORMColumn::Type
230 {
231  return UT_ORMColumn::Type::DateTime;
232 }
233 
234 #endif // __UT_ORMCOLUMN_H__
UT_StringHolder myName
Definition: UT_ORMColumn.h:187
const UT_StringHolder & name() const
Definition: UT_ORMColumn.h:76
UT_ORMColumn(const UT_StringHolder &name, Type type, unsigned props=Properties::Empty, OnDelete on_delete=OnDelete::DoNothing)
Definition: UT_ORMColumn.h:51
UT_ORMColumn::Type UTsqlOrmColumnType< const char * >()
Definition: UT_ORMColumn.h:215
UT_ORMColumn & setAutoIncrement(bool inc)
Definition: UT_ORMColumn.h:107
UT_StringHolder myForeignModelName
Definition: UT_ORMColumn.h:190
UT_ORMColumn::Type UTsqlOrmColumnType< int >()
Definition: UT_ORMColumn.h:197
OnDelete myOnDelete
Definition: UT_ORMColumn.h:185
void setOnDelete(OnDelete ondelete)
Definition: UT_ORMColumn.h:125
const UT_StringHolder & foreignTable() const
Definition: UT_ORMColumn.h:169
SYS_FORCE_INLINE void clear()
UT_ORMColumn::Type UTsqlOrmColumnType< UT_StringHolder >()
Definition: UT_ORMColumn.h:209
bool isManyToMany() const
Definition: UT_ORMColumn.h:134
UT_ORMColumnType
UT_StringArray myForeignColumns
Definition: UT_ORMColumn.h:191
void setAsForeignKey(const UT_StringHolder &foreign_model, const UT_StringHolder &table_name, const UT_StringHolder &col, UT_ORMColumn::OnDelete ondelete=UT_ORMColumn::OnDelete::Cascade, const UT_StringHolder &related_name=UT_StringHolder::theEmptyString)
Definition: UT_ORMColumn.h:135
#define UT_API
Definition: UT_API.h:14
bool isTableColumn() const
Definition: UT_ORMColumn.h:119
bool isUnique() const
Definition: UT_ORMColumn.h:106
UT_StringHolder myForeignTable
Definition: UT_ORMColumn.h:189
bool isForeignKey() const
Definition: UT_ORMColumn.h:133
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
bool isAutoIncrement() const
Definition: UT_ORMColumn.h:115
void setAsForeignKey(const UT_StringHolder &foreign_model, const UT_StringHolder &table_name, const UT_StringArray &cols, UT_ORMColumn::OnDelete ondelete=UT_ORMColumn::OnDelete::Cascade, const UT_StringHolder &related_name=UT_StringHolder::theEmptyString)
Definition: UT_ORMColumn.h:150
Type type() const
Definition: UT_ORMColumn.h:70
bool operator!=(const UT_ORMColumn &rhs) const
Definition: UT_ORMColumn.h:66
UT_ORMColumn & setUnique(bool upk)
Definition: UT_ORMColumn.h:98
bool isLocal() const
Definition: UT_ORMColumn.h:120
static const UT_StringHolder theEmptyString
UT_ORMColumn & setNotNull(bool not_null)
Definition: UT_ORMColumn.h:77
UT_ORMColumn & setName(const UT_StringHolder &name)
Definition: UT_ORMColumn.h:71
UT_StringHolder myRelated
Definition: UT_ORMColumn.h:192
GLuint const GLchar * name
Definition: glcorearb.h:786
bool operator==(const UT_ORMColumn &rhs) const
Definition: UT_ORMColumn.h:60
const UT_StringHolder & foreignTableModelName() const
Definition: UT_ORMColumn.h:170
OnDelete onDelete() const
Definition: UT_ORMColumn.h:124
bool isNotNull() const
Definition: UT_ORMColumn.h:85
const UT_StringHolder & foreignFieldName() const
Definition: UT_ORMColumn.h:174
const UT_StringArray & foreignColumns() const
Definition: UT_ORMColumn.h:178
UT_ORMColumn & setPrimaryKey(bool pk)
Definition: UT_ORMColumn.h:86
UT_ORMColumn::Type UTsqlOrmColumnType< int64 >()
Definition: UT_ORMColumn.h:203
unsigned myProperties
Definition: UT_ORMColumn.h:184
UT_ORMColumn::Type UTsqlOrmColumnType< UT_DateTimeField >()
Definition: UT_ORMColumn.h:229
const UT_StringHolder & relatedName() const
Definition: UT_ORMColumn.h:181
UT_ORMColumn::Type UTsqlOrmColumnType< bool >()
Definition: UT_ORMColumn.h:222
unsigned properties() const
Definition: UT_ORMColumn.h:122
bool isPrimaryKey() const
Definition: UT_ORMColumn.h:94
GLenum GLuint GLsizei const GLenum * props
Definition: glcorearb.h:2525