summary refs log tree commit diff stats
path: root/lib/field.h
blob: f7999009c530193b1303ab1962c51b555bca348f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#ifndef FIELD_H_43258321
#define FIELD_H_43258321

#include "enums.h"
#include <stdexcept>
#include <tuple>

namespace verbly {

  class filter;

  class field {
  public:
    enum class type {
      undefined,
      string,
      integer,
      boolean,
      join,
      join_where,
      join_through,
      join_through_where,
      hierarchal_join
    };

    // Default constructor

    field()
    {
    }

    // Static factories

    static field stringField(
      object obj,
      const char* name,
      bool nullable = false)
    {
      return field(obj, type::string, name, nullable);
    }

    static field stringField(
      const char* table,
      const char* name,
      bool nullable = false)
    {
      return field(object::undefined, type::string, name, nullable, table);
    }

    static field integerField(
      object obj,
      const char* name,
      bool nullable = false)
    {
      return field(obj, type::integer, name, nullable);
    }

    static field integerField(
      const char* table,
      const char* name,
      bool nullable = false)
    {
      return field(object::undefined, type::integer, name, nullable, table);
    }

    static field booleanField(
      object obj,
      const char* name,
      bool nullable = false)
    {
      return field(obj, type::boolean, name, nullable);
    }

    static field booleanField(
      const char* table,
      const char* name,
      bool nullable = false)
    {
      return field(object::undefined, type::boolean, name, nullable, table);
    }

    static field joinField(
      object obj,
      const char* name,
      object joinWith,
      bool nullable = false)
    {
      return field(obj, type::join, name, nullable, 0, joinWith);
    }

    static field joinField(
      object obj,
      const char* name,
      const char* table,
      bool nullable = false)
    {
      return field(obj, type::join, name, nullable, table);
    }

    static field joinWhere(
      object obj,
      const char* name,
      object joinWith,
      const char* conditionColumn,
      int conditionValue,
      bool nullable = false)
    {
      return field(obj, type::join_where, name, nullable, 0, joinWith, 0, 0, 0, conditionColumn, conditionValue);
    }

    static field joinThrough(
      object obj,
      const char* name,
      object joinWith,
      const char* joinTable,
      const char* foreignColumn)
    {
      return field(obj, type::join_through, name, true, joinTable, joinWith, foreignColumn, name, foreignColumn);
    }

    static field joinThrough(
      object obj,
      const char* name,
      object joinWith,
      const char* joinTable,
      const char* foreignColumn,
      const char* joinColumn,
      const char* foreignJoinColumn)
    {
      return field(obj, type::join_through, name, true, joinTable, joinWith, foreignColumn, joinColumn, foreignJoinColumn);
    }

    static field joinThroughWhere(
      object obj,
      const char* name,
      object joinWith,
      const char* joinTable,
      const char* foreignColumn,
      const char* conditionColumn,
      int conditionValue,
      bool nullable = false)
    {
      return field(obj, type::join_through_where, name, nullable, joinTable, joinWith, foreignColumn, name, foreignColumn, conditionColumn, conditionValue);
    }

    static field selfJoin(
      object obj,
      const char* name,
      const char* joinTable,
      const char* joinColumn,
      const char* foreignJoinColumn)
    {
      return field(obj, type::join_through, name, true, joinTable, obj, name, joinColumn, foreignJoinColumn);
    }

    static field hierarchalSelfJoin(
      object obj,
      const char* name,
      const char* joinTable,
      const char* joinColumn,
      const char* foreignJoinColumn)
    {
      return field(obj, type::hierarchal_join, name, true, joinTable, obj, name, joinColumn, foreignJoinColumn);
    }

    // Accessors

    object getObject() const
    {
      return object_;
    }

    type getType() const
    {
      return type_;
    }

    bool isJoin() const
    {
      return ((type_ == type::join)
        || (type_ == type::join_where)
        || (type_ == type::join_through)
        || (type_ == type::join_through_where)
        || (type_ == type::hierarchal_join));
    }

    const char* getColumn() const
    {
      return column_;
    }

    bool isNullable() const
    {
      return nullable_;
    }

    bool hasTable() const
    {
      return (table_ != 0);
    }

    const char* getTable() const
    {
      return table_;
    }

    // Joins

    object getJoinObject() const
    {
      return (type_ == type::hierarchal_join)
        ? object_
        : ((type_ == type::join) || (type_ == type::join_where) || (type_ == type::join_through) || (type_ == type::join_through_where))
        ? joinObject_
        : throw std::domain_error("Non-join fields don't have join objects");
    }

    // Many-to-many joins

    const char* getForeignColumn() const
    {
      // We ignore hierarchal joins because they are always self joins.
      return ((type_ == type::join_through) || (type_ == type::join_through_where))
        ? foreignColumn_
        : throw std::domain_error("Only many-to-many join fields have a foreign column");
    }

