summary refs log tree commit diff stats
path: root/lib/statement.h
blob: bdf1a6aa28cc0422269d6fefa2176fd19b0613e1 (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
#ifndef STATEMENT_H_29F51659
#define STATEMENT_H_29F51659

#include <string>
#include <list>
#include <map>
#include <hkutil/database.h>
#include <variant>
#include "enums.h"
#include "field.h"
#include "filter.h"

namespace verbly {

  class filter;
  class order;

  using field_binding =
    std::tuple<std::string, std::string>;

  using binding =
    std::variant<
      std::monostate,
      std::string,
      int,
      field_binding>;

  class statement {
  public:

    statement(object context, filter queryFilter);

    std::string getQueryString(
      std::list<std::string> select,
      order sortOrder,
      int limit,
      bool debug = false) const;

    std::list<hatkirby::binding> getBindings() const;

  private:

    class join {
    public:

      join(
        bool outer,
        std::string foreignTableName,
        std::string joinTable,
        std::string joinColumn,
        std::string foreignTable,
        std::string foreignColumn) :
          outer_(outer),
          foreignTableName_(std::move(foreignTableName)),
          joinTable_(std::move(joinTable)),
          joinColumn_(std::move(joinColumn)),
          foreignTable_(std::move(foreignTable)),
          foreignColumn_(std::move(foreignColumn))
      {
      }

      bool isOuterJoin() const
      {
        return outer_;
      }

      const std::string& getForeignTableName() const
      {
        return foreignTableName_;
      }

      const std::string& getJoinTable() const
      {
        return joinTable_;
      }

      const std::string& getJoinColumn() const
      {
        return joinColumn_;
      }

      const std::string& getForeignTable() const
      {
        return foreignTable_;
      }

      const std::string& getForeignColumn() const
      {
        return foreignColumn_;
      }

    private:
      bool outer_ = false;
      std::string foreignTableName_;
      std::string joinTable_;
      std::string joinColumn_;
      std::string foreignTable_;
      std::string foreignColumn_;

    };

    friend std::ostream& operator<<(std::ostream& oss, const join& j);

    class condition {
    public:
      enum class type {
        empty,
        singleton,
        group
      };

      enum class comparison {
        equals,
        does_not_equal,
        is_greater_than,
        is_at_most,
        is_less_than,
        is_at_least,
        is_like,
        is_not_like,
        is_not_null,
        is_null
      };

      // Accessors

      type getType() const
      {
        return type_;
      }

      // Empty

      condition() = default;

      // Singleton

      condition(
        std::string table,
        std::string column,
        bool isNull);

      condition(
        std::string table,
        std::string column,
        comparison comp,
        binding value,
        object parentObject = object::undefined);

      // Group

      explicit condition(bool orlogic);

      condition& operator+=(condition n);

      condition& operator&=(condition n);

      const std::list<condition>& getChildren() const;

      // Utility

      std::string toSql(bool toplevel, bool debug = false) const;

      std::list<hatkirby::binding> flattenBindings() const;

      condition flatten() const;

      condition resolveCompareFields(
        object context,
        std::string tableName) const;

    private:

      struct singleton_type {
        std::string table;
        std::string column;
        comparison cmp;
        binding value;
        object parentObject;
      };

      struct group_type {
        std::list<condition> children;
        bool orlogic;
      };

      using variant_type =
        std::variant<
          std::monostate,
          singleton_type,
          group_type>;

      variant_type variant_;

      type type_ = type::empty;
    };

    class with {
    public:

      with(
        std::string identifier,
        field f,
        std::map<std::string, std::string> tables,
        std::string topTable,
        condition where,
        std::list<join> joins,
        bool recursive) :
          identifier_(std::move(identifier)),
          field_(f),
          tables_(std::move(tables)),
          topTable_(std::move(topTable)),
          topCondition_(std::move(where)),
          joins_(std::move(joins)),
          recursive_(recursive)
      {
      }

      const std::string& getIdentifier() const
      {
        return identifier_;
      }

      field getField() const
      {
        return field_;
      }

      std::string getTableForId(std::string identifier) const
      {
        return tables_.at(identifier);
      }

      const std::string& getTopTable() const
      {
        return topTable_;
      }

      const condition& getCondition() const
      {
        return topCondition_;
      }

      const std::list<join>& getJoins() const
      {
        return joins_;
      }

      bool isRecursive() const
      {
        return recursive_;
      }

    private:
      std::string identifier_;
      field field_;
      std::map<std::string, std::string> tables_;
      std::string topTable_;
      condition topCondition_;
      std::list<join> joins_;
      bool recursive_;

    };

    static constexpr const char* getTableForContext(object context)
    {
      return (context == object::notion) ? "notions"
        : (context == object::word) ? "words"
        : (context == object::frame) ? "frames"
        : (context == object::part) ? "parts"
        : (context == object::form) ? "forms"
        : (context == object::pronunciation) ? "pronunciations"
        : throw std::domain_error("Provided context has no associated table");
    }

    static const std::list<field> getSelectForContext(object context);

    statement(object context, std::string tableName, filter clause, int nextTableId = 0, int nextWithId = 0);

    condition parseFilter(filter queryFilter);

    std::string instantiateTable(std::string name);

    std::string instantiateWith(std::string name);

    condition integrate(statement subStmt, bool cte = false);

    int nextTableId_;
    int nextWithId_;

    object context_;
    std::map<std::string, std::string> tables_;
    std::string topTable_;
    std::list<join> joins_;
    std::list<with> withs_;
    condition topCondition_;

  };

};

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