    const char* getJoinColumn() const
    {
      return ((type_ == type::join_through) || (type_ == type::join_through_where) || (type_ == type::hierarchal_join))
        ? joinColumn_
        : throw std::domain_error("Only many-to-many join fields have a join column");
    }

    const char* getForeignJoinColumn() const
    {
      return ((type_ == type::join_through) || (type_ == type::join_through_where) || (type_ == type::hierarchal_join))
        ? foreignJoinColumn_
        : throw std::domain_error("Only many-to-many join fields have a foreign join column");
    }

    // Condition joins

    const char* getConditionColumn() const
    {
      if ((type_ == type::join_where) || (type_ == type::join_through_where))
      {
        return conditionColumn_;
      } else {
        throw std::domain_error("Only condition join fields have a condition column");
      }
    }

    int getConditionValue() const
    {
      return ((type_ == type::join_where) || (type_ == type::join_through_where))
        ? conditionValue_
        : throw std::domain_error("Only condition join fields have a condition value");
    }

    // Ordering

    bool operator<(const field& other) const
    {
      // For the most part, (object, column) uniquely identifies fields.
      // However, there do exist a number of relationships from an object to
      // itself, such as notion hypernymy/hyponymy. Hypernymy and hyponymy have
      // the same object (notion), the same column (notion_id), and the same
      // table (hypernymy); however, they have different join columns. For
      // condition joins, the condition field and condition value are also
      // significant.
      return std::tie(object_, column_, table_, joinColumn_, conditionColumn_, conditionValue_)
        < std::tie(other.object_, other.column_, other.table_, other.joinColumn_, other.conditionColumn_, other.conditionValue_);
    }

    // Equality

    bool operator==(const field& other) const
    {
      // See operator<() for documentation.
      return std::tie(object_, column_, table_, joinColumn_, conditionColumn_, conditionValue_)
        == std::tie(other.object_, other.column_, other.table_, other.joinColumn_, other.conditionColumn_, other.conditionValue_);
    }

    // Filter construction

    filter operator==(int value) const; // Integer equality
    filter operator!=(int value) const; // Integer inequality
    filter operator<(int value) const; // Integer is less than
    filter operator<=(int value) const; // Integer is at most
    filter operator>(int value) const; // Integer is greater than
    filter operator>=(int value) const; // Integer is at least

    filter operator==(part_of_speech value) const; // Part of speech equality
    filter operator==(positioning value) const; // Adjective positioning equality
    filter operator==(inflection value) const; // Inflection category equality
    filter operator==(part_type value) const; // Verb frame part type equality

    filter operator!=(part_of_speech value) const; // Part of speech inequality
    filter operator!=(positioning value) const; // Adjective positioning inequality
    filter operator!=(inflection value) const; // Inflection category inequality
    filter operator!=(part_type value) const; // Verb frame part type inequality

    filter operator==(bool value) const; // Boolean equality

    filter operator==(std::string value) const; // String equality
    filter operator!=(std::string value) const; // String inequality
    filter operator%=(std::string value) const; // String matching

    filter operator==(const char* value) const; // String equality
    filter operator!=(const char* value) const; // String inequality
    filter operator%=(const char* value) const; // String matching

    operator filter() const; // Non-nullity
    filter operator!() const; // Nullity

    filter operator%=(filter joinCondition) const; // Join

  private:

    // Constructor

    field(
      object obj,
      type datatype,
      const char* column,
      bool nullable = false,
      const char* table = 0,
      object joinObject = object::undefined,
      const char* foreignColumn = 0,
      const char* joinColumn = 0,
      const char* foreignJoinColumn = 0,
      const char* conditionColumn = 0,
      int conditionValue = 0) :
        object_(obj),
        type_(datatype),
        column_(column),
        nullable_(nullable),
        table_(table),
        joinObject_(joinObject),
        foreignColumn_(foreignColumn),
        joinColumn_(joinColumn),
        foreignJoinColumn_(foreignJoinColumn),
        conditionColumn_(conditionColumn),
        conditionValue_(conditionValue)
    {
    }

    // General
    object object_ = object::undefined;
    type type_ = type::undefined;
    const char* column_ = 0;
    const char* table_ = 0;

    // Non-joins and belongs-to joins
    bool nullable_ = false;

    // Joins
    object joinObject_ = object::undefined;

    // Many-to-many joins
    const char* foreignColumn_ = 0;
    const char* joinColumn_ = 0;
    const char* foreignJoinColumn_ = 0;

    // Condition joins
    const char* conditionColumn_ = 0;
    int conditionValue_ = 0;

  };

};

#endif /* end of include guard: FIELD_H_43258321 */